Files
web-Iot/src/components/SystemSetting/DeviceManagement.tsx

356 lines
11 KiB
TypeScript
Raw Normal View History

2026-05-25 13:06:38 +08:00
import React, { useState, useEffect } from 'react';
2026-05-08 15:53:27 +08:00
import {
Table,
Input,
Button,
Form,
Select,
Space,
Tag,
message,
Modal,
Tabs,
Card,
InputNumber,
2026-05-25 13:06:38 +08:00
Checkbox,
Tooltip,
Row,
Col,
2026-05-08 15:53:27 +08:00
} from 'antd';
import {
SearchOutlined,
ReloadOutlined,
PlusOutlined,
EditOutlined,
DeleteOutlined,
2026-05-25 13:06:38 +08:00
LinkOutlined,
DisconnectOutlined,
2026-05-08 15:53:27 +08:00
} from '@ant-design/icons';
import SerialNumGenerate from "./SerialNumGenerate"
2026-05-25 13:06:38 +08:00
import { getPlaneDeviceList, bindDrone, unbindDrone } from '@/api/device';
import { useSelector } from 'react-redux';
import { RootState } from '@/src/store';
import utils from '@/lib/utils';
2026-05-08 15:53:27 +08:00
// 模拟数据
const allDeviceList = [
{ id: 1, code: 'CAM-01', name: '摄像头 CAM-01', type: 'CAMERA', online: 'ONLINE', status: 'NORMAL' },
{ id: 2, code: 'CAM-02', name: '摄像头 CAM-02', type: 'CAMERA', online: 'OFFLINE', status: 'FAULT' },
{ id: 3, code: 'CAM-03', name: '摄像头 CAM-03', type: 'CAMERA', online: 'ONLINE', status: 'NORMAL' },
{ id: 4, code: 'CLEAN-01', name: '清洗机器人 CLEAN-01', type: 'CLEANING_DEVICE', online: 'ONLINE', status: 'IDLE' },
{ id: 5, code: 'CMB-A01', name: '汇流箱 CMB-A01', type: 'COMBINER_BOX', online: 'ONLINE', status: 'WARNING' },
{ id: 6, code: 'CMB-A02', name: '汇流箱 CMB-A02', type: 'COMBINER_BOX', online: 'ONLINE', status: 'NORMAL' },
{ id: 7, code: 'INV-A01', name: '逆变器 INV-A01', type: 'INVERTER', online: 'ONLINE', status: 'NORMAL' },
{ id: 8, code: 'INV-A02', name: '逆变器 INV-A02', type: 'INVERTER', online: 'ONLINE', status: 'WARNING' },
{ id: 9, code: 'INV-B01', name: '逆变器 INV-B01', type: 'INVERTER', online: 'ONLINE', status: 'NORMAL' },
{ id: 10, code: 'METER-01', name: '电表 METER-01', type: 'METER', online: 'ONLINE', status: 'NORMAL' },
{ id: 11, code: 'ROBOT-01', name: '运维机器人 ROBOT-01', type: 'ROBOT', online: 'ONLINE', status: 'IDLE' },
{ id: 12, code: 'DRONE-01', name: '无人机 DRONE-01', type: 'DRONE', online: 'OFFLINE', status: 'FAULT' },
{ id: 13, code: 'WEATHER-01', name: '气象站 WEATHER-01', type: 'WEATHER_STATION', online: 'ONLINE', status: 'NORMAL' },
];
// Tab 配置
const tabs = [
{ key: 'all', label: '设备管理', filter: () => true },
{ key: 'camera', label: '摄像头管理', filter: (item) => item.type === 'CAMERA' },
{ key: 'smart', label: '智能装备', filter: (item) => ['CLEANING_DEVICE', 'ROBOT', 'DRONE'].includes(item.type) },
];
2026-05-08 15:53:27 +08:00
export default function DeviceManagementPage() {
const [activeTab, setActiveTab] = useState('all');
const [form] = Form.useForm();
const [loading, setLoading] = useState(false);
const [deviceList, setDeviceList] = useState(allDeviceList);
2026-05-25 13:06:38 +08:00
// 无人机相关状态
const { userInfo } = useSelector((state: RootState) => state.user);
const [droneList, setDroneList] = useState([]);
const [bindModalVisible, setBindModalVisible] = useState(false);
const [bindForm] = Form.useForm();
const [selectedDroneKeys, setSelectedDroneKeys] = useState([]);
const fetchDroneList = async () => {
setLoading(true);
try {
const res: any = await getPlaneDeviceList();
if (res.code == 200) {
setDroneList(res?.data?.detail || []);
}
} catch (err) {
utils.message.error('获取无人机列表失败');
} finally {
setLoading(false);
}
};
useEffect(() => {
if (activeTab === 'drone') {
fetchDroneList();
}
}, [activeTab]);
const handleBind = async (values) => {
try {
const payload = {
siteId: values.siteId,
orgId: values.orgId,
userId: values.userId || userInfo?.userId,
sns: values.sns.split(',').map(s => s.trim()),
};
const res: any = await bindDrone(payload);
if (res.code === 200) {
utils.message.success('绑定成功');
setBindModalVisible(false);
bindForm.resetFields();
fetchDroneList();
} else {
utils.message.error(res.msg || '绑定失败');
}
} catch (err) {
utils.message.error('操作异常');
}
};
const handleUnbind = (sns, type) => {
const typeText = { 1: '人员', 2: '场站', 3: '组织' }[type];
Modal.confirm({
title: '确认解绑',
content: `确定要为选中的无人机清除${typeText}绑定吗?`,
onOk: async () => {
try {
const res: any = await unbindDrone({ sns, type });
if (res.code === 200) {
utils.message.success('解绑成功');
fetchDroneList();
setSelectedDroneKeys([]);
} else {
utils.message.error(res.msg || '解绑失败');
}
} catch (err) {
utils.message.error('操作异常');
}
}
});
};
const droneColumns = [
{ title: '设备名称', dataIndex: 'drone_callsign', key: 'name' },
{ title: 'SN码', dataIndex: 'device_sn', key: 'sn' },
{ title: '网关SN', dataIndex: 'gateway_sn', key: 'gsn' },
{ title: '状态', dataIndex: 'onlineStatus', key: 'status', render: s => <Tag color={s === 1 ? 'green' : 'default'}>{s === 1 ? '在线' : '离线'}</Tag> },
{
title: '操作',
key: 'action',
render: (_, record) => (
<Space>
<Tooltip title="清除人员绑定">
<Button size="small" icon={<DisconnectOutlined />} onClick={() => handleUnbind([record.device_sn], 1)}>人</Button>
</Tooltip>
<Tooltip title="清除场站绑定">
<Button size="small" icon={<DisconnectOutlined />} onClick={() => handleUnbind([record.device_sn], 2)}>场</Button>
</Tooltip>
<Tooltip title="清除组织绑定">
<Button size="small" icon={<DisconnectOutlined />} onClick={() => handleUnbind([record.device_sn], 3)}>组</Button>
</Tooltip>
</Space>
),
},
];
2026-05-08 15:53:27 +08:00
// 根据 Tab 过滤数据
const filteredByTab = deviceList.filter(tabs.find(t => t.key === activeTab)?.filter || (() => false));
2026-05-08 15:53:27 +08:00
// 表格列定义
const columns = [
{ title: '设备编码', dataIndex: 'code', key: 'code' },
{ title: '设备名称', dataIndex: 'name', key: 'name' },
{ title: '类型', dataIndex: 'type', key: 'type' },
{
title: '在线',
dataIndex: 'online',
key: 'online',
render: (text) => (
<Tag color={text === 'ONLINE' ? 'green' : 'red'}>{text}</Tag>
),
},
{
title: '运行',
dataIndex: 'status',
key: 'status',
render: (text) => {
const colorMap = {
NORMAL: 'green',
FAULT: 'red',
WARNING: 'orange',
IDLE: 'blue',
};
return <Tag color={colorMap[text] || 'default'}>{text}</Tag>;
},
},
{
title: '操作',
key: 'action',
render: (_, record) => (
<Space>
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
<Button type="link" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record)}>删除</Button>
</Space>
),
},
];
// 查询
const handleSearch = () => {
form.validateFields().then(values => {
const keyword = values.keyword?.toLowerCase() || '';
setLoading(true);
setTimeout(() => {
const filtered = allDeviceList.filter(item =>
item.name.toLowerCase().includes(keyword) || item.code.toLowerCase().includes(keyword)
);
setDeviceList(filtered);
setLoading(false);
}, 300);
});
};
// 重置
const handleReset = () => {
form.resetFields();
setDeviceList(allDeviceList);
};
// 新增
const handleAdd = () => {
message.info('打开新增设备弹窗');
};
// 编辑
const handleEdit = (record) => {
message.info(`编辑设备:${record.name}`);
};
// 删除
const handleDelete = (record) => {
Modal.confirm({
title: '确认删除',
content: `确定要删除设备 ${record.name} 吗?`,
onOk: () => {
message.success(`设备 ${record.name} 已删除`);
setDeviceList(prev => prev.filter(item => item.id !== record.id));
},
});
};
return (
2026-05-15 15:39:28 +08:00
<div style={{ padding: "8px 0px", paddingTop: 0 }}>
2026-05-08 15:53:27 +08:00
<Tabs
activeKey={activeTab}
onChange={setActiveTab}
2026-05-15 15:39:28 +08:00
style={{ marginBottom: 0 }}
2026-05-08 15:53:27 +08:00
>
<Tabs.TabPane key="all" tab="设备管理" />
<Tabs.TabPane key="camera" tab="摄像头管理" />
<Tabs.TabPane key="smart" tab="智能装备" />
2026-05-25 13:06:38 +08:00
<Tabs.TabPane key="drone" tab="无人机管理" />
<Tabs.TabPane key="serialGenerate" tab="机器人生成" />
2026-05-08 15:53:27 +08:00
</Tabs>
{/* ====================== 判断显示内容 ====================== */}
{activeTab === 'serialGenerate' ? (
<SerialNumGenerate />
2026-05-25 13:06:38 +08:00
) : activeTab === 'drone' ? (
<>
<div style={{ marginBottom: 16 }}>
<Space>
<Button type="primary" icon={<LinkOutlined />} onClick={() => setBindModalVisible(true)}>绑定无人机</Button>
<Button disabled={selectedDroneKeys.length === 0} onClick={() => handleUnbind(selectedDroneKeys, 1)}>批量解绑(人)</Button>
<Button disabled={selectedDroneKeys.length === 0} onClick={() => handleUnbind(selectedDroneKeys, 2)}>批量解绑(场)</Button>
<Button disabled={selectedDroneKeys.length === 0} onClick={() => handleUnbind(selectedDroneKeys, 3)}>批量解绑(组)</Button>
</Space>
</div>
<Table
rowSelection={{
selectedRowKeys: selectedDroneKeys,
onChange: setSelectedDroneKeys,
}}
rowKey="device_sn"
columns={droneColumns}
dataSource={droneList}
loading={loading}
pagination={{ showSizeChanger: true, showTotal: t => `共 ${t} 条` }}
/>
<Modal
title="绑定无人机"
open={bindModalVisible}
onCancel={() => setBindModalVisible(false)}
onOk={() => bindForm.submit()}
width={500}
>
<Form form={bindForm} layout="vertical" onFinish={handleBind} initialValues={{ userId: userInfo?.userId }}>
<Form.Item label="无人机 SN (多个用逗号隔开)" name="sns" rules={[{ required: true, message: '请输入 SN' }]}>
<Input.TextArea placeholder="例如: SN001, SN002" rows={3} />
</Form.Item>
<Row gutter={16}>
<Col span={12}>
<Form.Item label="场站 ID (siteId)" name="siteId" rules={[{ required: true, message: '请输入场站 ID' }]}>
<InputNumber style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item label="组织 ID (orgId)" name="orgId" rules={[{ required: true, message: '请输入组织 ID' }]}>
<InputNumber style={{ width: '100%' }} />
</Form.Item>
</Col>
</Row>
<Form.Item label="用户 ID (userId)" name="userId">
<InputNumber style={{ width: '100%' }} />
</Form.Item>
</Form>
</Modal>
</>
) : (
// 显示原来的设备列表
<>
{/* 筛选栏 */}
<div style={{ marginBottom: 16 }}>
<Form form={form} layout="inline">
<Form.Item name="keyword">
<Input placeholder="请输入设备名称" style={{ width: 220 }} />
</Form.Item>
<Form.Item>
<Button type="primary" icon={<SearchOutlined />} onClick={handleSearch}>查询</Button>
</Form.Item>
<Form.Item>
<Button icon={<ReloadOutlined />} onClick={handleReset}>重置</Button>
</Form.Item>
<Form.Item>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>新增</Button>
</Form.Item>
</Form>
</div>
2026-05-08 15:53:27 +08:00
{/* 表格 */}
<Table
rowKey="id"
columns={columns}
dataSource={filteredByTab}
loading={loading}
pagination={{
total: filteredByTab.length,
pageSize: 10,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total) => `共 ${total} 条`,
}}
/>
</>
)}
2026-05-08 15:53:27 +08:00
</div>
);
}