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

153 lines
4.3 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 {
2026-04-30 10:49:44 +08:00
Layout, Menu, Button, Space, ConfigProvider, theme
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,
2026-04-30 10:49:44 +08:00
BellOutlined, UserOutlined, LogoutOutlined,
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-30 10:49:44 +08:00
const [menuList, setMenuList] = 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
// ======================
2026-04-30 10:49:44 +08:00
// 只取一级菜单(parentId=0)
2026-04-29 15:01:00 +08:00
// ======================
2026-04-30 10:49:44 +08:00
const getMenu = () => {
const data = { pageNum: 1, pageSize: 10000 };
getMenuList(data).then(res => {
if (res.code == 200) {
const rawList = res.rows || [];
// 只保留 parentId=0 的一级菜单
const topMenus = rawList.filter(item => item.parentId === 0);
setMenuList(topMenus);
2026-04-29 15:01:00 +08:00
}
});
};
2026-04-28 14:45:07 +08:00
2026-04-29 15:01:00 +08:00
// ======================
2026-04-30 10:49:44 +08:00
// 菜单图标映射
2026-04-29 15:01:00 +08:00
// ======================
2026-04-30 10:49:44 +08:00
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 />;
2026-04-29 15:01:00 +08:00
};
// ======================
2026-04-30 10:49:44 +08:00
// 构造 antd menu items(纯平铺,无children)
2026-04-29 15:01:00 +08:00
// ======================
2026-04-30 10:49:44 +08:00
const menuItems = menuList.map(item => ({
key: item.path || `/menu/${item.menuId}`,
icon: getIcon(item.path),
label: item.menuName,
}));
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-30 10:49:44 +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' }}
>
2026-04-30 10:49:44 +08:00
{/* 纯平铺一级菜单 */}
<Menu
mode="inline"
2026-04-29 15:01:00 +08:00
theme="light"
selectedKeys={[location.pathname]}
2026-04-30 10:49:44 +08:00
items={menuItems}
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
}