258 lines
8.8 KiB
JavaScript
258 lines
8.8 KiB
JavaScript
import React, { useState, useRef, useEffect } from 'react';
|
|
import {
|
|
Form, Select, Input, Button, Space, message, Card, Typography,
|
|
Row, Col, Modal, Table, Tag, Divider
|
|
} from 'antd';
|
|
import QRCode from 'qrcode';
|
|
import axios from 'axios';
|
|
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: '日本' },
|
|
]);
|
|
// 省份
|
|
const [provinces, setProvinces] = useState([
|
|
{ id: 1, value: '江苏', label: '江苏' },
|
|
{ id: 2, value: '浙江', label: '浙江' },
|
|
{ id: 3, value: '广东', label: '广东' },
|
|
]);
|
|
|
|
return {
|
|
types, setTypes,
|
|
models, setModels,
|
|
configs, setConfigs,
|
|
countries, setCountries,
|
|
provinces, setProvinces,
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// ====================== 主组件 ======================
|
|
const SerialNumGenerate = () => {
|
|
const [form] = Form.useForm();
|
|
const [qrUrl, setQrUrl] = useState('');
|
|
const [qrText, setQrText] = 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,
|
|
} = useDict();
|
|
|
|
// 生成二维码
|
|
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 url = await QRCode.toDataURL(qrContent);
|
|
setQrUrl(url);
|
|
setQrText(qrContent);
|
|
message.success('生成成功!');
|
|
} catch (e) { message.error('请完善信息'); }
|
|
};
|
|
|
|
// 保存
|
|
const saveToDatabase = async () => {
|
|
if (!qrUrl) return message.warning('先生成二维码');
|
|
setLoading(true);
|
|
try {
|
|
const values = await form.validateFields();
|
|
await axios.post('/api/save-qrcode', { ...values, qrCode: qrUrl, qrText });
|
|
message.success('保存成功');
|
|
} catch {
|
|
message.error('保存失败');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
// 查询
|
|
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' }}
|
|
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 name="code" label="编号" rules={[{ required: true }]}>
|
|
<Input placeholder="唯一编号" />
|
|
</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.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 xs={24} md={10}>
|
|
<div style={{
|
|
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>
|
|
</div>
|
|
)}
|
|
|
|
{qrUrl ? (
|
|
<img ref={qrRef} src={qrUrl} style={{ width: 240, height: 240, borderRadius: 8 }} />
|
|
) : (
|
|
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
|
|
)}
|
|
|
|
<Space style={{ marginTop: 24 }}>
|
|
<Button onClick={() => window.open(qrRef.current.src)}>下载</Button>
|
|
<Button type="primary" loading={loading} onClick={saveToDatabase}>保存到数据库</Button>
|
|
</Space>
|
|
</div>
|
|
</Col>
|
|
</Row>
|
|
</Card>
|
|
|
|
{/* ====================== 数据管理弹窗 ====================== */}
|
|
<Modal
|
|
open={manageVisible}
|
|
title="机器人数据管理(型号/类型/配置/国家/省份)"
|
|
width={900}
|
|
footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>}
|
|
destroyOnClose
|
|
onCancel={() => setManageVisible(false)}
|
|
|
|
>
|
|
<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))}
|
|
/>
|
|
</Modal>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SerialNumGenerate;
|