Files
web-Iot/src/components/SystemSetting/SerialNumGenerate.jsx

283 lines
8.4 KiB
React
Raw Normal View History

2026-05-13 13:31:49 +08:00
import React, { useState, useRef } from 'react';
import {
2026-05-14 08:45:14 +08:00
Form, Select, Button, Space, message, Card, Typography,
Row, Col, Modal, Input
} from 'antd';
import QRCode from 'qrcode';
import axios from 'axios';
2026-05-14 08:45:14 +08:00
import DictManager from "../DictManager";
const { Text } = Typography;
2026-05-13 13:31:49 +08:00
// 字典数据
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' },
]);
const [configs, setConfigs] = useState([
{ id: 1, value: 'AIR', label: 'AIR' },
{ id: 2, value: 'PRO', label: 'PRO' },
]);
const [countries, setCountries] = useState([
2026-05-13 13:31:49 +08:00
{ id: 1, value: 'CN', label: '中国' },
{ id: 2, value: 'US', label: '美国' },
]);
const [provinces, setProvinces] = useState([
2026-05-13 13:31:49 +08:00
{ id: 1, value: 'JS', label: '江苏' },
{ id: 2, value: 'ZJ', label: '浙江' },
{ id: 3, value: 'GD', label: '广东' },
]);
return {
types, setTypes,
models, setModels,
configs, setConfigs,
countries, setCountries,
provinces, setProvinces,
};
};
2026-05-13 13:31:49 +08:00
// 主组件
const SerialNumGenerate = () => {
const [form] = Form.useForm();
const [qrUrl, setQrUrl] = useState('');
2026-05-13 13:31:49 +08:00
const [serialNumber, setSerialNumber] = useState('');
const [qrJson, setQrJson] = useState('');
const [loading, setLoading] = useState(false);
const qrRef = useRef(null);
2026-05-13 13:31:49 +08:00
2026-05-14 08:45:14 +08:00
// 查询相关状态
const [queryCode, setQueryCode] = useState('');
const [queryLoading, setQueryLoading] = useState(false);
const [manageVisible, setManageVisible] = useState(false);
const {
2026-05-13 13:31:49 +08:00
types,
models,
configs,
countries,
provinces
} = useDict();
2026-05-13 13:31:49 +08:00
// 生成 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}`;
};
2026-05-14 08:45:14 +08:00
// 生成序列号 + JSON二维码
const generateQRCode = async () => {
try {
const values = await form.validateFields();
2026-05-13 13:31:49 +08:00
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);
2026-05-13 13:31:49 +08:00
message.success('生成成功!');
2026-05-13 13:31:49 +08:00
} 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();
};
2026-05-13 13:31:49 +08:00
// 保存到数据库
const saveToDatabase = async () => {
2026-05-13 13:31:49 +08:00
if (!serialNumber || !qrUrl) {
message.warning('请先生成二维码');
return;
}
setLoading(true);
try {
2026-05-13 13:31:49 +08:00
await axios.post('/api/save-qrcode', {
serialNumber,
qrCode: qrUrl,
qrJson: qrJson,
...form.getFieldsValue()
});
message.success('保存成功');
} catch {
message.error('保存失败');
} finally {
setLoading(false);
}
};
2026-05-14 08:45:14 +08:00
// 查询二维码
const handleQueryQRCode = async () => {
if (!queryCode) return message.warning('请输入序列号');
setQueryLoading(true);
try {
const res = await axios.get('/api/query-qrcode', { params: { sn: queryCode } });
if (res.data?.qrUrl) {
setQrUrl(res.data.qrUrl);
setQrJson(res.data.qrJson || '');
setSerialNumber(res.data.serialNumber || '');
message.success('查询成功');
} else {
message.warning('未找到该序列号');
}
} catch {
message.error('查询失败');
} finally {
setQueryLoading(false);
}
};
return (
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
<Card
title="机器人序列号二维码生成器"
2026-05-13 13:31:49 +08:00
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} initialValues={{ type: 'mower' }} labelCol={{ md: 5 }} wrapperCol={{ md: 19 }} size="large">
<Form.Item name="model" label="型号" rules={[{ required: true }]}>
<Select placeholder="选择型号">
{models.map(m => <Select.Option key={m.value} value={m.value}>{m.label}</Select.Option>)}
</Select>
</Form.Item>
<Form.Item name="config" label="配置" rules={[{ required: true }]}>
<Select placeholder="选择配置">
{configs.map(c => <Select.Option key={c.value} value={c.value}>{c.label}</Select.Option>)}
</Select>
</Form.Item>
<Form.Item name="type" label="类型" rules={[{ required: true }]}>
<Select placeholder="选择类型">
{types.map(t => <Select.Option key={t.value} value={t.value}>{t.label}</Select.Option>)}
</Select>
</Form.Item>
<Form.Item name="country" label="国家" rules={[{ required: true }]}>
<Select placeholder="选择国家">
{countries.map(c => <Select.Option key={c.value} value={c.value}>{c.label}</Select.Option>)}
</Select>
</Form.Item>
<Form.Item name="province" label="省份" rules={[{ required: true }]}>
<Select placeholder="选择省份">
{provinces.map(p => <Select.Option key={p.value} value={p.value}>{p.label}</Select.Option>)}
</Select>
</Form.Item>
<Form.Item wrapperCol={{ offset: 5 }}>
2026-05-13 13:31:49 +08:00
<Button type="primary" onClick={generateQRCode} block>
生成序列号 & 二维码
</Button>
</Form.Item>
</Form>
2026-05-14 08:45:14 +08:00
{/* 查询模块(已加回) */}
<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
}}>
2026-05-13 13:31:49 +08:00
{serialNumber && (
<div style={{
padding: 12, background: '#e6f7ff', borderRadius: 8,
width: '100%', textAlign: 'center', marginBottom: 20
}}>
<Text strong style={{ fontSize: 15 }}>
序列号:{serialNumber}
</Text>
</div>
)}
{qrUrl ? (
2026-05-13 13:31:49 +08:00
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240 }} />
) : (
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
)}
<Space style={{ marginTop: 24 }}>
2026-05-13 13:31:49 +08:00
<Button onClick={downloadQRCode}>下载二维码</Button>
2026-05-14 08:45:14 +08:00
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存</Button>
</Space>
</div>
</Col>
</Row>
</Card>
2026-05-13 13:31:49 +08:00
{/* 数据管理弹窗 */}
<Modal
open={manageVisible}
2026-05-13 13:31:49 +08:00
title="数据管理"
width={900}
onCancel={() => setManageVisible(false)}
2026-05-13 13:31:49 +08:00
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
>
2026-05-13 13:31:49 +08:00
<DictManager title="机器人类型" data={types} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
<DictManager title="机器人型号" data={models} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
<DictManager title="配置" data={configs} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
<DictManager title="国家" data={countries} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
2026-05-14 08:45:14 +08:00
<DictManager title="省份" data={provinces} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
</Modal>
</div>
);
};
export default SerialNumGenerate;