From d9c541dacc947dc55752a4f3a01d341ed43abd53 Mon Sep 17 00:00:00 2001 From: wangziqi Date: Thu, 6 Aug 2026 14:53:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin):=20=E8=B7=A8=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E9=A1=B5=E6=9D=83=E9=99=90=E5=86=99=E5=85=A5=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E6=95=B4=E9=A1=B5=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit storage 事件里 permissions 变更改为原地写入权限 store, 仅 gongxue-auth token 实际变化(登录/退出/切换账号)才 reload, 避免多标签页相互触发刷新风暴 --- apps/admin/src/layouts/MainLayout.tsx | 40 +++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/admin/src/layouts/MainLayout.tsx b/apps/admin/src/layouts/MainLayout.tsx index 89eff12..4a210b7 100644 --- a/apps/admin/src/layouts/MainLayout.tsx +++ b/apps/admin/src/layouts/MainLayout.tsx @@ -36,6 +36,7 @@ import api from '../api'; import { useAppStore } from '../store/app/appStore'; import { usePermissionStore } from '../store/permission/permissionStore'; import { useUserStore } from '../store/user/userStore'; +import { AUTH_STORAGE_NAME, PERMISSION_STORAGE_NAME } from '../store/middleware/persist'; import NotificationBell from '../components/NotificationBell'; import RouteDock from '../components/RouteDock'; import RouteKeeper from '../components/RouteKeeper'; @@ -125,9 +126,42 @@ const MainLayout: React.FC = () => { }; const handleStorage = (event: StorageEvent) => { - if (event.key !== 'token' && event.key !== 'permissions') return; - usePermissionStore.getState().beginPermissionVerification(); - window.location.reload(); + if (event.key === PERMISSION_STORAGE_NAME) { + // 其他标签页的权限更新:原地应用,避免整页刷新造成刷新风暴。 + try { + if (event.newValue === null) { + usePermissionStore.getState().clearPermissions(); + return; + } + const parsed = JSON.parse(event.newValue) as { + state?: { permissions?: string[] }; + }; + const permissions = parsed?.state?.permissions; + if (Array.isArray(permissions)) { + usePermissionStore.getState().writePermissions(permissions); + } + } catch { + // 忽略无法解析的跨标签页权限写入 + } + return; + } + if (event.key === AUTH_STORAGE_NAME) { + // 仅当登录态(token)确实变化时才整页刷新:登录、退出或切换账号。 + const currentToken = useUserStore.getState().token; + let otherToken: string | null = null; + if (event.newValue) { + try { + const parsed = JSON.parse(event.newValue) as { + state?: { token?: string | null }; + }; + otherToken = parsed?.state?.token ?? null; + } catch { + otherToken = null; + } + } + if (otherToken === currentToken) return; + window.location.reload(); + } }; const handleOnline = () => verifyPermissions(); const handleVisibilityChange = () => {