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, "缺少手机号"); return ResponseUtil.badRequest(ctx, "缺少手机号");
} }
// 未验证的手机号只允许用于新建账号,不能合并到已有账号 // 未验证的手机号:该账号还没进入当前应用时允许并入,只补一条成员关系;
// 已在本应用注册过则拒绝,避免换微信顶号
if (!verifiedPhoneNumber) { if (!verifiedPhoneNumber) {
const existing = await DBModel.User.findOne({ "profile.mobile": mobile }); const existing = await DBModel.User.findOne({ "profile.mobile": mobile });
if (existing) { 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 { if (!user) {
user = await DBModel.User.create({ try {
profile: { name: name || mobile, mobile }, user = await DBModel.User.create({
status: { account: "normal" }, profile: { name: name || mobile, mobile },
social: { wechat: {} }, status: { account: "normal" },
app: {}, social: { wechat: {} },
}); app: {},
} catch (err) { });
if (err?.code === 11000) { } catch (err) {
return ResponseUtil.error(ctx, "该手机号已注册,请先使用手机号登录后再绑定微信", null, 409); if (err?.code === 11000) {
return ResponseUtil.error(ctx, "该手机号已注册,请先使用手机号登录后再绑定微信", null, 409);
}
throw err;
} }
throw err;
} }
} }
+59
View File
@@ -575,4 +575,63 @@ describe('微信登录', () => {
}) })
); );
}); });
it('手机号已在其他应用注册、本应用无成员关系时直接并入', async () => {
const mobile = nextMobile();
const openid = `wx-merge-${mobile}`;
openidByCode.set('code-merge', openid);
// 老账号只有就诊人端成员关系,陪诊师端还没有
const existing = await createUser(mobile, '梁娟');
await addMembership(existing.user._id, CONSUMER_APP, ['user']);
const res = await agent.post('/user/wxsignin').send({
code: 'code-merge',
appId: ADMIN_APP,
phoneNumber: mobile,
name: '张三',
});
assert.equal(res.body.code, 0, res.body.msg);
assert.equal(String(res.body.data.user._id), String(existing.user._id));
assert.equal(await DBModel.User.countDocuments({ 'profile.mobile': mobile }), 1);
// 只补当前应用的成员关系与 openid 身份,账号资料不被覆盖
const membership = await DBModel.UserApp.findOne({
userId: existing.user._id,
appId: ADMIN_APP,
}).lean();
assert.ok(membership);
assert.deepEqual(membership.roles, ['user']);
assert.ok(
await DBModel.UserIdentity.exists({
userId: existing.user._id,
appId: ADMIN_APP,
provider: 'weixin-mini',
openid,
})
);
const user = await DBModel.User.findById(existing.user._id).lean();
assert.equal(user.profile.name, '梁娟');
});
it('手机号已在本应用注册时,换微信登录被拒绝', async () => {
const mobile = nextMobile();
const existing = await createUser(mobile, '梁娟');
await addMembership(existing.user._id, ADMIN_APP, ['user']);
openidByCode.set('code-merge-bound', `wx-merge-bound-${mobile}`);
const res = await agent.post('/user/wxsignin').send({
code: 'code-merge-bound',
appId: ADMIN_APP,
phoneNumber: mobile,
name: '梁娟',
});
assert.equal(res.body.code, 409);
assert.match(res.body.msg, /已在本应用注册/);
});
}); });