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

173 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,
Avatar,
Space,
ConfigProvider,
theme,
Breadcrumb,
2026-04-21 14:57:52 +08:00
Badge,
message
2026-04-17 17:20:41 +08:00
} from 'antd';
2026-04-21 08:51:57 +08:00
import {
DashboardOutlined,
ExperimentOutlined,
VideoCameraOutlined,
BellOutlined,
DownloadOutlined,
UserOutlined,
LogoutOutlined,
MenuUnfoldOutlined,
MenuFoldOutlined,
GlobalOutlined,
2026-04-22 09:43:55 +08:00
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-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());
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();
const getMenu = () => {
getMenuList().then(res => {
if (res.code == 200) {
message.success("获取菜单成功")
} else {
message.error("获取菜单失败:" + res.msg)
}
console.log("菜单列表:", res);
//if (res.code == 200) {
// setMenuList(res.data)
//} 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-22 09:43:55 +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'] },
{ 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'] },
{ key: '/downloads', icon: <DownloadOutlined />, label: '视频下载', roles: ['admin', 'operator'] },
{ key: '/users', icon: <TeamOutlined />, label: '用户管理', roles: ['admin'] },
{ key: '/roles', icon: <UserOutlined />, label: '角色权限', roles: ['admin'] },
{ key: '/serialNumGenerate', icon: <UserOutlined />, label: '序列号生成', roles: ['admin'] },
2026-04-22 09:43:55 +08:00
{
key: '/menuList',
icon: <MenuOutlined />,
label: '菜单管理',
roles: ['admin']
}
2026-04-21 08:51:57 +08:00
];
2026-04-17 17:20:41 +08:00
2026-04-22 09:43:55 +08:00
const filteredMenu = menuItems
//.filter(item => item.roles.includes(userInfo?.role));
2026-04-17 17:20:41 +08:00
2026-04-21 08:51:57 +08:00
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
theme={{
algorithm: theme.defaultAlgorithm,
token: {
colorPrimary: '#1890ff',
borderRadius: 2,
},
components: {
Layout: {
headerBg: '#fff',
siderBg: '#001529',
},
Menu: {
darkItemBg: '#001529',
darkItemSelectedBg: '#1890ff',
}
}
}}
>
2026-04-21 14:57:52 +08:00
2026-04-21 08:51:57 +08:00
<Layout style={{ minHeight: '100vh' }}>
<Sider trigger={null} collapsible collapsed={collapsed} width={220} theme="dark">
<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>}
</Space>
</div>
<Menu
theme="dark"
mode="inline"
selectedKeys={[location.pathname]}
items={filteredMenu}
onClick={({ key }) => navigate(key)}
/>
2026-04-21 14:57:52 +08:00
2026-04-21 08:51:57 +08:00
</Sider>
<Layout>
<Header className="px-6 flex items-center justify-between shadow-sm border-b border-gray-100 h-16 bg-white shrink-0 z-10">
<div className="flex items-center gap-4">
<Button
type="text"
icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
onClick={() => setCollapsed(!collapsed)}
style={{ fontSize: '16px', width: 40, height: 40 }}
/>
<Breadcrumb
items={[
{ title: '系统首页' },
{ title: currentPathLabel }
]}
/>
</div>
2026-04-17 17:20:41 +08:00
2026-04-21 08:51:57 +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" />
2026-04-22 09:43:55 +08:00
<span className="text-gray-800 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>
<Content className="p-4 overflow-y-auto bg-[#f4f7f9] min-h-0">
<Outlet />
</Content>
</Layout>
</Layout>
</ConfigProvider>
);
2026-04-17 17:20:41 +08:00
}