Files
JayuCloud/JC-Salon-WeChat/pages/register/index.js
T
xiaowang 8d84cb25d4
发布 Nacos 配置 / publish (push) Successful in 27s
Salon 生产发布准备 / prepare (pull_request) Has been skipped
实现手机号登录自动进入注册审核流程
2026-09-08 22:12:29 +08:00

96 lines
4.4 KiB
JavaScript

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: '',
registrationTicket: '',
applicationId: '',
phoneVerified: false,
shopOptions: [],
shopIndex: 0,
selectedShopName: '',
loading: false,
codeLoading: false,
countdown: 0,
countdownTimer: null
},
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
})
try {
const shopOptions = await authApi.getAvailableRegistrationShops()
this.setData({ shopOptions: Array.isArray(shopOptions) ? shopOptions : [] })
} catch (error) {
this.setData({ shopOptions: [] })
}
},
onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) },
togglePasswordMode() { this.setData({ passwordMode: !this.data.passwordMode }) },
async sendCode() {
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.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 })
},
selectRole(event) { this.setData({ role: event.currentTarget.dataset.role }) },
continueFromRole() { this.setData({ step: 3 }) },
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 })
},
goBackStep() { this.setData({ step: Math.max(1, this.data.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 }
this.setData({ loading: true })
try {
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.data.wxCode })
this.setData({ step: 4 })
} 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() }
})