机器人序列号 接口初始化
This commit is contained in:
@@ -1,48 +1,28 @@
|
||||
import React, { useState, useRef } from 'react';
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import {
|
||||
Form, Select, Button, Space, message, Card, Typography,
|
||||
Row, Col, Modal, Input
|
||||
} from 'antd';
|
||||
import QRCode from 'qrcode';
|
||||
import axios from 'axios';
|
||||
import DictManager from "../DictManager";
|
||||
import request from '../../api/request';
|
||||
|
||||
// 接口导入
|
||||
import {
|
||||
getRobotTypeListApi,
|
||||
addRobotTypeApi,
|
||||
updateRobotTypeApi,
|
||||
deleteRobotTypeApi,
|
||||
getRobotProvinceListApi,
|
||||
addRobotProvinceApi,
|
||||
updateRobotProvinceApi,
|
||||
deleteRobotProvinceApi,
|
||||
getRobotModelPageApi,
|
||||
addRebotModel
|
||||
} from '../../api/systemSetting';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
// 字典数据
|
||||
const useDict = () => {
|
||||
const [types, setTypes] = useState([
|
||||
{ id: 1, value: 'mower', label: '割草机' },
|
||||
{ id: 2, value: 'vacuum', label: '巡检机' },
|
||||
]);
|
||||
const [models, setModels] = useState([
|
||||
{ id: 1, value: 'MC700', label: 'MC700' },
|
||||
{ id: 2, value: 'MC800', label: 'MC800' },
|
||||
]);
|
||||
const [configs, setConfigs] = useState([
|
||||
{ id: 1, value: 'AIR', label: 'AIR' },
|
||||
{ id: 2, value: 'PRO', label: 'PRO' },
|
||||
]);
|
||||
const [countries, setCountries] = useState([
|
||||
{ id: 1, value: 'CN', label: '中国' },
|
||||
{ id: 2, value: 'US', label: '美国' },
|
||||
]);
|
||||
const [provinces, setProvinces] = useState([
|
||||
{ id: 1, value: 'JS', label: '江苏' },
|
||||
{ id: 2, value: 'ZJ', label: '浙江' },
|
||||
{ id: 3, value: 'GD', label: '广东' },
|
||||
]);
|
||||
|
||||
return {
|
||||
types, setTypes,
|
||||
models, setModels,
|
||||
configs, setConfigs,
|
||||
countries, setCountries,
|
||||
provinces, setProvinces,
|
||||
};
|
||||
};
|
||||
|
||||
// 主组件
|
||||
const SerialNumGenerate = () => {
|
||||
const [form] = Form.useForm();
|
||||
const [qrUrl, setQrUrl] = useState('');
|
||||
@@ -51,62 +31,90 @@ const SerialNumGenerate = () => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const qrRef = useRef(null);
|
||||
|
||||
// 查询相关状态
|
||||
const [queryCode, setQueryCode] = useState('');
|
||||
const [queryLoading, setQueryLoading] = useState(false);
|
||||
|
||||
const [manageVisible, setManageVisible] = useState(false);
|
||||
|
||||
const {
|
||||
types,
|
||||
models,
|
||||
configs,
|
||||
countries,
|
||||
provinces
|
||||
} = useDict();
|
||||
// 接口数据
|
||||
const [types, setTypes] = useState([]);
|
||||
const [models, setModels] = useState([]);
|
||||
const [provinces, setProvinces] = useState([]);
|
||||
const [countries] = useState([
|
||||
{ id: 1, value: 'CN', label: '中国' },
|
||||
{ id: 2, value: 'US', label: '美国' },
|
||||
]);
|
||||
const [configs] = useState([
|
||||
{ id: 1, value: 'AIR', label: 'AIR' },
|
||||
{ id: 2, value: 'PRO', label: 'PRO' },
|
||||
]);
|
||||
|
||||
// 生成 8 位随机序号 + 2 位校验码
|
||||
// 初始化加载
|
||||
useEffect(() => {
|
||||
loadTypeList();
|
||||
loadModelList();
|
||||
loadProvinceList();
|
||||
}, []);
|
||||
|
||||
// 加载机器人类型
|
||||
const loadTypeList = async () => {
|
||||
try {
|
||||
const res = await getRobotTypeListApi({});
|
||||
setTypes(res.rows || []);
|
||||
} catch (e) {
|
||||
message.error('加载类型失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 加载机器人型号
|
||||
const loadModelList = async () => {
|
||||
try {
|
||||
const res = await getRobotModelPageApi({ pageNum: 1, pageSize: 100 });
|
||||
setModels(res.rows || []);
|
||||
} catch (e) {
|
||||
message.error('加载型号失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 加载省份
|
||||
const loadProvinceList = async () => {
|
||||
try {
|
||||
const res = await getRobotProvinceListApi({});
|
||||
setProvinces(res.rows || []);
|
||||
} catch (e) {
|
||||
message.error('加载省份失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 生成序号
|
||||
const generateSerialSuffix = () => {
|
||||
const num = Math.floor(Math.random() * 100000000).toString(16).toUpperCase().padStart(8, '0');
|
||||
const check = Math.floor(Math.random() * 256).toString(16).toUpperCase().padStart(2, '0');
|
||||
return `${num}-${check}`;
|
||||
};
|
||||
|
||||
// 生成序列号 + JSON二维码
|
||||
// 生成二维码
|
||||
const generateQRCode = async () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
const { model, config, type, country, province } = values;
|
||||
const ts = Date.now();
|
||||
const suffix = generateSerialSuffix();
|
||||
|
||||
// 1. 生成序列号
|
||||
const sn = `${model}${config}-${country}-${province}-${ts}-${suffix}`;
|
||||
setSerialNumber(sn);
|
||||
|
||||
// 2. 二维码内容 = JSON
|
||||
const qrData = {
|
||||
model,
|
||||
config,
|
||||
type,
|
||||
country,
|
||||
province,
|
||||
sn: sn
|
||||
};
|
||||
const qrData = { model, config, type, country, province, sn };
|
||||
const qrContent = JSON.stringify(qrData, null, 2);
|
||||
setQrJson(qrContent);
|
||||
|
||||
// 3. 生成二维码
|
||||
const url = await QRCode.toDataURL(qrContent);
|
||||
setQrUrl(url);
|
||||
|
||||
setSerialNumber(sn);
|
||||
setQrJson(qrContent);
|
||||
setQrUrl(url);
|
||||
message.success('生成成功!');
|
||||
} catch (err) {
|
||||
message.error('请完善信息');
|
||||
}
|
||||
};
|
||||
|
||||
// 下载二维码
|
||||
// 下载
|
||||
const downloadQRCode = () => {
|
||||
if (!qrRef.current || !qrUrl) {
|
||||
message.warning('请先生成二维码');
|
||||
@@ -118,49 +126,116 @@ const SerialNumGenerate = () => {
|
||||
link.click();
|
||||
};
|
||||
|
||||
// 保存到数据库
|
||||
// 保存序列号
|
||||
const saveToDatabase = async () => {
|
||||
if (!serialNumber || !qrUrl) {
|
||||
message.warning('请先生成二维码');
|
||||
return;
|
||||
}
|
||||
if (!serialNumber) return message.warning('请先生成');
|
||||
setLoading(true);
|
||||
try {
|
||||
await axios.post('/api/save-qrcode', {
|
||||
serialNumber,
|
||||
qrCode: qrUrl,
|
||||
qrJson: qrJson,
|
||||
...form.getFieldsValue()
|
||||
await request({
|
||||
url: '/iot/device/saveSerial',
|
||||
method: 'post',
|
||||
data: {
|
||||
serialNumber,
|
||||
model: form.getFieldValue('model'),
|
||||
config: form.getFieldValue('config'),
|
||||
type: form.getFieldValue('type'),
|
||||
country: form.getFieldValue('country'),
|
||||
province: form.getFieldValue('province'),
|
||||
qrCode: qrUrl,
|
||||
qrJson: qrJson
|
||||
}
|
||||
});
|
||||
message.success('保存成功');
|
||||
} catch {
|
||||
} catch (e) {
|
||||
message.error('保存失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 查询二维码
|
||||
// 查询
|
||||
const handleQueryQRCode = async () => {
|
||||
if (!queryCode) return message.warning('请输入序列号');
|
||||
setQueryLoading(true);
|
||||
try {
|
||||
const res = await axios.get('/api/query-qrcode', { params: { sn: queryCode } });
|
||||
if (res.data?.qrUrl) {
|
||||
setQrUrl(res.data.qrUrl);
|
||||
setQrJson(res.data.qrJson || '');
|
||||
setSerialNumber(res.data.serialNumber || '');
|
||||
const res = await request({
|
||||
url: '/iot/device/querySerial',
|
||||
method: 'get',
|
||||
params: { sn: queryCode }
|
||||
});
|
||||
if (res.data) {
|
||||
setSerialNumber(res.data.serialNumber);
|
||||
setQrUrl(res.data.qrCode);
|
||||
setQrJson(res.data.qrJson);
|
||||
form.setFieldsValue({
|
||||
model: res.data.model,
|
||||
config: res.data.config,
|
||||
type: res.data.type,
|
||||
country: res.data.country,
|
||||
province: res.data.province
|
||||
});
|
||||
message.success('查询成功');
|
||||
} else {
|
||||
message.warning('未找到该序列号');
|
||||
message.warning('未查询到数据');
|
||||
}
|
||||
} catch {
|
||||
} catch (e) {
|
||||
message.error('查询失败');
|
||||
} finally {
|
||||
setQueryLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// ==================== 机器人类型:增删改查 ====================
|
||||
const handleAddType = async (values) => {
|
||||
const params = {
|
||||
typeName: values.typeName,
|
||||
storeValue: values.storeValue,
|
||||
remark: values.remark || ''
|
||||
};
|
||||
return await addRobotTypeApi(params);
|
||||
};
|
||||
|
||||
const handleUpdateType = async (values) => {
|
||||
const params = {
|
||||
id: values.id,
|
||||
typeName: values.typeName,
|
||||
storeValue: values.storeValue,
|
||||
remark: values.remark || ''
|
||||
};
|
||||
return await updateRobotTypeApi(params);
|
||||
};
|
||||
|
||||
// ==================== 机器人型号:增删改查 ====================
|
||||
const handleAddModel = async (values) => {
|
||||
const params = {
|
||||
modelName: values.modelName,
|
||||
storeValue: values.storeValue,
|
||||
remark: values.remark || ''
|
||||
};
|
||||
return await addRebotModel(params)
|
||||
};
|
||||
|
||||
const handleUpdateModel = async (values) => {
|
||||
const params = {
|
||||
id: values.id,
|
||||
modelName: values.modelName,
|
||||
storeValue: values.storeValue,
|
||||
remark: values.remark || ''
|
||||
};
|
||||
return await request({
|
||||
url: '/system/robot/model',
|
||||
method: 'put',
|
||||
data: params
|
||||
});
|
||||
};
|
||||
|
||||
const handleDeleteModel = async (id) => {
|
||||
return await request({
|
||||
url: `/system/robot/model/${id}`,
|
||||
method: 'delete'
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
|
||||
<Card
|
||||
@@ -169,12 +244,13 @@ const SerialNumGenerate = () => {
|
||||
extra={<Button type="primary" onClick={() => setManageVisible(true)}>数据管理</Button>}
|
||||
>
|
||||
<Row gutter={[32, 24]}>
|
||||
{/* 左侧表单 */}
|
||||
<Col xs={24} md={14}>
|
||||
<Form form={form} initialValues={{ type: 'mower' }} labelCol={{ md: 5 }} wrapperCol={{ md: 19 }} size="large">
|
||||
<Form.Item name="model" label="型号" rules={[{ required: true }]}>
|
||||
<Select placeholder="选择型号">
|
||||
{models.map(m => <Select.Option key={m.value} value={m.value}>{m.label}</Select.Option>)}
|
||||
{models.map(m => (
|
||||
<Select.Option key={m.id} value={m.storeValue}>{m.modelName}</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
@@ -186,7 +262,9 @@ const SerialNumGenerate = () => {
|
||||
|
||||
<Form.Item name="type" label="类型" rules={[{ required: true }]}>
|
||||
<Select placeholder="选择类型">
|
||||
{types.map(t => <Select.Option key={t.value} value={t.value}>{t.label}</Select.Option>)}
|
||||
{types.map(t => (
|
||||
<Select.Option key={t.id} value={t.storeValue}>{t.typeName}</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
@@ -198,7 +276,9 @@ const SerialNumGenerate = () => {
|
||||
|
||||
<Form.Item name="province" label="省份" rules={[{ required: true }]}>
|
||||
<Select placeholder="选择省份">
|
||||
{provinces.map(p => <Select.Option key={p.value} value={p.value}>{p.label}</Select.Option>)}
|
||||
{provinces.map(p => (
|
||||
<Select.Option key={p.id} value={p.storeValue}>{p.typeName}</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
@@ -209,18 +289,13 @@ const SerialNumGenerate = () => {
|
||||
</Form.Item>
|
||||
</Form>
|
||||
|
||||
{/* 查询模块(已加回) */}
|
||||
<Card size="small" title="查询历史二维码" style={{ marginTop: 20 }}>
|
||||
<Space style={{ width: '100%' }}>
|
||||
<Input
|
||||
value={queryCode}
|
||||
onChange={(e) => setQueryCode(e.target.value)}
|
||||
onChange={(e) => setQueryCode(e.value)}
|
||||
placeholder="输入序列号查询"
|
||||
style={
|
||||
{
|
||||
width: '300px'
|
||||
}
|
||||
}
|
||||
style={{ width: '300px' }}
|
||||
/>
|
||||
<Button type="primary" loading={queryLoading} onClick={handleQueryQRCode}>
|
||||
查询
|
||||
@@ -229,25 +304,19 @@ const SerialNumGenerate = () => {
|
||||
</Card>
|
||||
</Col>
|
||||
|
||||
{/* 右侧二维码 */}
|
||||
<Col xs={24} md={10}>
|
||||
<div style={{
|
||||
padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9',
|
||||
display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 480
|
||||
}}>
|
||||
{serialNumber && (
|
||||
<div style={{
|
||||
padding: 12, background: '#e6f7ff', borderRadius: 8,
|
||||
width: '100%', textAlign: 'center', marginBottom: 20
|
||||
}}>
|
||||
<Text strong style={{ fontSize: 15 }}>
|
||||
序列号:{serialNumber}
|
||||
</Text>
|
||||
<div style={{ padding: 12, background: '#e6f7ff', borderRadius: 8, width: '100%', textAlign: 'center', marginBottom: 20 }}>
|
||||
<Text strong style={{ fontSize: 15 }}>序列号:{serialNumber}</Text>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{qrUrl ? (
|
||||
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240 }} />
|
||||
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240 }} alt="qrcode" />
|
||||
) : (
|
||||
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
|
||||
)}
|
||||
@@ -261,19 +330,57 @@ const SerialNumGenerate = () => {
|
||||
</Row>
|
||||
</Card>
|
||||
|
||||
{/* 数据管理弹窗 */}
|
||||
<Modal
|
||||
open={manageVisible}
|
||||
title="数据管理"
|
||||
width={900}
|
||||
onCancel={() => setManageVisible(false)}
|
||||
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
||||
destroyOnClose
|
||||
>
|
||||
<DictManager title="机器人类型" data={types} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||
<DictManager title="机器人型号" data={models} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||
<DictManager title="配置" data={configs} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||
<DictManager title="国家" data={countries} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||
<DictManager title="省份" data={provinces} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||
{/* 机器人类型 */}
|
||||
<DictManager
|
||||
title="机器人类型"
|
||||
data={types}
|
||||
fields={[
|
||||
{ name: 'typeName', label: '类型名称', required: true },
|
||||
{ name: 'storeValue', label: '存储值', required: true },
|
||||
{ name: 'remark', label: '备注', required: false }
|
||||
]}
|
||||
onAdd={handleAddType}
|
||||
onUpdate={handleUpdateType}
|
||||
onDelete={deleteRobotTypeApi}
|
||||
refresh={loadTypeList}
|
||||
/>
|
||||
|
||||
{/* 机器人型号 */}
|
||||
<DictManager
|
||||
title="机器人型号"
|
||||
data={models}
|
||||
fields={[
|
||||
{ name: 'modelName', label: '型号名称', required: true },
|
||||
{ name: 'storeValue', label: '存储值', required: true },
|
||||
]}
|
||||
onAdd={handleAddModel}
|
||||
onUpdate={handleUpdateModel}
|
||||
onDelete={handleDeleteModel}
|
||||
refresh={loadModelList}
|
||||
/>
|
||||
|
||||
{/* 省份 */}
|
||||
<DictManager
|
||||
title="省份"
|
||||
data={provinces}
|
||||
fields={[
|
||||
{ name: 'typeName', label: '省份名称', required: true },
|
||||
{ name: 'storeValue', label: '存储值', required: true },
|
||||
{ name: 'remark', label: '备注', required: false }
|
||||
]}
|
||||
onAdd={addRobotProvinceApi}
|
||||
onUpdate={updateRobotProvinceApi}
|
||||
onDelete={deleteRobotProvinceApi}
|
||||
refresh={loadProvinceList}
|
||||
/>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user