diff --git a/src/components/SystemSetting/SerialNumGenerate.jsx b/src/components/SystemSetting/SerialNumGenerate.jsx index 0c0cdfd..2315cd6 100644 --- a/src/components/SystemSetting/SerialNumGenerate.jsx +++ b/src/components/SystemSetting/SerialNumGenerate.jsx @@ -1,7 +1,7 @@ -import React, { useState, useRef, useEffect } from 'react'; +import React, { useState, useRef } from 'react'; import { Form, Select, Input, Button, Space, message, Card, Typography, - Row, Col, Modal, Table, Tag, Divider + Row, Col, Modal } from 'antd'; import QRCode from 'qrcode'; import axios from 'axios'; @@ -9,36 +9,28 @@ import DictManager from "../DictManager" 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' }, - { id: 3, value: 'MC900', label: 'MC900' }, ]); - // 配置 const [configs, setConfigs] = useState([ { id: 1, value: 'AIR', label: 'AIR' }, { id: 2, value: 'PRO', label: 'PRO' }, - { id: 3, value: 'MAX', label: 'MAX' }, ]); - // 国家 const [countries, setCountries] = useState([ - { id: 1, value: '中国', label: '中国' }, - { id: 2, value: '美国', label: '美国' }, - { id: 3, value: '日本', label: '日本' }, + { id: 1, value: 'CN', label: '中国' }, + { id: 2, value: 'US', label: '美国' }, ]); - // 省份 const [provinces, setProvinces] = useState([ - { id: 1, value: '江苏', label: '江苏' }, - { id: 2, value: '浙江', label: '浙江' }, - { id: 3, value: '广东', label: '广东' }, + { id: 1, value: 'JS', label: '江苏' }, + { id: 2, value: 'ZJ', label: '浙江' }, + { id: 3, value: 'GD', label: '广东' }, ]); return { @@ -50,47 +42,95 @@ const useDict = () => { }; }; - - -// ====================== 主组件 ====================== +// 主组件 const SerialNumGenerate = () => { const [form] = Form.useForm(); const [qrUrl, setQrUrl] = useState(''); - const [qrText, setQrText] = 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, - models, setModels, - configs, setConfigs, - countries, setCountries, - provinces, setProvinces, + types, + models, + configs, + countries, + provinces } = useDict(); - // 生成二维码 + // 生成 8 位随机序号 + 2 位校验码 + 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 timestamp = new Date().getTime(); - const qrContent = `${values.model}-${values.config}-${values.type}-${values.country}-${values.province}-${values.code}-${timestamp}`; + 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 qrContent = JSON.stringify(qrData, null, 2); + setQrJson(qrContent); + + // 3. 生成二维码 const url = await QRCode.toDataURL(qrContent); setQrUrl(url); - setQrText(qrContent); + message.success('生成成功!'); - } catch (e) { message.error('请完善信息'); } + } 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 (!qrUrl) return message.warning('先生成二维码'); + if (!serialNumber || !qrUrl) { + message.warning('请先生成二维码'); + return; + } setLoading(true); try { - const values = await form.validateFields(); - await axios.post('/api/save-qrcode', { ...values, qrCode: qrUrl, qrText }); + await axios.post('/api/save-qrcode', { + serialNumber, + qrCode: qrUrl, + qrJson: qrJson, + ...form.getFieldsValue() + }); message.success('保存成功'); } catch { message.error('保存失败'); @@ -99,30 +139,11 @@ const SerialNumGenerate = () => { } }; - // 查询 - const handleQueryQRCode = async () => { - if (!queryCode) return message.warning('请输入编号'); - setQueryLoading(true); - try { - const res = await axios.get('/api/query-qrcode', { params: { code: queryCode } }); - if (res.data?.qrCode) { - setQrUrl(res.data.qrCode); - setQrText(res.data.qrText || ''); - message.success('查询成功'); - } else message.warning('未找到'); - } catch { - message.error('查询失败'); - } finally { - setQueryLoading(false); - } - }; - return (