packServer/backend/app/service/verify_code.js
liufumingren 228723e0c3 设备端扫码上报 + 桌面端/Android 正式包发布脚手架
AndroidStation(原中控改造为纯设备端)
- 只做三件事:UDP 48899 被桌面端发现、TCP 8087 推送 USB 扫码结果、离线语音播报
- 扫码不再经过 UI:去掉逐键重绘,50ms 自动提交,同一码重复扫码也一律上报
- 离线语音种子(数字/字母/“开始打包”/欢迎语)随包安装,MediaPlayer 播放,启动把音量拉满
- 启动播报「欢迎使用溯源系统,初始化已经完成,扫码即可开始打包」
- 移除 Device Owner / 设备管理员 / lock task / 网络与 IP 设置等非普通应用能力(仅保留开机自启)
- 正式签名 keystore + release 签名配置,versionName/versionCode 可由打包脚本覆盖

发布脚手架
- backend/android_client:Android 正式包构建(build.ps1 + package.config.json)
- backend/scripts/deploy/publish-android.cjs + npm run android:build / android:publish
- 桌面端安装包改为 download/desktop,Android 为 download/android_client;
  version.json 仍走 /download/version.json(老客户端写死),新增 /download/android_client/version.json
- 谷歌插件随桌面端 MSI 安装到 <安装目录>\extension,工具菜单新增「复制谷歌插件地址」
- git-safety-check 增加仓库级禁区(keystore.properties / *.jks / local.properties)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 05:53:27 +08:00

119 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Service } = require('egg');
/** 场景 → Redis key 段(不同场景的验证码互不影响) */
const SCENES = { register: 'verify:register' };
/** 6 位数字验证码 */
function randomCode() {
return String(Math.floor(Math.random() * 1000000)).padStart(6, '0');
}
/**
* 邮箱验证码:生成 / 校验 / 消费
*
* 存在 Redis:
* - `packs:verify:<scene>:<email>` 验证码本身(TTL = config.email.codeTtl,默认 10 分钟)
* - `packs:verify:<scene>:<email>:sent` 重发节流标记(SET NX,TTL = resendInterval,默认 60 秒)
* - `packs:verify:<scene>:<email>:try` 错误次数(超过 maxAttempts 作废验证码)
*
* 对外不抛异常,统一返回 `{ ok, message }`,方便页面回提示(不弹 500)。
*/
class VerifyCodeService extends Service {
get redis() {
return this.app.redis;
}
get cfg() {
return this.app.config.email;
}
keyOf(scene, email) {
const segment = SCENES[scene] || scene;
return `${this.app.config.cache.prefix}${segment}:${email}`;
}
/**
* 生成验证码并发邮件
* @param {string} scene 场景(register)
* @param {string} email 邮箱(调用方保证已小写归一)
* @return {Promise<{ok:boolean, message?:string, code?:string, devMode?:boolean, ttl?:number, resendInterval?:number}>}
*/
async send(scene, email) {
if (!this.redis) return { ok: false, message: '验证码服务不可用(Redis 未就绪)' };
const key = this.keyOf(scene, email);
try {
// 重发节流:SET NX 原子占位,间隔内只允许发一次
const locked = await this.redis.set(`${key}:sent`, '1', 'EX', this.cfg.resendInterval, 'NX');
if (locked !== 'OK') {
return { ok: false, message: `验证码已发送,请 ${this.cfg.resendInterval} 秒后再试` };
}
const code = randomCode();
await this.redis.setex(key, this.cfg.codeTtl, code);
await this.redis.del(`${key}:try`);
const mail = await this.ctx.service.mail.sendVerifyCode(email, code, { ttl: this.cfg.codeTtl });
if (mail.failed) {
await this.redis.del(`${key}:sent`); // 发失败就不要占着节流位
return { ok: false, message: '验证码邮件发送失败,请稍后再试' };
}
this.ctx.logger.info('[verify] %s 验证码已生成: %s (devMode=%s)', scene, email, !!mail.devMode);
// 生产环境绝不回传验证码(即使配置误开 devMode 也兜底)
const exposeDevCode = this.app.config.env !== 'prod' && !!mail.devMode;
return {
ok: true,
ttl: this.cfg.codeTtl,
resendInterval: this.cfg.resendInterval,
devMode: exposeDevCode,
// 开发模式才把验证码回传给页面(生产走邮件)
code: exposeDevCode ? code : undefined,
};
} catch (err) {
this.ctx.logger.error('[verify] 验证码发送失败 %s : %s', email, err.message);
return { ok: false, message: '验证码服务暂不可用,请稍后再试' };
}
}
/**
* 校验验证码(不消费,注册成功后再 consume,这样用户名冲突时可以改完重提)
* @return {Promise<{ok:boolean, message?:string}>}
*/
async check(scene, email, code) {
if (!this.redis) return { ok: false, message: '验证码服务不可用(Redis 未就绪)' };
const key = this.keyOf(scene, email);
try {
const expect = await this.redis.get(key);
if (!expect) return { ok: false, message: '验证码已过期,请重新获取' };
const tries = await this.redis.incr(`${key}:try`);
await this.redis.expire(`${key}:try`, this.cfg.codeTtl);
if (tries > this.cfg.maxAttempts) {
await this.redis.del(key);
return { ok: false, message: '验证码错误次数过多,请重新获取' };
}
if (String(code) !== String(expect)) {
return { ok: false, message: '邮箱验证码不正确' };
}
return { ok: true };
} catch (err) {
this.ctx.logger.error('[verify] 验证码校验失败 %s : %s', email, err.message);
return { ok: false, message: '验证码服务暂不可用,请稍后再试' };
}
}
/** 消费掉验证码(注册成功后调用) */
async consume(scene, email) {
if (!this.redis) return;
const key = this.keyOf(scene, email);
await this.redis.del(key);
await this.redis.del(`${key}:try`);
}
}
module.exports = VerifyCodeService;