From 1636418f061aa72d8975046811d12517dfd1b484 Mon Sep 17 00:00:00 2001 From: loong-dev Date: Tue, 24 Nov 2026 13:53:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(otp):=20=E9=AA=8C=E8=AF=81=E7=A0=817?= =?UTF-8?q?=E4=BD=8D=E5=8F=8D=E9=A6=88=E5=8F=8C=E4=BF=9D=E9=99=A9=E2=80=94?= =?UTF-8?q?=E2=80=94=E9=82=AE=E4=BB=B6=E6=A8=A1=E6=9D=BF=E6=8B=AC=E5=8F=B7?= =?UTF-8?q?=E9=9A=94=E7=A6=BB=20+=20=E5=89=8D=E7=AB=AF=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=B8=85=E6=B4=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户反馈(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 位重试通过。 --- wechat-miniprogram/pages/edit-profile/index.js | 8 +++++++- wechat-miniprogram/pages/edit-profile/index.wxml | 2 +- wechat-miniprogram/pages/login/index.js | 8 +++++++- wechat-miniprogram/pages/login/index.wxml | 2 +- yaoyue-sever/server/server.js | 4 ++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/wechat-miniprogram/pages/edit-profile/index.js b/wechat-miniprogram/pages/edit-profile/index.js index da7b938..ee288a9 100644 --- a/wechat-miniprogram/pages/edit-profile/index.js +++ b/wechat-miniprogram/pages/edit-profile/index.js @@ -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(); diff --git a/wechat-miniprogram/pages/edit-profile/index.wxml b/wechat-miniprogram/pages/edit-profile/index.wxml index 7a1a36f..2f84895 100644 --- a/wechat-miniprogram/pages/edit-profile/index.wxml +++ b/wechat-miniprogram/pages/edit-profile/index.wxml @@ -70,7 +70,7 @@ class="form-input email-otp-input" value="{{emailOtp}}" type="number" - maxlength="6" + maxlength="8" placeholder="邮箱验证码" bindinput="onEmailOtpInput" /> diff --git a/wechat-miniprogram/pages/login/index.js b/wechat-miniprogram/pages/login/index.js index dc74527..60c834c 100644 --- a/wechat-miniprogram/pages/login/index.js +++ b/wechat-miniprogram/pages/login/index.js @@ -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); diff --git a/wechat-miniprogram/pages/login/index.wxml b/wechat-miniprogram/pages/login/index.wxml index 515a6d4..ca3ab3b 100644 --- a/wechat-miniprogram/pages/login/index.wxml +++ b/wechat-miniprogram/pages/login/index.wxml @@ -54,7 +54,7 @@ { } 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' });