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

215 lines
7.3 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';
const { Controller } = require('egg');
/** 路径参数转正整数,非法返回 null */
function toId(value) {
const id = Number(value);
return Number.isInteger(id) && id > 0 ? id : null;
}
/**
* web 主体端:主体与成员管理(`/api/web/subjects`)
*
* 鉴权:`middleware.apiAuth()`(只认 header 令牌)。
* **这些接口不需要 `X-Subject-Id`**:主体 id 在路径里,服务层再校验「当前用户是不是这个主体的成员」。
*
* 关键规则:
* - 创建主体:管理员 = 创建人,使用期限默认赠送 7 天(`config.subject.trialDays`)
* - 主体列表:只返回「我是成员」的主体(含角色、成员数、是否过期)
* - 成员:可按姓名快速创建(尚未绑定账号),再用「扫码绑定」绑微信/账号
* - **成员管理(增 / 删 / 扫码绑定 / 改角色)只有主体管理员能做**,普通成员只能看列表;
* 一个主体可以有多个管理员(`subject_members.role=1`),创建者恒为管理员且不能降级
*/
class WebSubjectController extends Controller {
/** GET /api/web/subjects —— 我可进入的主体列表 */
async index() {
const { ctx } = this;
const list = await ctx.service.subject.listForUser(ctx.state.user.id);
ctx.body = { ok: true, data: { list, trialDays: ctx.service.subject.trialDays } };
}
/** POST /api/web/subjects —— { name, days?, remark? } */
async create() {
const { ctx } = this;
const { name, days, remark } = ctx.request.body || {};
const data = await ctx.service.subject.create({
userId: ctx.state.user.id,
name,
days,
remark,
});
ctx.status = 201;
ctx.body = { ok: true, data };
}
/** GET /api/web/subjects/:id —— 主体详情(含我的角色) */
async show() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { subject, member } = await ctx.service.subject.findForUser(id, ctx.state.user.id);
const memberCount = await ctx.service.subject
.listForUser(ctx.state.user.id)
.then(list => (list.find(item => Number(item.id) === id) || {}).memberCount || 0);
ctx.body = {
ok: true,
data: Object.assign(ctx.service.subject.present(subject), {
role: Number(member.role),
memberName: member.name,
memberCount,
}),
};
}
/** PUT /api/web/subjects/:id —— { name?, remark?, extendDays? }(仅管理员) */
async update() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { name, remark, extendDays } = ctx.request.body || {};
const data = await ctx.service.subject.update(id, ctx.state.user.id, { name, remark, extendDays });
ctx.body = { ok: true, data };
}
/** GET /api/web/subjects/:id/members —— 成员列表 */
async members() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const list = await ctx.service.subject.members(id, ctx.state.user.id);
ctx.body = { ok: true, data: { subjectId: id, list } };
}
/** POST /api/web/subjects/:id/members —— { name } 按姓名快速添加成员(仅管理员) */
async addMember() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { name, role } = ctx.request.body || {};
const data = await ctx.service.subject.addMember({
subjectId: id,
userId: ctx.state.user.id,
name,
role,
});
ctx.status = 201;
ctx.body = { ok: true, data };
}
/** PUT /api/web/subjects/:id/members/:memberId/role —— { role: 1|2 } 设为管理员 / 取消管理员(仅管理员) */
async updateMemberRole() {
const { ctx } = this;
const id = toId(ctx.params.id);
const memberId = toId(ctx.params.memberId);
if (!id || !memberId) ctx.throw(400, 'id 必须是正整数');
const { role } = ctx.request.body || {};
const data = await ctx.service.subject.setMemberRole({
subjectId: id,
operatorUserId: ctx.state.user.id,
memberId,
role,
});
ctx.body = { ok: true, data };
}
/** DELETE /api/web/subjects/:id/members/:memberId */
async removeMember() {
const { ctx } = this;
const id = toId(ctx.params.id);
const memberId = toId(ctx.params.memberId);
if (!id || !memberId) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.subject.removeMember({
subjectId: id,
operatorUserId: ctx.state.user.id,
memberId,
});
ctx.body = { ok: true, data };
}
/**
* POST /api/web/subjects/:id/members/:memberId/bind-qr —— 生成「扫码绑定」二维码(微信登录网关)
*
* 返回 `{ memberId, memberName, state, pageUrl, qrUrl, expiresIn }`:前端把 `qrUrl` 当图片显示,
* 再拿 `state` 去 `GET .../bind-qr/status` 长轮询结果。
*/
async memberBindQr() {
const { ctx } = this;
const id = toId(ctx.params.id);
const memberId = toId(ctx.params.memberId);
if (!id || !memberId) ctx.throw(400, 'id 必须是正整数');
// 5xx 的 ctx.throw 会被统一错误处理遮成「服务器内部错误」,这里直接给明确提示
if (!ctx.service.wxGateway.enabled) {
ctx.status = 503;
ctx.body = { ok: false, message: '未开通微信扫码绑定(服务端未配置微信登录网关)' };
return;
}
const data = await ctx.service.subject.createBindQr({
subjectId: id,
memberId,
operatorUserId: ctx.state.user.id,
});
ctx.body = { ok: true, data };
}
/**
* GET /api/web/subjects/:id/members/:memberId/bind-qr/status?state=&wait= —— 长轮询绑定结果
*
* `data.status`:pending / confirmed(附 `memberName`、`boundName`、`autoCreated`)/ failed(附原因)/ expired
*/
async memberBindQrStatus() {
const { ctx } = this;
const id = toId(ctx.params.id);
const memberId = toId(ctx.params.memberId);
if (!id || !memberId) ctx.throw(400, 'id 必须是正整数');
// 仍然校验管理员身份:二维码能扫,但不是谁都能来看结果
await ctx.service.subject.ensureAdmin(id, ctx.state.user.id);
const result = await ctx.service.wxGateway.awaitSession(String(ctx.query.state || ''), {
wait: ctx.query.wait,
});
if (result.status === 'aborted') {
// 客户端已经断开(关弹窗 / 换页):不用写响应
ctx.respond = false;
return;
}
ctx.body = { ok: true, data: result };
}
/**
* POST /api/web/subjects/:id/members/:memberId/unbind —— 解除成员的账号绑定(**仅管理员**)
*
* 绑错了 / 手机换人时用:解绑后成员回到「未绑定」,可以重新出二维码绑别人;
* 自动建给成员的临时账号会一并删掉。
*/
async unbindMember() {
const { ctx } = this;
const id = toId(ctx.params.id);
const memberId = toId(ctx.params.memberId);
if (!id || !memberId) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.subject.unbindMember({
subjectId: id,
memberId,
operatorUserId: ctx.state.user.id,
});
ctx.body = { ok: true, data };
}
}
module.exports = WebSubjectController;