Files
JayuCloud/FM-Wechat-Fronted/pages/login/index.js
T
2026-06-24 22:13:00 +08:00

204 lines
5.5 KiB
JavaScript

const authApi = require('../../api/auth')
const { BRAND_CONFIG } = require('../../utils/config')
const PHONE_REGEX = /^1[3-9]\d{9}$/
function buildSession(data) {
return {
...data,
accessToken: data.accessToken || data.token,
userInfo: data.userInfo || data.user || {},
shopName: data.shopName || (data.shop && data.shop.shopName)
}
}
function getSelectableShopIds(session) {
const userInfo = session.userInfo || {}
if (Array.isArray(userInfo.shopIds)) {
return userInfo.shopIds
}
return Array.isArray(session.shopIds) ? session.shopIds : []
}
Page({
data: {
loginMode: 'phone',
form: {
account: '',
password: '',
phone: '',
code: ''
},
loading: false,
codeLoading: false,
countdown: 0,
countdownTimer: null,
agreementAccepted: false,
agreements: [],
appName: BRAND_CONFIG.appName
},
onInput(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`form.${field}`]: event.detail.value
})
},
switchLoginMode(event) {
const loginMode = event.currentTarget.dataset.mode
if (loginMode === this.data.loginMode) {
return
}
this.setData({ loginMode })
},
async sendLoginCode() {
const phone = String(this.data.form.phone || '').trim()
if (!PHONE_REGEX.test(phone)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
return
}
if (this.data.codeLoading || this.data.countdown > 0) {
return
}
this.setData({ codeLoading: true })
try {
await authApi.sendLoginCode(phone)
wx.showToast({ title: '验证码已发送' })
this.startCountdown()
} catch (error) {
wx.showToast({ title: error.message || '验证码发送失败', icon: 'none' })
} finally {
this.setData({ codeLoading: false })
}
},
async loadAgreements() {
if (this.data.agreements.length) {
return
}
try {
const agreements = await authApi.getCurrentAgreements()
this.setData({ agreements: Array.isArray(agreements) ? agreements : [] })
} catch (error) {
wx.showToast({ title: error.message || '协议加载失败', icon: 'none' })
}
},
toggleAgreement() {
this.setData({
agreementAccepted: !this.data.agreementAccepted
})
},
buildAgreementPayload() {
const agreementVersions = {}
;(this.data.agreements || []).forEach(item => {
if (item && item.agreementType && item.version) {
agreementVersions[item.agreementType] = item.version
}
})
return {
agreementAccepted: !!this.data.agreementAccepted,
agreementVersions,
clientType: 'WECHAT_MINI'
}
},
ensureAgreementAccepted() {
if (!this.data.agreementAccepted) {
wx.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
return false
}
const versions = this.buildAgreementPayload().agreementVersions
if (!versions.USER_TERMS || !versions.PRIVACY_POLICY) {
wx.showToast({ title: '协议版本加载中,请稍后再试', icon: 'none' })
return false
}
return true
},
async resolveLoginSession(data) {
const session = buildSession(data)
const userInfo = session.userInfo || {}
const shopIds = getSelectableShopIds(session)
if (userInfo.shopId || shopIds.length !== 1) {
return session
}
const app = getApp()
app.setSession(session)
return buildSession(await authApi.switchShop(shopIds[0]))
},
startCountdown() {
if (this.data.countdownTimer) {
clearInterval(this.data.countdownTimer)
}
this.setData({ countdown: 60 })
const timer = setInterval(() => {
const next = this.data.countdown - 1
if (next <= 0) {
clearInterval(timer)
this.setData({ countdown: 0, countdownTimer: null })
return
}
this.setData({ countdown: next })
}, 1000)
this.setData({ countdownTimer: timer })
},
onUnload() {
if (this.data.countdownTimer) {
clearInterval(this.data.countdownTimer)
}
},
async submit() {
const form = this.data.form
if (this.data.loginMode === 'password' && (!form.account || !form.password)) {
wx.showToast({ title: '请填写手机号和密码', icon: 'none' })
return
}
if (this.data.loginMode === 'phone' && (!PHONE_REGEX.test(String(form.phone || '').trim()) || !String(form.code || '').trim())) {
wx.showToast({ title: '请输入正确手机号和验证码', icon: 'none' })
return
}
if (this.data.loginMode === 'phone') {
await this.loadAgreements()
}
if (this.data.loginMode === 'phone' && !this.ensureAgreementAccepted()) {
return
}
this.setData({ loading: true })
try {
const data = this.data.loginMode === 'phone'
? await authApi.phoneLogin({
phone: String(form.phone || '').trim(),
verificationCode: String(form.code || '').trim(),
...this.buildAgreementPayload()
})
: await authApi.login({
account: form.account,
password: form.password
})
const app = getApp()
const session = await this.resolveLoginSession(data)
app.setSession(session)
wx.reLaunch({ url: '/pages/planning/index' })
} catch (error) {
wx.showToast({ title: error.message || '登录失败', icon: 'none' })
} finally {
this.setData({ loading: false })
}
},
goRegister() {
wx.navigateTo({ url: '/pages/register/index' })
},
wechatLogin() {
wx.showToast({ title: '请先使用账号登录', icon: 'none' })
}
})