机器人生成页面完成
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useRef, useEffect } from 'react';
|
import React, { useState, useRef } from 'react';
|
||||||
import {
|
import {
|
||||||
Form, Select, Input, Button, Space, message, Card, Typography,
|
Form, Select, Input, Button, Space, message, Card, Typography,
|
||||||
Row, Col, Modal, Table, Tag, Divider
|
Row, Col, Modal
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import QRCode from 'qrcode';
|
import QRCode from 'qrcode';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
@@ -9,36 +9,28 @@ import DictManager from "../DictManager"
|
|||||||
|
|
||||||
const { Text } = Typography;
|
const { Text } = Typography;
|
||||||
|
|
||||||
// ====================== 【可管理】字典数据 ======================
|
// 字典数据
|
||||||
const useDict = () => {
|
const useDict = () => {
|
||||||
// 类型
|
|
||||||
const [types, setTypes] = useState([
|
const [types, setTypes] = useState([
|
||||||
{ id: 1, value: 'mower', label: '割草机' },
|
{ id: 1, value: 'mower', label: '割草机' },
|
||||||
{ id: 2, value: 'vacuum', label: '巡检机' },
|
{ id: 2, value: 'vacuum', label: '巡检机' },
|
||||||
]);
|
]);
|
||||||
// 型号
|
|
||||||
const [models, setModels] = useState([
|
const [models, setModels] = useState([
|
||||||
{ id: 1, value: 'MC700', label: 'MC700' },
|
{ id: 1, value: 'MC700', label: 'MC700' },
|
||||||
{ id: 2, value: 'MC800', label: 'MC800' },
|
{ id: 2, value: 'MC800', label: 'MC800' },
|
||||||
{ id: 3, value: 'MC900', label: 'MC900' },
|
|
||||||
]);
|
]);
|
||||||
// 配置
|
|
||||||
const [configs, setConfigs] = useState([
|
const [configs, setConfigs] = useState([
|
||||||
{ id: 1, value: 'AIR', label: 'AIR' },
|
{ id: 1, value: 'AIR', label: 'AIR' },
|
||||||
{ id: 2, value: 'PRO', label: 'PRO' },
|
{ id: 2, value: 'PRO', label: 'PRO' },
|
||||||
{ id: 3, value: 'MAX', label: 'MAX' },
|
|
||||||
]);
|
]);
|
||||||
// 国家
|
|
||||||
const [countries, setCountries] = useState([
|
const [countries, setCountries] = useState([
|
||||||
{ id: 1, value: '中国', label: '中国' },
|
{ id: 1, value: 'CN', label: '中国' },
|
||||||
{ id: 2, value: '美国', label: '美国' },
|
{ id: 2, value: 'US', label: '美国' },
|
||||||
{ id: 3, value: '日本', label: '日本' },
|
|
||||||
]);
|
]);
|
||||||
// 省份
|
|
||||||
const [provinces, setProvinces] = useState([
|
const [provinces, setProvinces] = useState([
|
||||||
{ id: 1, value: '江苏', label: '江苏' },
|
{ id: 1, value: 'JS', label: '江苏' },
|
||||||
{ id: 2, value: '浙江', label: '浙江' },
|
{ id: 2, value: 'ZJ', label: '浙江' },
|
||||||
{ id: 3, value: '广东', label: '广东' },
|
{ id: 3, value: 'GD', label: '广东' },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -50,47 +42,95 @@ const useDict = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 主组件
|
||||||
|
|
||||||
// ====================== 主组件 ======================
|
|
||||||
const SerialNumGenerate = () => {
|
const SerialNumGenerate = () => {
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
const [qrUrl, setQrUrl] = useState('');
|
const [qrUrl, setQrUrl] = useState('');
|
||||||
const [qrText, setQrText] = useState('');
|
const [serialNumber, setSerialNumber] = useState('');
|
||||||
|
const [qrJson, setQrJson] = useState('');
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const qrRef = useRef(null);
|
const qrRef = useRef(null);
|
||||||
const [queryCode, setQueryCode] = useState('');
|
|
||||||
const [queryLoading, setQueryLoading] = useState(false);
|
|
||||||
const [manageVisible, setManageVisible] = useState(false);
|
const [manageVisible, setManageVisible] = useState(false);
|
||||||
|
|
||||||
|
// 全部解构出来
|
||||||
const {
|
const {
|
||||||
types, setTypes,
|
types,
|
||||||
models, setModels,
|
models,
|
||||||
configs, setConfigs,
|
configs,
|
||||||
countries, setCountries,
|
countries,
|
||||||
provinces, setProvinces,
|
provinces
|
||||||
} = useDict();
|
} = 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 () => {
|
const generateQRCode = async () => {
|
||||||
try {
|
try {
|
||||||
const values = await form.validateFields();
|
const values = await form.validateFields();
|
||||||
const timestamp = new Date().getTime();
|
const { model, config, type, country, province } = values;
|
||||||
const qrContent = `${values.model}-${values.config}-${values.type}-${values.country}-${values.province}-${values.code}-${timestamp}`;
|
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);
|
const url = await QRCode.toDataURL(qrContent);
|
||||||
setQrUrl(url);
|
setQrUrl(url);
|
||||||
setQrText(qrContent);
|
|
||||||
message.success('生成成功!');
|
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 () => {
|
const saveToDatabase = async () => {
|
||||||
if (!qrUrl) return message.warning('先生成二维码');
|
if (!serialNumber || !qrUrl) {
|
||||||
|
message.warning('请先生成二维码');
|
||||||
|
return;
|
||||||
|
}
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const values = await form.validateFields();
|
await axios.post('/api/save-qrcode', {
|
||||||
await axios.post('/api/save-qrcode', { ...values, qrCode: qrUrl, qrText });
|
serialNumber,
|
||||||
|
qrCode: qrUrl,
|
||||||
|
qrJson: qrJson,
|
||||||
|
...form.getFieldsValue()
|
||||||
|
});
|
||||||
message.success('保存成功');
|
message.success('保存成功');
|
||||||
} catch {
|
} catch {
|
||||||
message.error('保存失败');
|
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 (
|
return (
|
||||||
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
|
<div style={{ padding: '0 20px', minHeight: '100vh' }}>
|
||||||
<Card
|
<Card
|
||||||
title="机器人序列号二维码生成器"
|
title="机器人序列号二维码生成器"
|
||||||
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12, boxShadow: '0 4px 20px rgba(0,0,0,0.08)' }}
|
style={{ maxWidth: 1000, margin: '0 auto', borderRadius: 12 }}
|
||||||
headStyle={{ fontSize: 20, fontWeight: 600, textAlign: 'center' }}
|
|
||||||
extra={<Button type="primary" onClick={() => setManageVisible(true)}>数据管理</Button>}
|
extra={<Button type="primary" onClick={() => setManageVisible(true)}>数据管理</Button>}
|
||||||
>
|
>
|
||||||
<Row gutter={[32, 24]}>
|
<Row gutter={[32, 24]}>
|
||||||
@@ -159,22 +180,12 @@ const SerialNumGenerate = () => {
|
|||||||
</Select>
|
</Select>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item name="code" label="编号" rules={[{ required: true }]}>
|
|
||||||
<Input placeholder="唯一编号" />
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<Form.Item wrapperCol={{ offset: 5 }}>
|
<Form.Item wrapperCol={{ offset: 5 }}>
|
||||||
<Button type="primary" onClick={generateQRCode} block>生成二维码</Button>
|
<Button type="primary" onClick={generateQRCode} block>
|
||||||
|
生成序列号 & 二维码
|
||||||
|
</Button>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Form>
|
</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>
|
</Col>
|
||||||
|
|
||||||
{/* 右侧二维码 */}
|
{/* 右侧二维码 */}
|
||||||
@@ -183,20 +194,25 @@ const SerialNumGenerate = () => {
|
|||||||
padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9',
|
padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9',
|
||||||
display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 480
|
display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 480
|
||||||
}}>
|
}}>
|
||||||
{qrText && (
|
{serialNumber && (
|
||||||
<div style={{ padding: 12, background: '#e6f7ff', borderRadius: 8, width: '100%', textAlign: 'center', marginBottom: 20 }}>
|
<div style={{
|
||||||
<Text strong style={{ wordBreak: 'break-all' }}>{qrText}</Text>
|
padding: 12, background: '#e6f7ff', borderRadius: 8,
|
||||||
|
width: '100%', textAlign: 'center', marginBottom: 20
|
||||||
|
}}>
|
||||||
|
<Text strong style={{ fontSize: 15 }}>
|
||||||
|
序列号:{serialNumber}
|
||||||
|
</Text>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{qrUrl ? (
|
{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>
|
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Space style={{ marginTop: 24 }}>
|
<Space style={{ marginTop: 24 }}>
|
||||||
<Button onClick={() => window.open(qrRef.current.src)}>下载</Button>
|
<Button onClick={downloadQRCode}>下载二维码</Button>
|
||||||
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存到数据库</Button>
|
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存到数据库</Button>
|
||||||
</Space>
|
</Space>
|
||||||
</div>
|
</div>
|
||||||
@@ -204,51 +220,18 @@ const SerialNumGenerate = () => {
|
|||||||
</Row>
|
</Row>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* ====================== 数据管理弹窗 ====================== */}
|
{/* 数据管理弹窗 */}
|
||||||
<Modal
|
<Modal
|
||||||
open={manageVisible}
|
open={manageVisible}
|
||||||
title="机器人数据管理(型号/类型/配置/国家/省份)"
|
title="数据管理"
|
||||||
width={900}
|
width={900}
|
||||||
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
|
||||||
destroyOnClose
|
|
||||||
onCancel={() => setManageVisible(false)}
|
onCancel={() => setManageVisible(false)}
|
||||||
|
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
||||||
>
|
>
|
||||||
<DictManager
|
<DictManager title="机器人类型" data={types} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||||
title="机器人类型"
|
<DictManager title="机器人型号" data={models} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||||
data={types}
|
<DictManager title="配置" data={configs} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||||
onAdd={(item) => setTypes([...types, { id: Date.now(), ...item }])}
|
<DictManager title="国家" data={countries} onAdd={() => { }} onUpdate={() => { }} onDelete={() => { }} />
|
||||||
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))}
|
|
||||||
/>
|
|
||||||
</Modal>
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user