From aa295db6ae816c263d12c2cc542ae70cd61bce37 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Wed, 1 Nov 2023 11:42:31 +0800 Subject: [PATCH] Menu permission control --- src/global.d.ts | 10 +++++++--- src/pages/UserFramework.tsx | 17 ++++++++++------- src/router/index.tsx | 5 ++++- src/utils/auth.ts | 12 +++++++++--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index 644b9f4..0a7bbd7 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -16,6 +16,7 @@ type RouteJsonObject = { id?: string element?: React.JSX.Element component?: React.ComponentType + children?: RouteJsonObject[] name?: string titlePrefix?: string title?: string @@ -23,18 +24,21 @@ type RouteJsonObject = { icon?: IconComponent menu?: boolean auth?: boolean - children?: RouteJsonObject[] + permission?: boolean + autoHide?: boolean } type RouteHandle = { absolutePath: string name?: string - menu?: boolean - auth?: boolean titlePrefix?: string title?: string titlePostfix?: string icon?: IconComponent + menu?: boolean + auth?: boolean + permission?: boolean + autoHide?: boolean } type _Response = { diff --git a/src/pages/UserFramework.tsx b/src/pages/UserFramework.tsx index d2a70e2..72b4b7a 100644 --- a/src/pages/UserFramework.tsx +++ b/src/pages/UserFramework.tsx @@ -5,6 +5,7 @@ import FitFullScreen from '@/components/common/FitFullScreen' import Sidebar from '@/components/common/sidebar' import SidebarItemList from '@/components/common/sidebar/SidebarItemList' import SidebarItem from '@/components/common/sidebar/SidebarItem' +import { hasPathPermission } from '@/utils/auth.ts' const ToolsFramework: React.FC = () => { return ( @@ -14,13 +15,15 @@ const ToolsFramework: React.FC = () => { - - + hasPathPermission('/system') ? ( + + + + ) : undefined } > diff --git a/src/router/index.tsx b/src/router/index.tsx index 435affd..eab5e64 100644 --- a/src/router/index.tsx +++ b/src/router/index.tsx @@ -11,13 +11,16 @@ const mapJsonToRoute = (jsonObject: RouteJsonObject[]): RouteObject[] => { element: value.element, Component: value.component, handle: { + absolutePath: value.absolutePath, name: value.name, titlePrefix: value.titlePrefix, title: value.title, titlePostfix: value.titlePostfix, icon: value.icon, menu: value.menu, - auth: value.auth + auth: value.auth, + permission: value.permission, + autoHide: value.autoHide }, children: value.children && mapJsonToRoute(value.children) })) diff --git a/src/utils/auth.ts b/src/utils/auth.ts index c5ef482..18f37cb 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -75,16 +75,21 @@ export const getPermissionPath = (): string[] => { const user = JSON.parse(s) as UserWithInfoVo const paths: string[] = [] user.menus.forEach((menu) => { - paths.join(menu.url) + paths.push(menu.url) }) return paths } +export const hasPathPermission = (path: string) => { + return getPermissionPath().indexOf(path) !== -1 +} + +/* export const getAuthRoute = (route: RouteJsonObject[]): RouteJsonObject[] => { return route.map((value) => { - if (value.auth) { - value.path + if (value.absolutePath) { + value.absolutePath } if (value.children) { value.children = getAuthRoute(value.children) @@ -92,6 +97,7 @@ export const getAuthRoute = (route: RouteJsonObject[]): RouteJsonObject[] => { return value }) } +*/ export const getCaptchaSrc = () => { captcha = getCaptcha(300, 150, 4)