packServer/backend/app/controller/web/subject_order.js
liufumingren 3030f50cd3 续期改为选套餐下单,支付回调确认后才续期
数据库
- 新增 subject_plans(天数 + 定价):迁移内置三档
  30 天 ¥99 / 180 天 ¥499(推荐,划线 594)/ 365 天 ¥899(最划算,划线 1204)
- subject_orders 增加 plan_id / plan_code / plan_name 快照

后端
- GET /api/web/subject-plans:上架套餐(含 pricePerDay)
- POST /api/web/subjects/:id/orders 改为 { planId }:金额只认套餐价,
  前端传的 amount/days 一律忽略;套餐不存在/下架/未配价格 → 422
- 新增 GET /api/web/subjects/:id/orders/:orderNo(轮询支付状态)、
  POST .../orders/:orderNo/close(关闭待支付订单)
- 下单仍然只建单不动期限;续期只发生在 POST /api/pay/notify(渠道回调)成功之后

前端
- 续期不再 prompt 输入天数:弹窗展示套餐卡片(天数/价格/划线价/角标/折合每天),
  点「生成订单(¥xxx)」→ 显示订单号、金额、状态(待支付)
- 每 2s 轮询订单状态,回调确认后自动刷新主体列表并提示新到期时间;
  待支付时可「关闭订单」,避免留下悬挂单

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 17:43:18 +08:00

83 lines
2.4 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/: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 —— { planId, remark? } 选套餐下单(只建单,支付回调后才续期) */
async create() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { planId, remark } = ctx.request.body || {};
const data = await ctx.service.subjectOrder.create({
subjectId: id,
userId: ctx.state.user.id,
planId,
remark,
});
ctx.status = 201;
ctx.body = { ok: true, data };
}
/** GET /api/web/subjects/:id/orders/:orderNo —— 单笔订单(续期弹窗轮询支付状态) */
async show() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.subjectOrder.detail({
subjectId: id,
userId: ctx.state.user.id,
orderNo: ctx.params.orderNo,
});
ctx.body = { ok: true, data };
}
/** POST /api/web/subjects/:id/orders/:orderNo/close —— 关闭待支付订单 */
async close() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.subjectOrder.close({
subjectId: id,
userId: ctx.state.user.id,
orderNo: ctx.params.orderNo,
});
ctx.body = { ok: true, data };
}
}
module.exports = WebSubjectOrderController;