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 DictManager from "../DictManager"; import request from '../../api/request'; import { Country, State } from 'country-state-city'; // 接口导入 import { getRobotTypeListApi, addRobotTypeApi, updateRobotTypeApi, deleteRobotTypeApi, getRobotModelPageApi, addRobotModelApi, updateRobotModelApi, deleteRobotModelApi, getRobotConfigListApi, addRobotConfigApi, updateRobotConfigApi, deleteRobotConfigApi, } from '../../api/systemSetting'; const { Text } = Typography; const { Option } = Select; const SerialNumGenerate = () => { // 表单实例 const [form] = Form.useForm(); // 国家/省份 数据(本地库获取) const [allCountries, setAllCountries] = useState([]); const [provinces, setProvinces] = useState([]); // 二维码相关 const [qrUrl, setQrUrl] = useState(''); const [serialNumber, setSerialNumber] = useState(''); const [qrJson, setQrJson] = useState(''); 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, setTypes] = useState([]); const [models, setModels] = useState([]); const [configs, setConfigs] = useState([]); // 初始化:加载国家列表 useEffect(() => { const countries = Country.getAllCountries(); setAllCountries(countries || []); // 加载其他数据 loadTypeList(); loadModelList(); loadConfigList(); }, []); // 国家改变 → 刷新省份 const handleCountryChange = (countryCode) => { const states = State.getStatesOfCountry(countryCode); setProvinces(states || []); // 切换国家时自动清空省份 form.setFieldsValue({ province: undefined }); }; // ==================== 接口加载 ==================== // 加载机器人类型 const loadTypeList = async () => { try { const res = await getRobotTypeListApi({}); if (res?.code === 200) setTypes(res.data || []); } catch (e) { } }; // 加载机器人型号 const loadModelList = async () => { try { const res = await getRobotModelPageApi({ pageNum: 1, pageSize: 100 }); if (res?.code === 200) setModels(res.data || []); } catch (e) { } }; // 加载配置 const loadConfigList = async () => { try { const res = await getRobotConfigListApi({}); if (res?.code === 200) setConfigs(res.data || []); } catch (e) { } }; // 生成序号 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}`; }; // 生成二维码 const generateQRCode = async () => { try { const values = await form.validateFields(); const { model, config, type, country, province } = values; const ts = Date.now(); const suffix = generateSerialSuffix(); const sn = `${model}${config}-${country}-${province}-${ts}-${suffix}`; const qrData = { model, config, type, country, province, sn }; const qrContent = JSON.stringify(qrData, null, 2); const url = await QRCode.toDataURL(qrContent); setSerialNumber(sn); setQrJson(qrContent); setQrUrl(url); message.success('生成成功!'); } catch (err) { message.error('请完善信息'); } }; // 下载二维码 const downloadQRCode = () => { if (!qrRef.current || !qrUrl) { message.warning('请先生成二维码'); return; } const link = document.createElement('a'); link.download = `${serialNumber || 'qrcode'}.png`; link.href = qrUrl; link.click(); }; // 保存到数据库 const saveToDatabase = async () => { if (!serialNumber) return message.warning('请先生成'); setLoading(true); try { 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 (e) { message.error('保存失败'); } finally { setLoading(false); } }; // 查询序列号 const handleQueryQRCode = async () => { if (!queryCode) return message.warning('请输入序列号'); setQueryLoading(true); try { 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('未查询到数据'); } } catch (e) { message.error('查询失败'); } finally { setQueryLoading(false); } }; // ==================== 数据处理方法 ==================== // 类型 const handleAddType = async (values) => { return await addRobotTypeApi({ storeValue: values.storeValue, displayName: values.displayName, remark: values.remark || '' }); }; const handleUpdateType = async (values) => { return await updateRobotTypeApi({ id: values.id, storeValue: values.storeValue, displayName: values.displayName, remark: values.remark || '' }); }; // 型号 const handleAddModel = async (values) => { return await addRobotModelApi({ modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName }); }; const handleUpdateModel = async (values) => { return await updateRobotModelApi({ id: values.id, modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName }); }; // 配置 const handleAddConfig = async (values) => { return await addRobotConfigApi({ configName: values.displayName, storeValue: values.storeValue, displayName: values.displayName }); }; const handleUpdateConfig = async (values) => { return await updateRobotConfigApi({ id: values.id, configName: values.displayName, storeValue: values.storeValue, displayName: values.displayName }); }; return (
setManageVisible(true)}>数据管理} >
{/* 型号 */} {/* 配置 */} {/* 类型 */} {/* 国家(级联) */} {/* 国家(级联 + 可清空) */} {/* 省份(级联 + 可清空 + 中国显示中文) */}
setQueryCode(e.target.value)} placeholder="输入序列号查询" style={{ width: '300px' }} />
{serialNumber && (
序列号:{serialNumber}
)} {qrUrl ? ( qrcode ) : (
请先生成二维码
)}
setManageVisible(false)} footer={} destroyOnClose > {/* 机器人类型 */} {/* 机器人型号 */} {/* 配置 */}
); }; export default SerialNumGenerate;