|
@ -8,6 +8,7 @@ import EmptyLayout from '@/layout/EmptyLayout.tsx' |
|
|
import ListPageLayout from '@/layout/ListPageLayout.tsx' |
|
|
import ListPageLayout from '@/layout/ListPageLayout.tsx' |
|
|
import { Route as DashboardImport } from '@/pages/dashboard' |
|
|
import { Route as DashboardImport } from '@/pages/dashboard' |
|
|
import { Route as LoginRouteImport } from '@/pages/login' |
|
|
import { Route as LoginRouteImport } from '@/pages/login' |
|
|
|
|
|
import { generateUUID } from '@/utils/uuid.ts' |
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
|
|
import { |
|
|
import { |
|
|
AnyRoute, |
|
|
AnyRoute, |
|
@ -85,10 +86,10 @@ const notAuthRoute = createRoute({ |
|
|
component: NotPermission |
|
|
component: NotPermission |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
const dashboardRoute = DashboardImport.update({ |
|
|
|
|
|
path: '/dashboard', |
|
|
|
|
|
getParentRoute: () => layoutAuthRoute, |
|
|
|
|
|
} as any) |
|
|
|
|
|
|
|
|
// const dashboardRoute = DashboardImport.update({
|
|
|
|
|
|
// path: '/dashboard',
|
|
|
|
|
|
// getParentRoute: () => layoutAuthRoute,
|
|
|
|
|
|
// } as any)
|
|
|
|
|
|
|
|
|
const loginRoute = LoginRouteImport.update({ |
|
|
const loginRoute = LoginRouteImport.update({ |
|
|
path: '/login', |
|
|
path: '/login', |
|
@ -165,50 +166,21 @@ declare module '@tanstack/react-router' { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const routeTree = rootRoute.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
//非Layout
|
|
|
|
|
|
loginRoute, |
|
|
|
|
|
emptyRoute, |
|
|
|
|
|
|
|
|
|
|
|
//不带权限Layout
|
|
|
|
|
|
layoutNormalRoute.addChildren([ |
|
|
|
|
|
notAuthRoute, |
|
|
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
|
//带权限Layout
|
|
|
|
|
|
dashboardRoute, |
|
|
|
|
|
authRoute.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
layoutAuthRoute.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
menusRoute, |
|
|
|
|
|
departmentsRoute, |
|
|
|
|
|
usersRoute, |
|
|
|
|
|
rolesRoute, |
|
|
|
|
|
] |
|
|
|
|
|
), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const generateDynamicRoutes = (menuData: MenuItem[]) => { |
|
|
|
|
|
|
|
|
export const generateDynamicRoutes = (menuData: MenuItem[], parentRoute: AnyRoute) => { |
|
|
// 递归生成路由,如果有routes则递归生成子路由
|
|
|
// 递归生成路由,如果有routes则递归生成子路由
|
|
|
|
|
|
|
|
|
const generateRoutes = (menu: MenuItem, parentRoute: AnyRoute) => { |
|
|
const generateRoutes = (menu: MenuItem, parentRoute: AnyRoute) => { |
|
|
|
|
|
|
|
|
|
|
|
console.log('gen route', menu) |
|
|
const path = menu.path?.replace(parentRoute.options?.path, '') |
|
|
const path = menu.path?.replace(parentRoute.options?.path, '') |
|
|
const isLayout = menu.children && menu.children.length > 0 && menu.type === 'menu' |
|
|
const isLayout = menu.children && menu.children.length > 0 && menu.type === 'menu' |
|
|
|
|
|
|
|
|
if (isLayout && !menu.component) { |
|
|
|
|
|
|
|
|
if (isLayout && (!menu.path || !menu.component)) { |
|
|
//没有component的layout,直接返回
|
|
|
//没有component的layout,直接返回
|
|
|
|
|
|
console.log('no-page') |
|
|
return createRoute({ |
|
|
return createRoute({ |
|
|
getParentRoute: () => parentRoute, |
|
|
|
|
|
id: path!, |
|
|
|
|
|
component: ListPageLayout, |
|
|
|
|
|
|
|
|
getParentRoute: () => layoutAuthRoute, |
|
|
|
|
|
id: `/layout-no-path-${generateUUID()}`, |
|
|
|
|
|
component: RootLayout, |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -219,11 +191,17 @@ export const generateDynamicRoutes = (menuData: MenuItem[]) => { |
|
|
} as any |
|
|
} as any |
|
|
|
|
|
|
|
|
if (isLayout) { |
|
|
if (isLayout) { |
|
|
options.id = path! |
|
|
|
|
|
|
|
|
options.id = path ?? `/layout-${generateUUID()}` |
|
|
} else { |
|
|
} else { |
|
|
options.path = path! |
|
|
|
|
|
|
|
|
if (!path) { |
|
|
|
|
|
console.log(`${menu.name}没有设置视图`) |
|
|
|
|
|
} else { |
|
|
|
|
|
options.path = path |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('gen route', options) |
|
|
|
|
|
|
|
|
//删除掉parentRoute的path,避免重复
|
|
|
//删除掉parentRoute的path,避免重复
|
|
|
const route = createRoute(options).lazy(async () => { |
|
|
const route = createRoute(options).lazy(async () => { |
|
|
|
|
|
|
|
@ -238,52 +216,95 @@ export const generateDynamicRoutes = (menuData: MenuItem[]) => { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!component) { |
|
|
if (!component) { |
|
|
return createLazyRoute(menu.path)({ |
|
|
|
|
|
|
|
|
return createLazyRoute(options.path)({ |
|
|
component: () => <div>404 Not Found</div> |
|
|
component: () => <div>404 Not Found</div> |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//处理component路径
|
|
|
|
|
|
component = component.replace(/^\/pages/, '') |
|
|
|
|
|
component = component.replace(/^\//, '') |
|
|
|
|
|
|
|
|
|
|
|
console.log('load', `/pages/${component}`) |
|
|
/* @vite-ignore */ |
|
|
/* @vite-ignore */ |
|
|
const d = await import(`${component}`) |
|
|
|
|
|
|
|
|
const d = await import(`/pages/${component}`) |
|
|
if (d.Route) { |
|
|
if (d.Route) { |
|
|
|
|
|
console.log(d.Route) |
|
|
return d.Route |
|
|
return d.Route |
|
|
} |
|
|
} |
|
|
if (d.GenRoute) { |
|
|
if (d.GenRoute) { |
|
|
return d.GenRoute(menu.path) |
|
|
|
|
|
|
|
|
return d.GenRoute(options.path) |
|
|
} |
|
|
} |
|
|
return createLazyRoute(menu.path)({ |
|
|
|
|
|
|
|
|
return createLazyRoute(options.path)({ |
|
|
component: d.default || d |
|
|
component: d.default || d |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
return route |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return route |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 对menuData递归生成路由,只处理type =1 的菜单
|
|
|
// 对menuData递归生成路由,只处理type =1 的菜单
|
|
|
const did = (menus: MenuItem[], parentRoute: AnyRoute) => { |
|
|
const did = (menus: MenuItem[], parentRoute: AnyRoute) => { |
|
|
return menus.filter((item) => item.type === 'menu').map((item) => { |
|
|
|
|
|
|
|
|
return menus.filter((item) => item.type === 'menu').map((item, index) => { |
|
|
// 如果有children则递归生成子路由,同样只处理type =1 的菜单
|
|
|
// 如果有children则递归生成子路由,同样只处理type =1 的菜单
|
|
|
const route = generateRoutes(item, parentRoute) |
|
|
const route = generateRoutes(item, parentRoute) |
|
|
|
|
|
|
|
|
|
|
|
// console.log(route)
|
|
|
if (item.children && item.children.length > 0) { |
|
|
if (item.children && item.children.length > 0) { |
|
|
const children = did(item.children, route) |
|
|
const children = did(item.children, route) |
|
|
if (children.length > 0) { |
|
|
if (children.length > 0) { |
|
|
route.addChildren(children) |
|
|
route.addChildren(children) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
route.init({ originalIndex: index }) |
|
|
return route |
|
|
return route |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return did(menuData, rootRoute) |
|
|
|
|
|
|
|
|
const routes = did(menuData, parentRoute) |
|
|
|
|
|
|
|
|
|
|
|
parentRoute.addChildren(routes) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
const routeTree = rootRoute.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
//非Layout
|
|
|
|
|
|
loginRoute, |
|
|
|
|
|
emptyRoute, |
|
|
|
|
|
|
|
|
|
|
|
//不带权限Layout
|
|
|
|
|
|
layoutNormalRoute.addChildren([ |
|
|
|
|
|
notAuthRoute, |
|
|
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
|
//带权限Layout
|
|
|
|
|
|
// dashboardRoute,
|
|
|
|
|
|
authRoute.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
layoutAuthRoute |
|
|
|
|
|
/*.addChildren( |
|
|
|
|
|
[ |
|
|
|
|
|
menusRoute, |
|
|
|
|
|
departmentsRoute, |
|
|
|
|
|
usersRoute, |
|
|
|
|
|
rolesRoute, |
|
|
|
|
|
] |
|
|
|
|
|
),*/ |
|
|
|
|
|
]), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
const router = createRouter({ |
|
|
|
|
|
routeTree, |
|
|
|
|
|
context: { queryClient, menuData: [] }, |
|
|
|
|
|
defaultPreload: 'intent' |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
export const RootProvider = memo((props: { context: Partial<IRootContext> }) => { |
|
|
export const RootProvider = memo((props: { context: Partial<IRootContext> }) => { |
|
|
|
|
|
|
|
|
|
|
|
generateDynamicRoutes(props.context.menuData ?? [], layoutAuthRoute) |
|
|
|
|
|
|
|
|
|
|
|
const router = createRouter({ |
|
|
|
|
|
routeTree, |
|
|
|
|
|
context: { queryClient, menuData: [] }, |
|
|
|
|
|
defaultPreload: 'intent' |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<QueryClientProvider client={queryClient}> |
|
|
<QueryClientProvider client={queryClient}> |
|
|
<RouterProvider router={router} context={{ ...props.context, queryClient }}/> |
|
|
<RouterProvider router={router} context={{ ...props.context, queryClient }}/> |
|
|