二级菜单初步实现

This commit is contained in:
mmc
2026-08-03 13:19:40 +08:00
parent fa5d0363e9
commit e2663b912c
2 changed files with 86 additions and 21 deletions

View File

@@ -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) => {
if (path.includes('overview')) return <DashboardOutlined />;
if (path.includes('device')) return <ExperimentOutlined />;
@@ -90,11 +79,68 @@ export default function AppLayout() {
return <AppstoreOutlined />;
};
const menuItems = menuList.map(item => ({
key: item.path || `/menu/${item.menuId}`,
icon: getIcon(item.path),
label: item.menuName,
}));
const buildMenuTree = (list: any[]) => {
const map = new Map();
const tree: any[] = [];
// 建立映射
list.forEach(item => {
map.set(item.menuId, {
...item,
key: item.path,
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 () => {
await dispatch(logout());
@@ -102,6 +148,18 @@ export default function AppLayout() {
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(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
getMenu();
@@ -234,7 +292,7 @@ export default function AppLayout() {
collapsible
collapsed={collapsed}
trigger={null}
width={150}
width={180}
theme="light"
style={{ borderRight: '1px solid #e5e7eb' }}
>
@@ -243,12 +301,10 @@ export default function AppLayout() {
<Menu
mode="inline"
theme="light"
items={menuList}
selectedKeys={[location.pathname]}
items={menuItems}
onClick={({ key }) => navigate(key)}
style={{ flex: 1, overflowY: 'auto', borderRight: 0 }}
/>
{/* 场站信息卡片:仅在侧边栏展开时显示 */}
{!collapsed && currentStation && (
<Card