功能:改造Salon小程序OpenID快捷登录
This commit is contained in:
@@ -1,112 +1,82 @@
|
||||
const authApi = require('../../api/auth')
|
||||
|
||||
const PHONE_REGEX = /^1[3-9]\d{9}$/
|
||||
|
||||
Page({
|
||||
data: {
|
||||
step: 1,
|
||||
role: 'manager',
|
||||
passwordMode: false,
|
||||
form: { phone: '', code: '', password: '', confirmPassword: '', shopName: '', shopAddress: '', shopId: '' },
|
||||
wxCode: '',
|
||||
form: { shopName: '', shopAddress: '', shopId: '' },
|
||||
registrationTicket: '',
|
||||
applicationId: '',
|
||||
phoneVerified: false,
|
||||
shopOptions: [],
|
||||
shopIndex: 0,
|
||||
selectedShopName: '',
|
||||
loading: false,
|
||||
codeLoading: false,
|
||||
countdown: 0,
|
||||
codeSent: false,
|
||||
countdownTimer: null,
|
||||
agreementAccepted: false
|
||||
ticketInvalid: false
|
||||
},
|
||||
|
||||
async onLoad(options) {
|
||||
const registrationTicket = options && options.registrationTicket ? options.registrationTicket : ''
|
||||
this.setData({
|
||||
wxCode: options && options.wxCode ? options.wxCode : '',
|
||||
registrationTicket,
|
||||
applicationId: options && options.applicationId ? options.applicationId : '',
|
||||
phoneVerified: Boolean(registrationTicket),
|
||||
'form.phone': options && options.phone ? options.phone : '',
|
||||
step: registrationTicket ? 2 : 1
|
||||
ticketInvalid: !registrationTicket
|
||||
})
|
||||
try {
|
||||
const shopOptions = await authApi.getAvailableRegistrationShops()
|
||||
this.setData({ shopOptions: Array.isArray(shopOptions) ? shopOptions : [] })
|
||||
const shops = await authApi.getAvailableRegistrationShops()
|
||||
this.setData({
|
||||
shopOptions: (Array.isArray(shops) ? shops : []).map(item => ({
|
||||
id: item.id,
|
||||
name: item.shopName || item.name,
|
||||
address: item.address || ''
|
||||
}))
|
||||
})
|
||||
} catch (error) {
|
||||
this.setData({ shopOptions: [] })
|
||||
}
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
const value = field === 'phone'
|
||||
? String(event.detail.value || '').replace(/\D/g, '').slice(0, 11)
|
||||
: event.detail.value
|
||||
this.setData({ [`form.${field}`]: value })
|
||||
},
|
||||
|
||||
togglePasswordMode() { this.setData({ passwordMode: !this.data.passwordMode }) },
|
||||
|
||||
toggleAgreement() { this.setData({ agreementAccepted: !this.data.agreementAccepted }) },
|
||||
|
||||
async sendCode() {
|
||||
if (!this.data.agreementAccepted) { wx.showToast({ title: '请先同意用户协议和隐私政策', icon: 'none' }); return }
|
||||
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.sendCode(phone); wx.showToast({ title: '验证码已发送' }); this.setData({ codeSent: true }); this.startCountdown() } catch (error) { wx.showToast({ title: error.message || '发送失败', icon: 'none' }) } finally { this.setData({ codeLoading: false }) }
|
||||
},
|
||||
|
||||
continueFromPhone() {
|
||||
const form = this.data.form
|
||||
if (!PHONE_REGEX.test(String(form.phone || '').trim()) || !/^\d{6}$/.test(String(form.code || '').trim())) { wx.showToast({ title: '请输入正确手机号和6位验证码', icon: 'none' }); return }
|
||||
if (this.data.passwordMode && (!form.password || form.password !== form.confirmPassword)) { wx.showToast({ title: '请确认两次密码一致', icon: 'none' }); return }
|
||||
this.setData({ step: 2 })
|
||||
},
|
||||
|
||||
onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) },
|
||||
selectRole(event) { this.setData({ role: event.currentTarget.dataset.role }) },
|
||||
|
||||
continueFromRole() { this.setData({ step: 3 }) },
|
||||
continueFromRole() { if (!this.data.ticketInvalid) this.setData({ step: 2 }) },
|
||||
|
||||
selectShop(event) {
|
||||
const index = Number(event.detail.value)
|
||||
const shop = this.data.shopOptions[index]
|
||||
if (!shop) return
|
||||
this.setData({ shopIndex: index, selectedShopName: shop.name, 'form.shopId': shop.id })
|
||||
if (shop) this.setData({ shopIndex: index, selectedShopName: shop.name, 'form.shopId': shop.id })
|
||||
},
|
||||
|
||||
goBackStep() { this.setData({ step: Math.max(1, this.data.step - 1) }) },
|
||||
|
||||
async getCurrentWxCode() {
|
||||
const result = await new Promise((resolve, reject) => wx.login({ success: resolve, fail: reject }))
|
||||
return result.code
|
||||
},
|
||||
goBackStep() { this.setData({ step: 1 }) },
|
||||
|
||||
async submit() {
|
||||
const form = this.data.form
|
||||
if (this.data.role === 'manager' && !String(form.shopName || '').trim()) { wx.showToast({ title: '请输入店铺名称', icon: 'none' }); return }
|
||||
if (this.data.role === 'staff' && !form.shopId) { wx.showToast({ title: '请选择要加入的店铺', icon: 'none' }); return }
|
||||
if (!this.data.registrationTicket) {
|
||||
wx.showToast({ title: '注册票据已失效,请返回重新快捷登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (this.data.role === 'manager' && !String(form.shopName || '').trim()) {
|
||||
wx.showToast({ title: '请输入店铺名称', icon: 'none' }); return
|
||||
}
|
||||
if (this.data.role === 'staff' && !form.shopId) {
|
||||
wx.showToast({ title: '请选择要加入的店铺', icon: 'none' }); return
|
||||
}
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const wxCode = this.data.registrationTicket ? await this.getCurrentWxCode() : this.data.wxCode
|
||||
await authApi.register({ phone: form.phone, verificationCode: this.data.registrationTicket ? undefined : form.code, registrationTicket: this.data.registrationTicket || undefined, applicationId: this.data.applicationId || undefined, password: form.password, confirmPassword: form.confirmPassword, shopName: form.shopName, shopAddress: form.shopAddress, shopId: form.shopId, roleType: this.data.role === 'manager' ? 'MANAGER' : 'STAFF', agreementAccepted: true, wxCode })
|
||||
this.setData({ step: 4 })
|
||||
} catch (error) { wx.showToast({ title: error.message || '提交失败', icon: 'none' }) } finally { this.setData({ loading: false }) }
|
||||
await authApi.register({
|
||||
registrationTicket: this.data.registrationTicket,
|
||||
applicationId: this.data.applicationId || undefined,
|
||||
shopName: String(form.shopName || '').trim(),
|
||||
shopAddress: String(form.shopAddress || '').trim(),
|
||||
shopId: form.shopId || undefined,
|
||||
roleType: this.data.role === 'manager' ? 'MANAGER' : 'STAFF',
|
||||
agreementAccepted: true
|
||||
})
|
||||
this.setData({ step: 3, registrationTicket: '' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '提交失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
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) },
|
||||
|
||||
back() { wx.navigateBack() }
|
||||
back() { wx.reLaunch({ url: '/pages/login/index' }) }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user