This commit is contained in:
lik
2026-11-06 21:01:56 +08:00
parent af9a1543ae
commit adaf10376d
3 changed files with 43 additions and 1 deletions
+2 -1
View File
@@ -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
+39
View File
@@ -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 {
+2
View File
@@ -124,6 +124,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));