diff --git a/src/api/systemSetting.ts b/src/api/systemSetting.ts index 1473d0d..ccc02a4 100644 --- a/src/api/systemSetting.ts +++ b/src/api/systemSetting.ts @@ -260,3 +260,13 @@ export function deleteRobotConfigApi(ids) { method: 'delete' }); } + +//b保存二维码/ + +export function saveCodeApi(data) { + return request({ + url: `/system/qrcode/generate`, + method: 'post', + data + }); +} diff --git a/src/components/SystemSetting/SerialNumGenerate.jsx b/src/components/SystemSetting/SerialNumGenerate.jsx index f02b20f..47650da 100644 --- a/src/components/SystemSetting/SerialNumGenerate.jsx +++ b/src/components/SystemSetting/SerialNumGenerate.jsx @@ -22,16 +22,19 @@ import { addRobotConfigApi, updateRobotConfigApi, deleteRobotConfigApi, + saveCodeApi } from '../../api/systemSetting'; const { Text } = Typography; const { Option } = Select; const SerialNumGenerate = () => { + const [messageApi, contextHolder] = message.useMessage(); + // 表单实例 const [form] = Form.useForm(); - // 国家/省份 数据(本地库获取) + // 国家/省份 数据 const [allCountries, setAllCountries] = useState([]); const [provinces, setProvinces] = useState([]); @@ -49,17 +52,24 @@ const SerialNumGenerate = () => { // 数据管理弹窗 const [manageVisible, setManageVisible] = useState(false); - // 接口数据(类型、型号、配置) + // 接口数据 const [types, setTypes] = useState([]); const [models, setModels] = useState([]); const [configs, setConfigs] = useState([]); - // 初始化:加载国家列表 + // 存储完整中文名称映射 + const countryNameMap = useRef({}); + const provinceNameMap = useRef({}); + + // 初始化 useEffect(() => { const countries = Country.getAllCountries(); setAllCountries(countries || []); + // 构建 简码=>中文名称 映射 + countries.forEach(item => { + countryNameMap.current[item.isoCode] = item.name; + }) - // 加载其他数据 loadTypeList(); loadModelList(); loadConfigList(); @@ -69,11 +79,13 @@ const SerialNumGenerate = () => { const handleCountryChange = (countryCode) => { const states = State.getStatesOfCountry(countryCode); setProvinces(states || []); - - // 切换国家时自动清空省份 + // 构建省份简码=>中文名称映射 + states.forEach(item => { + provinceNameMap.current[item.isoCode] = item.name; + }) form.setFieldsValue({ province: undefined }); }; - // ==================== 接口加载 ==================== + // 加载机器人类型 const loadTypeList = async () => { try { @@ -98,7 +110,7 @@ const SerialNumGenerate = () => { } 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'); @@ -112,25 +124,35 @@ const SerialNumGenerate = () => { 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 serialNumber = `${model}${config}-${country}-${province}-${ts}-${suffix}`; + + // 二维码JSON存入【完整中文名称】,扫码查看完整信息 + const qrData = { + model, + config, + type, + countryCode: country, + countryFullName: countryNameMap.current[country] || country, + provinceCode: province, + provinceFullName: provinceNameMap.current[province] || province, + serialNumber + }; const qrContent = JSON.stringify(qrData, null, 2); const url = await QRCode.toDataURL(qrContent); - setSerialNumber(sn); + setSerialNumber(serialNumber); setQrJson(qrContent); setQrUrl(url); - message.success('生成成功!'); } catch (err) { - message.error('请完善信息'); } }; // 下载二维码 const downloadQRCode = () => { if (!qrRef.current || !qrUrl) { - message.warning('请先生成二维码'); + messageApi.warning('请先生成二维码'); return; } const link = document.createElement('a'); @@ -141,26 +163,28 @@ const SerialNumGenerate = () => { // 保存到数据库 const saveToDatabase = async () => { - if (!serialNumber) return message.warning('请先生成'); + if (!serialNumber) return messageApi.warning('请先生成'); setLoading(true); + const data = { + serialNumber, + modelId: form.getFieldValue('model'), + configId: form.getFieldValue('config'), + typeId: form.getFieldValue('type'), + countryId: form.getFieldValue('country'), + provinceId: form.getFieldValue('province'), + qrCode: qrUrl, + qrJson: qrJson + } 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('保存成功'); + const res = await saveCodeApi(data); + if (res?.code == 200) { + messageApi.success('保存成功'); + } else { + messageApi.error('保存失败' + res?.msg); + + } } catch (e) { - message.error('保存失败'); + messageApi.error('保存失败'); } finally { setLoading(false); } @@ -168,13 +192,13 @@ const SerialNumGenerate = () => { // 查询序列号 const handleQueryQRCode = async () => { - if (!queryCode) return message.warning('请输入序列号'); + if (!queryCode) return messageApi.warning('请输入序列号'); setQueryLoading(true); try { const res = await request({ url: '/iot/device/querySerial', method: 'get', - params: sn.queryCode + params: { sn: queryCode } }); if (res.data) { setSerialNumber(res.data.serialNumber); @@ -187,18 +211,17 @@ const SerialNumGenerate = () => { country: res.data.country, province: res.data.province }); - message.success('查询成功'); + messageApi.success('查询成功'); } else { - message.warning('未查询到数据'); + messageApi.warning('未查询到数据'); } } catch (e) { - message.error('查询失败'); + messageApi.error('查询失败'); } finally { setQueryLoading(false); } }; - // ==================== 数据处理方法 ==================== // 类型 const handleAddType = async (values) => { return await addRobotTypeApi({ @@ -222,7 +245,6 @@ const SerialNumGenerate = () => { modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName - }); }; const handleUpdateModel = async (values) => { @@ -231,7 +253,6 @@ const SerialNumGenerate = () => { modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName - }); }; @@ -241,7 +262,6 @@ const SerialNumGenerate = () => { configName: values.displayName, storeValue: values.storeValue, displayName: values.displayName - }); }; const handleUpdateConfig = async (values) => { @@ -255,6 +275,7 @@ const SerialNumGenerate = () => { return (