feat(react19-migration): 阶段3 - 状态管理与路由
- T3.1: 创建 useAuthStore (Zustand) - login/logout/initFromStorage - T3.2: 创建 usePermissionStore (Zustand) - fetchUserMenus/hasPermission/hasRole - T3.3: 创建 useAppStore (Zustand) - collapsed 状态管理 - T3.4: 创建 React Router v7 数据路由配置 (createBrowserRouter) - T3.5: 创建 authLoader 路由守卫 (token校验→初始化→权限加载) - 创建占位页面组件 (13个路由页面 + DefaultLayout) - 更新 App.tsx 使用 RouterProvider - 安装 jwt-decode 依赖 验证: npx tsc --noEmit 通过, npm run dev 启动成功
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { redirect } from 'react-router'
|
||||
import { useAuthStore } from '@/stores/useAuthStore'
|
||||
import { usePermissionStore } from '@/stores/usePermissionStore'
|
||||
|
||||
export async function authLoader() {
|
||||
const token = localStorage.getItem('token')
|
||||
|
||||
if (!token) {
|
||||
return redirect('/login')
|
||||
}
|
||||
|
||||
const authState = useAuthStore.getState()
|
||||
|
||||
if (!authState.initialized) {
|
||||
authState.initFromStorage()
|
||||
}
|
||||
|
||||
if (!authState.isAuthenticated) {
|
||||
return redirect('/login')
|
||||
}
|
||||
|
||||
const permState = usePermissionStore.getState()
|
||||
|
||||
if (!permState.loaded) {
|
||||
const restored = permState.initFromStorage()
|
||||
if (!restored) {
|
||||
try {
|
||||
await permState.fetchUserMenus()
|
||||
} catch {
|
||||
authState.logout()
|
||||
return redirect('/login')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { createBrowserRouter, Navigate } from 'react-router'
|
||||
import { authLoader } from './guards'
|
||||
import {
|
||||
DefaultLayout,
|
||||
Login,
|
||||
Dashboard,
|
||||
UserManagement,
|
||||
RoleManagement,
|
||||
MenuManagement,
|
||||
ConfigManagement,
|
||||
DictManagement,
|
||||
FileManagement,
|
||||
NoticeManagement,
|
||||
LoginLog,
|
||||
OperationLog,
|
||||
ExceptionLog,
|
||||
Forbidden,
|
||||
} from './routes'
|
||||
|
||||
export const router = createBrowserRouter([
|
||||
{
|
||||
path: '/login',
|
||||
element: <Login />,
|
||||
},
|
||||
{
|
||||
path: '/403',
|
||||
element: <Forbidden />,
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
element: <DefaultLayout />,
|
||||
loader: authLoader,
|
||||
children: [
|
||||
{ index: true, element: <Navigate to="/dashboard" replace /> },
|
||||
{ path: 'dashboard', element: <Dashboard /> },
|
||||
{ path: 'users', element: <UserManagement /> },
|
||||
{ path: 'roles', element: <RoleManagement /> },
|
||||
{ path: 'menus', element: <MenuManagement /> },
|
||||
{ path: 'sys/config', element: <ConfigManagement /> },
|
||||
{ path: 'dict', element: <DictManagement /> },
|
||||
{ path: 'files', element: <FileManagement /> },
|
||||
{ path: 'notice', element: <NoticeManagement /> },
|
||||
{ path: 'loginlog', element: <LoginLog /> },
|
||||
{ path: 'oplog', element: <OperationLog /> },
|
||||
{ path: 'exceptionlog', element: <ExceptionLog /> },
|
||||
],
|
||||
},
|
||||
])
|
||||
@@ -0,0 +1,19 @@
|
||||
import { lazy } from 'react'
|
||||
|
||||
const Login = lazy(() => import('@/pages/login'))
|
||||
const Dashboard = lazy(() => import('@/pages/dashboard'))
|
||||
const UserManagement = lazy(() => import('@/pages/system/user'))
|
||||
const RoleManagement = lazy(() => import('@/pages/system/role'))
|
||||
const MenuManagement = lazy(() => import('@/pages/system/menu'))
|
||||
const ConfigManagement = lazy(() => import('@/pages/config/config'))
|
||||
const DictManagement = lazy(() => import('@/pages/config/dict'))
|
||||
const FileManagement = lazy(() => import('@/pages/file'))
|
||||
const NoticeManagement = lazy(() => import('@/pages/notify'))
|
||||
const LoginLog = lazy(() => import('@/pages/audit/login-log'))
|
||||
const OperationLog = lazy(() => import('@/pages/audit/operation-log'))
|
||||
const ExceptionLog = lazy(() => import('@/pages/audit/exception-log'))
|
||||
const Forbidden = lazy(() => import('@/pages/403'))
|
||||
const DefaultLayout = lazy(() => import('@/layouts/DefaultLayout'))
|
||||
|
||||
export { authLoader } from './guards'
|
||||
export { DefaultLayout, Login, Dashboard, UserManagement, RoleManagement, MenuManagement, ConfigManagement, DictManagement, FileManagement, NoticeManagement, LoginLog, OperationLog, ExceptionLog, Forbidden }
|
||||
Reference in New Issue
Block a user