Files
web-Iot/src/components/Layout.tsx

191 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-04-17 17:20:41 +08:00
import React, { useState, useEffect } from 'react';
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
2026-04-21 08:51:57 +08:00
import {
Layout, Menu, Button, Space, ConfigProvider, theme,
2026-04-29 15:01:00 +08:00
Breadcrumb, message
2026-04-17 17:20:41 +08:00
} from 'antd';
2026-04-28 14:45:07 +08:00
import zhCN from 'antd/locale/zh_CN';
2026-04-21 08:51:57 +08:00
import {
DashboardOutlined, ExperimentOutlined, VideoCameraOutlined,
BellOutlined, DownloadOutlined, UserOutlined, LogoutOutlined,
2026-04-29 15:01:00 +08:00
TeamOutlined, MenuOutlined, AppstoreOutlined, SettingOutlined
2026-04-17 17:20:41 +08:00
} from '@ant-design/icons';
2026-04-21 08:51:57 +08:00
import logo from '../assets/logo.png';
2026-04-21 14:57:52 +08:00
import { useSelector } from 'react-redux';
2026-04-22 09:43:55 +08:00
import { RootState, useAppDispatch } from '../store';
import { logout } from '../store/userSlice';
import { getMenuList } from '../api/mune';
2026-04-17 17:20:41 +08:00
const { Header, Sider, Content } = Layout;
export default function AppLayout() {
2026-04-21 08:51:57 +08:00
const [collapsed, setCollapsed] = useState(false);
const [time, setTime] = useState(new Date().toLocaleString());
2026-04-29 15:01:00 +08:00
const [menuTree, setMenuTree] = useState([]); // 动态菜单树
2026-04-21 08:51:57 +08:00
const navigate = useNavigate();
const location = useLocation();
2026-04-21 14:57:52 +08:00
const { userInfo } = useSelector((state: RootState) => state.user);
2026-04-22 09:43:55 +08:00
const dispatch = useAppDispatch();
2026-04-29 15:01:00 +08:00
// ======================
// 工具函数:构建树形菜单
// ======================
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;
};
2026-04-28 14:45:07 +08:00
2026-04-29 15:01:00 +08:00
// ======================
// 把后端菜单转成 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);
}
2026-04-28 14:45:07 +08:00
2026-04-29 15:01:00 +08:00
return item;
});
};
// ======================
// 获取动态菜单
// ======================
2026-04-22 09:43:55 +08:00
const getMenu = () => {
2026-04-29 15:01:00 +08:00
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);
2026-04-22 09:43:55 +08:00
}
2026-04-29 15:01:00 +08:00
}).catch(() => {
});
};
2026-04-17 17:20:41 +08:00
2026-04-21 08:51:57 +08:00
useEffect(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
2026-04-29 15:01:00 +08:00
getMenu(); // 加载动态菜单
2026-04-21 08:51:57 +08:00
return () => clearInterval(timer);
}, []);
2026-04-17 17:20:41 +08:00
2026-04-22 09:43:55 +08:00
const handleLogout = async () => {
await dispatch(logout());
navigate('/login', { replace: true });
2026-04-21 08:51:57 +08:00
};
2026-04-17 17:20:41 +08:00
2026-04-21 08:51:57 +08:00
return (
<ConfigProvider
2026-04-28 14:45:07 +08:00
locale={zhCN}
2026-04-21 08:51:57 +08:00
theme={{
algorithm: theme.defaultAlgorithm,
token: { colorPrimary: '#1890ff', borderRadius: 2 },
2026-04-21 08:51:57 +08:00
components: {
2026-04-29 15:01:00 +08:00
Layout: { headerBg: '#fff', siderBg: '#fff' },
Menu: { itemBg: '#fff', itemSelectedBg: '#e6f4ff' }
2026-04-21 08:51:57 +08:00
}
}}
>
<Layout style={{ maxHeight: '100vh' }}>
2026-04-29 15:01:00 +08:00
<Header style={{
background: '#0837a8',
color: '#fff',
2026-04-29 15:01:00 +08:00
height: "50px",
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
padding: '0 16px'
}}>
2026-04-29 15:01:00 +08:00
<Space size="small">
<img src={logo} alt="Logo" style={{ width: 24, height: 24 }} />
{!collapsed && (
<span style={{ fontWeight: 'bold', fontSize: 16, color: '#fff' }}>
光伏电站智能监控与运维系统
</span>
)}
</Space>
<Space size="middle">
<span style={{ fontSize: 13 }}>{time}</span>
<span>{userInfo?.username}</span>
<Button type="text" danger icon={<LogoutOutlined />} onClick={handleLogout}>
退出登录
</Button>
2026-04-29 15:01:00 +08:00
</Space>
</Header>
2026-04-28 14:45:07 +08:00
<Layout style={{
2026-04-29 15:01:00 +08:00
height: 'calc(100vh - 50px)',
overflow: 'hidden',
background: '#fff'
2026-04-28 14:45:07 +08:00
}}>
<Sider
collapsible
collapsed={collapsed}
2026-04-29 15:01:00 +08:00
trigger={null}
width={220}
theme="light"
2026-04-29 15:01:00 +08:00
style={{ borderRight: '1px solid #e5e7eb' }}
>
<Menu
mode="inline"
2026-04-29 15:01:00 +08:00
theme="light"
selectedKeys={[location.pathname]}
2026-04-29 15:01:00 +08:00
items={menuTree}
onClick={({ key }) => navigate(key)}
/>
</Sider>
2026-04-29 15:01:00 +08:00
<Content style={{
background: '#f4f7f9',
overflowY: 'auto',
padding: '16px'
}}>
2026-04-21 08:51:57 +08:00
<Outlet />
</Content>
</Layout>
</Layout>
2026-04-29 15:01:00 +08:00
</ConfigProvider>
2026-04-21 08:51:57 +08:00
);
2026-04-17 17:20:41 +08:00
}