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, getRobotProvinceListApi, addRobotProvinceApi, updateRobotProvinceApi, deleteRobotProvinceApi, getRobotModelPageApi, addRobotModelApi, updateRobotModelApi, deleteRobotModelApi, getRobotCountryListApi, addRobotCountryApi, updateRobotCountryApi, deleteRobotCountryApi, getRobotConfigListApi, addRobotConfigApi, updateRobotConfigApi, deleteRobotConfigApi, } from '../../api/systemSetting'; const { Text } = Typography; const SerialNumGenerate = () => { // 获取国家 const countries1 = Country.getAllCountries(); // 根据国家获取省份 const states = State.getStatesOfCountry('CN'); const [form] = Form.useForm(); 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 [provinces, setProvinces] = useState([]); const [countries, setCountries] = useState([]); const [configs, setConfigs] = useState([]); // 初始化加载 useEffect(() => { loadTypeList(); loadModelList(); loadProvinceList(); loadCountryList(); loadConfigList(); }, []); // 加载机器人类型 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 loadProvinceList = async () => { try { const res = await getRobotProvinceListApi({}); setProvinces(res.rows || []); } catch (e) { message.error('加载省份失败'); } }; // 加载国家 const loadCountryList = async () => { try { const res = await getRobotCountryListApi({}); setCountries(res.rows || []); } catch (e) { message.error('加载国家失败'); } }; // 加载配置 const loadConfigList = async () => { try { const res = await getRobotConfigListApi({}); setConfigs(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}`; }; // 生成二维码 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.modelName, storeValue: values.storeValue, displayName: '' }); }; const handleUpdateModel = async (values) => { return await updateRobotModelApi({ id: values.id, modelName: values.modelName, displayName: values.displayName }); }; // ==================== 省份 ==================== const handleAddProvince = async (values) => { return await addRobotProvinceApi({ storeValue: values.storeValue, displayName: values.displayName, remark: values.remark || '' }); }; const handleUpdateProvince = async (values) => { return await updateRobotProvinceApi({ id: values.id, storeValue: values.storeValue, displayName: values.displayName, remark: values.remark || '' }); }; // ==================== 国家 ==================== const handleAddCountry = async (values) => { return await addRobotCountryApi({ countryName: values.countryName, displayName: values.displayName }); }; const handleUpdateCountry = async (values) => { return await updateRobotCountryApi({ id: values.id, countryName: values.countryName, displayName: values.displayName }); }; // ==================== 配置 ==================== const handleAddConfig = async (values) => { return await addRobotConfigApi({ configName: values.configName, displayName: values.displayName }); }; const handleUpdateConfig = async (values) => { return await updateRobotConfigApi({ id: values.id, configName: values.configName, 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;