181 lines
4.9 KiB
JavaScript
181 lines
4.9 KiB
JavaScript
const auth = require('../../../utils/auth');
|
||
const { ROLE_META } = require('../../../utils/role');
|
||
|
||
const PHONE_RE = /^1[3456789]\d{9}$/;
|
||
|
||
Page({
|
||
data: {
|
||
isReset: false, // true = 重置密码模式,false = 注册模式
|
||
mobile: '',
|
||
password: '',
|
||
password2: '',
|
||
verificationCode: '',
|
||
nickName: '',
|
||
selectedRole: 'participant',
|
||
selectedRoleDescription: ROLE_META.participant.description,
|
||
roleOptions: [ROLE_META.participant, ROLE_META.referee],
|
||
submitting: false,
|
||
codeSending: false,
|
||
countdown: 0,
|
||
statusBarHeight: 0
|
||
},
|
||
|
||
_timer: null,
|
||
|
||
onLoad(options) {
|
||
const { statusBarHeight = 0 } = wx.getSystemInfoSync();
|
||
const isReset = options && options.mode === 'reset';
|
||
const selectedRole = !isReset && options && options.role === 'referee' ? 'referee' : 'participant';
|
||
this.setData({
|
||
statusBarHeight,
|
||
isReset,
|
||
selectedRole,
|
||
selectedRoleDescription: ROLE_META[selectedRole].description
|
||
});
|
||
},
|
||
|
||
onUnload() {
|
||
if (this._timer) {
|
||
clearInterval(this._timer);
|
||
this._timer = null;
|
||
}
|
||
},
|
||
|
||
/** 返回上一页 */
|
||
onBack() {
|
||
if (getCurrentPages().length > 1) {
|
||
wx.navigateBack();
|
||
} else {
|
||
wx.switchTab({ url: '/pages/mine/mine' });
|
||
}
|
||
},
|
||
|
||
onMobileInput(e) {
|
||
this.setData({ mobile: (e.detail.value || '').trim() });
|
||
},
|
||
|
||
onPasswordInput(e) {
|
||
this.setData({ password: e.detail.value || '' });
|
||
},
|
||
|
||
onPassword2Input(e) {
|
||
this.setData({ password2: e.detail.value || '' });
|
||
},
|
||
|
||
onCodeInput(e) {
|
||
this.setData({ verificationCode: (e.detail.value || '').trim() });
|
||
},
|
||
|
||
onNickInput(e) {
|
||
this.setData({ nickName: (e.detail.value || '').trim() });
|
||
},
|
||
|
||
onRoleTap(e) {
|
||
if (this.data.isReset) return;
|
||
const role = e.currentTarget.dataset.role;
|
||
if (!ROLE_META[role]) return;
|
||
this.setData({
|
||
selectedRole: role,
|
||
selectedRoleDescription: ROLE_META[role].description
|
||
});
|
||
},
|
||
|
||
onSendCodeTap() {
|
||
const { mobile, countdown, codeSending } = this.data;
|
||
if (codeSending || countdown > 0) return;
|
||
if (!PHONE_RE.test(mobile)) {
|
||
wx.showToast({ title: '请输入正确手机号', icon: 'none' });
|
||
return;
|
||
}
|
||
|
||
this.setData({ codeSending: true });
|
||
wx.showLoading({ title: '发送中...' });
|
||
|
||
auth
|
||
.sendVerificationCode(mobile)
|
||
.then(() => {
|
||
wx.showToast({ title: '验证码已发送', icon: 'success' });
|
||
this._startCountdown(60);
|
||
})
|
||
.catch(() => {
|
||
wx.showToast({ title: '发送失败', icon: 'none' });
|
||
})
|
||
.finally(() => {
|
||
this.setData({ codeSending: false });
|
||
// wx.hideLoading();
|
||
});
|
||
},
|
||
|
||
_startCountdown(sec) {
|
||
if (this._timer) {
|
||
clearInterval(this._timer);
|
||
this._timer = null;
|
||
}
|
||
this.setData({ countdown: sec });
|
||
this._timer = setInterval(() => {
|
||
const next = this.data.countdown - 1;
|
||
if (next <= 0) {
|
||
clearInterval(this._timer);
|
||
this._timer = null;
|
||
this.setData({ countdown: 0 });
|
||
} else {
|
||
this.setData({ countdown: next });
|
||
}
|
||
}, 1000);
|
||
},
|
||
|
||
onSubmitTap() {
|
||
if (this.data.submitting) return;
|
||
const { isReset, mobile, password, password2, verificationCode, nickName, selectedRole } = this.data;
|
||
|
||
if (!PHONE_RE.test(mobile)) {
|
||
wx.showToast({ title: '请输入正确手机号', icon: 'none' });
|
||
return;
|
||
}
|
||
if (!verificationCode) {
|
||
wx.showToast({ title: '请输入验证码', icon: 'none' });
|
||
return;
|
||
}
|
||
if (!password || password.length < 6 || password.length > 32) {
|
||
wx.showToast({ title: '密码为 6–32 位', icon: 'none' });
|
||
return;
|
||
}
|
||
if (password !== password2) {
|
||
wx.showToast({ title: '两次密码不一致', icon: 'none' });
|
||
return;
|
||
}
|
||
|
||
this.setData({ submitting: true });
|
||
wx.showLoading({ title: '提交中...' });
|
||
|
||
const apiCall = isReset
|
||
? auth.resetPassword({ mobile, password, verificationCode })
|
||
: auth.registerWithVerification({ mobile, password, verificationCode, nickName, role: selectedRole });
|
||
|
||
const successMsg = isReset ? '密码重置成功' : '注册成功';
|
||
|
||
apiCall
|
||
.then(() => {
|
||
if (!isReset) wx.setStorageSync('registeredRole', selectedRole);
|
||
wx.showToast({ title: successMsg, icon: 'success' });
|
||
setTimeout(() => {
|
||
if (isReset) {
|
||
// 重置密码后返回登录页
|
||
// wx.redirectTo({ url: '/subpackages/mine/login/login' });
|
||
wx.navigateBack();
|
||
} else {
|
||
wx.navigateBack();
|
||
}
|
||
}, 800);
|
||
})
|
||
.catch((err) => {
|
||
const msg = (err && err.errors) || (err && err.message) || (isReset ? '重置失败' : '注册失败');
|
||
wx.showToast({ title: msg, icon: 'none' });
|
||
})
|
||
.finally(() => {
|
||
this.setData({ submitting: false });
|
||
// wx.hideLoading();
|
||
});
|
||
}
|
||
});
|