用户反馈(smartgz@qq.com):验证码收到 7 位、前台只能填 6 位。 排查:OTP 生成 randomInt(100000,999999) 恒 6 位;真因=邮件正文「5 分钟」等 相邻数字被用户一并阅读/输入(或 QQ 邮箱 base64 折行渲染粘连观感)。 双保险: - 邮件模板:验证码独立成行加【 】括号醒目隔离,明示「输入方括号内 6 位数字」 - 小程序双输入框(登录页+资料页):maxlength 6→8 放宽;提交时提取纯数字取前 6 位, 非 6 位给明确指引文案;登录/绑定两处 post 均改用清洗后 cleanCode 冒烟:新模板 mock SMTP 截码【 851639 】渲染正常;凭码登录成功; 污染输入(前置粘连数字)服务端拒、纯 6 位重试通过。
190 lines
6.4 KiB
JavaScript
190 lines
6.4 KiB
JavaScript
// pages/login/index.js
|
|
const app = getApp();
|
|
const { post } = require('../../utils/request');
|
|
const { getWechatCode } = require('../../utils/helpers');
|
|
|
|
const saveLoginState = (token, userId, userInfo) => {
|
|
wx.setStorageSync('token', token);
|
|
wx.setStorageSync('userId', userId);
|
|
wx.setStorageSync('isLoggedIn', true);
|
|
wx.setStorageSync('isAdmin', !!(userInfo && userInfo.isAdmin));
|
|
wx.setStorageSync('isOrganizer', !!(userInfo && userInfo.isOrganizer));
|
|
if (userInfo) {
|
|
wx.setStorageSync('userInfo', userInfo);
|
|
}
|
|
if (app.globalData) {
|
|
app.globalData.token = token;
|
|
app.globalData.userId = userId;
|
|
app.globalData.userInfo = userInfo;
|
|
app.globalData.isAdmin = !!(userInfo && userInfo.isAdmin);
|
|
app.globalData.isOrganizer = !!(userInfo && userInfo.isOrganizer);
|
|
}
|
|
};
|
|
|
|
const isLoggedIn = () => {
|
|
return wx.getStorageSync('isLoggedIn') || false;
|
|
};
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
email: '',
|
|
emailOtp: '',
|
|
otpSending: false,
|
|
otpCountdown: 0,
|
|
phoneLoading: false,
|
|
},
|
|
|
|
goBack() {
|
|
wx.navigateBack({ delta: 1 });
|
|
},
|
|
|
|
onLoad() {
|
|
if (isLoggedIn()) {
|
|
wx.switchTab({ url: '/pages/home/index' });
|
|
}
|
|
},
|
|
|
|
onUnload() {
|
|
if (this._otpTimer) {
|
|
clearInterval(this._otpTimer);
|
|
this._otpTimer = null;
|
|
}
|
|
},
|
|
|
|
onEmailInput(e) {
|
|
this.setData({ email: e.detail.value });
|
|
},
|
|
|
|
onOtpInput(e) {
|
|
this.setData({ emailOtp: e.detail.value });
|
|
},
|
|
|
|
startOtpCountdown() {
|
|
this.setData({ otpCountdown: 60 });
|
|
if (this._otpTimer) clearInterval(this._otpTimer);
|
|
this._otpTimer = setInterval(() => {
|
|
const next = this.data.otpCountdown - 1;
|
|
if (next <= 0) {
|
|
clearInterval(this._otpTimer);
|
|
this._otpTimer = null;
|
|
this.setData({ otpCountdown: 0 });
|
|
} else {
|
|
this.setData({ otpCountdown: next });
|
|
}
|
|
}, 1000);
|
|
},
|
|
|
|
// 微信手机号一键登录:getPhoneNumber 授权码 + wx.login code → /auth/phone/login
|
|
// 失败(个人主体/权限未开/网络)→ 明确降级提示引导邮箱登录
|
|
async handlePhoneLogin(e) {
|
|
if (this.data.phoneLoading) return;
|
|
const detail = e.detail || {};
|
|
// 用户点拒绝/取消:静默返回,不打扰
|
|
if (!detail.code) {
|
|
if (detail.errMsg && detail.errMsg.includes('deny')) return;
|
|
// errMsg 无 deny 但无 code(老基础库/个人主体)→ 引导邮箱
|
|
wx.showToast({ title: '当前环境不支持一键登录,请使用邮箱登录', icon: 'none', duration: 2500 });
|
|
return;
|
|
}
|
|
this.setData({ phoneLoading: true });
|
|
try {
|
|
let wxCode = '';
|
|
try { wxCode = await getWechatCode(); } catch (err) { /* 不阻断:openid 补绑可选 */ }
|
|
const res = await post('/auth/phone/login', { phoneCode: detail.code, wxCode }, { showLoading: false });
|
|
if (res.success && res.data) {
|
|
const { token, user } = res.data;
|
|
saveLoginState(token, user?.id, user);
|
|
wx.showToast({ title: '登录成功', icon: 'success' });
|
|
setTimeout(() => { wx.switchTab({ url: '/pages/home/index' }); }, 1500);
|
|
} else if (res.code === 'PHONE_AUTH_FAILED') {
|
|
// 服务端明确失败(40029/48001 等)→ 降级引导
|
|
wx.showModal({
|
|
title: '一键登录暂不可用',
|
|
content: (res.message || '手机号授权失败') + '。可在下方使用邮箱验证码登录。',
|
|
showCancel: false,
|
|
confirmText: '知道了',
|
|
});
|
|
} else {
|
|
wx.showToast({ title: res.message || '登录失败,请重试', icon: 'none' });
|
|
}
|
|
} catch (err) {
|
|
wx.showToast({ title: (err && err.message) || '登录失败,请重试', icon: 'none' });
|
|
} finally {
|
|
this.setData({ phoneLoading: false });
|
|
}
|
|
},
|
|
|
|
async handleSendOtp() {
|
|
const email = (this.data.email || '').trim();
|
|
if (!email) {
|
|
wx.showToast({ title: '请输入邮箱', icon: 'none' });
|
|
return;
|
|
}
|
|
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
wx.showToast({ title: '邮箱格式不正确', icon: 'none' });
|
|
return;
|
|
}
|
|
if (this.data.otpSending || this.data.otpCountdown > 0) return;
|
|
this.setData({ otpSending: true });
|
|
try {
|
|
const res = await post('/auth/email/otp/send', { email });
|
|
if (res.success) {
|
|
wx.showToast({ title: '验证码已发送', icon: 'none' });
|
|
this.startOtpCountdown();
|
|
} else {
|
|
wx.showToast({ title: res.message || '发送失败', icon: 'none' });
|
|
}
|
|
} catch (err) {
|
|
wx.showToast({ title: err.message || '发送失败', icon: 'none' });
|
|
} finally {
|
|
this.setData({ otpSending: false });
|
|
}
|
|
},
|
|
|
|
async handleEmailLogin() {
|
|
const email = (this.data.email || '').trim();
|
|
const code = (this.data.emailOtp || '').trim();
|
|
if (!email) {
|
|
wx.showToast({ title: '请输入邮箱', icon: 'none' });
|
|
return;
|
|
}
|
|
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
wx.showToast({ title: '邮箱格式不正确', icon: 'none' });
|
|
return;
|
|
}
|
|
if (!code) {
|
|
wx.showToast({ title: '请输入邮箱验证码', icon: 'none' });
|
|
return;
|
|
}
|
|
// 验证码清洗:提取纯数字取前 6 位(邮件中「5 分钟」等数字可能被一并输入)
|
|
const cleanCode = (code.replace(/\D/g, '') || '').slice(0, 6);
|
|
if (cleanCode.length !== 6) {
|
|
wx.showToast({ title: '验证码为 6 位数字,请检查邮件中的【 】括号内数字', icon: 'none', duration: 2500 });
|
|
return;
|
|
}
|
|
if (this.data.loading) return;
|
|
this.setData({ loading: true });
|
|
try {
|
|
// 携带微信 code:登录成功后服务端补绑 openid(用于裸号合并升级)
|
|
let wxCode = '';
|
|
try { wxCode = await getWechatCode(); } catch (e) { /* 不阻断邮箱登录 */ }
|
|
const res = await post('/auth/email/login', { email, code: cleanCode, wxCode }, { showLoading: false });
|
|
if (res.success && res.data) {
|
|
const { token, user } = res.data;
|
|
saveLoginState(token, user?.id, user);
|
|
wx.showToast({ title: '登录成功', icon: 'success' });
|
|
setTimeout(() => {
|
|
wx.switchTab({ url: '/pages/home/index' });
|
|
}, 1500);
|
|
} else {
|
|
wx.showToast({ title: res.message || '登录失败', icon: 'none' });
|
|
}
|
|
} catch (err) {
|
|
wx.showToast({ title: err.message || '登录失败,请重试', icon: 'none' });
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
});
|