From adaf10376d1f211fa0c5ec18f110148520168155 Mon Sep 17 00:00:00 2001 From: lik Date: Fri, 6 Nov 2026 21:01:56 +0800 Subject: [PATCH] tmp --- app.js | 3 ++- handler/health_profile.js | 39 +++++++++++++++++++++++++++++++++++++++ routes/index.js | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 765b6d7..15122c8 100644 --- a/app.js +++ b/app.js @@ -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 diff --git a/handler/health_profile.js b/handler/health_profile.js index e497f96..d32f265 100644 --- a/handler/health_profile.js +++ b/handler/health_profile.js @@ -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 { diff --git a/routes/index.js b/routes/index.js index 3e08449..37ca74b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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));