'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;