This commit is contained in:
mmc
2026-04-30 10:49:44 +08:00
parent 941ad9c4f1
commit 00def159bf
9 changed files with 388 additions and 665 deletions

View File

@@ -1,14 +1,13 @@
import React, { useState, useEffect } from 'react';
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
import {
Layout, Menu, Button, Space, ConfigProvider, theme,
Breadcrumb, message
Layout, Menu, Button, Space, ConfigProvider, theme
} from 'antd';
import zhCN from 'antd/locale/zh_CN';
import {
DashboardOutlined, ExperimentOutlined, VideoCameraOutlined,
BellOutlined, DownloadOutlined, UserOutlined, LogoutOutlined,
TeamOutlined, MenuOutlined, AppstoreOutlined, SettingOutlined
BellOutlined, UserOutlined, LogoutOutlined,
AppstoreOutlined, SettingOutlined
} from '@ant-design/icons';
import logo from '../assets/logo.png';
import { useSelector } from 'react-redux';
@@ -21,91 +20,53 @@ const { Header, Sider, Content } = Layout;
export default function AppLayout() {
const [collapsed, setCollapsed] = useState(false);
const [time, setTime] = useState(new Date().toLocaleString());
const [menuTree, setMenuTree] = useState([]); // 动态菜单树
const [menuList, setMenuList] = useState([]); // 一级菜单列表
const navigate = useNavigate();
const location = useLocation();
const { userInfo } = useSelector((state: RootState) => state.user);
const dispatch = useAppDispatch();
// ======================
// 工具函数:构建树形菜单
// ======================
const buildMenuTree = (data) => {
const map = {};
const roots = [];
// 先把所有菜单放进map
data.forEach(item => {
map[item.menuId] = { ...item, children: [] };
});
// 构建父子关系
data.forEach(item => {
if (item.parentId !== 0) {
if (map[item.parentId]) {
map[item.parentId].children.push(map[item.menuId]);
}
} else {
roots.push(map[item.menuId]);
}
});
return roots;
};
// ======================
// 把后端菜单转成 antd menu 格式
// ======================
const convertToAntdMenu = (tree) => {
return tree.map(node => {
// 自动匹配图标
let IconComponent = MenuOutlined;
if (node.path.includes('overview')) IconComponent = DashboardOutlined;
if (node.path.includes('device')) IconComponent = ExperimentOutlined;
if (node.path.includes('video')) IconComponent = VideoCameraOutlined;
if (node.path.includes('alert')) IconComponent = BellOutlined;
if (node.path.includes('user')) IconComponent = UserOutlined;
if (node.path.includes('role')) IconComponent = TeamOutlined;
if (node.path.includes('menu')) IconComponent = AppstoreOutlined;
if (node.path.includes('system')) IconComponent = SettingOutlined;
const item = {
key: node.path || `/menu/${node.menuId}`,
icon: <IconComponent />,
label: node.menuName,
};
// 有子菜单递归渲染
if (node.children && node.children.length > 0) {
item.children = convertToAntdMenu(node.children);
}
return item;
});
};
// ======================
// 获取动态菜单
// 只取一级菜单(parentId=0)
// ======================
const getMenu = () => {
const data = {
pageNum: 1,
pageSize: 10000
}
const data = { pageNum: 1, pageSize: 10000 };
getMenuList(data).then(res => {
if (res.code === 200) {
const treeData = buildMenuTree(res.data || []);
const antdMenu = convertToAntdMenu(treeData);
setMenuTree(antdMenu);
if (res.code == 200) {
const rawList = res.rows || [];
// 只保留 parentId=0 的一级菜单
const topMenus = rawList.filter(item => item.parentId === 0);
setMenuList(topMenus);
}
}).catch(() => {
});
};
// ======================
// 菜单图标映射
// ======================
const getIcon = (path) => {
if (path.includes('overview')) return <DashboardOutlined />;
if (path.includes('device')) return <ExperimentOutlined />;
if (path.includes('monitor')) return <VideoCameraOutlined />;
if (path.includes('alert')) return <BellOutlined />;
if (path.includes('user')) return <UserOutlined />;
if (path.includes('system')) return <SettingOutlined />;
if (path.includes('menu')) return <AppstoreOutlined />;
return <AppstoreOutlined />;
};
// ======================
// 构造 antd menu items(纯平铺,无children)
// ======================
const menuItems = menuList.map(item => ({
key: item.path || `/menu/${item.menuId}`,
icon: getIcon(item.path),
label: item.menuName,
}));
useEffect(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
getMenu(); // 加载动态菜单
getMenu();
return () => clearInterval(timer);
}, []);
@@ -167,11 +128,12 @@ export default function AppLayout() {
theme="light"
style={{ borderRight: '1px solid #e5e7eb' }}
>
{/* 纯平铺一级菜单 */}
<Menu
mode="inline"
theme="light"
selectedKeys={[location.pathname]}
items={menuTree}
items={menuItems}
onClick={({ key }) => navigate(key)}
/>
</Sider>