侧边栏选中效果修复
This commit is contained in:
@@ -18,9 +18,7 @@ const Sidebar = ({ collapsed }) => {
|
|||||||
if (!user?.id||user.menukeys?.length===0) return [];
|
if (!user?.id||user.menukeys?.length===0) return [];
|
||||||
return getMenuItems(user?.menukeys || []);
|
return getMenuItems(user?.menukeys || []);
|
||||||
}, [user]);
|
}, [user]);
|
||||||
useEffect(()=>{
|
|
||||||
console.log(menuClient,'menuClient');
|
|
||||||
},[menuClient])
|
|
||||||
const defaultOpenKeys = useMemo(() => {
|
const defaultOpenKeys = useMemo(() => {
|
||||||
const pathSegments = location.pathname.split("/").filter(Boolean);
|
const pathSegments = location.pathname.split("/").filter(Boolean);
|
||||||
return pathSegments.reduce((acc, _, index) => {
|
return pathSegments.reduce((acc, _, index) => {
|
||||||
@@ -30,6 +28,10 @@ useEffect(()=>{
|
|||||||
}, []);
|
}, []);
|
||||||
}, [location.pathname]);
|
}, [location.pathname]);
|
||||||
|
|
||||||
|
const selectedKey = useMemo(() => {
|
||||||
|
return location.pathname;
|
||||||
|
}, [location.pathname]);
|
||||||
|
|
||||||
const handleMenuClick = ({ key }) => {
|
const handleMenuClick = ({ key }) => {
|
||||||
navigate(key);
|
navigate(key);
|
||||||
};
|
};
|
||||||
@@ -48,7 +50,7 @@ useEffect(()=>{
|
|||||||
<Menu
|
<Menu
|
||||||
theme={isDarkMode ? "dark" : "light"}
|
theme={isDarkMode ? "dark" : "light"}
|
||||||
mode="inline"
|
mode="inline"
|
||||||
selectedKeys={[location.pathname]}
|
selectedKeys={[selectedKey]}
|
||||||
defaultOpenKeys={defaultOpenKeys}
|
defaultOpenKeys={defaultOpenKeys}
|
||||||
items={menuClient}
|
items={menuClient}
|
||||||
onClick={handleMenuClick}
|
onClick={handleMenuClick}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React from "react";
|
|||||||
import { Navigate, useLocation } from "react-router-dom";
|
import { Navigate, useLocation } from "react-router-dom";
|
||||||
import { useAuth } from "@/contexts/AuthContext";
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
|
|
||||||
const PUBLIC_PATHS = ['login', '404'];
|
const PUBLIC_PATHS = ['login', '404','home'];
|
||||||
export const ProtectedRoute = ({ children }) => {
|
export const ProtectedRoute = ({ children }) => {
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
@@ -16,12 +16,11 @@ export const ProtectedRoute = ({ children }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!hasPermission) {
|
if (!hasPermission) {
|
||||||
return <Navigate to="/404" replace />;
|
return <Navigate to="/home" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果用户未登录,重定向到登录页
|
|
||||||
return <Navigate to="/login" state={{ from: location }} replace />;
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
||||||
};
|
};
|
||||||
|
|||||||
11
src/pages/home/index.jsx
Normal file
11
src/pages/home/index.jsx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
const Home = () => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
首页
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Home;
|
||||||
@@ -68,8 +68,8 @@ const AppRoutes = () => {
|
|||||||
<Route
|
<Route
|
||||||
path="/login"
|
path="/login"
|
||||||
element={
|
element={
|
||||||
user?.id && user.menukeys?.includes('company/serviceTemplate') ? (
|
user?.id ? (
|
||||||
<Navigate to="/company/serviceTemplate" replace />
|
<Navigate to="/home" replace />
|
||||||
) : (
|
) : (
|
||||||
<Login />
|
<Login />
|
||||||
)
|
)
|
||||||
@@ -84,10 +84,9 @@ const AppRoutes = () => {
|
|||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{/* 默认重定向到服务管理页面 */}
|
|
||||||
<Route
|
<Route
|
||||||
index
|
index
|
||||||
element={<Navigate to="/company/serviceTemplate" replace />}
|
element={<Navigate to="/home" replace />}
|
||||||
/>
|
/>
|
||||||
{renderRoutes(allRoutes)}
|
{renderRoutes(allRoutes)}
|
||||||
<Route path="*" element={<NotFound />} />
|
<Route path="*" element={<NotFound />} />
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
import { lazy } from "react";
|
import { lazy } from "react";
|
||||||
|
|
||||||
// 所有可用的路由配置
|
|
||||||
export const allRoutes = [
|
export const allRoutes = [
|
||||||
|
{
|
||||||
|
path: "/home",
|
||||||
|
component: lazy(() => import("@/pages/home")),
|
||||||
|
name: "首页",
|
||||||
|
hidden: true,
|
||||||
|
key: "home",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: "dashboard",
|
path: "dashboard",
|
||||||
component: lazy(() => import("@/pages/Dashboard")),
|
component: lazy(() => import("@/pages/Dashboard")),
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ const generateMenuItems = (routes, menuKeys = [], parentPath = "") => {
|
|||||||
const icon = route.icon && <ColorIcon icon={getAntIcon(route.icon)} />;
|
const icon = route.icon && <ColorIcon icon={getAntIcon(route.icon)} />;
|
||||||
|
|
||||||
const menuItem = {
|
const menuItem = {
|
||||||
key: route.key, // 使用 key 而不是 fullPath
|
key: fullPath,
|
||||||
icon,
|
icon,
|
||||||
label: route.name,
|
label: route.name,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user