"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 };