机器人生成页面完成

This commit is contained in:
mmc
2026-05-13 13:31:49 +08:00
parent 07e6488811
commit 13bfbca03d

View File

@@ -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 (
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
<Card
title="机器人序列号二维码生成器"
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12, boxShadow: '0 4px 20px rgba(0,0,0,0.08)' }}
headStyle={{ fontSize: 20, fontWeight: 600, textAlign: 'center' }}
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }}
extra={<Button type="primary" onClick={() => setManageVisible(true)}>数据管理</Button>}
>
<Row gutter={[32, 24]}>
@@ -159,22 +180,12 @@ const SerialNumGenerate = () => {
</Select>
</Form.Item>
<Form.Item name="code" label="编号" rules={[{ required: true }]}>
<Input placeholder="唯一编号" />
</Form.Item>
<Form.Item wrapperCol={{ offset: 5 }}>
<Button type="primary" onClick={generateQRCode} block>生成二维码</Button>
<Button type="primary" onClick={generateQRCode} block>
生成序列号 & 二维码
</Button>
</Form.Item>
</Form>
{/* 查询 */}
<Card size="small" title="查询历史二维码" style={{ marginTop: 20 }}>
<Space.Compact style={{ width: '100%' }}>
<Input value={queryCode} onChange={e => setQueryCode(e.target.value)} placeholder="输入编号" />
<Button type="primary" loading={queryLoading} onClick={handleQueryQRCode}>查询</Button>
</Space.Compact>
</Card>
</Col>
{/* 右侧二维码 */}
@@ -183,20 +194,25 @@ const SerialNumGenerate = () => {
padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9',
display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 480
}}>
{qrText && (
<div style={{ padding: 12, background: '#e6f7ff', borderRadius: 8, width: '100%', textAlign: 'center', marginBottom: 20 }}>
<Text strong style={{ wordBreak: 'break-all' }}>{qrText}</Text>
{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, borderRadius: 8 }} />
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240 }} />
) : (
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
)}
<Space style={{ marginTop: 24 }}>
<Button onClick={() => window.open(qrRef.current.src)}>下载</Button>
<Button onClick={downloadQRCode}>下载二维码</Button>
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存到数据库</Button>
</Space>
</div>
@@ -204,51 +220,18 @@ const SerialNumGenerate = () => {
</Row>
</Card>
{/* ====================== 数据管理弹窗 ====================== */}
{/* 数据管理弹窗 */}
<Modal
open={manageVisible}
title="机器人数据管理(型号/类型/配置/国家/省份)"
title="数据管理"
width={900}
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
destroyOnClose
onCancel={() => setManageVisible(false)}
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
>
<DictManager
title="机器人类型"
data={types}
onAdd={(item) => setTypes([...types, { id: Date.now(), ...item }])}
onUpdate={(item) => setTypes(types.map(t => t.id === item.id ? item : t))}
onDelete={(id) => setTypes(types.filter(t => t.id !== id))}
/>
<DictManager
title="机器人型号"
data={models}
onAdd={(item) => setModels([...models, { id: Date.now(), ...item }])}
onUpdate={(item) => setModels(models.map(m => m.id === item.id ? item : m))}
onDelete={(id) => setModels(models.filter(m => m.id !== id))}
/>
<DictManager
title="配置"
data={configs}
onAdd={(item) => setConfigs([...configs, { id: Date.now(), ...item }])}
onUpdate={(item) => setConfigs(configs.map(c => c.id === item.id ? item : c))}
onDelete={(id) => setConfigs(configs.filter(c => c.id !== id))}
/>
<DictManager
title="国家"
data={countries}
onAdd={(item) => setCountries([...countries, { id: Date.now(), ...item }])}
onUpdate={(item) => setCountries(countries.map(c => c.id === item.id ? item : c))}
onDelete={(id) => setCountries(countries.filter(c => c.id !== id))}
/>
<DictManager
title="省份"
data={provinces}
onAdd={(item) => setProvinces([...provinces, { id: Date.now(), ...item }])}
onUpdate={(item) => setProvinces(provinces.map(p => p.id === item.id ? item : p))}
onDelete={(id) => setProvinces(provinces.filter(p => p.id !== id))}
/>
<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={() => { }} />
</Modal>
</div>
);