Files
web-Iot/src/pages/Devices.tsx

104 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-05-11 10:47:51 +08:00
import React, { useState, useEffect } from 'react';
import { Layout, Typography, Menu } from 'antd';
import { AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
import DeviceOverviewPage from '../components/devices/DeviceOverviewPage';
import { getDeviceListApi, getPlaneDeviceList } from '../api/device';
import { useSelector } from 'react-redux';
import { RootState } from '../store';
const { Header, Content } = Layout;
const { Text } = Typography;
const DeviceStatusSettingPage = () => (
<div style={{ padding: 20 }}>
<h2>设置状态页面(/devices/status)</h2>
</div>
);
export default function DeviceIndexPage() {
const [activeTab, setActiveTab] = useState('overview');
const { userInfo } = useSelector((state: RootState) => state.user);
const [devices, setDevices] = useState([]);
const [planedevices, setPlaneDevices] = useState([]);
const [devicesAll, setDevicesAll] = useState([]);
const tabs = [
2026-05-11 14:59:36 +08:00
{ key: 'overview', label: '装备总览', icon: <AppstoreOutlined /> },
{ key: 'status', label: '设置状态', icon: <SettingOutlined /> },
2026-04-21 08:51:57 +08:00
];
2026-04-17 17:20:41 +08:00
2026-05-11 14:59:36 +08:00
// 👇 核心修复:同时请求 + 同时合并
const fetchAllData = async () => {
if (!userInfo?.username) return;
try {
// 1. 同时发请求
const [deviceRes, planeRes] = await Promise.all([
getDeviceListApi({ tenantName: userInfo.username, deptId: 1 }),
getPlaneDeviceList(),
]);
// 2. 处理设备数据
const devData = deviceRes?.code === 200 ? (deviceRes.data || []) : [];
const planeData = planeRes?.code === 200 ? (planeRes.data?.detail || []) : [];
2026-05-11 10:47:51 +08:00
2026-05-11 14:59:36 +08:00
// 3. 分别保存
setDevices(devData);
setPlaneDevices(planeData);
// 4. 最终合并(只执行一次!)
setDevicesAll([...devData, ...planeData]);
} catch (err) {
console.error('数据请求失败', err);
}
2026-05-11 10:47:51 +08:00
};
useEffect(() => {
2026-05-11 14:59:36 +08:00
fetchAllData();
}, [userInfo]);
2026-04-17 17:20:41 +08:00
2026-05-11 14:59:36 +08:00
const renderContent = () => {
if (activeTab === 'overview') {
return <DeviceOverviewPage
devices={devices}
planedevices={planedevices}
devicesAll={devicesAll}
/>;
}
if (activeTab === 'status') return <DeviceStatusSettingPage />;
return null;
};
2026-04-17 17:20:41 +08:00
2026-05-11 10:47:51 +08:00
return (
2026-05-11 15:15:49 +08:00
<Layout style={{ minHeight: '100vh', background: '#fff', overflow: 'hidden', scrollbarWidth: "thin", scrollbarColor: '#ccc transparent' }}>
2026-05-11 10:47:51 +08:00
<div style={{ display: 'flex', paddingBottom: "12px" }}>
2026-05-11 14:59:36 +08:00
<div style={{ padding: '20px 24px 0', flexShrink: 0 }}>
2026-05-11 10:47:51 +08:00
<Typography.Title level={4} style={{ margin: 0 }}>装备控制</Typography.Title>
<Text type="secondary"> 装备总览、状态监控与参数配置</Text>
2026-05-09 16:44:39 +08:00
</div>
2026-05-11 14:59:36 +08:00
<Header style={{ flex: 1, background: '#fff', padding: 0, borderBottom: '1px solid #f0f0f0' }}>
2026-05-11 10:47:51 +08:00
<Menu
mode="horizontal"
selectedKeys={[activeTab]}
onClick={({ key }) => setActiveTab(key)}
items={tabs}
style={{ border: 0, height: 64, lineHeight: '64px' }}
/>
</Header>
</div>
<Content style={{
maxHeight: 'calc(100vh - 64px)',
overflowY: 'auto',
background: '#f5f7fa',
padding: '20px',
2026-05-11 14:59:36 +08:00
scrollbarWidth: "thin"
2026-05-11 10:47:51 +08:00
}}>
{renderContent()}
2026-05-09 16:44:39 +08:00
</Content>
</Layout>
2026-04-21 08:51:57 +08:00
);
2026-04-17 17:20:41 +08:00
}