'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/admin/subjects`) * * 鉴权:`middleware.apiAuth()` + `middleware.platformAdmin()`。 * * - `GET /api/web/admin/subjects?page=&pageSize=&keyword=&status=&expired=` * 所有主体(带成员数 / 管理员账号 / 是否过期),keyword 模糊匹配主体名或备注 * - `PUT /api/web/admin/subjects/:id` * 改授权到期时间:`{ expireAt: '2027-01-01 00:00:00' }` 直接设, * 或 `{ extendDays: 30 }` 顺延;也可顺带改 `remark` / `status` */ class WebAdminSubjectController extends Controller { /** GET /api/web/admin/subjects */ async index() { const { ctx } = this; const data = await ctx.service.subject.listAll({ page: ctx.query.page, pageSize: ctx.query.pageSize, keyword: ctx.query.keyword, status: ctx.query.status, expired: ctx.query.expired, }); ctx.body = { ok: true, data }; } /** PUT /api/web/admin/subjects/:id */ async update() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const { expireAt, extendDays, remark, status } = ctx.request.body || {}; const data = await ctx.service.subject.setExpireAt(id, { expireAt, extendDays, remark, status }, ctx.state.user.id); ctx.body = { ok: true, data }; } } module.exports = WebAdminSubjectController;