feat(signin): 手机号已在其他应用注册时并入原账号并补成员关系

This commit is contained in:
lik
2026-11-10 20:51:02 +08:00
parent efd4e4444c
commit ff262de194
2 changed files with 98 additions and 13 deletions
+39 -13
View File
@@ -1166,26 +1166,52 @@ class HandlerUser {
return ResponseUtil.badRequest(ctx, "缺少手机号");
}
// 未验证的手机号只允许用于新建账号,不能合并到已有账号
// 未验证的手机号:该账号还没进入当前应用时允许并入,只补一条成员关系;
// 已在本应用注册过则拒绝,避免换微信顶号
if (!verifiedPhoneNumber) {
const existing = await DBModel.User.findOne({ "profile.mobile": mobile });
if (existing) {
return ResponseUtil.error(ctx, "该手机号已注册,请先使用手机号登录后再绑定微信", null, 409);
const [membership, identity] = await Promise.all([
DBModel.UserApp.findOne({ userId: existing._id, appId }).lean(),
DBModel.UserIdentity.findOne({
userId: existing._id,
appId,
provider: meta.provider,
}).lean(),
]);
// 兼容过渡期旧字段:老数据可能只有 app[appId],没有 user_app 行
const legacyApp = existing.app && existing.app[appId];
if (membership || identity || legacyApp) {
return ResponseUtil.error(
ctx,
"该手机号已在本应用注册,请使用原微信登录或联系客服处理",
null,
409
);
}
// 同一手机号首次进入当前应用:并入已有平台账号,
// 步骤 5/6 会补 openid 身份与 user_app 成员关系,不重复建账号
console.warn(
`[wxSignin] 手机号已在其他应用注册,并入已有账号 user=${existing._id} app=${appId}`
);
user = existing;
}
}
try {
user = await DBModel.User.create({
profile: { name: name || mobile, mobile },
status: { account: "normal" },
social: { wechat: {} },
app: {},
});
} catch (err) {
if (err?.code === 11000) {
return ResponseUtil.error(ctx, "该手机号已注册,请先使用手机号登录后再绑定微信", null, 409);
if (!user) {
try {
user = await DBModel.User.create({
profile: { name: name || mobile, mobile },
status: { account: "normal" },
social: { wechat: {} },
app: {},
});
} catch (err) {
if (err?.code === 11000) {
return ResponseUtil.error(ctx, "该手机号已注册,请先使用手机号登录后再绑定微信", null, 409);
}
throw err;
}
throw err;
}
}