fix: 订单与档案接口收敛错误细节与写入校验

internalError 只把通用文案回给前端、详情进日志;状态迁移规则抽到 utils/record_state.js 单点维护;档案更新开 runValidators 并补姓名/手机号/身高体重校验与每人档案上限;已支付订单改为软删除保留审计。
This commit is contained in:
lik
2026-10-24 22:24:39 +08:00
committed by trae-solo-agent
parent 1774239553
commit f4760a4c5b
6 changed files with 174 additions and 32 deletions
+27
View File
@@ -0,0 +1,27 @@
/**
* 陪诊订单状态机(HTTP 接口与管理端 Agent 工具共用同一份规则)
*
* 之前 Agent 的 escort_record_set 直接改 status,绕过了接口层的迁移校验,
* 可以把已完成订单改回进行中。规则集中在这里,任何入口都必须走 canTransition。
*/
// 允许的状态迁移;终态不可再变
const STATUS_TRANSITIONS = {
pending: ["confirmed", "cancelled"],
confirmed: ["in_progress", "cancelled"],
in_progress: ["completed"],
completed: [],
cancelled: [],
};
/**
* 判断状态迁移是否允许
* @param {string} from 当前状态
* @param {string} to 目标状态
* @returns {boolean}
*/
function canTransition(from, to) {
return (STATUS_TRANSITIONS[from] || []).includes(to);
}
export { STATUS_TRANSITIONS, canTransition };
+19 -3
View File
@@ -1,6 +1,11 @@
import logger from "./logger.js";
/**
* 响应工具类
* 提供统一的响应格式
*
* 约定(与小程序端保持一致):业务状态一律走 body.code,HTTP 状态码固定 200;
* 同时把 code 写进 X-Biz-Code 响应头,便于 nginx/监控按业务码告警而不必解析响应体。
*/
class ResponseUtil {
/**
@@ -11,6 +16,7 @@ class ResponseUtil {
*/
static success(ctx, data, message = '操作成功') {
ctx.status = 200;
ctx.set('X-Biz-Code', '0');
ctx.body = {
code: 0,
msg: message,
@@ -28,6 +34,7 @@ class ResponseUtil {
static error(ctx, message, data = null, errorCode = 500) {
// 根据规则,只要进入controller,http状态码都是200
ctx.status = 200;
ctx.set('X-Biz-Code', String(errorCode));
ctx.body = {
code: errorCode,
msg: message,
@@ -74,11 +81,20 @@ class ResponseUtil {
/**
* 服务器内部错误响应
*
* 详细原因只进日志:err.message 可能包含 Mongo 连接串、字段名、堆栈等内部信息,
* 直接回传等于把服务端实现细节暴露给前端/攻击者。
*
* @param {Object} ctx - Koa上下文对象
* @param {string} message - 错误消息
* @param {string|Error} detail - 内部错误详情(仅用于日志)
* @param {Object} data - 附加数据
*/
static internalError(ctx, message = '服务器内部错误', data = null) {
return ResponseUtil.error(ctx, message, data, 500);
static internalError(ctx, detail = null, data = null) {
if (detail) {
const text = detail instanceof Error ? detail.stack : String(detail);
logger.error(`[internal] ${ctx.method} ${ctx.path}: ${text}`);
}
return ResponseUtil.error(ctx, '服务器繁忙,请稍后重试', data, 500);
}
}