import React, { useState, useEffect } from 'react';
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
import {
Layout, Menu, Button, Space, ConfigProvider, theme
} from 'antd';
import zhCN from 'antd/locale/zh_CN';
import {
DashboardOutlined, ExperimentOutlined, VideoCameraOutlined,
BellOutlined, UserOutlined, LogoutOutlined,
AppstoreOutlined, SettingOutlined
} from '@ant-design/icons';
import logo from '../assets/logo.png';
import { useSelector } from 'react-redux';
import { RootState, useAppDispatch } from '../store';
import { logout } from '../store/userSlice';
import { getMenuList } from '../api/mune';
const { Header, Sider, Content } = Layout;
export default function AppLayout() {
const [collapsed, setCollapsed] = useState(false);
const [time, setTime] = useState(new Date().toLocaleString());
const [menuList, setMenuList] = useState([]); // 一级菜单列表
const navigate = useNavigate();
const location = useLocation();
const { userInfo } = useSelector((state: RootState) => state.user);
const dispatch = useAppDispatch();
// ======================
// 只取一级菜单(parentId=0)
// ======================
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);
}
});
};
// ======================
// 菜单图标映射
// ======================
const getIcon = (path) => {
if (path.includes('overview')) return ;
if (path.includes('device')) return ;
if (path.includes('monitor')) return ;
if (path.includes('alert')) return ;
if (path.includes('user')) return ;
if (path.includes('system')) return ;
if (path.includes('menu')) return ;
return ;
};
// ======================
// 构造 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();
return () => clearInterval(timer);
}, []);
const handleLogout = async () => {
await dispatch(logout());
navigate('/login', { replace: true });
};
return (
{!collapsed && (
光伏电站智能监控与运维系统
)}
{time}
{userInfo?.username}
} onClick={handleLogout}>
退出登录
{/* 纯平铺一级菜单 */}
);
}