Files
lik cd3b109730 feat(attendant): 入驻免身份证材料、城市单选、材料后补与订单池同城过滤
入驻申请只收基本信息(身份证选填、材料全可空),服务城市改为单选白名单(北京/上海/天津/西安,默认北京);新增 POST /attendant-application/mine/materials 材料后补与 PATCH /employee/me/service-area 改城市接口;订单池按陪诊师服务城市过滤(本人已接/被共享不限城市),接单与详情二次校验;appId wxapp-escort-admin 更名为 wxapp_companion。
2026-11-10 16:33:47 +08:00

132 lines
4.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import mongoose from "mongoose";
import { DBModel } from "../models/index.js";
const ADMIN_APP = "wxapp_companion";
const WEB_ADMIN_APP = "web-escort-admin";
/**
* 管理端应用清单:传统管理小程序 + Web 后台
*
* 两者在 user_app 里是彼此独立的成员关系,这里并列是过渡期兼容写法——
* 任一应用内 role=admin 即视为管理员。等 user 服务重构完成、token 能带出
* 应用维度后,改为按请求来源应用分别校验
*/
const ADMIN_APP_IDS = [ADMIN_APP, WEB_ADMIN_APP];
/**
* 管理端 app 条目里的角色数组
*
* 注意:普通账号首次登录管理端小程序时,user 服务会写入 role: ["user"],
* 所以「有 wxapp_companion 条目」不等于陪诊员,陪诊员身份必须以 Employee 记录为准。
*
* @param {Object} user 登录用户
* @returns {string[]}
*/
function appRoles(user) {
const entry = user?.app?.[ADMIN_APP];
if (!entry) return [];
if (Array.isArray(entry.role)) return entry.role;
return entry.role ? [entry.role] : [];
}
/**
* 管理端身份判定:路由中间件、订单 handler、WebSocket 入口共用同一口径。
*
* 为什么不再用「app 里存在 wxapp_companion 键」:
* 该键历史上可由 /user/register 的客户端入参写入,任何人都能据此自封管理员。
* 现在注册侧已拒绝客户端写 app,但库里可能残留历史账号,
* 因此要求显式 role === 'admin';role 缺失的历史账号暂按管理员放行并打告警,
* 待库中补齐 app.wxapp_companion.role = ['admin'] 后再收紧(见 doc/api-review-plan.md B1)。
*
* 用户模型重构后(doc/user-multi-app-plan.md),userInfo 响应会带只读数组 apps,
* 此时优先按成员关系判定,status=disabled 的成员不再视为管理员。
*/
function isAdmin(user) {
if (!user) return false;
// 重构后的只读视图:apps: [{ appId, roles, status }]
if (Array.isArray(user.apps)) {
return user.apps.some(
(m) =>
ADMIN_APP_IDS.includes(m?.appId) &&
m.status !== "disabled" &&
Array.isArray(m.roles) &&
m.roles.includes("admin")
);
}
// 过渡期:仍读 user.app
for (const appId of ADMIN_APP_IDS) {
const entry = user.app?.[appId];
if (!entry) continue;
const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean);
if (roles.includes("admin")) return true;
// 历史兼容:role 缺失时按管理员放行并告警
if (entry.role === undefined) {
console.warn(
`[permissions] 管理员账号缺少 role 字段,请补齐 app.${appId}.role = ['admin']`
);
return true;
}
}
return false;
}
/**
* 陪诊员身份判定:以 Employee 记录为唯一可信来源
*
* @param {ObjectId|string} userId 登录用户 ID
* @returns {Promise<Object|null>} 在职员工档案(只含 role/employeeNo)
*/
async function findActiveEmployee(userId) {
const raw = typeof userId === "string" ? userId.trim() : String(userId ?? "");
if (!/^[0-9a-fA-F]{24}$/.test(raw)) return null;
try {
return await DBModel.Employee.findOne({
userId: new mongoose.Types.ObjectId(raw),
status: "active",
})
.select("role employeeNo")
.lean();
} catch (err) {
// 员工库查询失败不能把管理员一起拒之门外:返回 null 后仍走 app role 判定
console.warn("[permissions] 查询员工档案失败:", err.message);
return null;
}
}
/**
* 管理端 Agent 的使用角色
*
* - admin:显式管理员,或员工档案为管理员;
* - attendant:员工档案为在职陪诊员(app role 一般为 ["user"],不能据此判断);
* - null:无权使用。
*
* 先查员工档案再看 app role:历史账号 role 缺失时 isAdmin 会兼容放行,
* 若该账号同时也是在职陪诊员,必须收敛为陪诊员权限,不能拿到管理员级工具。
*
* @param {Object} user 登录用户
* @returns {Promise<"admin"|"attendant"|null>}
*/
async function resolveEscortAdminRole(user) {
if (!user) return null;
const roles = appRoles(user);
// 显式管理员优先,避免为管理员多查一次员工库
if (roles.includes("admin")) return "admin";
const employee = await findActiveEmployee(user._id);
if (employee?.role === "admin") return "admin";
if (employee?.role === "attendant") return "attendant";
// 兼容历史管理员(role 缺失);普通管理端账号 role 为 ["user"],不会走到这里
if (isAdmin(user)) return "admin";
if (roles.includes("attendant")) return "attendant";
return null;
}
export { isAdmin, resolveEscortAdminRole };