137 lines
4.3 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 { 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`)
* - 主体列表:只返回「我是成员」的主体(含角色、成员数、是否过期)
* - 成员:可按姓名快速创建(尚未绑定账号),再用「绑定码」扫码绑定
*/
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 };
}
/** 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-code —— 生成成员绑定码 */
async memberBindCode() {
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.createBindCode({
subjectId: id,
memberId,
operatorUserId: ctx.state.user.id,
});
ctx.body = { ok: true, data };
}
}
module.exports = WebSubjectController;