From e2663b912ca57839965002b47431eb4b74bfa201 Mon Sep 17 00:00:00 2001
From: mmc <1556375442@qq.com>
Date: Mon, 3 Aug 2026 13:19:40 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E8=8F=9C=E5=8D=95=E5=88=9D?=
=?UTF-8?q?=E6=AD=A5=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/components/Layout.tsx | 96 +++++++++++++++++++++++++++++++--------
src/router/index.tsx | 11 ++++-
2 files changed, 86 insertions(+), 21 deletions(-)
diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx
index 211074c..c8157af 100644
--- a/src/components/Layout.tsx
+++ b/src/components/Layout.tsx
@@ -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 ;
if (path.includes('device')) return ;
@@ -90,11 +79,68 @@ export default function AppLayout() {
return ;
};
- 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() {