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

301 lines
9.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-05-18 12:55:41 +08:00
Layout, Menu, Button, Space, ConfigProvider, theme, Select, Avatar, Card, Tag
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,
2026-06-12 11:03:38 +08:00
AppstoreOutlined, SettingOutlined, MenuUnfoldOutlined, MenuFoldOutlined,
CompressOutlined,
ExpandOutlined
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';
2026-05-14 08:45:14 +08:00
import { setCurrentStation, clearStation } from '../store/stationSlice';
2026-04-22 09:43:55 +08:00
import { getMenuList } from '../api/mune';
2026-05-06 16:49:07 +08:00
import { getStationList } from '../api/stationManage';
2026-05-13 11:12:17 +08:00
import envConfig from '../../env';
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-05-06 16:49:07 +08:00
const [menuList, setMenuList] = useState([]);
const [stationList, setStationList] = useState([]);
2026-06-12 11:03:38 +08:00
const [isFullScreen, setIsFullScreen] = useState(false); // 新增全屏标识
2026-05-06 16:49:07 +08:00
2026-04-21 08:51:57 +08:00
const navigate = useNavigate();
const location = useLocation();
2026-04-22 09:43:55 +08:00
const dispatch = useAppDispatch();
2026-05-06 16:49:07 +08:00
const { userInfo } = useSelector((state: RootState) => state.user);
const { currentStation, stationId } = useSelector((state: RootState) => state.station);
const getStationData = () => {
const params = {
pageNum: 1,
pageSize: 9999,
orgId: userInfo?.orgId,
};
getStationList(params).then(res => {
if (res.code === 200) {
const list = res.rows || [];
setStationList(list);
if (!currentStation && list.length > 0) {
dispatch(setCurrentStation(list[0]));
}
}
});
};
2026-06-12 11:03:38 +08:00
const toggleFullScreen = () => {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
setIsFullScreen(true);
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
setIsFullScreen(false);
}
}
};
2026-05-06 16:49:07 +08:00
const handleStationChange = (id: number) => {
const station = stationList.find(s => s.id === id);
if (station) {
2026-05-14 08:45:14 +08:00
dispatch(setCurrentStation(station));
2026-05-06 16:49:07 +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 || [];
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-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
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-22 09:43:55 +08:00
const handleLogout = async () => {
await dispatch(logout());
2026-05-14 08:45:14 +08:00
dispatch(clearStation());
2026-04-22 09:43:55 +08:00
navigate('/login', { replace: true });
2026-04-21 08:51:57 +08:00
};
2026-04-17 17:20:41 +08:00
2026-05-06 16:49:07 +08:00
useEffect(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
getMenu();
getStationData();
return () => clearInterval(timer);
}, [userInfo]);
2026-06-12 11:03:38 +08:00
useEffect(() => {
const fullscreenChange = () => {
setIsFullScreen(!!document.fullscreenElement);
};
document.addEventListener('fullscreenchange', fullscreenChange);
return () => document.removeEventListener('fullscreenchange', fullscreenChange);
}, []);
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,
2026-05-14 17:23:12 +08:00
token: {
colorPrimary: '#1890ff',
borderRadius: 2,
padding: 12,
paddingLG: 16,
margin: 12,
marginLG: 16,
},
2026-04-21 08:51:57 +08:00
components: {
2026-04-29 15:01:00 +08:00
Layout: { headerBg: '#fff', siderBg: '#fff' },
2026-05-14 17:23:12 +08:00
Menu: { itemBg: '#fff', itemSelectedBg: '#e6f4ff' },
Card: {
2026-05-18 12:55:41 +08:00
paddingLG: 12,
2026-05-14 17:23:12 +08:00
},
Table: {
2026-05-18 12:55:41 +08:00
padding: 8,
2026-05-14 17:23:12 +08:00
paddingXS: 4,
},
Form: {
2026-05-18 12:55:41 +08:00
itemMarginBottom: 12,
2026-05-14 17:23:12 +08:00
},
Space: {
2026-05-18 12:55:41 +08:00
gap: 8,
2026-05-14 17:23:12 +08:00
}
2026-04-21 08:51:57 +08:00
}
}}
>
2026-05-14 10:03:51 +08:00
<Layout style={{ height: '100vh', display: 'flex', flexDirection: 'column' }}>
2026-04-29 15:01:00 +08:00
<Header style={{
background: '#0837a8',
color: '#fff',
2026-05-18 12:55:41 +08:00
minHeight: "40px",
2026-05-14 10:03:51 +08:00
height: "auto",
2026-04-29 15:01:00 +08:00
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
2026-05-18 12:55:41 +08:00
padding: '2px 12px',
2026-05-14 10:03:51 +08:00
flexWrap: 'wrap',
2026-05-14 17:23:12 +08:00
gap: '4px'
}}>
2026-05-15 12:59:51 +08:00
<Space size="large" style={{ display: 'flex', alignItems: 'center', flexWrap: 'wrap', height: "100%", justifyContent: "center" }}>
2026-05-14 08:45:14 +08:00
<Button
type="text"
icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
onClick={() => setCollapsed(!collapsed)}
style={{
fontSize: '16px',
color: '#fff',
width: 40,
height: 40,
}}
/>
2026-05-15 12:59:51 +08:00
<div style={{ height: "100%", display: "flex", gap: "10px", justifyContent: "center", alignItems: "center" }}>
2026-05-06 16:49:07 +08:00
<img src={logo} alt="Logo" style={{ width: 24, height: 24 }} />
{!collapsed && (
2026-05-14 10:03:51 +08:00
<span style={{ fontWeight: 'bold', fontSize: 'clamp(14px, 1.2vw, 16px)', color: '#fff', whiteSpace: 'nowrap' }}>
2026-05-06 16:49:07 +08:00
光伏电站智能监控与运维系统
</span>
)}
2026-05-15 12:59:51 +08:00
</div>
2026-05-06 16:49:07 +08:00
<Select
value={stationId}
placeholder="请选择电站"
onChange={handleStationChange}
options={stationList.map(s => ({ label: s.siteName, value: s.id }))}
style={{
2026-05-19 15:55:02 +08:00
width: 'clamp(160px, 15vw, 220px)',
2026-05-06 16:49:07 +08:00
backgroundColor: '#0837a8',
color: '#fff',
border: 'none',
2026-05-19 15:55:02 +08:00
2026-05-06 16:49:07 +08:00
}}
/>
2026-04-29 15:01:00 +08:00
</Space>
2026-05-14 10:03:51 +08:00
<Space size="middle" style={{ flexWrap: 'wrap' }}>
2026-06-12 11:03:38 +08:00
<Button
type="text"
icon={isFullScreen ? <CompressOutlined /> : <ExpandOutlined />}
onClick={toggleFullScreen}
style={{ color: '#fff', fontSize: 'clamp(12px, 1vw, 14px)' }}
>
{isFullScreen ? '退出全屏' : '全屏'}
</Button>
2026-05-14 10:03:51 +08:00
<span style={{ fontSize: 'clamp(12px, 1vw, 13px)' }}>{time}</span>
2026-05-13 11:12:17 +08:00
<Space
style={{ cursor: 'pointer', padding: '0 4px' }}
onClick={() => navigate('/profile')}
className="hover:opacity-80"
>
{userInfo?.avatar && (
<Avatar
size="small"
src={userInfo.avatar.startsWith('http') ? userInfo.avatar : `${envConfig.baseURL}${userInfo.avatar}`}
/>
)}
2026-05-14 10:03:51 +08:00
<span style={{ fontSize: 'clamp(12px, 1vw, 14px)' }}>{userInfo?.nickName || userInfo?.username}</span>
2026-05-13 11:12:17 +08:00
</Space>
2026-05-14 10:03:51 +08:00
<Button type="text" danger icon={<LogoutOutlined />} onClick={handleLogout} style={{ fontSize: 'clamp(12px, 1vw, 14px)' }}>
退出登录
</Button>
2026-04-29 15:01:00 +08:00
</Space>
</Header>
2026-05-14 10:03:51 +08:00
<Layout style={{ flex: 1, overflow: 'hidden', background: '#fff' }}>
<Sider
collapsible
collapsed={collapsed}
2026-04-29 15:01:00 +08:00
trigger={null}
2026-05-14 08:45:14 +08:00
width={150}
theme="light"
2026-04-29 15:01:00 +08:00
style={{ borderRight: '1px solid #e5e7eb' }}
>
2026-05-18 12:55:41 +08:00
{/* 关键:用Layout实现上下分布 */}
<Layout style={{ height: '100%', flexDirection: 'column', background: '#fff' }}>
<Menu
mode="inline"
theme="light"
selectedKeys={[location.pathname]}
items={menuItems}
onClick={({ key }) => navigate(key)}
style={{ flex: 1, overflowY: 'auto', borderRight: 0 }}
/>
{/* 场站信息卡片:仅在侧边栏展开时显示 */}
{!collapsed && currentStation && (
<Card
size="small"
style={{
border: '1px solid #e5e7eb',
borderRadius: 8,
2026-05-18 14:28:42 +08:00
2026-05-18 12:55:41 +08:00
margin: 4,
marginBottom: 50
}}
2026-05-18 14:28:42 +08:00
styles={{ body: { padding: '12px 4px' } }}
2026-05-18 12:55:41 +08:00
>
<div style={{ textAlign: 'left' }}>
<div style={{ fontSize: 14, fontWeight: 600, marginBottom: 8 }}>
{currentStation.siteName || '1MW 光伏电站'}
</div>
<img style={{ width: '100%', borderRadius: "8px", marginBottom: 8 }} src="https://images.unsplash.com/photo-1509391366360-2e959784a276?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80" />
{/* <img src="电站图片地址" style={{ width: '100%', borderRadius: 4, marginBottom: 8 }} alt="电站" /> */}
2026-05-18 14:28:42 +08:00
<div style={{ fontSize: 11, color: '#666', lineHeight: 1.8 }}>
2026-06-24 15:47:52 +08:00
{/*<div>装机容量:{currentStation.capacity || '1.00'} MWp</div>
<div>并网时间:{currentStation.gridTime || '2024-01-15'}</div>*/}
<div>电站地址:{currentStation.address || '河南省林州'}</div>
2026-05-18 12:55:41 +08:00
<div>电站类型:{currentStation.type || '地面电站'}</div>
2026-05-18 14:28:42 +08:00
<div style={{ marginTop: 4, display: 'flex', alignItems: 'center', color: '#41ad0a' }}>
<span>运行状态:</span>
<span style={{ marginLeft: 4 }}>●</span>
<span style={{ marginLeft: 4 }}>正常运行</span>
2026-05-18 12:55:41 +08:00
</div>
</div>
</div>
</Card>
)}
</Layout>
</Sider>
2026-05-14 17:23:12 +08:00
<Content style={{ background: '#f4f7f9', overflowY: 'auto', padding: '4px 8px' }}>
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
}