From 81127b61aa864dd7362caa4077bff3e7e5368505 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 26 Sep 2026 18:00:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E8=A7=92=E8=89=B2=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=8C=89=E5=B9=B3=E5=8F=B0=E7=BB=B4=E5=BA=A6=E6=B8=85?= =?UTF-8?q?=E7=90=86=E7=BC=93=E5=AD=98=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=AB=AF=E5=88=87=E6=8D=A2=E8=BA=AB=E4=BB=BD?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit changeRole 写库用的是请求头 platform 原值(如 miniProgram),但清缓存调用的却是 clearUserCache(userId)(不带平台),其内部只清 web/pda/mini/app/workerstation/workstation 这几个硬编码平台;miniProgram/wx 等取值不在清单内,且 Redis key 大小写敏感, user:info:{accountId}:{platform} 会残留切换前的旧身份,紧随其后的 findById 命中 5 分钟旧缓存, 导致签发的 token current_role_id 与返回的 data.user 都是切换前的角色 (实测出现 current_role_id=34 + current_store_id=1000000 这种自相矛盾的 token)。 修复:clearUserCache(user.id, platformCode),并同步清理 platform_active:{accountId}:{platformCode}(30 分钟 TTL)。 验证(开发环境 temp/verify-change-role-fix.cjs):注入旧身份缓存后 user.info 返回注入值 → changeRole 返回 employees_id=33,token 的 current_role_id/current_store_id 自洽 → 回读缓存已刷新,DB 行保持 33 未被改动。 --- app/service/systems/auth/user.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/service/systems/auth/user.js b/app/service/systems/auth/user.js index 8b5916d..77c21f0 100644 --- a/app/service/systems/auth/user.js +++ b/app/service/systems/auth/user.js @@ -1619,8 +1619,13 @@ class UserService extends Service { platform_code: platformCode, }); - // 清理缓存 - await this.clearUserCache(user.id); + // 清理缓存:必须带上 platformCode + // clearUserCache(userId) 不带平台时只清 web/pda/mini/app/workerstation/workstation 这几个硬编码平台, + // 小程序(miniProgram)、微信(wx)等取值不在清单内且 Redis key 大小写敏感,会残留旧身份, + // 导致紧随其后的 findById 命中 5 分钟旧缓存,签发 token / 返回 user 时仍是切换前的角色。 + await this.clearUserCache(user.id, platformCode); + // 同步清理平台激活上下文缓存(platform_active:{accountId}:{platformCode},30 分钟 TTL) + await this.service.systems.accountPlatform.clearPlatformCache(user.id, platformCode); // 获取更新后的用户信息 const userInfo = await this.findById(user.id);