保存二维码

This commit is contained in:
mmc
2026-05-15 10:53:53 +08:00
parent d99ea5db2e
commit 8673baa57e
2 changed files with 75 additions and 49 deletions

View File

@@ -260,3 +260,13 @@ export function deleteRobotConfigApi(ids) {
method: 'delete' method: 'delete'
}); });
} }
//b保存二维码/
export function saveCodeApi(data) {
return request({
url: `/system/qrcode/generate`,
method: 'post',
data
});
}

View File

@@ -22,16 +22,19 @@ import {
addRobotConfigApi, addRobotConfigApi,
updateRobotConfigApi, updateRobotConfigApi,
deleteRobotConfigApi, deleteRobotConfigApi,
saveCodeApi
} from '../../api/systemSetting'; } from '../../api/systemSetting';
const { Text } = Typography; const { Text } = Typography;
const { Option } = Select; const { Option } = Select;
const SerialNumGenerate = () => { const SerialNumGenerate = () => {
const [messageApi, contextHolder] = message.useMessage();
// 表单实例 // 表单实例
const [form] = Form.useForm(); const [form] = Form.useForm();
// 国家/省份 数据(本地库获取) // 国家/省份 数据
const [allCountries, setAllCountries] = useState([]); const [allCountries, setAllCountries] = useState([]);
const [provinces, setProvinces] = useState([]); const [provinces, setProvinces] = useState([]);
@@ -49,17 +52,24 @@ const SerialNumGenerate = () => {
// 数据管理弹窗 // 数据管理弹窗
const [manageVisible, setManageVisible] = useState(false); const [manageVisible, setManageVisible] = useState(false);
// 接口数据(类型、型号、配置) // 接口数据
const [types, setTypes] = useState([]); const [types, setTypes] = useState([]);
const [models, setModels] = useState([]); const [models, setModels] = useState([]);
const [configs, setConfigs] = useState([]); const [configs, setConfigs] = useState([]);
// 初始化:加载国家列表 // 存储完整中文名称映射
const countryNameMap = useRef({});
const provinceNameMap = useRef({});
// 初始化
useEffect(() => { useEffect(() => {
const countries = Country.getAllCountries(); const countries = Country.getAllCountries();
setAllCountries(countries || []); setAllCountries(countries || []);
// 构建 简码=>中文名称 映射
countries.forEach(item => {
countryNameMap.current[item.isoCode] = item.name;
})
// 加载其他数据
loadTypeList(); loadTypeList();
loadModelList(); loadModelList();
loadConfigList(); loadConfigList();
@@ -69,11 +79,13 @@ const SerialNumGenerate = () => {
const handleCountryChange = (countryCode) => { const handleCountryChange = (countryCode) => {
const states = State.getStatesOfCountry(countryCode); const states = State.getStatesOfCountry(countryCode);
setProvinces(states || []); setProvinces(states || []);
// 构建省份简码=>中文名称映射
// 切换国家时自动清空省份 states.forEach(item => {
provinceNameMap.current[item.isoCode] = item.name;
})
form.setFieldsValue({ province: undefined }); form.setFieldsValue({ province: undefined });
}; };
// ==================== 接口加载 ====================
// 加载机器人类型 // 加载机器人类型
const loadTypeList = async () => { const loadTypeList = async () => {
try { try {
@@ -98,7 +110,7 @@ const SerialNumGenerate = () => {
} catch (e) { } } catch (e) { }
}; };
// 生成序号 // 生成随机后缀
const generateSerialSuffix = () => { const generateSerialSuffix = () => {
const num = Math.floor(Math.random() * 100000000).toString(16).toUpperCase().padStart(8, '0'); 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'); 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 { model, config, type, country, province } = values;
const ts = Date.now(); const ts = Date.now();
const suffix = generateSerialSuffix(); 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 qrContent = JSON.stringify(qrData, null, 2);
const url = await QRCode.toDataURL(qrContent); const url = await QRCode.toDataURL(qrContent);
setSerialNumber(sn); setSerialNumber(serialNumber);
setQrJson(qrContent); setQrJson(qrContent);
setQrUrl(url); setQrUrl(url);
message.success('生成成功!');
} catch (err) { } catch (err) {
message.error('请完善信息');
} }
}; };
// 下载二维码 // 下载二维码
const downloadQRCode = () => { const downloadQRCode = () => {
if (!qrRef.current || !qrUrl) { if (!qrRef.current || !qrUrl) {
message.warning('请先生成二维码'); messageApi.warning('请先生成二维码');
return; return;
} }
const link = document.createElement('a'); const link = document.createElement('a');
@@ -141,26 +163,28 @@ const SerialNumGenerate = () => {
// 保存到数据库 // 保存到数据库
const saveToDatabase = async () => { const saveToDatabase = async () => {
if (!serialNumber) return message.warning('请先生成'); if (!serialNumber) return messageApi.warning('请先生成');
setLoading(true); 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 { try {
await request({ const res = await saveCodeApi(data);
url: '/iot/device/saveSerial', if (res?.code == 200) {
method: 'post', messageApi.success('保存成功');
data: { } else {
serialNumber, messageApi.error('保存失败' + res?.msg);
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) { } catch (e) {
message.error('保存失败'); messageApi.error('保存失败');
} finally { } finally {
setLoading(false); setLoading(false);
} }
@@ -168,13 +192,13 @@ const SerialNumGenerate = () => {
// 查询序列号 // 查询序列号
const handleQueryQRCode = async () => { const handleQueryQRCode = async () => {
if (!queryCode) return message.warning('请输入序列号'); if (!queryCode) return messageApi.warning('请输入序列号');
setQueryLoading(true); setQueryLoading(true);
try { try {
const res = await request({ const res = await request({
url: '/iot/device/querySerial', url: '/iot/device/querySerial',
method: 'get', method: 'get',
params: sn.queryCode params: { sn: queryCode }
}); });
if (res.data) { if (res.data) {
setSerialNumber(res.data.serialNumber); setSerialNumber(res.data.serialNumber);
@@ -187,18 +211,17 @@ const SerialNumGenerate = () => {
country: res.data.country, country: res.data.country,
province: res.data.province province: res.data.province
}); });
message.success('查询成功'); messageApi.success('查询成功');
} else { } else {
message.warning('未查询到数据'); messageApi.warning('未查询到数据');
} }
} catch (e) { } catch (e) {
message.error('查询失败'); messageApi.error('查询失败');
} finally { } finally {
setQueryLoading(false); setQueryLoading(false);
} }
}; };
// ==================== 数据处理方法 ====================
// 类型 // 类型
const handleAddType = async (values) => { const handleAddType = async (values) => {
return await addRobotTypeApi({ return await addRobotTypeApi({
@@ -222,7 +245,6 @@ const SerialNumGenerate = () => {
modelName: values.displayName, modelName: values.displayName,
storeValue: values.storeValue, storeValue: values.storeValue,
displayName: values.displayName displayName: values.displayName
}); });
}; };
const handleUpdateModel = async (values) => { const handleUpdateModel = async (values) => {
@@ -231,7 +253,6 @@ const SerialNumGenerate = () => {
modelName: values.displayName, modelName: values.displayName,
storeValue: values.storeValue, storeValue: values.storeValue,
displayName: values.displayName displayName: values.displayName
}); });
}; };
@@ -241,7 +262,6 @@ const SerialNumGenerate = () => {
configName: values.displayName, configName: values.displayName,
storeValue: values.storeValue, storeValue: values.storeValue,
displayName: values.displayName displayName: values.displayName
}); });
}; };
const handleUpdateConfig = async (values) => { const handleUpdateConfig = async (values) => {
@@ -255,6 +275,7 @@ const SerialNumGenerate = () => {
return ( return (
<div style={{ padding: '0 20px', minHeight: '100vh' }}> <div style={{ padding: '0 20px', minHeight: '100vh' }}>
{contextHolder}
<Card <Card
title="机器人序列号二维码生成器" title="机器人序列号二维码生成器"
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }} style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }}
@@ -292,8 +313,7 @@ const SerialNumGenerate = () => {
</Select> </Select>
</Form.Item> </Form.Item>
{/* 国家(级联) */} {/* 国家 显示中文 存简码 可清空 */}
{/* 国家(级联 + 可清空) */}
<Form.Item name="country" label="国家" rules={[{ required: true }]}> <Form.Item name="country" label="国家" rules={[{ required: true }]}>
<Select <Select
placeholder="选择国家" placeholder="选择国家"
@@ -313,7 +333,7 @@ const SerialNumGenerate = () => {
</Select> </Select>
</Form.Item> </Form.Item>
{/* 省份(级联 + 可清空 + 中国显示中文) */} {/* 省份 显示中文 存简码 可清空 */}
<Form.Item name="province" label="省份" rules={[{ required: true }]}> <Form.Item name="province" label="省份" rules={[{ required: true }]}>
<Select <Select
placeholder="请先选择国家" placeholder="请先选择国家"
@@ -326,9 +346,8 @@ const SerialNumGenerate = () => {
} }
> >
{provinces.map((p) => ( {provinces.map((p) => (
<Select.Option key={p.isoCode} value={p.name}> <Select.Option key={p.isoCode} value={p.isoCode}>
{p.isoCode === 'CN' ? p.name : p.name} {p.name}
{/* 中国省份默认就是中文,这里直接显示 name 即可 */}
</Select.Option> </Select.Option>
))} ))}
</Select> </Select>
@@ -390,7 +409,6 @@ const SerialNumGenerate = () => {
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>} footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
destroyOnClose destroyOnClose
> >
{/* 机器人类型 */}
<DictManager <DictManager
title="机器人类型" title="机器人类型"
data={types} data={types}
@@ -405,7 +423,6 @@ const SerialNumGenerate = () => {
refresh={loadTypeList} refresh={loadTypeList}
/> />
{/* 机器人型号 */}
<DictManager <DictManager
title="机器人型号" title="机器人型号"
data={models} data={models}
@@ -419,7 +436,6 @@ const SerialNumGenerate = () => {
refresh={loadModelList} refresh={loadModelList}
/> />
{/* 配置 */}
<DictManager <DictManager
title="配置" title="配置"
data={configs} data={configs}