产品列表接口接入 新增产品
This commit is contained in:
@@ -7,32 +7,12 @@ import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, ProductOutlined,
|
||||
AppstoreOutlined, SwapOutlined, VideoCameraOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { addDeviceModel, addProduct, getConnectTypes, getDeviceModelList, updateDeviceModel } from '../../api/deviceAccess.ts';
|
||||
import { addDeviceModel, addProduct, getConnectTypes, getDataTypesApi, getDeviceModelList, getProductPage, updateDeviceModel } from '../../api/deviceAccess.ts';
|
||||
|
||||
const { Option } = Select;
|
||||
const { TabPane } = Tabs;
|
||||
|
||||
// ====================== 模拟基础数据 ======================
|
||||
const productList = [
|
||||
{
|
||||
id: 1, name: '海康威视摄像头', code: 'hikvision', protocolType: 'onvif', manufacturer: '海康',
|
||||
publishTopic: '/hik/device/upload', subscribeTopic: '/hik/device/cmd'
|
||||
},
|
||||
{
|
||||
id: 2, name: '大华摄像头', code: 'dahua', protocolType: 'rtsp', manufacturer: '大华',
|
||||
publishTopic: '/dahua/upload', subscribeTopic: '/dahua/cmd'
|
||||
},
|
||||
{
|
||||
id: 3, name: '通用MQTT设备', code: 'mqtt_device', protocolType: 'mqtt', manufacturer: '通用',
|
||||
publishTopic: '/iot/upload', subscribeTopic: '/iot/cmd'
|
||||
},
|
||||
];
|
||||
|
||||
const modelList = [
|
||||
{ id: 10, name: '通用视频监控设备', code: 'video_camera', desc: '摄像头、云台、告警', connectType: 'MQTT' },
|
||||
{ id: 11, name: '环境监测设备', code: 'env_sensor', desc: '温湿度、烟感', connectType: 'MQTT' },
|
||||
{ id: 12, name: '电池供电设备', code: 'battery_device', desc: '电量、电压监测', connectType: 'HTTP' },
|
||||
];
|
||||
|
||||
const modelDetailList: Record<number, {
|
||||
props: any[];
|
||||
@@ -85,7 +65,6 @@ const deviceList = [
|
||||
}
|
||||
];
|
||||
|
||||
const alarmList = [{ id: 1, deviceName: '1#环境传感器', level: 'warn', content: '温度超过阈值 62℃', time: '2026-05-07 10:24:00' }];
|
||||
|
||||
export default function DeviceAccessPage() {
|
||||
const [activeTab, setActiveTab] = useState('device');
|
||||
@@ -93,6 +72,7 @@ export default function DeviceAccessPage() {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [modalType, setModalType] = useState('');
|
||||
const [editData, setEditData] = useState<any>(null);
|
||||
const [productList, setProductList] = useState([]);
|
||||
|
||||
const [deviceProtocol, setDeviceProtocol] = useState('onvif');
|
||||
const [productProtocol, setProductProtocol] = useState('');
|
||||
@@ -118,7 +98,7 @@ export default function DeviceAccessPage() {
|
||||
|
||||
// 筛选条件
|
||||
const [filterForm] = Form.useForm();
|
||||
const [filteredModelList, setFilteredModelList] = useState(modelList);
|
||||
const [filteredModelList, setFilteredModelList] = useState([]);
|
||||
|
||||
// ========== 物模型分页 ==========
|
||||
const [pagination, setPagination] = useState({
|
||||
@@ -238,20 +218,53 @@ export default function DeviceAccessPage() {
|
||||
// 筛选
|
||||
const handleFilter = () => {
|
||||
setPagination(prev => ({ ...prev, current: 1 }));
|
||||
getModel();
|
||||
if (activeTab === 'model') getModel();
|
||||
if (activeTab === 'product') getProductList(); // 产品列表
|
||||
};
|
||||
|
||||
// 重置
|
||||
const handleReset = () => {
|
||||
filterForm.resetFields();
|
||||
setPagination(prev => ({ ...prev, current: 1 }));
|
||||
getModel();
|
||||
if (activeTab === 'model') getModel();
|
||||
if (activeTab === 'product') getProductList();
|
||||
};
|
||||
const getDataTypes = () => {
|
||||
getDataTypesApi("").then(res => {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// 分页切换
|
||||
const handleTableChange = (pag) => {
|
||||
setPagination(pag);
|
||||
getModel();
|
||||
if (activeTab === 'model') getModel();
|
||||
if (activeTab === 'product') getProductList();
|
||||
};
|
||||
// ====================== 获取产品列表 ======================
|
||||
const getProductList = () => {
|
||||
const values = filterForm.getFieldsValue();
|
||||
const params = {
|
||||
pageNum: pagination.current,
|
||||
pageSize: pagination.pageSize,
|
||||
code: values.code || '',
|
||||
name: values.name || '',
|
||||
manufacturer: values.manufacturer || '',
|
||||
categoryId: values.categoryId || '',
|
||||
protocolCode: values.protocolCode || '',
|
||||
transport: values.transport || '',
|
||||
uploadTopic: values.uploadTopic || '',
|
||||
subscribeTopic: values.subscribeTopic || '',
|
||||
status: values.status ?? '',
|
||||
};
|
||||
|
||||
// 这里调用你的【产品列表接口】
|
||||
getProductPage(params).then(res => {
|
||||
if (res.code === 200) {
|
||||
setProductList(res.rows);
|
||||
setPagination(prev => ({ ...prev, total: res.total }));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// ===================== 物模型提交 =====================
|
||||
@@ -304,18 +317,34 @@ export default function DeviceAccessPage() {
|
||||
form.setFieldValue('modelId', selectedModelId);
|
||||
const newValues = form.getFieldsValue();
|
||||
const { modelId, limitConfig, ...params } = newValues;
|
||||
// 生成你需要的格式
|
||||
|
||||
// ======================
|
||||
// 👇 强制给默认值 0 ~ 100
|
||||
// ======================
|
||||
const finalLimitConfig = {};
|
||||
if (limitConfig) {
|
||||
Object.keys(limitConfig).forEach(key => {
|
||||
finalLimitConfig[key] = {
|
||||
min: limitConfig[key].min ?? 0,
|
||||
max: limitConfig[key].max ?? 100,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const thingsModels = {};
|
||||
if (modelId) {
|
||||
thingsModels[modelId] = limitConfig || {};
|
||||
thingsModels[modelId] = finalLimitConfig; // 👈 用处理后的
|
||||
}
|
||||
|
||||
const submitData = {
|
||||
...params,
|
||||
thingsModels: thingsModels
|
||||
thingsModels: thingsModels,
|
||||
};
|
||||
|
||||
addProduct(submitData).then(res => {
|
||||
if (res.code == 200) {
|
||||
message.success('新增产品成功');
|
||||
getProductList()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -382,6 +411,8 @@ export default function DeviceAccessPage() {
|
||||
useEffect(() => {
|
||||
getConnectList();
|
||||
getModelAll();
|
||||
getProductList();
|
||||
getDataTypes();
|
||||
}, []);
|
||||
|
||||
// 表格列
|
||||
@@ -475,8 +506,95 @@ export default function DeviceAccessPage() {
|
||||
</Card>
|
||||
|
||||
{activeTab === 'device' && <Card><Space className="mb-3"><Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('device')}>新增设备</Button></Space><Table rowKey="id" columns={deviceColumns} dataSource={deviceList} /></Card>}
|
||||
{activeTab === 'product' && <Card><Space className="mb-3"><Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('product')}>新增产品</Button></Space><Table rowKey="id" columns={productColumns} dataSource={productList} /></Card>}
|
||||
{activeTab === 'product' && (
|
||||
<Card>
|
||||
{/* ====================== 产品筛选表单 ====================== */}
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between', alignItems: 'end' }}>
|
||||
<Form
|
||||
form={filterForm}
|
||||
layout="inline"
|
||||
initialValues={{ pageNum: 1, pageSize: 10 }}
|
||||
style={{ display: 'flex', gap: '16px', flexWrap: 'wrap', alignItems: 'center' }}
|
||||
>
|
||||
<Form.Item name="code" label="产品编码">
|
||||
<Input placeholder="请输入编码" allowClear />
|
||||
</Form.Item>
|
||||
<Form.Item name="name" label="产品名称">
|
||||
<Input placeholder="请输入名称" allowClear />
|
||||
</Form.Item>
|
||||
<Form.Item name="manufacturer" label="生产厂家">
|
||||
<Input placeholder="请输入厂家" allowClear />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item name="protocolCode" label="协议编码">
|
||||
<Select placeholder="请选择协议" allowClear style={{ width: 140 }}>
|
||||
{protocolList.map(p => (
|
||||
<Option key={p.code} value={p.code}>{p.name}</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item name="transport" label="传输方式">
|
||||
<Select placeholder="传输方式" allowClear style={{ width: 120 }}>
|
||||
<Option value="tcp">TCP</Option>
|
||||
<Option value="udp">UDP</Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item name="status" label="状态">
|
||||
<Select placeholder="状态" allowClear style={{ width: 100 }}>
|
||||
<Option value={1}>启用</Option>
|
||||
<Option value={0}>禁用</Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item style={{ marginLeft: 8 }}>
|
||||
<Button type="primary" onClick={handleFilter}>查询</Button>
|
||||
<Button onClick={handleReset} style={{ marginLeft: 8 }}>重置</Button>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => openModal('product')}>
|
||||
新增产品
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* ====================== 产品表格 ====================== */}
|
||||
<Table
|
||||
rowKey="id"
|
||||
columns={[
|
||||
{ title: '产品编码', dataIndex: 'code', width: 120 },
|
||||
{ title: '产品名称', dataIndex: 'name', width: 160 },
|
||||
{ title: '生产厂家', dataIndex: 'manufacturer', width: 130 },
|
||||
{ title: '分类ID', dataIndex: 'categoryId', width: 90 },
|
||||
{ title: '协议', dataIndex: 'protocolCode', width: 100 },
|
||||
{ title: '传输方式', dataIndex: 'transport', width: 100 },
|
||||
{ title: '上报Topic', dataIndex: 'uploadTopic', ellipsis: true },
|
||||
{ title: '订阅Topic', dataIndex: 'subscribeTopic', ellipsis: true },
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 90,
|
||||
render: (s) => <Tag color={s === 1 ? 'green' : 'red'}>{s === 1 ? '启用' : '禁用'}</Tag>
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 120,
|
||||
render: (r) => (
|
||||
<Space>
|
||||
<Button type="text" icon={<EditOutlined />} onClick={() => openModal('product', r)}>编辑</Button>
|
||||
</Space>
|
||||
)
|
||||
}
|
||||
]}
|
||||
dataSource={productList}
|
||||
pagination={{
|
||||
current: pagination.current,
|
||||
pageSize: pagination.pageSize,
|
||||
total: pagination.total,
|
||||
showSizeChanger: true,
|
||||
showTotal: (total) => `共 ${total} 条`
|
||||
}}
|
||||
onChange={handleTableChange}
|
||||
/>
|
||||
</Card>
|
||||
)}
|
||||
{/* 物模型 + 筛选 + 分页 */}
|
||||
{activeTab === 'model' && (
|
||||
<Card>
|
||||
@@ -519,7 +637,7 @@ export default function DeviceAccessPage() {
|
||||
<Form.Item label="设备名称" name="name" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item label="设备SN" name="sn" rules={[{ required: true }]}><Input /></Form.Item>
|
||||
<Form.Item label="关联产品" name="productId" rules={[{ required: true }]}><Select>{productList.map(p => <Option key={p.id} value={p.id}>{p.name}</Option>)}</Select></Form.Item>
|
||||
<Form.Item label="关联物模型" name="modelId" rules={[{ required: true }]}><Select onChange={val => setModelDetail(modelDetailList[val] || { props: [] })}>{modelList.map(m => <Option key={m.id} value={m.id}>{m.name}</Option>)}</Select></Form.Item>
|
||||
{/*<Form.Item label="关联物模型" name="modelId" rules={[{ required: true }]}><Select onChange={val => setModelDetail(modelDetailList[val] || { props: [] })}>{modelList.map(m => <Option key={m.id} value={m.id}>{m.name}</Option>)}</Select></Form.Item>*/}
|
||||
</TabPane>
|
||||
<TabPane tab="协议配置" key="protocol">
|
||||
<Form.Item label="设备协议类型" name="protocolType" rules={[{ required: true }]}>
|
||||
@@ -659,14 +777,15 @@ export default function DeviceAccessPage() {
|
||||
style={{ flex: 1 }}
|
||||
|
||||
>
|
||||
<InputNumber style={{ width: '100%' }} placeholder="最小值" defaultValue={0} />
|
||||
<InputNumber style={{ width: '100%' }} placeholder="最小值" initialValue={0}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
label="最大值"
|
||||
name={['limitConfig', item.propertyKey, 'max']}
|
||||
style={{ flex: 1 }}
|
||||
>
|
||||
<InputNumber style={{ width: '100%' }} placeholder="最大值" defaultValue={100} />
|
||||
<InputNumber style={{ width: '100%' }} placeholder="最大值" initialValue={100} />
|
||||
</Form.Item>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user