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

164 lines
5.4 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,
Breadcrumb, Badge, 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,
MenuUnfoldOutlined, MenuFoldOutlined, TeamOutlined, MenuOutlined
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-28 14:45:07 +08:00
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-24 15:28:16 +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-28 14:45:07 +08:00
2026-04-22 09:43:55 +08:00
const getMenu = () => {
getMenuList().then(res => {
if (res.code == 200) {
2026-04-24 15:28:16 +08:00
setMenuList(res.data);
2026-04-22 09:43:55 +08:00
} else {
message.error("获取菜单失败:" + res.msg)
}
})
}
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 12:49:47 +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
const menuItems = [
{ key: '/', icon: <DashboardOutlined />, label: '总览首页', roles: ['admin', 'operator', 'viewer'] },
2026-04-21 08:51:57 +08:00
{ key: '/devices', icon: <ExperimentOutlined />, label: '设备管理', roles: ['admin', 'operator'] },
{ key: '/video', icon: <VideoCameraOutlined />, label: '在线视频', roles: ['admin', 'operator', 'viewer'] },
{ key: '/alerts', icon: <BellOutlined />, label: '告警中心', roles: ['admin', 'operator'] },
2026-04-21 08:51:57 +08:00
{ key: '/downloads', icon: <DownloadOutlined />, label: '视频下载', roles: ['admin', 'operator'] },
{ key: '/serialNumGenerate', icon: <UserOutlined />, label: '序列号生成', roles: ['admin'] },
2026-04-24 15:28:16 +08:00
{ key: '/users', icon: <TeamOutlined />, label: '用户管理', roles: ['admin'] },
{ key: '/roles', icon: <UserOutlined />, label: '角色管理', roles: ['admin'] },
{ key: '/menuList', icon: <MenuOutlined />, label: '菜单管理', roles: ['admin'] },
{ key: '/organization', icon: <MenuOutlined />, label: '组织管理', roles: ['admin'] },
2026-04-29 14:26:01 +08:00
{ key: '/station', icon: <MenuOutlined />, label: '场站管理', roles: ['admin'] },
{ key: '/system', icon: <MenuOutlined />, label: '系统设置', roles: ['admin'] }
2026-04-21 08:51:57 +08:00
];
2026-04-17 17:20:41 +08:00
const filteredMenu = menuItems;
const currentPathLabel = menuItems.find(i => i.key === location.pathname)?.label || '总览首页';
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: {
Layout: { headerBg: '#fff', siderBg: '#001529' },
Menu: { darkItemBg: '#001529', darkItemSelectedBg: '#1890ff' }
2026-04-21 08:51:57 +08:00
}
}}
>
<Layout style={{ maxHeight: '100vh' }}>
{/* 顶部通栏 */}
<Header className=" px-6 flex items-center justify-between shadow-sm border-b border-gray-100 h-0 shrink-0 z-10" style={{
background: '#0837a8',
color: '#fff',
height: "50px"
}}>
2026-04-21 08:51:57 +08:00
<div className="h-16 flex items-center justify-center border-b border-white/10 overflow-hidden">
<Space size="small" className="px-2">
<img src={logo} alt="Logo" style={{ width: "24px", height: "24px" }} />
{!collapsed && (
<span className="text-white font-bold text-lg whitespace-nowrap">
光伏电站智能监控与运维系统
</span>
)}
2026-04-21 08:51:57 +08:00
</Space>
</div>
2026-04-21 14:57:52 +08:00
<div className="flex items-center gap-6">
<div className="flex items-center gap-3 pr-6 border-r border-gray-100">
<span className="text-gray-400 font-mono text-[13px]">{time}</span>
<Badge status="success" />
<span className="text-white font-medium">{userInfo?.username}</span>
2026-04-21 08:51:57 +08:00
</div>
<Button
type="text"
danger
icon={<LogoutOutlined />}
onClick={handleLogout}
className="font-bold text-[12px] uppercase"
>
退出登录
</Button>
</div>
</Header>
2026-04-28 14:45:07 +08:00
<Layout style={{
overflow: 'auto', background: '#ffffff',
height: "calc(100vh - 50px)",
overflowX: 'hidden'
}}>
{/* 左侧:菜单 + 下部区域 */}
<Sider
trigger={null}
collapsible
collapsed={collapsed}
width={220}
theme="light"
style={{
overflow: 'auto', background: '#ffffff',
borderRight: '1px solid #e5e7eb',
}}
>
2026-04-17 17:20:41 +08:00
<Menu
theme="light"
mode="inline"
selectedKeys={[location.pathname]}
items={filteredMenu}
onClick={({ key }) => navigate(key)}
/>
{/* 左侧下方固定区域(你可以放任何内容) */}
<div className="p-3 text-white text-xs opacity-70 text-center mt-4">
{!collapsed && '快捷操作面板'}
2026-04-21 08:51:57 +08:00
</div>
</Sider>
{/* 右侧:主内容 */}
2026-04-28 14:45:07 +08:00
<Content className=" overflow-y-auto bg-[#f4f7f9] min-h-0 overflow-x-hidden">
2026-04-21 08:51:57 +08:00
<Outlet />
</Content>
</Layout>
</Layout>
2026-04-28 14:45:07 +08:00
</ConfigProvider >
2026-04-21 08:51:57 +08:00
);
2026-04-17 17:20:41 +08:00
}