fix(admin): 平台路由不再依赖商户上下文,消除管理端被残留上下文锁死

问题:Tenant 中间件挂在整个已登录分组上,包括 /api/platform/*。管理员
localStorage 里一旦残留一个已禁用/已删除的商户 ID,ResolveMerchantForAdmin 就会对
所有请求返回 403——连"去把它改回来"的平台页面都打不开,整个管理端被锁死。

判断依据:逐条核对 35 条 /platform/* 路由与 13 个平台侧 handler,租户依赖为 0。
平台接口本身就是跨商户的,商户上下文对它们没有意义。

- router:平台分组从租户中间件里摘出,只挂 Auth + RequireRole(admin);
  商户侧分组保持 Tenant 不变,单商户隔离语义一点没动
- 搬迁后逐条比对路由集合:平台 35 条、商户侧全部一致
- request.ts:对 /platform/* 以及管理员的 /dashboard 不再发送 X-Merchant-ID,
  作为第二道防线(后者后端本就按角色忽略商户,带上只会引入 403 风险)

回归测试:新增 TestPlatformRoutesIgnoreStaleMerchantContext(无上下文 / 残留不存在的
商户两种情形都必须 200)与 TestMerchantRoutesStillRejectStaleContext(商户侧仍须 403)。
已实测这两个用例在未修复时会失败,报 403「当前账号无权访问该商户」/「商户不存在或已禁用」。

实测(无效商户上下文下):/api/platform/orders → 200 total=37;
/api/platform/merchants、rock/config、product-catalog → 200;/api/merchant/orders → 403。
This commit is contained in:
yml2213
2026-10-06 20:39:57 +08:00
parent 8c12d1d448
commit e692790aa7
3 changed files with 144 additions and 61 deletions
+16 -1
View File
@@ -6,13 +6,28 @@ const request = axios.create({
timeout: 15000,
})
function currentRole(): string {
try {
return JSON.parse(localStorage.getItem('user') || 'null')?.role || ''
} catch {
return ''
}
}
request.interceptors.request.use((config) => {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
// 对「商户上下文本来就不起作用」的接口,刻意不带 X-Merchant-ID:
// - /platform/*:平台侧接口本身就是跨商户的;
// - 管理员的 /dashboard:后端按角色判定为平台口径,压根不看商户。
// 更要紧的是,一旦 localStorage 里残留的是已禁用/已删除的商户,服务端会对这些
// 本该正常的请求直接 403。后端已把平台路由从租户中间件里摘出来,这里是第二道防线。
const url = config.url || ''
const merchantContextIrrelevant = url.startsWith('/platform/') || (url === '/dashboard' && currentRole() === 'admin')
const merchantId = localStorage.getItem('merchant_id')
if (merchantId) {
if (merchantId && !merchantContextIrrelevant) {
config.headers['X-Merchant-ID'] = merchantId
}
return config