209 lines
14 KiB
JavaScript
209 lines
14 KiB
JavaScript
import Router from "koa-router";
|
||
import { HandlerEscortRecord } from "../handler/escort_record.js";
|
||
import { HandlerPayment } from "../handler/payment.js";
|
||
import { HandlerHealthProfile } from "../handler/health_profile.js";
|
||
import { HandlerResource } from "../handler/resource.js";
|
||
import { HandlerEmployee } from "../handler/employee.js";
|
||
import { HandlerAttendantApplication } from "../handler/attendant_application.js";
|
||
import { HandlerAIMedia } from "../handler/ai_media.js";
|
||
import { HandlerTrainingApplication } from "../handler/training_application.js";
|
||
import { HandlerHospital } from "../handler/hospital.js";
|
||
import ResponseUtil from "../utils/responseUtil.js";
|
||
import { getUserByToken } from "../utils/user_service.js";
|
||
import { isAdmin } from "../utils/permissions.js";
|
||
import { rateLimit } from "../middleware/ratelimit.js";
|
||
|
||
// 写接口限流:下单最容易造成脏数据与费用纠纷,给最紧的额度;
|
||
// 修改/取消次之;查询不限制(列表页会轮询刷新)
|
||
const createLimit = rateLimit(5);
|
||
const updateLimit = rateLimit(20);
|
||
const deleteLimit = rateLimit(10);
|
||
// 语音 / 图片识别按次计费,单独限流;与普通聊天频次限制同一量级
|
||
const aiMediaLimit = rateLimit(20);
|
||
|
||
/**
|
||
* 鉴权中间件:校验 token 并将用户信息挂到 ctx.state.user
|
||
* token→user 结果带 5 分钟进程内缓存(见 utils/user_service.js)
|
||
*
|
||
* token 只从请求头读取:曾支持 ?token= 传参,但 access log 会原样记录 URL,
|
||
* 等于把登录凭证写进日志。另兼容 "Bearer xxx" 头格式,与 user 服务口径一致。
|
||
*/
|
||
async function requireAuth(ctx, next) {
|
||
const raw = ctx.header["token"] || ctx.header["authorization"];
|
||
const token = raw ? String(raw).replace(/^Bearer\s+/i, "").trim() : "";
|
||
if (!token) {
|
||
ResponseUtil.unauthorized(ctx, "缺少token");
|
||
return;
|
||
}
|
||
|
||
let user;
|
||
try {
|
||
user = await getUserByToken(token, { strict: true });
|
||
} catch (err) {
|
||
// 用户服务故障不等于登录失效:区分开才能让前端提示「稍后重试」而不是踢回登录
|
||
return ResponseUtil.error(ctx, "服务暂不可用,请稍后重试", null, 503);
|
||
}
|
||
if (!user) {
|
||
ResponseUtil.unauthorized(ctx, "登录已失效");
|
||
return;
|
||
}
|
||
ctx.state.user = user;
|
||
await next();
|
||
}
|
||
|
||
/**
|
||
* 管理员校验中间件:仅管理端小程序绑定的账号可访问管理端接口
|
||
* 与 WS 管理端 agent、订单 handler 共用 utils/permissions.js 的同一口径
|
||
*/
|
||
async function requireAdmin(ctx, next) {
|
||
const user = ctx.state.user;
|
||
if (!isAdmin(user)) {
|
||
ResponseUtil.forbidden(ctx, "无管理员权限");
|
||
return;
|
||
}
|
||
await next();
|
||
}
|
||
|
||
function printRoutes(stack) {
|
||
for (const layer of stack) {
|
||
if (layer.path) {
|
||
const methods = layer.methods.filter(m => m !== '_all');
|
||
methods.forEach(m => console.log(` [${m.toUpperCase()}] ${layer.path}`));
|
||
}
|
||
}
|
||
}
|
||
|
||
function registerRoutes(app) {
|
||
const router = new Router({ prefix: '/health' });
|
||
|
||
const handlerEscortRecord = new HandlerEscortRecord();
|
||
const handlerPayment = new HandlerPayment();
|
||
const handlerHealthProfile = new HandlerHealthProfile();
|
||
const handlerResource = new HandlerResource();
|
||
const handlerEmployee = new HandlerEmployee();
|
||
const handlerAttendantApplication = new HandlerAttendantApplication();
|
||
const handlerAIMedia = new HandlerAIMedia();
|
||
const handlerTrainingApplication = new HandlerTrainingApplication();
|
||
const handlerHospital = new HandlerHospital();
|
||
|
||
// 微信支付回调:微信服务器直调,无鉴权,靠验签保安全;
|
||
// 必须注册在 /escort-record/:id 之前,避免被通用路由抢占
|
||
router.post("/pay/notify", handlerPayment.paymentNotify.bind(handlerPayment));
|
||
router.post("/pay/refund-notify", handlerPayment.refundNotify.bind(handlerPayment));
|
||
|
||
// 敏感数据路由需要登录后访问
|
||
router.get("/escort-record/my", requireAuth, handlerEscortRecord.getMyRecords.bind(handlerEscortRecord));
|
||
router.get("/escort-record/attendant", requireAuth, handlerEscortRecord.getAttendantRecords.bind(handlerEscortRecord));
|
||
// 全量查询属管理端能力,必须管理员
|
||
router.get("/escort-record", requireAuth, requireAdmin, handlerEscortRecord.getRecords.bind(handlerEscortRecord));
|
||
|
||
// 支付相关:发起支付 / 查单确认 / 管理端退款查单兜底
|
||
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));
|
||
|
||
router.get("/escort-record/:id", requireAuth, handlerEscortRecord.getRecordById.bind(handlerEscortRecord));
|
||
// 陪诊师接单 / 共享:身份与可见范围全部由服务端判定
|
||
router.post("/escort-record/:id/accept", requireAuth, updateLimit, handlerEscortRecord.acceptRecord.bind(handlerEscortRecord));
|
||
// 管理端指派:仅未分配订单,目标须在职正式陪诊师,条件更新防并发
|
||
router.post("/escort-record/:id/assign", requireAuth, requireAdmin, updateLimit, handlerEscortRecord.assignRecord.bind(handlerEscortRecord));
|
||
router.post("/escort-record/:id/share", requireAuth, updateLimit, handlerEscortRecord.shareRecord.bind(handlerEscortRecord));
|
||
router.post("/escort-record/:id/unshare", requireAuth, updateLimit, handlerEscortRecord.unshareRecord.bind(handlerEscortRecord));
|
||
router.post("/escort-record", requireAuth, createLimit, handlerEscortRecord.createRecord.bind(handlerEscortRecord));
|
||
router.put("/escort-record/:id", requireAuth, updateLimit, handlerEscortRecord.updateRecord.bind(handlerEscortRecord));
|
||
router.patch("/escort-record/:id/status", requireAuth, updateLimit, handlerEscortRecord.updateStatus.bind(handlerEscortRecord));
|
||
router.delete("/escort-record/:id", requireAuth, deleteLimit, handlerEscortRecord.deleteRecord.bind(handlerEscortRecord));
|
||
|
||
// C 端"我的健康档案":userId 一律取自 token,必须注册在 /health-profile/:id 之前
|
||
router.get("/health-profile/mine", requireAuth, handlerHealthProfile.getMyProfiles.bind(handlerHealthProfile));
|
||
router.post("/health-profile/mine", requireAuth, createLimit, handlerHealthProfile.createMyProfile.bind(handlerHealthProfile));
|
||
router.get("/health-profile/mine/:id", requireAuth, handlerHealthProfile.getMyProfileById.bind(handlerHealthProfile));
|
||
router.put("/health-profile/mine/:id", requireAuth, updateLimit, handlerHealthProfile.updateMyProfile.bind(handlerHealthProfile));
|
||
router.delete("/health-profile/mine/:id", requireAuth, deleteLimit, handlerHealthProfile.deleteMyProfile.bind(handlerHealthProfile));
|
||
|
||
// 陪诊师培训考证报名:C 端只能给自己报名,userId 取自 token;
|
||
// 写接口与下单共用 createLimit,避免重复提交刷出多条报名
|
||
router.post("/training-application", requireAuth, createLimit, handlerTrainingApplication.createApplication.bind(handlerTrainingApplication));
|
||
router.get("/training-application/mine", requireAuth, handlerTrainingApplication.getMyApplication.bind(handlerTrainingApplication));
|
||
// 管理端报名列表必须管理员
|
||
router.get("/training-application", requireAuth, requireAdmin, handlerTrainingApplication.getApplications.bind(handlerTrainingApplication));
|
||
|
||
// 以下为管理端档案接口(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));
|
||
router.delete("/health-profile/:id", requireAuth, requireAdmin, handlerHealthProfile.deleteProfile.bind(handlerHealthProfile));
|
||
|
||
// 当前账号的员工档案:登录即可访问,供管理端判定陪诊员身份。
|
||
// 必须注册在 /employee 管理端路由之前,避免被更宽的路由抢占
|
||
// 共享订单时的陪诊师候选:在职陪诊师(含实习)可访问,最多看到姓名与等级
|
||
router.get("/employee/shareable-attendants", requireAuth, handlerEmployee.getShareableAttendants.bind(handlerEmployee));
|
||
router.get("/employee/me", requireAuth, handlerEmployee.getMyEmployee.bind(handlerEmployee));
|
||
// 首页接单开关落库:仅在职正式陪诊师可切换
|
||
router.patch("/employee/me/online", requireAuth, updateLimit, handlerEmployee.updateMyOnlineStatus.bind(handlerEmployee));
|
||
// 陪诊师本人修改服务城市:仅在职陪诊师,城市取值受服务目录白名单限制
|
||
router.patch("/employee/me/service-area", requireAuth, updateLimit, handlerEmployee.updateMyServiceArea.bind(handlerEmployee));
|
||
|
||
// 员工(陪诊师)管理:管理端 CRUD,必须管理员。
|
||
// 与下方公开的 /attendant 公示接口分离:这里返回真实姓名等内部字段
|
||
router.get("/employee", requireAuth, requireAdmin, handlerEmployee.getEmployees.bind(handlerEmployee));
|
||
router.post("/employee", requireAuth, requireAdmin, createLimit, handlerEmployee.createEmployee.bind(handlerEmployee));
|
||
router.put("/employee/:id", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployee.bind(handlerEmployee));
|
||
router.patch("/employee/:id/status", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployeeStatus.bind(handlerEmployee));
|
||
// 实习转正 / 调级 / 钻石设星:等级只做展示与排序,权限仍只看 stage
|
||
router.patch("/employee/:id/level", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployeeLevel.bind(handlerEmployee));
|
||
|
||
// 陪诊师入驻申请:提交与查询本人申请只要求登录;审核相关必须管理员。
|
||
// /mine 必须注册在 /:id 之前,否则会被当成 id
|
||
router.post("/attendant-application", requireAuth, createLimit, handlerAttendantApplication.createApplication.bind(handlerAttendantApplication));
|
||
router.get("/attendant-application/mine", requireAuth, handlerAttendantApplication.getMyApplication.bind(handlerAttendantApplication));
|
||
// 待审核期间修改基本信息:仅本人,且仅 pending 状态生效
|
||
router.put("/attendant-application/mine", requireAuth, updateLimit, handlerAttendantApplication.updateMyApplication.bind(handlerAttendantApplication));
|
||
// 证件材料后补:审核通过后仍可补传,必须注册在 /:id 路由之前
|
||
router.post("/attendant-application/mine/materials", requireAuth, updateLimit, handlerAttendantApplication.updateMyMaterials.bind(handlerAttendantApplication));
|
||
router.get("/attendant-application", requireAuth, requireAdmin, handlerAttendantApplication.getApplications.bind(handlerAttendantApplication));
|
||
router.get("/attendant-application/:id/materials", requireAuth, requireAdmin, handlerAttendantApplication.getApplicationMaterials.bind(handlerAttendantApplication));
|
||
router.post("/attendant-application/:id/review", requireAuth, requireAdmin, updateLimit, handlerAttendantApplication.reviewApplication.bind(handlerAttendantApplication));
|
||
|
||
// 医院基础信息:列表/详情公开,写接口仅管理员
|
||
router.get("/hospital", handlerHospital.getHospitals.bind(handlerHospital));
|
||
router.get("/hospital/:id", handlerHospital.getHospitalById.bind(handlerHospital));
|
||
router.post("/hospital", requireAuth, requireAdmin, createLimit, handlerHospital.createHospital.bind(handlerHospital));
|
||
router.put("/hospital/:id", requireAuth, requireAdmin, updateLimit, handlerHospital.updateHospital.bind(handlerHospital));
|
||
router.delete("/hospital/:id", requireAuth, requireAdmin, deleteLimit, handlerHospital.deleteHospital.bind(handlerHospital));
|
||
|
||
router.get("/service", handlerResource.getServices.bind(handlerResource));
|
||
router.get("/agreement", handlerResource.getAgreement.bind(handlerResource));
|
||
router.get("/hospital-info", handlerResource.getHospitalInfo.bind(handlerResource));
|
||
router.get("/hospital-ranking", handlerResource.getHospitalRanking.bind(handlerResource));
|
||
router.get("/department-rankings", handlerResource.getDepartmentRankings.bind(handlerResource));
|
||
router.get("/ai-quick-questions", handlerResource.getAiQuickQuestions.bind(handlerResource));
|
||
|
||
// 语音 / 图像识别:只返回文本供输入框回填,用户确认后仍走原聊天链路。
|
||
// 识别有调用成本且涉及隐私,必须登录并单独限流。
|
||
router.post("/ai/asr", requireAuth, aiMediaLimit, handlerAIMedia.transcribe.bind(handlerAIMedia));
|
||
router.post("/ai/ocr", requireAuth, aiMediaLimit, handlerAIMedia.ocr.bind(handlerAIMedia));
|
||
|
||
// 陪诊师公示(公开):面向 C 端患者展示陪同人员信息。
|
||
// 与上面的 /service、/agreement 一样属公示类资源,不加 requireAuth
|
||
router.get("/attendant", handlerEmployee.getAttendants.bind(handlerEmployee));
|
||
// 单个陪诊师详情。必须注册在 /attendant 之后,避免被它抢匹配
|
||
router.get("/attendant/:id", handlerEmployee.getAttendantById.bind(handlerEmployee));
|
||
|
||
app.use(router.routes());
|
||
app.use(router.allowedMethods());
|
||
|
||
console.log('API Routes:');
|
||
printRoutes(router.stack);
|
||
|
||
return router;
|
||
}
|
||
|
||
export default registerRoutes; |