feat: 用户多应用成员关系与微信扫码登录

- 新增 user_app / user_identity 模型、应用注册表与成员关系服务,启动时校验配置覆盖

- 新增微信开放平台扫码登录:二维码地址、回调与 ticket 换取 token

- 管理端接口改为路由层声明 appIds + 角色鉴权,新增批量用户信息、锁定、授权与个人配置

- 删除账号同步清理成员关系与外部身份,避免关系被新账号复用

- 补充多应用、应用配置、注册表测试与迁移/建号脚本
This commit is contained in:
lik
2026-11-06 19:47:51 +08:00
parent a429079cd9
commit 946160f3f4
23 changed files with 2638 additions and 99 deletions
+37 -40
View File
@@ -40,10 +40,19 @@ async function findUserByToken(rawToken) {
* 认证中间件 - 验证 token 并挂载用户信息到 ctx
* 可选参数:
* - required: 是否必须登录(默认 true)
* - roles: 允许的角色列表(可选)
* - appId / appIds: 允许访问的应用(任一即可),声明后必须存在有效成员关系
* - roles: 允许的角色列表(必须与 appId / appIds 一起使用)
*
* 角色只在该应用内判定,不再遍历用户的所有应用;
* 传 appIds 是为了同时支持小程序管理端与 Web 管理端。
*/
function auth(options = {}) {
const { required = true, roles } = options;
const { required = true, appId, appIds, roles } = options;
const allowedAppIds = Array.isArray(appIds) && appIds.length
? appIds
: appId
? [appId]
: [];
return async (ctx, next) => {
const token = extractToken(ctx);
@@ -69,17 +78,32 @@ function auth(options = {}) {
return ResponseUtil.forbidden(ctx, '账户已被锁定');
}
// 角色检查
if (roles && roles.length > 0) {
const hasRole = roles.some(role => {
// 检查 app 字段中的角色
return Object.values(user.app || {}).some(appData =>
appData && Array.isArray(appData.role) && appData.role.includes(role)
);
});
if (!hasRole) {
return ResponseUtil.forbidden(ctx, '权限不足');
if (allowedAppIds.length > 0) {
// 一次取出所有允许应用的启用成员关系:用户可能同时是多个管理端成员,
// 角色判定必须逐条看,不能只取排序第一条(否则 A 应用的 user 会挡住 B 应用的 admin)
const memberships = await DBModel.UserApp.find({
userId: user._id,
appId: { $in: allowedAppIds },
status: 'active',
}).sort({ appId: 1 });
if (!memberships.length) {
return ResponseUtil.forbidden(ctx, '无权访问该应用');
}
let membership = memberships[0];
if (roles && roles.length > 0) {
membership = memberships.find((m) => roles.some((r) => m.roles?.includes(r)));
if (!membership) {
return ResponseUtil.forbidden(ctx, '权限不足');
}
}
ctx.state.membership = membership;
ctx.state.appId = membership.appId;
} else if (roles && roles.length > 0) {
// 角色校验必须带应用维度,缺 appId 视为路由配置错误
return ResponseUtil.internalError(ctx, '角色校验缺少 appId');
}
ctx.state.user = user;
@@ -87,33 +111,6 @@ function auth(options = {}) {
};
}
/**
* 管理端身份判定(与 health 服务 utils/permissions.js 同一口径)
*
* 不再用「app 里存在 wxapp-escort-admin 键」:该键历史上可由 /user/register 的
* 客户端入参写入,任何人都能据此自封管理员。现在注册侧拒绝写 app,
* 但库里可能残留历史账号,因此要求显式 role === 'admin';
* role 缺失的历史账号暂按管理员放行并打告警,待库中补齐 role 后收紧。
*
* @param {Object} user 用户文档或脱敏后的用户对象
* @returns {boolean}
*/
function isAdmin(user) {
const entry = user?.app?.['wxapp-escort-admin'];
if (!entry) return false;
const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean);
if (roles.includes('admin')) return true;
if (entry.role === undefined) {
console.warn(
"[auth] 管理员账号缺少 role 字段,请补齐 app.wxapp-escort-admin.role = ['admin']"
);
return true;
}
return false;
}
/**
* 返回用户安全对象(去除密码、token 等敏感字段)
* 注意:登录/注册响应中的 token 由 handler 单独附加
@@ -129,4 +126,4 @@ function sanitizeUser(user) {
return obj;
}
export { auth, extractToken, sanitizeUser, findUserByToken, isAdmin };
export { auth, extractToken, sanitizeUser, findUserByToken };