472 lines
13 KiB
JavaScript
472 lines
13 KiB
JavaScript
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,
|
||
getRobotModelPageApi,
|
||
addRobotModelApi,
|
||
updateRobotModelApi,
|
||
deleteRobotModelApi,
|
||
getRobotConfigListApi,
|
||
addRobotConfigApi,
|
||
updateRobotConfigApi,
|
||
deleteRobotConfigApi,
|
||
saveCodeApi,
|
||
getCodeImgApi
|
||
} 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([]);
|
||
|
||
// 二维码相关
|
||
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 [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();
|
||
}, []);
|
||
|
||
// 国家改变 → 刷新省份
|
||
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 {
|
||
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 loadConfigList = async () => {
|
||
try {
|
||
const res = await getRobotConfigListApi({});
|
||
if (res?.code === 200) setConfigs(res.data || []);
|
||
} 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');
|
||
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 parts = [];
|
||
if (model) parts.push(model + config);
|
||
//if (config) parts.push(config);
|
||
if (country) parts.push(country);
|
||
if (province) parts.push(province);
|
||
parts.push(ts);
|
||
parts.push(suffix);
|
||
|
||
// 用 - 连接所有存在的部分
|
||
const serialNumber = parts.join('-');
|
||
// 二维码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(serialNumber);
|
||
setQrJson(qrContent);
|
||
setQrUrl(url);
|
||
} catch (err) {
|
||
}
|
||
};
|
||
|
||
// 下载二维码
|
||
const downloadQRCode = () => {
|
||
if (!qrRef.current || !qrUrl) {
|
||
messageApi.warning('请先生成二维码');
|
||
return;
|
||
}
|
||
const link = document.createElement('a');
|
||
link.download = `${serialNumber || 'qrcode'}.png`;
|
||
link.href = qrUrl;
|
||
link.click();
|
||
};
|
||
|
||
// 保存到数据库
|
||
const saveToDatabase = async () => {
|
||
if (!serialNumber) return messageApi.warning('请先生成');
|
||
try {
|
||
setLoading(true);
|
||
const values = form.getFieldsValue();
|
||
|
||
const model = models.find(m => m.displayName === values.model);
|
||
const config = configs.find(c => c.displayName === values.config);
|
||
const type = types.find(t => t.displayName === values.type);
|
||
|
||
const data = {
|
||
serialNumber,
|
||
modelId: model?.id,
|
||
configId: config?.id,
|
||
typeId: type?.id,
|
||
//countryId: values.country,
|
||
//provinceId: values.province,
|
||
qrCodeUrl: qrUrl,
|
||
qrJson: qrJson
|
||
};
|
||
|
||
const res = await saveCodeApi(data);
|
||
if (res?.code == 200) {
|
||
messageApi.success('保存成功');
|
||
setLoading(false);
|
||
|
||
} else {
|
||
messageApi.error('保存失败' + res?.msg);
|
||
|
||
}
|
||
} catch (e) {
|
||
setLoading(false);
|
||
|
||
messageApi.error('保存失败');
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
// 查询序列号
|
||
const handleQueryQRCode = async () => {
|
||
if (!queryCode) return messageApi.warning('请输入序列号');
|
||
setQueryLoading(true);
|
||
try {
|
||
const res = await getCodeImgApi(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
|
||
});
|
||
messageApi.success('查询成功');
|
||
} else {
|
||
messageApi.warning('未查询到数据');
|
||
}
|
||
} catch (e) {
|
||
messageApi.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.displayName,
|
||
storeValue: values.storeValue,
|
||
displayName: values.displayName
|
||
});
|
||
};
|
||
const handleUpdateModel = async (values) => {
|
||
return await updateRobotModelApi({
|
||
id: values.id,
|
||
modelName: values.displayName,
|
||
storeValue: values.storeValue,
|
||
displayName: values.displayName
|
||
});
|
||
};
|
||
|
||
// 配置
|
||
const handleAddConfig = async (values) => {
|
||
return await addRobotConfigApi({
|
||
configName: values.displayName,
|
||
storeValue: values.storeValue,
|
||
displayName: values.displayName
|
||
});
|
||
};
|
||
const handleUpdateConfig = async (values) => {
|
||
return await updateRobotConfigApi({
|
||
id: values.id,
|
||
configName: values.displayName,
|
||
storeValue: values.storeValue,
|
||
displayName: values.displayName
|
||
});
|
||
};
|
||
|
||
return (
|
||
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
|
||
{contextHolder}
|
||
<Card
|
||
title="机器人序列号二维码生成器"
|
||
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }}
|
||
extra={<Button type="primary" onClick={() => setManageVisible(true)}>数据管理</Button>}
|
||
>
|
||
<Row gutter={[32, 24]}>
|
||
<Col xs={24} md={14}>
|
||
<Form form={form} labelCol={{ md: 5 }} wrapperCol={{ md: 19 }} size="large">
|
||
{/* 型号 */}
|
||
<Form.Item name="model" label="型号" rules={[{ required: true }]}>
|
||
<Select placeholder="选择型号" showSearch optionFilterProp="children" filterOption={(input, option) =>
|
||
option.children.toLowerCase().includes(input.toLowerCase())
|
||
}>
|
||
{models.map(m => (
|
||
<Option key={m.id} value={m.displayName}>{m.modelName}</Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
{/* 配置 */}
|
||
<Form.Item name="config" label="配置" rules={[{ required: true }]}>
|
||
<Select placeholder="选择配置" showSearch optionFilterProp="children">
|
||
{configs.map(c => (
|
||
<Option key={c.id} value={c.displayName}>{c.configName}</Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
{/* 类型 */}
|
||
<Form.Item name="type" label="类型" rules={[{ required: true }]}>
|
||
<Select placeholder="选择类型" showSearch optionFilterProp="children">
|
||
{types.map(t => (
|
||
<Option key={t.id} value={t.displayName}>{t.storeValue}</Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
{/* 国家 显示中文 存简码 可清空 */}
|
||
<Form.Item name="country" label="国家" >
|
||
<Select
|
||
placeholder="选择国家"
|
||
showSearch
|
||
allowClear
|
||
optionFilterProp="children"
|
||
onChange={handleCountryChange}
|
||
filterOption={(input, option) =>
|
||
option.children.toLowerCase().includes(input.toLowerCase())
|
||
}
|
||
>
|
||
{allCountries.map((c) => (
|
||
<Select.Option key={c.isoCode} value={c.isoCode}>
|
||
{c.name}
|
||
</Select.Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
{/* 省份 显示中文 存简码 可清空 */}
|
||
<Form.Item name="province" label="省份" >
|
||
<Select
|
||
placeholder="请先选择国家"
|
||
showSearch
|
||
allowClear
|
||
disabled={provinces.length === 0}
|
||
optionFilterProp="children"
|
||
filterOption={(input, option) =>
|
||
option.children.toLowerCase().includes(input.toLowerCase())
|
||
}
|
||
>
|
||
{provinces.map((p) => (
|
||
<Select.Option key={p.isoCode} value={p.isoCode}>
|
||
{p.name}
|
||
</Select.Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
<Form.Item wrapperCol={{ offset: 5 }}>
|
||
<Button type="primary" onClick={generateQRCode} block>
|
||
生成序列号 & 二维码
|
||
</Button>
|
||
</Form.Item>
|
||
</Form>
|
||
|
||
<Card size="small" title="查询历史二维码" style={{ marginTop: 20 }}>
|
||
<Space style={{ width: '100%' }}>
|
||
<Input
|
||
value={queryCode}
|
||
onChange={(e) => setQueryCode(e.target.value)}
|
||
placeholder="输入序列号查询"
|
||
style={{ width: '300px' }}
|
||
/>
|
||
<Button type="primary" loading={queryLoading} onClick={handleQueryQRCode}>
|
||
查询
|
||
</Button>
|
||
</Space>
|
||
</Card>
|
||
</Col>
|
||
|
||
<Col xs={24} md={10}>
|
||
<div style={{
|
||
padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9',
|
||
display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 480
|
||
}}>
|
||
{serialNumber && (
|
||
<div style={{ padding: 12, background: '#e6f7ff', borderRadius: 8, width: '100%', textAlign: 'center', marginBottom: 20 }}>
|
||
<Text strong style={{ fontSize: 15 }}>序列号:{serialNumber}</Text>
|
||
</div>
|
||
)}
|
||
|
||
{qrUrl ? (
|
||
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240 }} alt="qrcode" />
|
||
) : (
|
||
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
|
||
)}
|
||
|
||
<Space style={{ marginTop: 24 }}>
|
||
<Button onClick={downloadQRCode}>下载二维码</Button>
|
||
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存</Button>
|
||
</Space>
|
||
</div>
|
||
</Col>
|
||
</Row>
|
||
</Card>
|
||
|
||
<Modal
|
||
open={manageVisible}
|
||
title="数据管理"
|
||
width={900}
|
||
onCancel={() => setManageVisible(false)}
|
||
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
||
destroyOnClose
|
||
>
|
||
<DictManager
|
||
title="机器人类型"
|
||
data={types}
|
||
fields={[
|
||
{ name: 'storeValue', label: '类型名称', required: true },
|
||
{ name: 'displayName', label: '存储值', required: true },
|
||
{ name: 'remark', label: '备注', required: false }
|
||
]}
|
||
onAdd={handleAddType}
|
||
onUpdate={handleUpdateType}
|
||
onDelete={deleteRobotTypeApi}
|
||
refresh={loadTypeList}
|
||
/>
|
||
|
||
<DictManager
|
||
title="机器人型号"
|
||
data={models}
|
||
fields={[
|
||
{ name: 'displayName', label: '型号名称', required: true },
|
||
{ name: 'storeValue', label: '存储值', required: true },
|
||
]}
|
||
onAdd={handleAddModel}
|
||
onUpdate={handleUpdateModel}
|
||
onDelete={deleteRobotModelApi}
|
||
refresh={loadModelList}
|
||
/>
|
||
|
||
<DictManager
|
||
title="配置"
|
||
data={configs}
|
||
fields={[
|
||
{ name: 'displayName', label: '配置名称', required: true },
|
||
{ name: 'storeValue', label: '存储值', required: true },
|
||
]}
|
||
onAdd={handleAddConfig}
|
||
onUpdate={handleUpdateConfig}
|
||
onDelete={deleteRobotConfigApi}
|
||
refresh={loadConfigList}
|
||
/>
|
||
</Modal>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default SerialNumGenerate;
|