fix(admin): 跨标签页权限写入不再整页刷新
storage 事件里 permissions 变更改为原地写入权限 store, 仅 gongxue-auth token 实际变化(登录/退出/切换账号)才 reload, 避免多标签页相互触发刷新风暴
This commit is contained in:
@@ -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 = () => {
|
||||
|
||||
Reference in New Issue
Block a user