fix(admin): 跨标签页权限写入不再整页刷新

storage 事件里 permissions 变更改为原地写入权限 store,
仅 gongxue-auth token 实际变化(登录/退出/切换账号)才 reload,
避免多标签页相互触发刷新风暴
This commit is contained in:
2026-08-06 14:53:22 +08:00
parent ae88372ef8
commit d9c541dacc

View File

@@ -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 = () => {