'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/:id/orders`) * * 鉴权:`middleware.apiAuth()` + 服务层校验「当前用户是不是该主体成员 / 管理员」。 * **不挂 `subjectAuth`**:主体过期后仍然要能下单续期。 * * 下单不会立刻续期;等支付渠道回调 `POST /api/pay/notify` 后才顺延期限。 */ class WebSubjectOrderController extends Controller { /** GET /api/web/subjects/:id/orders —— 订单列表 */ async index() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const { page, pageSize } = ctx.query; const data = await ctx.service.subjectOrder.list({ subjectId: id, userId: ctx.state.user.id, page, pageSize, }); ctx.body = { ok: true, data }; } /** POST /api/web/subjects/:id/orders —— { days, amount?, remark? } */ async create() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const { days, amount, remark } = ctx.request.body || {}; const data = await ctx.service.subjectOrder.create({ subjectId: id, userId: ctx.state.user.id, days, amount, remark, }); ctx.status = 201; ctx.body = { ok: true, data }; } } module.exports = WebSubjectOrderController;