diff --git a/src/App.tsx b/src/App.tsx index 26880b1..a574898 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { Truck, Route, Activity, Zap } from 'lucide-react'; import { Shell, type ModuleConfig } from './components/Shell'; import AssetsModule from './modules/assets/AssetsModule'; @@ -22,8 +22,29 @@ const SCHEDULING_MODULE: ModuleConfig = { id: 'scheduling', label: '智能调度', icon: Activity, component: SchedulingModule, }; +function getRouteKey(): string { + if (typeof window === 'undefined') return ''; + const path = window.location.pathname; + const hash = window.location.hash; + if (path === '/ele/import' || hash === '#/ele/import' || hash === '#ele/import') return 'ele/import'; + if (path === '/admin/feedback' || hash === '#/admin/feedback' || hash === '#admin/feedback') return 'admin/feedback'; + return ''; +} + function AuthGate() { const { isLoading, isAuthenticated, error, user } = useAuth(); + const [routeKey, setRouteKey] = useState(getRouteKey); + + // 监听 hashchange / popstate,让 a href="#/..." 跳转能即时生效 + useEffect(() => { + const update = () => setRouteKey(getRouteKey()); + window.addEventListener('hashchange', update); + window.addEventListener('popstate', update); + return () => { + window.removeEventListener('hashchange', update); + window.removeEventListener('popstate', update); + }; + }, []); const modules = useMemo(() => { if (canAccessScheduling(user?.roles)) { @@ -48,16 +69,8 @@ function AuthGate() { } // 隐藏后端管理页:通过路径或 hash 直接访问,主导航不出现 - if (typeof window !== 'undefined') { - const path = window.location.pathname; - const hash = window.location.hash; - if (path === '/ele/import' || hash === '#/ele/import' || hash === '#ele/import') { - return ; - } - if (path === '/admin/feedback' || hash === '#/admin/feedback' || hash === '#admin/feedback') { - return ; - } - } + if (routeKey === 'ele/import') return ; + if (routeKey === 'admin/feedback') return ; return ; } diff --git a/src/auth/AuthProvider.tsx b/src/auth/AuthProvider.tsx index 0a7f3fd..4b5d398 100644 --- a/src/auth/AuthProvider.tsx +++ b/src/auth/AuthProvider.tsx @@ -46,7 +46,7 @@ export default function AuthProvider({ children }: { children: ReactNode }) { userName: '本地开发', permissionLevel: 'full', depName: '', - roles: ['所有权限', 'BI-SCHEDULE-OPT'], + roles: ['所有权限', 'BI-SCHEDULE-OPT', 'BI-ADMIN-FEEDBACK'], }, error: null, }); diff --git a/src/server/auth/middleware.ts b/src/server/auth/middleware.ts index a535b2d..fd2eefd 100644 --- a/src/server/auth/middleware.ts +++ b/src/server/auth/middleware.ts @@ -23,7 +23,7 @@ export async function authMiddleware(c: Context, next: Next) { depCode: '', depName: '', permissionLevel: 'full', - roles: ['所有权限', 'BI-SCHEDULE-OPT'], + roles: ['所有权限', 'BI-SCHEDULE-OPT', 'BI-ADMIN-FEEDBACK'], }; c.set('user', devUser); return next();