Compare commits
2
Commits
af9a1543ae
...
295e80a20e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
295e80a20e | ||
|
|
adaf10376d |
@@ -50,7 +50,8 @@ class APP {
|
||||
// 同时提供 /health/static,避免独立 /static 请求落到其他服务的默认响应
|
||||
this.app.use(mount('/health/static', serve(STATIC_DIR)));
|
||||
this.app.use(mount('/static', serve(STATIC_DIR)));
|
||||
this.app.use(cors());
|
||||
// Web 后台(escort.huashengtec.com)跨域直连时需要 PATCH,koa-cors 默认不含
|
||||
this.app.use(cors({ methods: 'GET,HEAD,PUT,POST,DELETE,PATCH' }));
|
||||
this.app.use((ctx, next) => {
|
||||
const parser = LARGE_BODY_PREFIXES.some((prefix) => ctx.path.startsWith(prefix))
|
||||
? largeBodyParser
|
||||
|
||||
@@ -114,6 +114,45 @@ class HandlerHealthProfile {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理端批量统计就诊人数量
|
||||
*
|
||||
* 用户列表页一屏最多 200 个用户,逐个查会产生 N+1;
|
||||
* 这里按 userIds 一次聚合,未建档用户返回 0。
|
||||
*/
|
||||
async getProfileCounts(ctx) {
|
||||
try {
|
||||
const raw = String(ctx.request.query.userIds || "");
|
||||
const ids = [...new Set(raw.split(",").map((id) => id.trim()).filter(Boolean))];
|
||||
|
||||
if (ids.length > HandlerHealthProfile.MAX_PAGE_SIZE) {
|
||||
return ResponseUtil.badRequest(ctx, `单次最多查询 ${HandlerHealthProfile.MAX_PAGE_SIZE} 位用户`);
|
||||
}
|
||||
if (!ids.length) {
|
||||
return ResponseUtil.success(ctx, { counts: {} }, "查询成功");
|
||||
}
|
||||
|
||||
const invalid = ids.filter((id) => !mongoose.Types.ObjectId.isValid(id));
|
||||
if (invalid.length) {
|
||||
return ResponseUtil.badRequest(ctx, "用户ID不合法");
|
||||
}
|
||||
|
||||
const rows = await DBModel.HealthProfile.aggregate([
|
||||
{ $match: { userId: { $in: ids.map((id) => new mongoose.Types.ObjectId(id)) } } },
|
||||
{ $group: { _id: "$userId", count: { $sum: 1 } } },
|
||||
]).exec();
|
||||
|
||||
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
|
||||
for (const row of rows) {
|
||||
counts[String(row._id)] = row.count;
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { counts }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询"我的"健康档案列表:强制以登录用户身份查询,忽略请求中的 userId
|
||||
async getMyProfiles(ctx) {
|
||||
try {
|
||||
|
||||
+50
-4
@@ -346,8 +346,9 @@ class HandlerPayment {
|
||||
/**
|
||||
* 取消已支付订单的自动原路退款(一单一笔全额退款,outRefundNo 固定 -R1)
|
||||
* 先落 refund_record(processing) 再调接口;超时/系统异常先查退款,未受理则原单号重试一次。
|
||||
* retry=true 时允许对已存在的退款流水按同一 outRefundNo 再次发起(微信对该单号幂等)。
|
||||
*/
|
||||
static async createRefundForRecord(record, { reason } = {}) {
|
||||
static async createRefundForRecord(record, { reason, retry = false } = {}) {
|
||||
const recordId = record?._id ? String(record._id) : "";
|
||||
if (!isValidObjectId(recordId)) return { skipped: true };
|
||||
|
||||
@@ -373,8 +374,8 @@ class HandlerPayment {
|
||||
});
|
||||
return existing;
|
||||
}
|
||||
// processing 不重复调接口;abnormal/closed 转人工
|
||||
return existing;
|
||||
// 默认 processing 不重复调接口;abnormal/closed 转人工;retry=true 时按同号重试
|
||||
if (!retry) return existing;
|
||||
}
|
||||
|
||||
if (!wxpay.isConfigured()) {
|
||||
@@ -382,8 +383,15 @@ class HandlerPayment {
|
||||
return { skipped: true };
|
||||
}
|
||||
|
||||
const refundReason = String(reason || record?.cancelReason || DEFAULT_REFUND_REASON).slice(0, 120);
|
||||
const refundReason = String(reason || record?.cancelReason || existing?.reason || DEFAULT_REFUND_REASON).slice(0, 120);
|
||||
let refundRecord;
|
||||
if (existing) {
|
||||
await DBModel.RefundRecord.updateOne(
|
||||
{ _id: existing._id },
|
||||
{ $set: { reason: refundReason, lastAttemptAt: new Date(), "meta.updatetime": nowMs() } }
|
||||
);
|
||||
refundRecord = await DBModel.RefundRecord.findById(existing._id);
|
||||
} else {
|
||||
try {
|
||||
refundRecord = await DBModel.RefundRecord.create({
|
||||
paymentId: payment._id,
|
||||
@@ -402,6 +410,7 @@ class HandlerPayment {
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// 先落库再调外部接口,并先把订单快照置为退款中
|
||||
await DBModel.EscortRecord.updateOne(
|
||||
@@ -962,6 +971,43 @@ class HandlerPayment {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /escort-record/:id/refund/retry (管理端)
|
||||
* 退款失败或结果不明时,按同一 outRefundNo 再次发起微信原路退款(微信幂等)
|
||||
*/
|
||||
async retryRefund(ctx) {
|
||||
try {
|
||||
const id = ctx.params.id;
|
||||
if (!isValidObjectId(id)) return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
||||
const record = await DBModel.EscortRecord.findById(id);
|
||||
if (!record || record.deletedAt) return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
||||
if ((record.payment?.status || "unpaid") !== "refunding") {
|
||||
return ResponseUtil.badRequest(ctx, "当前订单不在退款中");
|
||||
}
|
||||
|
||||
const existing = await DBModel.RefundRecord.findOne({ recordId: record._id, outRefundNo: `${record._id}-R1` });
|
||||
if (existing?.status === "success") {
|
||||
return ResponseUtil.badRequest(ctx, "该订单退款已成功,无需再次退款");
|
||||
}
|
||||
|
||||
const refund = await HandlerPayment.createRefundForRecord(record, { retry: true });
|
||||
if (refund?.skipped) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少成功支付流水,无法发起线上退款,请人工核销");
|
||||
}
|
||||
|
||||
const freshRecord = await DBModel.EscortRecord.findById(id);
|
||||
return ResponseUtil.success(ctx, {
|
||||
refund: refund ? refund.toObject() : null,
|
||||
paymentStatus: freshRecord?.payment?.status || "unpaid",
|
||||
}, "已重新发起退款");
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`[payment] 再次退款失败 recordId=${ctx.params.id} code=${err?.code || ""} requestId=${err?.requestId || ""} message=${err?.message || ""}`
|
||||
);
|
||||
return ResponseUtil.error(ctx, "再次退款失败,请稍后重试或人工核销", null, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 后台巡检查单 ====================
|
||||
|
||||
@@ -99,6 +99,8 @@ function registerRoutes(app) {
|
||||
router.post("/escort-record/:id/pay", requireAuth, createLimit, handlerPayment.createPayment.bind(handlerPayment));
|
||||
router.get("/escort-record/:id/pay/status", requireAuth, handlerPayment.queryPayment.bind(handlerPayment));
|
||||
router.post("/escort-record/:id/refund/query", requireAuth, requireAdmin, handlerPayment.queryRefundResult.bind(handlerPayment));
|
||||
// 退款失败/结果不明时按同一退款单号再次发起
|
||||
router.post("/escort-record/:id/refund/retry", requireAuth, requireAdmin, handlerPayment.retryRefund.bind(handlerPayment));
|
||||
// 线下/人工退款核销:由平台确认已退款后把订单置为 refunded
|
||||
router.post("/escort-record/:id/refund/manual", requireAuth, requireAdmin, handlerPayment.completeManualRefund.bind(handlerPayment));
|
||||
|
||||
@@ -124,6 +126,8 @@ function registerRoutes(app) {
|
||||
|
||||
// 以下为管理端档案接口(C 端一律走 /mine),必须管理员
|
||||
router.get("/health-profile", requireAuth, requireAdmin, handlerHealthProfile.getProfiles.bind(handlerHealthProfile));
|
||||
// 批量统计就诊人数量,必须注册在 /health-profile/:id 之前,否则会被当成 id
|
||||
router.get("/health-profile/counts", requireAuth, requireAdmin, handlerHealthProfile.getProfileCounts.bind(handlerHealthProfile));
|
||||
router.get("/health-profile/:id", requireAuth, requireAdmin, handlerHealthProfile.getProfileById.bind(handlerHealthProfile));
|
||||
router.post("/health-profile", requireAuth, requireAdmin, handlerHealthProfile.createProfile.bind(handlerHealthProfile));
|
||||
router.put("/health-profile/:id", requireAuth, requireAdmin, handlerHealthProfile.updateProfile.bind(handlerHealthProfile));
|
||||
|
||||
Reference in New Issue
Block a user