94 lines
2.2 KiB
JavaScript
94 lines
2.2 KiB
JavaScript
const authApi = require('../../api/auth')
|
|
|
|
Page({
|
|
data: {
|
|
form: {
|
|
phone: '',
|
|
code: '',
|
|
password: '',
|
|
confirmPassword: '',
|
|
shopName: ''
|
|
},
|
|
loading: false,
|
|
codeLoading: false,
|
|
countdown: 0,
|
|
countdownTimer: null
|
|
},
|
|
|
|
onInput(event) {
|
|
this.setData({
|
|
[`form.${event.currentTarget.dataset.field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
back() {
|
|
wx.navigateBack()
|
|
},
|
|
|
|
async sendCode() {
|
|
const phone = this.data.form.phone
|
|
if (!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 })
|
|
}
|
|
},
|
|
|
|
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 (!form.phone || !form.code || !form.password || !form.shopName) {
|
|
wx.showToast({ title: '请补全注册信息', icon: 'none' })
|
|
return
|
|
}
|
|
if (form.password !== form.confirmPassword) {
|
|
wx.showToast({ title: '两次密码不一致', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
await authApi.register(form)
|
|
wx.showToast({ title: '注册成功' })
|
|
setTimeout(() => wx.navigateBack(), 500)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '注册失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
}
|
|
})
|