liufumingren 0c59bede7e feat(admin): 平台管理新增套餐管理(改价 / 改天数 / 上下架 / 删除)
- 后端:新增 service/admin_plan.js、controller/web/admin_plan.js,
  GET/POST /api/web/admin/plans、PUT/DELETE /api/web/admin/plans/:id(需平台管理员)
- 套餐 code 创建后不可改(历史订单已快照 plan_code);留空按天数自动生成(如 p30)
- 前端:新增 /admin/plans「套餐管理」页面,并接入「平台管理」菜单与路由
- 续期下单只按库里的售价算钱,改完即生效、不用发版
- 文档与测试同步更新

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 16:23:17 +08:00

61 lines
2.0 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/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;