243 lines
5.6 KiB
JavaScript
Raw Normal View History

2026-03-30 18:08:27 +08:00
const auth = require('../../../utils/auth');
2026-03-31 18:09:37 +08:00
const PHONE_RE = /^1[3456789]\d{9}$/;
2026-03-31 23:27:35 +08:00
const CODE_COUNTDOWN = 60;
2026-03-31 18:09:37 +08:00
2026-03-30 18:08:27 +08:00
Page({
data: {
2026-03-31 23:27:35 +08:00
loginMode: 'password', // 'code' | 'password'
2026-03-31 18:09:37 +08:00
mobile: '',
password: '',
2026-03-31 23:27:35 +08:00
verificationCode: '',
showPassword: false,
logging: false,
agreed: false,
statusBarHeight: 0,
2026-09-17 14:00:10 +08:00
returnToPrevious: false,
refereeRegistration: false,
2026-03-31 23:27:35 +08:00
// 验证码倒计时
codeText: '发送验证码',
codeBtnDisabled: false,
countdown: 0
2026-03-30 18:08:27 +08:00
},
2026-03-31 23:27:35 +08:00
_timer: null,
2026-09-17 14:00:10 +08:00
onLoad(options) {
const { statusBarHeight = 0 } = wx.getSystemInfoSync();
2026-09-17 14:00:10 +08:00
this.setData({
statusBarHeight,
returnToPrevious: options && options.returnToPrevious === '1',
refereeRegistration: options && options.refereeRegistration === '1'
});
},
2026-03-30 18:08:27 +08:00
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (accessToken) {
2026-09-17 14:00:10 +08:00
this._navigateAfterLogin();
2026-03-30 18:08:27 +08:00
}
},
2026-03-31 23:27:35 +08:00
onUnload() {
this._clearTimer();
},
/** 返回上一页 */
onBack() {
if (getCurrentPages().length > 1) {
wx.navigateBack();
} else {
wx.switchTab({ url: '/pages/mine/mine' });
}
},
2026-03-31 23:27:35 +08:00
// ========= 事件处理 =========
/** 切换登录方式 */
onSwitchMode(e) {
const mode = e.currentTarget.dataset.mode;
if (mode === this.data.loginMode) return;
this.setData({
loginMode: mode,
verificationCode: '',
password: ''
});
},
/** 手机号输入 */
2026-03-31 18:09:37 +08:00
onMobileInput(e) {
this.setData({ mobile: (e.detail.value || '').trim() });
},
2026-03-31 23:27:35 +08:00
/** 清除手机号 */
onClearMobile() {
this.setData({ mobile: '' });
},
/** 验证码输入 */
onCodeInput(e) {
this.setData({ verificationCode: (e.detail.value || '').trim() });
},
2026-04-04 18:57:50 +08:00
//跳转隐私协议
goToPrivacy() {
wx.openPrivacyContract({
success: () => {
console.log('打开成功');
}, // 打开成功
fail: () => {
uni.showToast({
title: '打开失败,稍后重试',
icon: 'none'
})
} // 打开失败
})
},
2026-03-31 23:27:35 +08:00
/** 密码输入 */
2026-03-31 18:09:37 +08:00
onPasswordInput(e) {
this.setData({ password: e.detail.value || '' });
},
2026-03-31 23:27:35 +08:00
/** 显示/隐藏密码 */
onTogglePassword() {
this.setData({ showPassword: !this.data.showPassword });
},
/** 勾选协议 */
onToggleAgreement() {
this.setData({ agreed: !this.data.agreed });
},
/** 忘记密码 */
onForgetPassword() {
wx.navigateTo({ url: '/subpackages/mine/register/register?mode=reset' });
2026-03-31 23:27:35 +08:00
},
2026-09-17 14:00:10 +08:00
onRegisterTap() {
const roleQuery = this.data.refereeRegistration ? '?role=referee' : '';
wx.navigateTo({ url: `/subpackages/mine/register/register${roleQuery}` });
},
2026-03-31 23:27:35 +08:00
// ========= 发送验证码 =========
onSendCode() {
if (this.data.codeBtnDisabled) return;
const { mobile } = this.data;
if (!PHONE_RE.test(mobile)) {
wx.showToast({ title: '请输入正确的手机号', icon: 'none' });
return;
}
this.setData({ codeBtnDisabled: true, codeText: '发送中...' });
auth
.sendVerificationCode(mobile)
.then(() => {
wx.showToast({ title: '验证码已发送', icon: 'success' });
this._startCountdown();
})
.catch((err) => {
wx.showToast({ title: err.message || '发送失败', icon: 'none' });
this.setData({ codeBtnDisabled: false, codeText: '发送验证码' });
});
},
// ========= 登录 =========
2026-03-30 18:08:27 +08:00
onLoginTap() {
if (this.data.logging) return;
2026-03-31 23:27:35 +08:00
if (!this.data.agreed) {
wx.showToast({ title: '请先同意用户协议与隐私政策', icon: 'none' });
return;
}
const { mobile, loginMode, password, verificationCode } = this.data;
2026-03-31 18:09:37 +08:00
if (!PHONE_RE.test(mobile)) {
2026-03-31 23:27:35 +08:00
wx.showToast({ title: '请输入正确的手机号', icon: 'none' });
2026-03-31 18:09:37 +08:00
return;
}
2026-03-31 23:27:35 +08:00
if (loginMode === 'password' && !password) {
2026-03-31 18:09:37 +08:00
wx.showToast({ title: '请输入密码', icon: 'none' });
return;
}
2026-03-31 23:27:35 +08:00
if (loginMode === 'code' && !verificationCode) {
wx.showToast({ title: '请输入验证码', icon: 'none' });
return;
}
2026-03-31 18:09:37 +08:00
this.setData({ logging: true });
wx.showLoading({ title: '登录中...' });
2026-03-30 18:08:27 +08:00
2026-03-31 23:27:35 +08:00
const loginPromise =
loginMode === 'code'
? auth.loginWithMobileCode({ mobile, verificationCode })
: auth.loginWithMobilePassword({ mobile, password });
loginPromise
2026-03-31 18:09:37 +08:00
.then(() => {
wx.showToast({ title: '登录成功', icon: 'success' });
setTimeout(() => {
2026-09-17 14:00:10 +08:00
this._navigateAfterLogin();
2026-03-31 18:09:37 +08:00
}, 400);
})
2026-03-31 23:27:35 +08:00
.catch((err) => {
2026-04-13 23:56:07 +08:00
console.error('登录失败', err);
2026-03-31 23:27:35 +08:00
wx.showToast({ title: err.message || '登录失败', icon: 'none' });
2026-03-31 18:09:37 +08:00
})
.finally(() => {
this.setData({ logging: false });
2026-04-13 23:56:07 +08:00
// wx.hideLoading();
2026-03-30 18:08:27 +08:00
});
2026-03-31 23:27:35 +08:00
},
// ========= 内部方法 =========
2026-09-17 14:00:10 +08:00
_navigateAfterLogin() {
if (this.data.returnToPrevious && getCurrentPages().length > 1) {
wx.navigateBack();
return;
}
wx.switchTab({ url: '/pages/mine/mine' });
},
2026-03-31 23:27:35 +08:00
_startCountdown() {
let seconds = CODE_COUNTDOWN;
this.setData({
countdown: seconds,
codeText: `${seconds}s`,
codeBtnDisabled: true
});
this._timer = setInterval(() => {
seconds -= 1;
if (seconds <= 0) {
this._clearTimer();
this.setData({
countdown: 0,
codeText: '发送验证码',
codeBtnDisabled: false
});
} else {
this.setData({
countdown: seconds,
codeText: `${seconds}s`
});
}
}, 1000);
},
_clearTimer() {
if (this._timer) {
clearInterval(this._timer);
this._timer = null;
}
2026-03-30 18:08:27 +08:00
}
});