From 3d7b8d705dae24d8739debed163065b1cb451e29 Mon Sep 17 00:00:00 2001 From: liamzi Date: Wed, 15 Jan 2025 11:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=A7=E8=BE=B9=E6=A0=8F=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E6=95=88=E6=9E=9C=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Layout/Sidebar.jsx | 10 ++++++---- src/components/auth/ProtectedRoute.jsx | 7 +++---- src/pages/home/index.jsx | 11 +++++++++++ src/routes/AppRoutes.jsx | 7 +++---- src/routes/routes.js | 8 +++++++- src/utils/menuUtils.jsx | 2 +- 6 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 src/pages/home/index.jsx diff --git a/src/components/Layout/Sidebar.jsx b/src/components/Layout/Sidebar.jsx index 807cfa8..d606931 100644 --- a/src/components/Layout/Sidebar.jsx +++ b/src/components/Layout/Sidebar.jsx @@ -18,9 +18,7 @@ const Sidebar = ({ collapsed }) => { if (!user?.id||user.menukeys?.length===0) return []; return getMenuItems(user?.menukeys || []); }, [user]); -useEffect(()=>{ - console.log(menuClient,'menuClient'); -},[menuClient]) + const defaultOpenKeys = useMemo(() => { const pathSegments = location.pathname.split("/").filter(Boolean); return pathSegments.reduce((acc, _, index) => { @@ -30,6 +28,10 @@ useEffect(()=>{ }, []); }, [location.pathname]); + const selectedKey = useMemo(() => { + return location.pathname; + }, [location.pathname]); + const handleMenuClick = ({ key }) => { navigate(key); }; @@ -48,7 +50,7 @@ useEffect(()=>{ { const { user } = useAuth(); const location = useLocation(); @@ -15,13 +15,12 @@ export const ProtectedRoute = ({ children }) => { return currentPath === key || currentPath.startsWith(`${key}/`); }); - if (!hasPermission ) { - return ; + if (!hasPermission) { + return ; } return children; } - // 如果用户未登录,重定向到登录页 return ; }; diff --git a/src/pages/home/index.jsx b/src/pages/home/index.jsx new file mode 100644 index 0000000..2c46511 --- /dev/null +++ b/src/pages/home/index.jsx @@ -0,0 +1,11 @@ +import React from 'react'; + +const Home = () => { + return ( +
+ 首页 +
+ ); +}; + +export default Home; \ No newline at end of file diff --git a/src/routes/AppRoutes.jsx b/src/routes/AppRoutes.jsx index 287dc18..4f53cb1 100644 --- a/src/routes/AppRoutes.jsx +++ b/src/routes/AppRoutes.jsx @@ -68,8 +68,8 @@ const AppRoutes = () => { + user?.id ? ( + ) : ( ) @@ -84,10 +84,9 @@ const AppRoutes = () => { } > - {/* 默认重定向到服务管理页面 */} } + element={} /> {renderRoutes(allRoutes)} } /> diff --git a/src/routes/routes.js b/src/routes/routes.js index 23cb9c8..93a01af 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -1,7 +1,13 @@ import { lazy } from "react"; -// 所有可用的路由配置 export const allRoutes = [ + { + path: "/home", + component: lazy(() => import("@/pages/home")), + name: "首页", + hidden: true, + key: "home", + }, { path: "dashboard", component: lazy(() => import("@/pages/Dashboard")), diff --git a/src/utils/menuUtils.jsx b/src/utils/menuUtils.jsx index 19771bc..9119a75 100644 --- a/src/utils/menuUtils.jsx +++ b/src/utils/menuUtils.jsx @@ -39,7 +39,7 @@ const generateMenuItems = (routes, menuKeys = [], parentPath = "") => { const icon = route.icon && ; const menuItem = { - key: route.key, // 使用 key 而不是 fullPath + key: fullPath, icon, label: route.name, };