fix(otp): 验证码7位反馈双保险——邮件模板括号隔离 + 前端输入清洗

用户反馈(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 位重试通过。
This commit is contained in:
2026-11-24 13:54:22 +08:00
parent 5680a4f9fd
commit 1636418f06
5 changed files with 18 additions and 6 deletions
@@ -370,6 +370,12 @@ Page({
wx.showToast({ title: '请先获取并输入验证码', icon: 'none' });
return;
}
// 验证码清洗:提取纯数字取前 6 位
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.bindingEmail) return;
// 邮箱一经绑定不可更改:绑定前强确认
wx.showModal({
@@ -383,7 +389,7 @@ Page({
async doBindEmail(email, code) {
this.setData({ bindingEmail: true });
try {
const res = await bindEmail(email, code);
const res = await bindEmail(email, cleanCode);
if (res.success) {
wx.showToast({ title: res.data && res.data.chaishuAutoBound ? '绑定成功,已认证拆书帮身份' : '绑定成功', icon: 'none' });
this.stopOtpCountdown();
@@ -70,7 +70,7 @@
class="form-input email-otp-input"
value="{{emailOtp}}"
type="number"
maxlength="6"
maxlength="8"
placeholder="邮箱验证码"
bindinput="onEmailOtpInput"
/>
+7 -1
View File
@@ -157,13 +157,19 @@ Page({
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, wxCode }, { showLoading: false });
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);
+1 -1
View File
@@ -54,7 +54,7 @@
<input
class="email-otp-input"
type="number"
maxlength="6"
maxlength="8"
value="{{emailOtp}}"
placeholder="邮箱验证码"
placeholder-class="identifier-placeholder"
+2 -2
View File
@@ -870,7 +870,7 @@ app.post('/user/email/otp/send', async (req, res) => {
}
const code = String(crypto.randomInt(100000, 999999));
try {
await smtpSend(email, '拆约邮箱验证码', `您正在绑定邮箱(邮箱一经绑定不可更改)。\n\n验证码:${code}\n\n5 分钟内有效。若非本人操作请忽略本邮件。`);
await smtpSend(email, '拆约邮箱验证码', `您正在绑定邮箱(邮箱一经绑定不可更改)。\n\n您的验证码(6 位数字):\n\n【 ${code} 】\n\n5 分钟内有效。请输入方括号内的 6 位数字。若非本人操作请忽略本邮件。`);
} catch (e) {
console.error('OTP 邮件发送失败:', e.message);
return res.json({ success: false, message: '验证码邮件发送失败,请稍后重试或联系客服 554176937@qq.com' });
@@ -952,7 +952,7 @@ app.post('/auth/email/otp/send', async (req, res) => {
}
const code = String(crypto.randomInt(100000, 999999));
try {
await smtpSend(email, '拆约登录验证码', `您正在登录「拆约」小程序。\n\n验证码:${code}\n\n5 分钟内有效。若非本人操作请忽略本邮件。`);
await smtpSend(email, '拆约登录验证码', `您正在登录「拆约」小程序。\n\n您的验证码(6 位数字):\n\n【 ${code} 】\n\n5 分钟内有效。请输入方括号内的 6 位数字。若非本人操作请忽略本邮件。`);
} catch (e) {
console.error('登录 OTP 邮件发送失败:', e.message);
return res.json({ success: false, message: '验证码邮件发送失败,请稍后重试或联系客服 554176937@qq.com' });