import React, { useState, useEffect } from 'react'; import { Outlet, useNavigate, useLocation } from 'react-router-dom'; import { Layout, Menu, Button, Space, Select, Avatar, Card, Tag } from 'antd'; import { DashboardOutlined, ExperimentOutlined, VideoCameraOutlined, BellOutlined, UserOutlined, LogoutOutlined, AppstoreOutlined, SettingOutlined, MenuUnfoldOutlined, MenuFoldOutlined, CompressOutlined, ExpandOutlined } from '@ant-design/icons'; import { useTranslation } from 'react-i18next'; import logo from '../assets/logo.png'; import { useSelector } from 'react-redux'; import { RootState, useAppDispatch } from '../store'; import { logout } from '../store/userSlice'; import { setCurrentStation, clearStation, setStationList as setStoreStationList } from '../store/stationSlice'; import { getMenuList } from '../api/mune'; import { getStationByUserId } from '../api/stationManage'; import LanguageSwitcher from './LanguageSwitcher'; import { nowInTz } from '../lib/dayjs'; import envConfig from '../../env'; const { Header, Sider, Content } = Layout; export default function AppLayout() { const { t } = useTranslation(); const [collapsed, setCollapsed] = useState(false); const [time, setTime] = useState(new Date().toLocaleString()); const { timezone, dateFormat } = useSelector((state: RootState) => state.preferences); const [menuList, setMenuList] = useState([]); const [stationList, setStationList] = useState([]); const [isFullScreen, setIsFullScreen] = useState(false); const navigate = useNavigate(); const location = useLocation(); const dispatch = useAppDispatch(); const { userInfo } = useSelector((state: RootState) => state.user); // 从 store 中同时取出 stationList(优先级最高,保证最新) const { currentStation, stationId, stationList: storeStationList } = useSelector((state: RootState) => state.station); const { systemName } = useSelector((state: RootState) => state.basicSettings); // 真正用于展示和选择的列表:优先用 store 中的(StationManage 编辑后会同步),否则用本地接口的 const mergedStationList = storeStationList?.length ? storeStationList : stationList; const getStationData = () => { getStationByUserId().then(res => { if (res.code === 200) { const list = res.data || []; setStationList(list); // 同步到全局 store,确保各组件共享同一份最新列表 dispatch(setStoreStationList(list)); if (!currentStation && list.length > 0) { dispatch(setCurrentStation(list[0])); } } }); }; const toggleFullScreen = () => { if (!document.fullscreenElement) { document.documentElement.requestFullscreen(); setIsFullScreen(true); } else { if (document.exitFullscreen) { document.exitFullscreen(); setIsFullScreen(false); } } }; const handleStationChange = (id: number) => { // 优先从合并后的列表中取(包含 store 中最新编辑的数据),其次从本地 stationList const station = mergedStationList.find(s => Number(s.id) === Number(id)) || stationList.find(s => Number(s.id) === Number(id)); if (station) { dispatch(setCurrentStation(station)); } }; 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 ; }; const buildMenuTree = (list: any[]) => { const map = new Map(); const tree: any[] = []; list.forEach(item => { map.set(item.menuId, { key: item.path, label: item.menuName, icon: getIcon(item.path), children: [], }); }); list.forEach(item => { const node = map.get(item.menuId); if (item.parentId === 0) { tree.push(node); } else { const parent = map.get(item.parentId); if (parent) { parent.children.push(node); } } }); const sortMenu = (menus: any[]) => { menus.sort((a, b) => a.orderNum - b.orderNum); menus.forEach(menu => { if (menu.children.length) { sortMenu(menu.children); } else { delete menu.children; } }); }; sortMenu(tree); return tree; }; const renderMenuItems = (treeList, isChild = false) => { return treeList.map(item => { const hasChild = item.children && item.children.length > 0; const key = item.path || `/menu/${item.menuId}`; return { key, icon: isChild ? null : getIcon(item.path), label: item.menuName, ...(hasChild && { children: renderMenuItems(item.children, true), }), }; }); }; const handleLogout = async () => { await dispatch(logout()); dispatch(clearStation()); navigate('/login', { replace: true }); }; const getMenu = () => { getMenuList({ pageNum: 1, pageSize: 10000, }).then(res => { if (res.code === 200) { const menus = buildMenuTree(res.rows || []); setMenuList(menus); } }); }; useEffect(() => { const timer = setInterval(() => setTime(nowInTz(timezone, dateFormat)), 1000); getMenu(); getStationData(); return () => clearInterval(timer); }, [userInfo, timezone, dateFormat]); useEffect(() => { setTime(nowInTz(timezone, dateFormat)); }, [timezone, dateFormat]); useEffect(() => { const fullscreenChange = () => { setIsFullScreen(!!document.fullscreenElement); }; document.addEventListener('fullscreenchange', fullscreenChange); return () => document.removeEventListener('fullscreenchange', fullscreenChange); }, []); return (