Optimize route

This commit is contained in:
2024-01-24 16:09:16 +08:00
parent 7ad480e23a
commit 983d8e0923
5 changed files with 29 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
import { PRODUCTION_NAME } from '@/constants/common.constants' import { PRODUCTION_NAME } from '@/constants/common.constants'
import { getRedirectUrl } from '@/util/route' import { getRedirectUrl } from '@/util/route'
import { getLoginStatus, getVerifyStatus_async } from '@/util/auth' import { getLoginStatus, getVerifyStatus_async } from '@/util/auth'
import { Navigate } from 'react-router'
const AuthRoute = () => { const AuthRoute = () => {
const [searchParams] = useSearchParams() const [searchParams] = useSearchParams()

1
src/global.d.ts vendored
View File

@@ -25,6 +25,7 @@ interface RouteJsonObject {
menu?: boolean menu?: boolean
auth?: boolean auth?: boolean
permission?: boolean permission?: boolean
operationCode?: string
autoHide?: boolean autoHide?: boolean
children?: RouteJsonObject[] children?: RouteJsonObject[]
} }

View File

@@ -14,7 +14,8 @@ const SystemFramework = () => {
<Sidebar.ItemList> <Sidebar.ItemList>
{getSystemRouteJson().map((route) => { {getSystemRouteJson().map((route) => {
return ( return (
route.menu && ( route.menu &&
route.name && (
<Sidebar.Item <Sidebar.Item
end={route.id === 'system' ? true : undefined} end={route.id === 'system' ? true : undefined}
path={route.absolutePath} path={route.absolutePath}
@@ -22,14 +23,18 @@ const SystemFramework = () => {
text={route.name} text={route.name}
key={route.id} key={route.id}
> >
{route.children?.map((subRoute) => ( {route.children?.map(
(subRoute) =>
subRoute &&
subRoute.name && (
<Sidebar.Item <Sidebar.Item
end end
path={subRoute.absolutePath} path={subRoute.absolutePath}
text={subRoute.name} text={subRoute.name}
key={subRoute.id} key={subRoute.id}
/> />
))} )
)}
</Sidebar.Item> </Sidebar.Item>
) )
) )

View File

@@ -45,6 +45,7 @@ const system: RouteJsonObject[] = [
id: 'system-tools-index', id: 'system-tools-index',
component: lazy(() => import('@/pages/System/Tools')), component: lazy(() => import('@/pages/System/Tools')),
name: '工具管理', name: '工具管理',
operationCode: 'system:tool:query:tool',
menu: true, menu: true,
autoHide: true autoHide: true
}, },
@@ -54,6 +55,7 @@ const system: RouteJsonObject[] = [
id: 'system-tools-template', id: 'system-tools-template',
component: lazy(() => import('@/pages/System/Tools/Template')), component: lazy(() => import('@/pages/System/Tools/Template')),
name: '模板管理', name: '模板管理',
operationCode: 'system:tool:query:template',
menu: true, menu: true,
autoHide: true autoHide: true
}, },
@@ -63,6 +65,7 @@ const system: RouteJsonObject[] = [
id: 'system-tools-base', id: 'system-tools-base',
component: lazy(() => import('@/pages/System/Tools/Base')), component: lazy(() => import('@/pages/System/Tools/Base')),
name: '基板管理', name: '基板管理',
operationCode: 'system:tool:query:base',
menu: true, menu: true,
autoHide: true autoHide: true
}, },
@@ -72,6 +75,7 @@ const system: RouteJsonObject[] = [
id: 'system-tools-category', id: 'system-tools-category',
component: lazy(() => import('@/pages/System/Tools/Category')), component: lazy(() => import('@/pages/System/Tools/Category')),
name: '类别管理', name: '类别管理',
operationCode: 'system:tool:query:category',
menu: true, menu: true,
autoHide: true autoHide: true
} }

View File

@@ -1,5 +1,4 @@
import { RouteObject } from 'react-router' import { hasPathPermission, hasPermission } from '@/util/auth'
import { hasPathPermission } from '@/util/auth'
export const getRedirectUrl = (path: string, redirectUrl: string): string => { export const getRedirectUrl = (path: string, redirectUrl: string): string => {
return `${path}?redirect=${encodeURIComponent(redirectUrl)}` return `${path}?redirect=${encodeURIComponent(redirectUrl)}`
@@ -18,12 +17,13 @@ export const getAuthRoute = (
route: RouteJsonObject[], route: RouteJsonObject[],
parentPermission: boolean = false parentPermission: boolean = false
): RouteJsonObject[] => { ): RouteJsonObject[] => {
return route const temp = route
.filter( .filter(
(value) => (value) =>
value.path === '*' || value.path === '*' ||
!(value.permission || parentPermission) || !(value.permission || parentPermission) ||
hasPathPermission(value.absolutePath) (hasPathPermission(value.absolutePath) &&
(!value.operationCode || hasPermission(value.operationCode)))
) )
.map((value) => { .map((value) => {
if (value.children) { if (value.children) {
@@ -31,6 +31,12 @@ export const getAuthRoute = (
} }
return value return value
}) })
temp.push({
path: '',
absolutePath: '',
element: <Navigate to={temp[0].absolutePath} replace />
})
return temp
} }
export const mapJsonToRoute = (jsonObject: RouteJsonObject[]): RouteObject[] => { export const mapJsonToRoute = (jsonObject: RouteJsonObject[]): RouteObject[] => {