'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/plans`) * * 鉴权:`middleware.apiAuth()` + `middleware.platformAdmin()`(能进来的都是平台管理员)。 * * - `GET /api/web/admin/plans` 全部套餐(含已下架,带 `statusText` / `pricePerDay`) * - `POST /api/web/admin/plans` 新建 `{ code?, name, days, price, originalPrice?, tag?, description?, sort?, status? }` * - `PUT /api/web/admin/plans/:id` 修改(只改传了的字段;`code` 不可改) * - `DELETE /api/web/admin/plans/:id` 删除(软删;只是想下架就用 PUT 改 `status=0`) * * 用户端 `GET /api/web/subject-plans` 只返回 `status=1` 的套餐,续期金额只按 `price` 计算。 */ class WebAdminPlanController extends Controller { /** GET /api/web/admin/plans */ async index() { const { ctx } = this; const list = await ctx.service.adminPlan.list(); ctx.body = { ok: true, data: { list, total: list.length } }; } /** POST /api/web/admin/plans */ async create() { const { ctx } = this; const data = await ctx.service.adminPlan.create(ctx.request.body || {}); ctx.status = 201; ctx.body = { ok: true, data }; } /** PUT /api/web/admin/plans/:id */ async update() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const data = await ctx.service.adminPlan.update(id, ctx.request.body || {}); ctx.body = { ok: true, data }; } /** DELETE /api/web/admin/plans/:id */ async destroy() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const data = await ctx.service.adminPlan.remove(id); ctx.body = { ok: true, data }; } } module.exports = WebAdminPlanController;