test
This commit is contained in:
@@ -2,14 +2,13 @@ import React, { useState, useEffect } from 'react';
|
||||
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
|
||||
import {
|
||||
Layout, Menu, Button, Space, ConfigProvider, theme,
|
||||
Breadcrumb, Badge, message
|
||||
Breadcrumb, message
|
||||
} from 'antd';
|
||||
// 先引入中文语言包
|
||||
import zhCN from 'antd/locale/zh_CN';
|
||||
import {
|
||||
DashboardOutlined, ExperimentOutlined, VideoCameraOutlined,
|
||||
BellOutlined, DownloadOutlined, UserOutlined, LogoutOutlined,
|
||||
MenuUnfoldOutlined, MenuFoldOutlined, TeamOutlined, MenuOutlined
|
||||
TeamOutlined, MenuOutlined, AppstoreOutlined, SettingOutlined
|
||||
} from '@ant-design/icons';
|
||||
import logo from '../assets/logo.png';
|
||||
import { useSelector } from 'react-redux';
|
||||
@@ -17,33 +16,96 @@ 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 [menuTree, setMenuTree] = useState([]); // 动态菜单树
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const { userInfo } = useSelector((state: RootState) => state.user);
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
// ======================
|
||||
// 工具函数:构建树形菜单
|
||||
// ======================
|
||||
const buildMenuTree = (data) => {
|
||||
const map = {};
|
||||
const roots = [];
|
||||
|
||||
// 先把所有菜单放进map
|
||||
data.forEach(item => {
|
||||
map[item.menuId] = { ...item, children: [] };
|
||||
});
|
||||
|
||||
const getMenu = () => {
|
||||
getMenuList().then(res => {
|
||||
if (res.code == 200) {
|
||||
setMenuList(res.data);
|
||||
// 构建父子关系
|
||||
data.forEach(item => {
|
||||
if (item.parentId !== 0) {
|
||||
if (map[item.parentId]) {
|
||||
map[item.parentId].children.push(map[item.menuId]);
|
||||
}
|
||||
} else {
|
||||
message.error("获取菜单失败:" + res.msg)
|
||||
roots.push(map[item.menuId]);
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
return roots;
|
||||
};
|
||||
|
||||
// ======================
|
||||
// 把后端菜单转成 antd menu 格式
|
||||
// ======================
|
||||
const convertToAntdMenu = (tree) => {
|
||||
return tree.map(node => {
|
||||
// 自动匹配图标
|
||||
let IconComponent = MenuOutlined;
|
||||
|
||||
if (node.path.includes('overview')) IconComponent = DashboardOutlined;
|
||||
if (node.path.includes('device')) IconComponent = ExperimentOutlined;
|
||||
if (node.path.includes('video')) IconComponent = VideoCameraOutlined;
|
||||
if (node.path.includes('alert')) IconComponent = BellOutlined;
|
||||
if (node.path.includes('user')) IconComponent = UserOutlined;
|
||||
if (node.path.includes('role')) IconComponent = TeamOutlined;
|
||||
if (node.path.includes('menu')) IconComponent = AppstoreOutlined;
|
||||
if (node.path.includes('system')) IconComponent = SettingOutlined;
|
||||
|
||||
const item = {
|
||||
key: node.path || `/menu/${node.menuId}`,
|
||||
icon: <IconComponent />,
|
||||
label: node.menuName,
|
||||
};
|
||||
|
||||
// 有子菜单递归渲染
|
||||
if (node.children && node.children.length > 0) {
|
||||
item.children = convertToAntdMenu(node.children);
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
};
|
||||
|
||||
// ======================
|
||||
// 获取动态菜单
|
||||
// ======================
|
||||
const getMenu = () => {
|
||||
const data = {
|
||||
pageNum: 1,
|
||||
pageSize: 10000
|
||||
}
|
||||
getMenuList(data).then(res => {
|
||||
if (res.code === 200) {
|
||||
const treeData = buildMenuTree(res.data || []);
|
||||
const antdMenu = convertToAntdMenu(treeData);
|
||||
setMenuTree(antdMenu);
|
||||
}
|
||||
}).catch(() => {
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
|
||||
//getMenu();
|
||||
getMenu(); // 加载动态菜单
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
@@ -52,24 +114,6 @@ export default function AppLayout() {
|
||||
navigate('/login', { replace: true });
|
||||
};
|
||||
|
||||
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: '/serialNumGenerate', icon: <UserOutlined />, label: '序列号生成', roles: ['admin'] },
|
||||
{ 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'] },
|
||||
{ key: '/station', icon: <MenuOutlined />, label: '场站管理', roles: ['admin'] },
|
||||
{ key: '/system', icon: <MenuOutlined />, label: '系统设置', roles: ['admin'] }
|
||||
];
|
||||
|
||||
const filteredMenu = menuItems;
|
||||
const currentPathLabel = menuItems.find(i => i.key === location.pathname)?.label || '总览首页';
|
||||
|
||||
return (
|
||||
<ConfigProvider
|
||||
locale={zhCN}
|
||||
@@ -77,87 +121,70 @@ export default function AppLayout() {
|
||||
algorithm: theme.defaultAlgorithm,
|
||||
token: { colorPrimary: '#1890ff', borderRadius: 2 },
|
||||
components: {
|
||||
Layout: { headerBg: '#fff', siderBg: '#001529' },
|
||||
Menu: { darkItemBg: '#001529', darkItemSelectedBg: '#1890ff' }
|
||||
Layout: { headerBg: '#fff', siderBg: '#fff' },
|
||||
Menu: { itemBg: '#fff', itemSelectedBg: '#e6f4ff' }
|
||||
}
|
||||
}}
|
||||
>
|
||||
<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={{
|
||||
<Header style={{
|
||||
background: '#0837a8',
|
||||
color: '#fff',
|
||||
height: "50px"
|
||||
height: "50px",
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'space-between',
|
||||
padding: '0 16px'
|
||||
}}>
|
||||
<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>
|
||||
<Space size="small">
|
||||
<img src={logo} alt="Logo" style={{ width: 24, height: 24 }} />
|
||||
{!collapsed && (
|
||||
<span style={{ fontWeight: 'bold', fontSize: 16, color: '#fff' }}>
|
||||
光伏电站智能监控与运维系统
|
||||
</span>
|
||||
)}
|
||||
</Space>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
<Button
|
||||
type="text"
|
||||
danger
|
||||
icon={<LogoutOutlined />}
|
||||
onClick={handleLogout}
|
||||
className="font-bold text-[12px] uppercase"
|
||||
>
|
||||
<Space size="middle">
|
||||
<span style={{ fontSize: 13 }}>{time}</span>
|
||||
<span>{userInfo?.username}</span>
|
||||
<Button type="text" danger icon={<LogoutOutlined />} onClick={handleLogout}>
|
||||
退出登录
|
||||
</Button>
|
||||
</div>
|
||||
</Space>
|
||||
</Header>
|
||||
|
||||
<Layout style={{
|
||||
overflow: 'auto', background: '#ffffff',
|
||||
height: "calc(100vh - 50px)",
|
||||
overflowX: 'hidden'
|
||||
height: 'calc(100vh - 50px)',
|
||||
overflow: 'hidden',
|
||||
background: '#fff'
|
||||
}}>
|
||||
{/* 左侧:菜单 + 下部区域 */}
|
||||
<Sider
|
||||
trigger={null}
|
||||
collapsible
|
||||
collapsed={collapsed}
|
||||
trigger={null}
|
||||
width={220}
|
||||
theme="light"
|
||||
style={{
|
||||
overflow: 'auto', background: '#ffffff',
|
||||
borderRight: '1px solid #e5e7eb',
|
||||
}}
|
||||
style={{ borderRight: '1px solid #e5e7eb' }}
|
||||
>
|
||||
|
||||
|
||||
<Menu
|
||||
theme="light"
|
||||
mode="inline"
|
||||
theme="light"
|
||||
selectedKeys={[location.pathname]}
|
||||
items={filteredMenu}
|
||||
items={menuTree}
|
||||
onClick={({ key }) => navigate(key)}
|
||||
/>
|
||||
|
||||
{/* 左侧下方固定区域(你可以放任何内容) */}
|
||||
<div className="p-3 text-white text-xs opacity-70 text-center mt-4">
|
||||
{!collapsed && '快捷操作面板'}
|
||||
</div>
|
||||
</Sider>
|
||||
|
||||
{/* 右侧:主内容 */}
|
||||
<Content className=" overflow-y-auto bg-[#f4f7f9] min-h-0 overflow-x-hidden">
|
||||
<Content style={{
|
||||
background: '#f4f7f9',
|
||||
overflowY: 'auto',
|
||||
padding: '16px'
|
||||
}}>
|
||||
<Outlet />
|
||||
</Content>
|
||||
</Layout>
|
||||
</Layout>
|
||||
|
||||
</ConfigProvider >
|
||||
</ConfigProvider>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ export default function UserRolePage() {
|
||||
))
|
||||
},
|
||||
{
|
||||
title: '状态', render: (s) => s == '0' ?
|
||||
title: '状态', render: (_,s) => s.status == '0' ?
|
||||
<Tag color="green">正常</Tag> : <Tag color="red">停用</Tag>
|
||||
},
|
||||
{
|
||||
|
||||
@@ -19,7 +19,6 @@ const StationManage = lazy(() => import('../pages/StationManage'));
|
||||
const SerialNumGenerate = lazy(() => import('../pages/SerialNumGenerate'));
|
||||
const SystemSetting = lazy(() => import('../pages/SystemSetting'));
|
||||
|
||||
|
||||
function PageFallback() {
|
||||
return (
|
||||
<div className="flex items-center justify-center h-full min-h-[200px]">
|
||||
@@ -34,14 +33,21 @@ export const routeMeta: Record<string, {
|
||||
requiresAuth: boolean;
|
||||
}> = {
|
||||
'/': { title: '总览首页', roles: ['admin', 'operator', 'viewer'], requiresAuth: true },
|
||||
'/overview': { title: '总览首页', roles: ['admin', 'operator', 'viewer'], requiresAuth: true },
|
||||
'/devices': { title: '设备管理', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/video': { title: '在线视频', roles: ['admin', 'operator', 'viewer'], requiresAuth: true },
|
||||
'/monitor': { title: '实时监控', roles: ['admin', 'operator', 'viewer'], requiresAuth: true },
|
||||
'/alerts': { title: '告警中心', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/AIanalysis': { title: 'AI诊断分析', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/clean': { title: '清洗优化', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/task': { title: '工单任务', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/table': { title: '报表中心', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/systemSetting': { title: '系统设置', roles: ['admin'], requiresAuth: true },
|
||||
'/downloads': { title: '视频下载', roles: ['admin', 'operator'], requiresAuth: true },
|
||||
'/serialNumGenerate': { title: '序列号生成', roles: ['admin'], requiresAuth: true },
|
||||
'/users': { title: '用户管理', roles: ['admin'], requiresAuth: true },
|
||||
'/roles': { title: '角色管理', roles: ['admin'], requiresAuth: true },
|
||||
'/menuList': { title: '菜单管理', roles: ['admin'], requiresAuth: true },
|
||||
'/manage/users': { title: '用户管理', roles: ['admin'], requiresAuth: true },
|
||||
'/manage/roles': { title: '角色管理', roles: ['admin'], requiresAuth: true },
|
||||
'/manage/menuList': { title: '菜单管理', roles: ['admin'], requiresAuth: true },
|
||||
'/organization': { title: '组织管理', roles: ['admin'], requiresAuth: true },
|
||||
'/station': { title: '场站管理', roles: ['admin'], requiresAuth: true },
|
||||
};
|
||||
@@ -70,14 +76,21 @@ const routes: RouteObject[] = [
|
||||
),
|
||||
children: [
|
||||
{ index: true, element: <Home /> },
|
||||
{ path: 'overview', element: <Home /> },
|
||||
{ path: 'devices', element: <Devices /> },
|
||||
{ path: 'video', element: <Video /> },
|
||||
{ path: 'monitor', element: <Video /> },
|
||||
{ path: 'alerts', element: <Alerts /> },
|
||||
{ path: 'AIanalysis', element: <div>AI诊断分析</div> },
|
||||
{ path: 'clean', element: <div>清洗优化</div> },
|
||||
{ path: 'task', element: <div>工单任务</div> },
|
||||
{ path: 'table', element: <div>报表中心</div> },
|
||||
{ path: 'systemSetting', element: <SystemSetting /> },
|
||||
{ path: 'downloads', element: <Downloads /> },
|
||||
{ path: 'roles', element: <Roles /> },
|
||||
{ path: 'users', element: <Users /> },
|
||||
{ path: 'serialNumGenerate', element: <SerialNumGenerate /> },
|
||||
{ path: 'menuList', element: <MenuList /> },
|
||||
{ path: 'manage/users', element: <Users /> },
|
||||
{ path: 'manage/roles', element: <Roles /> },
|
||||
{ path: 'manage/menuList', element: <MenuList /> },
|
||||
{ path: 'organization', element: <Organization /> },
|
||||
{ path: 'station', element: <StationManage /> },
|
||||
{ path: 'system', element: <SystemSetting /> },
|
||||
|
||||
Reference in New Issue
Block a user