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
+13 -1
View File
@@ -208,7 +208,19 @@ UserSchema.statics.delUser = async function (_id, mobile, email, wxunionid, cb)
}
try {
return await this.findOneAndDelete(filter, cb);
const user = await this.findOne(filter);
if (!user) return null;
// 删除账号时同步清理成员关系与外部身份,避免残留关系被新账号复用。
// 用原生命令集合而不是 this.db.model(...):不依赖模型是否已注册。
await Promise.all([
this.db.collection('user_app').deleteMany({ userId: user._id }),
this.db.collection('user_identity').deleteMany({ userId: user._id }),
]);
await this.deleteOne({ _id: user._id });
if (typeof cb === 'function') cb(null, user);
return user;
} catch (error) {
return null;
}
+56
View File
@@ -0,0 +1,56 @@
"use strict";
import mongoose from "mongoose";
/**
* 用户与应用成员关系
*
* 一行代表「某用户在某应用中的授权状态」:
* - roles :应用内角色,首次登录固定为 ["user"],管理员只能由服务端授权
* - status :应用级启停,disabled 时挂 appId 的接口一律 403
* - config :应用内个人偏好,只放展示/偏好参数,禁止放权限与数据范围字段
*
* 注意:config 用 Map 而不是 Mixed,键级 set/delete 才能被 Mongoose 跟踪;
* 同时接口按叶子路径 $set,避免只改一个开关把同对象下其他字段清空。
*/
const UserAppSchema = new mongoose.Schema(
{
userId: { type: mongoose.Schema.Types.ObjectId, required: true, comment: "平台账号ID" },
appId: { type: String, required: true, comment: "应用ID" },
roles: { type: [String], default: ["user"], comment: "应用内角色" },
status: {
type: String,
enum: ["active", "disabled"],
default: "active",
comment: "应用级启停状态",
},
source: {
type: String,
enum: ["weixin-login", "manual", "migrated"],
default: "manual",
comment: "授权来源",
},
config: {
type: Map,
of: mongoose.Schema.Types.Mixed,
default: () => new Map(),
comment: "应用内个人配置",
},
configUpdatedAt: { type: Date, default: null, comment: "配置更新时间" },
grantedBy: { type: mongoose.Schema.Types.ObjectId, default: null, comment: "授权操作人" },
grantedAt: { type: Date, default: null, comment: "授权时间" },
lastLoginAt: { type: Date, default: null, comment: "最后登录时间" },
},
{
collection: "user_app",
minimize: false,
}
);
// 一个用户在一个应用内只能有一条成员关系
UserAppSchema.index({ userId: 1, appId: 1 }, { unique: true });
// 按应用列有效成员 / 管理员
UserAppSchema.index({ appId: 1, status: 1 });
UserAppSchema.index({ appId: 1, roles: 1 });
export { UserAppSchema };
+53
View File
@@ -0,0 +1,53 @@
"use strict";
import mongoose from "mongoose";
/**
* 外部登录身份
*
* 一行代表「某个外部账号(openid)对应哪个平台账号」:
* - appId + provider + openid:应用内身份唯一
* - provider + unionid :同一微信开放平台主体的跨应用身份
*
* openid / unionid 为空时必须写 undefined,不能写空字符串:
* partialFilterExpression 只按类型过滤,空字符串仍会被索引,
* 多个 openid: "" 会互相触发唯一冲突。
*/
const UserIdentitySchema = new mongoose.Schema(
{
userId: { type: mongoose.Schema.Types.ObjectId, required: true, comment: "平台账号ID" },
appId: { type: String, required: true, comment: "应用ID" },
platform: {
type: String,
enum: ["weixin", "douyin", "alipay", "web"],
required: true,
comment: "平台",
},
provider: {
type: String,
enum: ["weixin-mini", "weixin-web", "douyin-mini", "password"],
required: true,
comment: "登录方式",
},
openid: { type: String, comment: "应用内唯一身份" },
unionid: { type: String, comment: "跨应用身份" },
},
{
collection: "user_identity",
timestamps: true,
}
);
UserIdentitySchema.index(
{ appId: 1, provider: 1, openid: 1 },
{ unique: true, partialFilterExpression: { openid: { $type: "string" } } }
);
UserIdentitySchema.index(
{ provider: 1, unionid: 1 },
{ unique: true, partialFilterExpression: { unionid: { $type: "string" } } }
);
UserIdentitySchema.index({ userId: 1 });
export { UserIdentitySchema };