liufumingren b2eea28001 feat(微信网关): 扫码登录 / 绑定微信 / 工作台扫码登入,废弃自建登录码
对接 https://gateway.gztbyc.com:二维码与「扫码确认」由网关提供,本服务只负责
申请临时码 → 验签收通知 → 落地自己的业务。

- 新增 service/wx_gateway.js + controller/gateway/notify.js(POST /api/wechat-gateway/notify,验签 401 / 会话过期 200 ignored)
- 新增 controller/web/wx_gateway.js(登录二维码 / 长轮询 / SVG+GIF / 个人中心绑定与解绑)
- 新表 wechat_gateway_bindings(uk(app_key, openid)):一个微信只绑一个账号,解绑是软删,
  再绑定走 restore 复用(否则会撞唯一键报 Validation error)
- 「这个微信已绑别的账号」策略:原账号正常 → 拒绝并提示对方账号名;原账号禁用/已删 → 直接改绑;
  拿不到原账号 → 运维接口 GET/DELETE /api/internal/wechat-bindings 强制解绑
- 主体成员改为「扫码绑定」:管理员出二维码 → 成员微信扫码 → 已有账号直接继承(自动建的临时账号删掉),
  没有账号则由服务端建号并绑成其登录方式;subject_members.temp_user_id 记录临时账号
- 废弃自建登录码:删除 GET /api/wechat/login(6 位串码)与 workstations.login_code(迁移 20261028121000),
  工作台改扫码登入,成员绑定码与 /wechat?bind= 手机确认页下线
- 前端:登录页/个人中心/主体成员/工作台全部切到网关;桌面端登录窗改网关二维码 + 长轮询
- 测试:wx-gateway.test.js 覆盖登录/绑定/解绑-再绑/禁用账号改绑/运维强制解绑/成员绑定/工作台登入

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:20:05 +08:00

66 lines
2.7 KiB
JavaScript
Raw Permalink 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';
/**
* 接口鉴权(**只认 header 里的令牌,不看 cookie/session**)
*
* 用法:`router.post('/api/xxx', middleware.apiAuth(), controller.xxx.yyy)`
*
* - 令牌:`Authorization: Bearer <user JWT>`(也认 `X-Access-Token`,方便不爱用 Authorization 的客户端)
* JWT 由这些地方下发:账号密码登录 `/api/web/auth/login`、微信网关扫码登录
* `/api/web/auth/wechat-gateway/login` 的 `data.token`、页面里现场签发的同款令牌;
* - 不加 cookie、也不写 session:小程序 / App / SPA 这类没有浏览器 cookie 的客户端直接用它;
* - 未带或验不过 → 401 `{ ok:false, status:'unauthorized', message }`;
* 用户不存在/被禁用 → 403 `{ ok:false, status:'forbidden', message }`;
* - 通过后把用户放进 `ctx.locals.user` 与 `ctx.state.user`。
*/
module.exports = () => {
return async function apiAuth(ctx, next) {
const token = bearerToken(ctx);
const payload = token && ctx.service.jwt.verifyUser(token);
const userId = payload && payload.sub ? Number(payload.sub) : 0;
if (!userId) {
ctx.status = 401;
ctx.body = {
ok: false,
status: 'unauthorized',
message: '缺少或无效的令牌(请在 header 里带 Authorization: Bearer <token>)',
};
return;
}
const user = await ctx.service.user.findById(userId);
if (!user) {
ctx.status = 401;
ctx.body = { ok: false, status: 'unauthorized', message: '令牌对应的用户不存在' };
return;
}
if (Number(user.status) !== 1) {
ctx.status = 403;
ctx.body = { ok: false, status: 'forbidden', message: '账号已被禁用' };
return;
}
// 改密 / 重置密码之后,签发时间更早的旧令牌立即失效(JWT 是无状态的,靠这个时间戳收回)
if (ctx.service.user.isTokenStale(user, payload.iat)) {
ctx.status = 401;
ctx.body = { ok: false, status: 'password_changed', message: '密码已修改,请重新登录' };
return;
}
// 注意:**不碰 session/cookie**,接口鉴权完全基于这一次请求的 header;passwordChangedAt 只内部用,不外传
const safeUser = Object.assign({}, user);
delete safeUser.passwordChangedAt;
ctx.locals = ctx.locals || {};
ctx.locals.user = safeUser;
ctx.state.user = safeUser;
await next();
};
};
/** 取 header 里的令牌:优先 Authorization: Bearer,其次 X-Access-Token */
function bearerToken(ctx) {
const matched = /^Bearer\s+(.+)$/i.exec(ctx.get('authorization') || '');
if (matched) return matched[1].trim();
return String(ctx.get('x-access-token') || '').trim();
}