保存二维码
This commit is contained in:
@@ -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 (
|
||||
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
|
||||
{contextHolder}
|
||||
<Card
|
||||
title="机器人序列号二维码生成器"
|
||||
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }}
|
||||
@@ -292,8 +313,7 @@ const SerialNumGenerate = () => {
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
{/* 国家(级联) */}
|
||||
{/* 国家(级联 + 可清空) */}
|
||||
{/* 国家 显示中文 存简码 可清空 */}
|
||||
<Form.Item name="country" label="国家" rules={[{ required: true }]}>
|
||||
<Select
|
||||
placeholder="选择国家"
|
||||
@@ -313,7 +333,7 @@ const SerialNumGenerate = () => {
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
{/* 省份(级联 + 可清空 + 中国显示中文) */}
|
||||
{/* 省份 显示中文 存简码 可清空 */}
|
||||
<Form.Item name="province" label="省份" rules={[{ required: true }]}>
|
||||
<Select
|
||||
placeholder="请先选择国家"
|
||||
@@ -326,9 +346,8 @@ const SerialNumGenerate = () => {
|
||||
}
|
||||
>
|
||||
{provinces.map((p) => (
|
||||
<Select.Option key={p.isoCode} value={p.name}>
|
||||
{p.isoCode === 'CN' ? p.name : p.name}
|
||||
{/* 中国省份默认就是中文,这里直接显示 name 即可 */}
|
||||
<Select.Option key={p.isoCode} value={p.isoCode}>
|
||||
{p.name}
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
@@ -390,7 +409,6 @@ const SerialNumGenerate = () => {
|
||||
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
||||
destroyOnClose
|
||||
>
|
||||
{/* 机器人类型 */}
|
||||
<DictManager
|
||||
title="机器人类型"
|
||||
data={types}
|
||||
@@ -405,7 +423,6 @@ const SerialNumGenerate = () => {
|
||||
refresh={loadTypeList}
|
||||
/>
|
||||
|
||||
{/* 机器人型号 */}
|
||||
<DictManager
|
||||
title="机器人型号"
|
||||
data={models}
|
||||
@@ -419,7 +436,6 @@ const SerialNumGenerate = () => {
|
||||
refresh={loadModelList}
|
||||
/>
|
||||
|
||||
{/* 配置 */}
|
||||
<DictManager
|
||||
title="配置"
|
||||
data={configs}
|
||||
|
||||
Reference in New Issue
Block a user