二级菜单初步实现
This commit is contained in:
@@ -68,17 +68,6 @@ export default function AppLayout() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMenu = () => {
|
|
||||||
const data = { pageNum: 1, pageSize: 10000 };
|
|
||||||
getMenuList(data).then(res => {
|
|
||||||
if (res.code == 200) {
|
|
||||||
const rawList = res.rows || [];
|
|
||||||
const topMenus = rawList.filter(item => item.parentId === 0);
|
|
||||||
setMenuList(topMenus);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getIcon = (path) => {
|
const getIcon = (path) => {
|
||||||
if (path.includes('overview')) return <DashboardOutlined />;
|
if (path.includes('overview')) return <DashboardOutlined />;
|
||||||
if (path.includes('device')) return <ExperimentOutlined />;
|
if (path.includes('device')) return <ExperimentOutlined />;
|
||||||
@@ -90,11 +79,68 @@ export default function AppLayout() {
|
|||||||
return <AppstoreOutlined />;
|
return <AppstoreOutlined />;
|
||||||
};
|
};
|
||||||
|
|
||||||
const menuItems = menuList.map(item => ({
|
const buildMenuTree = (list: any[]) => {
|
||||||
key: item.path || `/menu/${item.menuId}`,
|
const map = new Map();
|
||||||
icon: getIcon(item.path),
|
const tree: any[] = [];
|
||||||
|
|
||||||
|
// 建立映射
|
||||||
|
list.forEach(item => {
|
||||||
|
map.set(item.menuId, {
|
||||||
|
...item,
|
||||||
|
key: item.path,
|
||||||
label: item.menuName,
|
label: item.menuName,
|
||||||
}));
|
icon: getIcon(item.path),
|
||||||
|
children: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 建树
|
||||||
|
list.forEach(item => {
|
||||||
|
const node = map.get(item.menuId);
|
||||||
|
|
||||||
|
if (item.parentId === 0) {
|
||||||
|
tree.push(node);
|
||||||
|
} else {
|
||||||
|
const parent = map.get(item.parentId);
|
||||||
|
if (parent) {
|
||||||
|
parent.children.push(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 排序
|
||||||
|
const sortMenu = (menus: any[]) => {
|
||||||
|
menus.sort((a, b) => a.orderNum - b.orderNum);
|
||||||
|
menus.forEach(menu => {
|
||||||
|
if (menu.children.length) {
|
||||||
|
sortMenu(menu.children);
|
||||||
|
} else {
|
||||||
|
delete menu.children;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
sortMenu(tree);
|
||||||
|
|
||||||
|
return tree;
|
||||||
|
};
|
||||||
|
const renderMenuItems = (treeList, isChild = false) => {
|
||||||
|
return treeList.map(item => {
|
||||||
|
const hasChild = item.children && item.children.length > 0;
|
||||||
|
const key = item.path || `/menu/${item.menuId}`;
|
||||||
|
return {
|
||||||
|
key,
|
||||||
|
icon: isChild ? null : getIcon(item.path), // 子菜单无图标
|
||||||
|
label: item.menuName,
|
||||||
|
...(hasChild && {
|
||||||
|
children: renderMenuItems(item.children, true),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
await dispatch(logout());
|
await dispatch(logout());
|
||||||
@@ -102,6 +148,18 @@ export default function AppLayout() {
|
|||||||
navigate('/login', { replace: true });
|
navigate('/login', { replace: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getMenu = () => {
|
||||||
|
getMenuList({
|
||||||
|
pageNum: 1,
|
||||||
|
pageSize: 10000,
|
||||||
|
}).then(res => {
|
||||||
|
if (res.code === 200) {
|
||||||
|
const menus = buildMenuTree(res.rows || []);
|
||||||
|
setMenuList(menus);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
|
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
|
||||||
getMenu();
|
getMenu();
|
||||||
@@ -234,7 +292,7 @@ export default function AppLayout() {
|
|||||||
collapsible
|
collapsible
|
||||||
collapsed={collapsed}
|
collapsed={collapsed}
|
||||||
trigger={null}
|
trigger={null}
|
||||||
width={150}
|
width={180}
|
||||||
theme="light"
|
theme="light"
|
||||||
style={{ borderRight: '1px solid #e5e7eb' }}
|
style={{ borderRight: '1px solid #e5e7eb' }}
|
||||||
>
|
>
|
||||||
@@ -243,12 +301,10 @@ export default function AppLayout() {
|
|||||||
<Menu
|
<Menu
|
||||||
mode="inline"
|
mode="inline"
|
||||||
theme="light"
|
theme="light"
|
||||||
|
items={menuList}
|
||||||
selectedKeys={[location.pathname]}
|
selectedKeys={[location.pathname]}
|
||||||
items={menuItems}
|
|
||||||
onClick={({ key }) => navigate(key)}
|
onClick={({ key }) => navigate(key)}
|
||||||
style={{ flex: 1, overflowY: 'auto', borderRight: 0 }}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* 场站信息卡片:仅在侧边栏展开时显示 */}
|
{/* 场站信息卡片:仅在侧边栏展开时显示 */}
|
||||||
{!collapsed && currentStation && (
|
{!collapsed && currentStation && (
|
||||||
<Card
|
<Card
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ const Organization = lazy(() => import('../components/SystemSetting/Organization
|
|||||||
const StationManage = lazy(() => import('../components/SystemSetting/StationManage'));
|
const StationManage = lazy(() => import('../components/SystemSetting/StationManage'));
|
||||||
const SystemSetting = lazy(() => import('../pages/SystemSetting'));
|
const SystemSetting = lazy(() => import('../pages/SystemSetting'));
|
||||||
const Profile = lazy(() => import('../pages/Profile'));
|
const Profile = lazy(() => import('../pages/Profile'));
|
||||||
|
const DeviceStatus = lazy(() => import('../components/devices/DeviceStatusPage'));
|
||||||
|
|
||||||
// 加载中动画
|
// 加载中动画
|
||||||
function PageFallback() {
|
function PageFallback() {
|
||||||
@@ -104,13 +105,21 @@ const routes: RouteObject[] = [
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'devices',
|
path: 'devices/overview',
|
||||||
element: (
|
element: (
|
||||||
<Suspense fallback={<PageFallback />}>
|
<Suspense fallback={<PageFallback />}>
|
||||||
<Devices />
|
<Devices />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: 'devices/status',
|
||||||
|
element: (
|
||||||
|
<Suspense fallback={<PageFallback />}>
|
||||||
|
<DeviceStatus />
|
||||||
|
</Suspense>
|
||||||
|
)
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: 'video',
|
path: 'video',
|
||||||
element: (
|
element: (
|
||||||
|
|||||||
Reference in New Issue
Block a user