Compare commits

...
2 Commits
Author SHA1 Message Date
lik ff262de194 feat(signin): 手机号已在其他应用注册时并入原账号并补成员关系 2026-11-10 20:51:02 +08:00
lik efd4e4444c refactor(app): appId wxapp-escort-admin 更名为 wxapp_companion
与陪诊师小程序工程名对齐;同步注册表、配置白名单、脚本示例与测试。数据库 user_app / user_identity / user.app 旧字段已同步迁移。
2026-11-10 16:33:35 +08:00
10 changed files with 111 additions and 26 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
"appid": "wxf73c79e16837af07",
"secret": "5a061d65f4d35d19e62e83b98f6c63cf"
},
"wxapp-escort-admin": {
"wxapp_companion": {
"appid": "wxf94dd97b91af09a2",
"secret": "79ba226684740830af0029763d3045f0"
},
+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;
}
}
+2 -2
View File
@@ -18,7 +18,7 @@ export const APP_CONFIG_SCHEMAS = {
}),
}),
"wxapp-escort-admin": Joi.object({
"wxapp_companion": Joi.object({
pageSize: Joi.number().integer().min(10).max(200),
defaultFilter: Joi.string().valid("all", "pending", "done"),
soundAlert: Joi.boolean(),
@@ -40,7 +40,7 @@ export const APP_CONFIG_DEFAULTS = {
language: "zh-CN",
notifications: { orderStatus: true, marketing: false },
},
"wxapp-escort-admin": {
"wxapp_companion": {
pageSize: 100,
defaultFilter: "all",
soundAlert: true,
+2 -2
View File
@@ -11,14 +11,14 @@ import config from "../conf.json" with { type: "json" };
*/
export const APP_REGISTRY = {
"wxapp-escort": { platform: "weixin", provider: "weixin-mini" },
"wxapp-escort-admin": { platform: "weixin", provider: "weixin-mini" },
"wxapp_companion": { platform: "weixin", provider: "weixin-mini" },
"ttapp-escort": { platform: "douyin", provider: "douyin-mini" },
"web-escort-admin": { platform: "weixin", provider: "weixin-web" },
tcm: { platform: "web", provider: "password" },
};
// 管理端应用:任一应用内的 admin 都视为管理端管理员
export const ADMIN_APP_IDS = ["wxapp-escort-admin", "web-escort-admin"];
export const ADMIN_APP_IDS = ["wxapp_companion", "web-escort-admin"];
// 本阶段只做固定角色白名单,不引入自定义 RBAC
export const GRANTABLE_ROLES = ["user", "admin"];
+1 -1
View File
@@ -77,7 +77,7 @@ async function main() {
user.meta = { ...(user.meta || {}), updatetime: Date.now() };
}
// 只授予 Web 后台这一个应用的 admin,不动小程序端(wxapp-escort-admin)的身份
// 只授予 Web 后台这一个应用的 admin,不动小程序端(wxapp_companion)的身份
user.app = user.app || {};
user.app[WEB_ADMIN_APP] = {
...(user.app[WEB_ADMIN_APP] || {}),
+2 -2
View File
@@ -7,8 +7,8 @@
* node scripts/grant_admin.js <userId> <appId> [roles]
*
* 示例:
* node scripts/grant_admin.js 66f0... wxapp-escort-admin admin
* node scripts/grant_admin.js 66f0... wxapp-escort-admin admin,user
* node scripts/grant_admin.js 66f0... wxapp_companion admin
* node scripts/grant_admin.js 66f0... wxapp_companion admin,user
*
* 同时写 user_app 与旧 app 字段,保证重构后的鉴权和 api/health 都能识别。
*/
+1 -1
View File
@@ -177,7 +177,7 @@ describe('用户接口集成测试', () => {
.send({ mobile: testMobile, passwd: testPasswd, appId });
const admin1 = await login('web-escort-admin');
const wx = await login('wxapp-escort-admin');
const wx = await login('wxapp_companion');
assert.equal(admin1.body.code, 0, admin1.body.msg);
assert.equal(wx.body.code, 0, wx.body.msg);
const admin1Token = admin1.body.data.user.security.token;
+1 -1
View File
@@ -53,7 +53,7 @@ describe('resolveConfig', () => {
});
it('兼容 Map 存储', () => {
const config = resolveConfig('wxapp-escort-admin', new Map([['pageSize', 50]]));
const config = resolveConfig('wxapp_companion', new Map([['pageSize', 50]]));
assert.equal(config.pageSize, 50);
assert.equal(config.soundAlert, true);
});
+1 -1
View File
@@ -23,7 +23,7 @@ describe('APP_REGISTRY', () => {
});
it('管理端应用与可授权角色配置正确', () => {
assert.ok(ADMIN_APP_IDS.includes('wxapp-escort-admin'));
assert.ok(ADMIN_APP_IDS.includes('wxapp_companion'));
assert.ok(ADMIN_APP_IDS.includes('web-escort-admin'));
assert.deepEqual(GRANTABLE_ROLES, ['user', 'admin']);
});
+61 -2
View File
@@ -6,7 +6,7 @@ import { APP } from '../app.js';
import { DBModel } from '../models/index.js';
import { hashToken } from '../utils/crypto.js';
const ADMIN_APP = 'wxapp-escort-admin';
const ADMIN_APP = 'wxapp_companion';
const WEB_ADMIN_APP = 'web-escort-admin';
const CONSUMER_APP = 'wxapp-escort';
@@ -135,7 +135,7 @@ describe('管理端权限', () => {
it('A 应用 user + B 应用 admin:按应用逐条判角色,必须放行', async () => {
// 典型画像:先登录小程序管理端(自动 ["user"]),后被授予 Web 后台 ["admin"]。
// wxapp-escort-admin 排序在 web-escort-admin 之前,只取第一条判角色会误拒。
// wxapp_companion 排序在 web-escort-admin 之前,只取第一条判角色会误拒。
const mixed = await createUser(nextMobile(), '混合角色管理员');
await addMembership(mixed.user._id, ADMIN_APP, ['user']);
await addMembership(mixed.user._id, WEB_ADMIN_APP, ['admin']);
@@ -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, /已在本应用注册/);
});
});