index.tsx
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @Author: liimin 17653212@qq.com
* @Date: 2026-09-29 18:09:37
* @LastEditors: liimin 17653212@qq.com
* @LastEditTime: 2026-09-30 11:27:07
* @FilePath: \uj-store\src\router\index.tsx
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { lazy, type ReactNode } from 'react';
import { createBrowserRouter, Navigate } from 'react-router-dom';
import BasicLayout from '@/layouts/BasicLayout';
import Login from '@/pages/Login';
import NotFound from '@/pages/NotFound';
import { useAppStore } from '@/store/useAppStore';
import RouteErrorBoundary from './RouteErrorBoundary';
// 路由级代码分割:首屏仅加载 Login/NotFound,其余页面按需懒加载
const Dashboard = lazy(() => import('@/pages/Dashboard'));
const StoreListing = lazy(() => import('@/pages/StoreListing'));
const TemplateImport = lazy(() => import('@/pages/TemplateImport'));
const StoreApprove = lazy(() => import('@/pages/StoreApprove'));
const RoleManage = lazy(() => import('@/pages/RoleManage'));
const AccountManage = lazy(() => import('@/pages/AccountManage'));
const Profile = lazy(() => import('@/pages/Profile'));
function RequireAuth({ children }: { children: ReactNode }) {
const loggedIn = useAppStore((s) => s.loggedIn);
if (!loggedIn) return <Navigate to="/login" replace />;
return <>{children}</>;
}
// React Router 7 数据路由(SPA / 声明式)
// 路由 path 必须与后端 /system/menu/tree/current 下发的 node.route 严格一致,
// 否则点菜单会 404(src/config/menu.json 仅在菜单接口未就绪时作兜底,路径同样对齐后端)。
// 新增页面 = 新增一条与后端 route 对应的路由。
export const router = createBrowserRouter([
{ path: '/login', element: <Login /> },
{
path: '/',
errorElement: <RouteErrorBoundary />,
element: (
<RequireAuth>
<BasicLayout />
</RequireAuth>
),
children: [
{ index: true, element: <Navigate to="/dashboard" replace /> },
{ path: 'dashboard', element: <Dashboard /> },
{ path: 'store/import', element: <TemplateImport /> },
{ path: 'store/approval', element: <StoreApprove /> },
{ path: 'store/listing', element: <StoreListing /> },
{ path: 'system/role', element: <RoleManage /> },
{ path: 'system/user', element: <AccountManage /> },
// 个人资料:不属于侧边栏菜单,由右上角头像下拉进入
{ path: 'profile', element: <Profile /> },
],
},
{ path: '*', element: <NotFound /> },
]);