oms/backend/app/lib/authToken.js
liufumingren 1847e17ba7 feat(auth): 接入微信扫码登录与微信绑定
- 新增网关客户端与通知验签(sha256(appSecret) 派生 HMAC-SHA256,5 分钟防重放),
  扫码会话与一次性结果存 Redis,openid 仅服务端保存
- 新增 user_wechat_binds 表与迁移:openid 唯一(一个微信只绑一个账号)、
  user_id 唯一(重新绑定覆盖旧微信)
- 新增 /api/auth/wechat/* 接口与 /api/wx/notify 网关回调;登录二维码按 IP 限流,
  未配置网关时相关接口返回 400 提示,不影响账号密码 / 邮箱验证码登录
- 前端:「微信登录」tab(二维码 + 倒计时 + 轮询)与「我的」页微信绑定 / 解绑
- 开发与生产各出一份 WX_GATEWAY_* 配置样板,生产权威副本走 shared-config 覆盖
- 同步补充 swagger 契约与请求日志脱敏字段(bind_code / sign)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-01 23:17:11 +08:00

38 lines
1.2 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';
/**
* 登录态签发工具。
* 所有登录方式(账号密码、邮箱验证码、微信扫码)签发的 JWT 必须完全一致:
* 载荷仅包含 user_id,有效期取 config.jwt.expiresIn。
*/
/**
* 将有效期配置转换为秒数(如 2h => 7200)
* @param {String|Number} expiresIn 有效期,如 2h / 7200
* @return {Number} 有效秒数,无法解析时为 0
*/
function parseExpiresIn(expiresIn) {
if (typeof expiresIn === 'number') return expiresIn;
const matched = /^(\d+)([smhd])$/.exec(String(expiresIn || ''));
if (!matched) return 0;
const units = { s: 1, m: 60, h: 3600, d: 86400 };
return Number(matched[1]) * units[matched[2]];
}
/**
* 签发登录态
* @param {Egg.Application} app egg application
* @param {Number} userId 用户ID
* @return {Object} { token, user_id, expires_in }
*/
function buildLoginData(app, userId) {
const expiresIn = app.config.jwt.expiresIn || '2h';
const token = app.jwt.sign({ user_id: userId }, app.config.jwt.secret, { expiresIn });
return { token, user_id: userId, expires_in: parseExpiresIn(expiresIn) };
}
module.exports = {
parseExpiresIn,
buildLoginData,
};