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

518 lines
16 KiB
React
Raw Normal View History

2026-05-15 09:37:22 +08:00
import React, { useState, useRef, useEffect } from 'react';
import {
2026-05-14 08:45:14 +08:00
Form, Select, Button, Space, message, Card, Typography,
2026-05-15 13:28:19 +08:00
Row, Col, Modal, Input, Table, Popconfirm, Checkbox
} from 'antd';
import QRCode from 'qrcode';
2026-05-14 08:45:14 +08:00
import DictManager from "../DictManager";
2026-05-15 10:16:36 +08:00
import request from '../../api/request';
import { Country, State } from 'country-state-city';
2026-05-15 09:37:22 +08:00
// 接口导入
import {
getRobotTypeListApi,
addRobotTypeApi,
updateRobotTypeApi,
deleteRobotTypeApi,
getRobotModelPageApi,
2026-05-15 10:03:33 +08:00
addRobotModelApi,
updateRobotModelApi,
deleteRobotModelApi,
getRobotConfigListApi,
addRobotConfigApi,
updateRobotConfigApi,
deleteRobotConfigApi,
2026-05-15 12:59:51 +08:00
saveCodeApi,
2026-05-15 13:09:28 +08:00
getCodeImgApi,
getCodeListApi,
getCodeListPageApi,
getCodeDeatil,
delteCodesApi
2026-05-15 09:37:22 +08:00
} from '../../api/systemSetting';
2026-05-15 09:37:22 +08:00
const { Text } = Typography;
2026-05-15 10:16:36 +08:00
const { Option } = Select;
const SerialNumGenerate = () => {
2026-05-15 10:53:53 +08:00
const [messageApi, contextHolder] = message.useMessage();
const [form] = Form.useForm();
2026-05-15 13:09:28 +08:00
const [searchForm] = Form.useForm();
2026-05-15 10:16:36 +08:00
2026-05-15 13:09:28 +08:00
// 国家/省份
2026-05-15 10:16:36 +08:00
const [allCountries, setAllCountries] = useState([]);
const [provinces, setProvinces] = useState([]);
2026-05-15 13:09:28 +08:00
// 二维码生成
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);
2026-05-13 13:31:49 +08:00
2026-05-15 13:09:28 +08:00
// 查询
2026-05-14 08:45:14 +08:00
const [queryLoading, setQueryLoading] = useState(false);
2026-05-15 10:16:36 +08:00
2026-05-15 13:28:19 +08:00
// 弹窗
const [manageVisible, setManageVisible] = useState(false);
2026-05-15 13:09:28 +08:00
const [addModalVisible, setAddModalVisible] = useState(false);
2026-05-15 13:09:28 +08:00
// 业务数据
2026-05-15 09:37:22 +08:00
const [types, setTypes] = useState([]);
const [models, setModels] = useState([]);
2026-05-15 10:03:33 +08:00
const [configs, setConfigs] = useState([]);
2026-05-15 09:37:22 +08:00
2026-05-15 13:28:19 +08:00
// 二维码列表
2026-05-15 13:09:28 +08:00
const [codeList, setCodeList] = useState([]);
const [codeLoading, setCodeLoading] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [pageSize] = useState(10);
const [total, setTotal] = useState(0);
2026-05-15 13:28:19 +08:00
// 多选删除
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
2026-05-15 13:09:28 +08:00
// 查看二维码弹窗
const [viewModal, setViewModal] = useState(false);
const [viewRecord, setViewRecord] = useState(null);
// 映射
2026-05-15 10:53:53 +08:00
const countryNameMap = useRef({});
const provinceNameMap = useRef({});
2026-05-15 13:28:19 +08:00
// 工具方法:解析 qrJson
const parseQrJson = (qrJson) => {
if (!qrJson) return {};
try {
return JSON.parse(qrJson);
} catch {
return {};
}
};
2026-05-15 10:53:53 +08:00
// 初始化
2026-05-15 09:37:22 +08:00
useEffect(() => {
2026-05-15 10:16:36 +08:00
const countries = Country.getAllCountries();
setAllCountries(countries || []);
2026-05-15 10:53:53 +08:00
countries.forEach(item => {
countryNameMap.current[item.isoCode] = item.name;
2026-05-15 13:09:28 +08:00
});
2026-05-15 09:37:22 +08:00
loadTypeList();
loadModelList();
2026-05-15 10:03:33 +08:00
loadConfigList();
2026-05-15 13:09:28 +08:00
loadCodeList();
2026-05-15 13:28:19 +08:00
}, [currentPage]);
2026-05-15 13:09:28 +08:00
// 国家切换
2026-05-15 10:16:36 +08:00
const handleCountryChange = (countryCode) => {
const states = State.getStatesOfCountry(countryCode);
setProvinces(states || []);
form.setFieldsValue({ province: undefined });
};
2026-05-15 10:53:53 +08:00
2026-05-15 13:09:28 +08:00
// 加载二维码列表
const loadCodeList = async () => {
setCodeLoading(true);
try {
const params = searchForm.getFieldsValue();
const res = await getCodeListPageApi({ ...params, current: currentPage, size: pageSize });
if (res?.code === 200) {
const rows = res.data.records || res.data || [];
setCodeList(rows);
setTotal(res.data.total || rows.length);
2026-05-15 13:28:19 +08:00
setSelectedRowKeys([]);
2026-05-15 13:09:28 +08:00
}
} catch (e) {
} finally {
setCodeLoading(false);
}
};
// 删除
const handleDelete = async (id) => {
try {
await delteCodesApi(id);
messageApi.success('删除成功');
loadCodeList();
} catch (e) {
messageApi.error('删除失败');
}
};
2026-05-15 13:28:19 +08:00
// 批量删除
const handleBatchDelete = async () => {
if (selectedRowKeys.length === 0) {
message.warning('请选择要删除的数据');
return;
}
try {
await delteCodesApi(selectedRowKeys.join(','));
messageApi.success('批量删除成功');
loadCodeList();
} catch (e) {
messageApi.error('批量删除失败');
}
};
2026-05-15 13:09:28 +08:00
// 查看
const handleView = (record) => {
setViewRecord(record);
setViewModal(true);
};
// 加载数据
2026-05-15 09:37:22 +08:00
const loadTypeList = async () => {
try {
const res = await getRobotTypeListApi({});
2026-05-15 10:16:36 +08:00
if (res?.code === 200) setTypes(res.data || []);
} catch (e) { }
2026-05-15 09:37:22 +08:00
};
const loadModelList = async () => {
try {
const res = await getRobotModelPageApi({ pageNum: 1, pageSize: 100 });
2026-05-15 10:16:36 +08:00
if (res?.code === 200) setModels(res.data || []);
} catch (e) { }
2026-05-15 10:03:33 +08:00
};
const loadConfigList = async () => {
try {
const res = await getRobotConfigListApi({});
2026-05-15 10:16:36 +08:00
if (res?.code === 200) setConfigs(res.data || []);
} catch (e) { }
2026-05-15 10:03:33 +08:00
};
2026-05-15 13:09:28 +08:00
// 生成序列号
2026-05-13 13:31:49 +08:00
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-15 09:37:22 +08:00
// 生成二维码
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();
2026-05-15 12:59:51 +08:00
const parts = [];
if (model) parts.push(model + config);
if (country) parts.push(country);
if (province) parts.push(province);
parts.push(ts);
parts.push(suffix);
2026-05-15 10:53:53 +08:00
2026-05-15 12:59:51 +08:00
const serialNumber = parts.join('-');
2026-05-15 13:09:28 +08:00
2026-05-15 10:53:53 +08:00
const qrData = {
model,
config,
type,
countryCode: country,
countryFullName: countryNameMap.current[country] || country,
provinceCode: province,
provinceFullName: provinceNameMap.current[province] || province,
serialNumber
};
2026-05-13 13:31:49 +08:00
const qrContent = JSON.stringify(qrData, null, 2);
const url = await QRCode.toDataURL(qrContent);
2026-05-13 13:31:49 +08:00
2026-05-15 10:53:53 +08:00
setSerialNumber(serialNumber);
2026-05-15 09:37:22 +08:00
setQrJson(qrContent);
setQrUrl(url);
2026-05-15 13:09:28 +08:00
} catch (err) { }
2026-05-13 13:31:49 +08:00
};
2026-05-15 13:09:28 +08:00
// 下载
2026-05-13 13:31:49 +08:00
const downloadQRCode = () => {
2026-05-15 13:09:28 +08:00
if (!qrUrl) return messageApi.warning('请先生成');
2026-05-13 13:31:49 +08:00
const link = document.createElement('a');
link.download = `${serialNumber || 'qrcode'}.png`;
link.href = qrUrl;
link.click();
};
2026-05-15 13:09:28 +08:00
// 保存
const saveToDatabase = async () => {
2026-05-15 10:53:53 +08:00
if (!serialNumber) return messageApi.warning('请先生成');
try {
2026-05-15 12:59:51 +08:00
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,
2026-05-15 13:28:19 +08:00
qrCode: qrUrl,
2026-05-15 12:59:51 +08:00
qrJson: qrJson
};
2026-05-15 10:53:53 +08:00
const res = await saveCodeApi(data);
2026-05-15 13:09:28 +08:00
if (res?.code === 200) {
2026-05-15 10:53:53 +08:00
messageApi.success('保存成功');
2026-05-15 13:09:28 +08:00
loadCodeList();
setAddModalVisible(false);
form.resetFields();
setQrUrl('');
setSerialNumber('');
setQrJson('');
2026-05-15 10:53:53 +08:00
} else {
2026-05-15 13:09:28 +08:00
messageApi.error('保存失败:' + res?.msg);
2026-05-15 10:53:53 +08:00
}
2026-05-15 09:37:22 +08:00
} catch (e) {
2026-05-15 10:53:53 +08:00
messageApi.error('保存失败');
} finally {
setLoading(false);
}
};
2026-05-15 13:09:28 +08:00
// 查询
2026-05-14 08:45:14 +08:00
const handleQueryQRCode = async () => {
2026-05-15 13:28:19 +08:00
// 从搜索表单中获取序列号
const serialNumber = searchForm.getFieldValue('serialNumber');
if (!serialNumber) {
loadCodeList();
return;
}
2026-05-14 08:45:14 +08:00
setQueryLoading(true);
try {
2026-05-15 13:28:19 +08:00
const res = await getCodeImgApi(serialNumber);
2026-05-15 09:37:22 +08:00
if (res.data) {
2026-05-15 13:28:19 +08:00
setCodeList([res.data]);
2026-05-14 08:45:14 +08:00
} else {
2026-05-15 10:53:53 +08:00
messageApi.warning('未查询到数据');
2026-05-14 08:45:14 +08:00
}
2026-05-15 09:37:22 +08:00
} catch (e) {
2026-05-15 10:53:53 +08:00
messageApi.error('查询失败');
2026-05-14 08:45:14 +08:00
} finally {
setQueryLoading(false);
}
};
2026-05-15 13:09:28 +08:00
// 类型/型号/配置
2026-05-15 09:37:22 +08:00
const handleAddType = async (values) => {
2026-05-15 13:09:28 +08:00
return await addRobotTypeApi({ storeValue: values.storeValue, displayName: values.displayName, remark: values.remark || '' });
2026-05-15 09:37:22 +08:00
};
const handleUpdateType = async (values) => {
2026-05-15 13:09:28 +08:00
return await updateRobotTypeApi({ id: values.id, storeValue: values.storeValue, displayName: values.displayName, remark: values.remark });
2026-05-15 09:37:22 +08:00
};
const handleAddModel = async (values) => {
2026-05-15 13:09:28 +08:00
return await addRobotModelApi({ modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName });
2026-05-15 10:03:33 +08:00
};
2026-05-15 10:16:36 +08:00
const handleUpdateModel = async (values) => {
2026-05-15 13:09:28 +08:00
return await updateRobotModelApi({ id: values.id, modelName: values.displayName, storeValue: values.storeValue, displayName: values.displayName });
2026-05-15 10:03:33 +08:00
};
const handleAddConfig = async (values) => {
2026-05-15 13:09:28 +08:00
return await addRobotConfigApi({ configName: values.displayName, storeValue: values.storeValue, displayName: values.displayName });
2026-05-15 10:03:33 +08:00
};
const handleUpdateConfig = async (values) => {
2026-05-15 13:09:28 +08:00
return await updateRobotConfigApi({ id: values.id, configName: values.displayName, storeValue: values.storeValue, displayName: values.displayName });
2026-05-15 09:37:22 +08:00
};
2026-05-15 13:09:28 +08:00
// 表格列
const columns = [
2026-05-15 13:28:19 +08:00
{
title: '序号',
key: 'idx',
width: 70,
render: (_, __, index) => (currentPage - 1) * pageSize + index + 1
},
2026-05-15 13:09:28 +08:00
{ title: '序列号', width: 380, dataIndex: 'serialNumber', key: 'sn', ellipsis: true },
{
title: '型号',
key: 'model',
2026-05-15 13:28:19 +08:00
render: (_, r) => parseQrJson(r.qrJson).model || '-'
2026-05-15 13:09:28 +08:00
},
{
title: '配置',
key: 'config',
2026-05-15 13:28:19 +08:00
render: (_, r) => parseQrJson(r.qrJson).config || '-'
},
{
title: '类型',
key: 'type',
render: (_, r) => parseQrJson(r.qrJson).type || '-'
},
{
title: '国家',
key: 'country',
render: (_, r) => parseQrJson(r.qrJson).countryFullName || '-'
},
{
title: '省份',
key: 'province',
render: (_, r) => parseQrJson(r.qrJson).provinceFullName || '-'
2026-05-15 13:09:28 +08:00
},
{
title: '二维码',
key: 'img',
width: 100,
2026-05-15 13:28:19 +08:00
render: (_, r) => r.qrCodeBase64 && <img src={r.qrCodeBase64} style={{ width: 50, height: 50 }} alt="qrcode" />
2026-05-15 13:09:28 +08:00
},
{
title: '操作',
key: 'opt',
width: 180,
2026-05-15 13:34:39 +08:00
fixed: "right",
2026-05-15 13:09:28 +08:00
render: (_, r) => (
<Space>
<Button type="link" onClick={() => handleView(r)}>查看</Button>
<Popconfirm
title="确定删除?"
onConfirm={() => handleDelete(r.id)}
>
<Button type="link" danger>删除</Button>
</Popconfirm>
</Space>
)
}
];
2026-05-15 13:28:19 +08:00
// 多选配置
const rowSelection = {
selectedRowKeys,
onChange: setSelectedRowKeys
};
return (
2026-05-15 13:09:28 +08:00
<div style={{ padding: '0px 20px 40px', minHeight: '100vh' }}>
2026-05-15 10:53:53 +08:00
{contextHolder}
2026-05-15 13:09:28 +08:00
2026-05-15 13:28:19 +08:00
<Card
2026-05-15 13:34:39 +08:00
style={{ maxWidth: '100%', margin: '0 auto', overflow: 'hidden' }}
2026-05-15 13:09:28 +08:00
title="机器人二维码管理"
extra={
2026-05-15 13:28:19 +08:00
<Space wrap>
2026-05-15 13:09:28 +08:00
<Button type="primary" onClick={() => setAddModalVisible(true)}>新增</Button>
<Button onClick={() => setManageVisible(true)}>数据管理</Button>
2026-05-15 13:28:19 +08:00
<Popconfirm title="确定删除选中项?" onConfirm={handleBatchDelete}>
<Button danger disabled={selectedRowKeys.length === 0}>批量删除</Button>
</Popconfirm>
2026-05-15 13:09:28 +08:00
</Space>
2026-05-15 13:28:19 +08:00
}
>
2026-05-15 13:09:28 +08:00
{/* 筛选栏 */}
2026-05-15 13:34:39 +08:00
<Form form={searchForm} layout="inline" style={{ marginBottom: 16, flexWrap: 'wrap', gap: 8 }}>
2026-05-15 13:09:28 +08:00
<Form.Item name="serialNumber" label="序列号">
2026-05-15 13:34:39 +08:00
<Input placeholder="请输入序列号" allowClear style={{ width: '100%', minWidth: 260, maxWidth: 420 }} />
2026-05-15 13:09:28 +08:00
</Form.Item>
2026-05-15 13:34:39 +08:00
<Form.Item style={{ margin: 0 }}>
2026-05-15 13:28:19 +08:00
<Space wrap>
<Button type="primary" onClick={handleQueryQRCode}>搜索</Button>
2026-05-15 13:09:28 +08:00
<Button onClick={() => { searchForm.resetFields(); loadCodeList(); }}>重置</Button>
</Space>
</Form.Item>
</Form>
2026-05-15 13:34:39 +08:00
{/* 列表 —— 完全自适应 + 右侧固定 + 不溢出 */}
<div style={{ width: '100%', overflow: 'auto' }}>
<Table
rowKey="id"
columns={columns}
dataSource={codeList}
loading={codeLoading}
rowSelection={rowSelection}
pagination={{ current: currentPage, pageSize, total, onChange: setCurrentPage }}
scroll={{ x: 'max-content' }}
style={{ width: '100%' }}
/>
</div>
2026-05-15 13:09:28 +08:00
</Card>
2026-05-15 13:28:19 +08:00
{/* 新增弹窗 */}
2026-05-15 13:09:28 +08:00
<Modal
title="生成机器人二维码"
open={addModalVisible}
2026-05-15 13:28:19 +08:00
width="90%"
style={{ maxWidth: 900 }}
2026-05-15 13:09:28 +08:00
onCancel={() => {
setAddModalVisible(false);
form.resetFields();
setQrUrl('');
setSerialNumber('');
}}
footer={null}
destroyOnClose
>
2026-05-15 13:09:28 +08:00
<Row gutter={[24, 16]}>
2026-05-15 13:28:19 +08:00
<Col xs={24} lg={14}>
2026-05-15 10:16:36 +08:00
<Form form={form} labelCol={{ md: 5 }} wrapperCol={{ md: 19 }} size="large">
<Form.Item name="model" label="型号" rules={[{ required: true }]}>
2026-05-15 13:09:28 +08:00
<Select placeholder="选择型号" showSearch>
{models.map(m => <Option key={m.id} value={m.displayName}>{m.modelName}</Option>)}
</Select>
</Form.Item>
<Form.Item name="config" label="配置" rules={[{ required: true }]}>
2026-05-15 13:09:28 +08:00
<Select placeholder="选择配置" showSearch>
{configs.map(c => <Option key={c.id} value={c.displayName}>{c.configName}</Option>)}
</Select>
</Form.Item>
<Form.Item name="type" label="类型" rules={[{ required: true }]}>
2026-05-15 13:09:28 +08:00
<Select placeholder="选择类型" showSearch>
{types.map(t => <Option key={t.id} value={t.displayName}>{t.storeValue}</Option>)}
</Select>
</Form.Item>
2026-05-15 13:09:28 +08:00
<Form.Item name="country" label="国家">
<Select placeholder="选择国家" showSearch allowClear onChange={handleCountryChange}>
{allCountries.map(c => <Option key={c.isoCode} value={c.isoCode}>{c.name}</Option>)}
</Select>
</Form.Item>
2026-05-15 13:09:28 +08:00
<Form.Item name="province" label="省份">
<Select placeholder="请先选择国家" showSearch allowClear disabled={provinces.length === 0}>
{provinces.map(p => <Option key={p.isoCode} value={p.isoCode}>{p.name}</Option>)}
</Select>
</Form.Item>
<Form.Item wrapperCol={{ offset: 5 }}>
2026-05-15 13:09:28 +08:00
<Button type="primary" onClick={generateQRCode} block>生成序列号 & 二维码</Button>
</Form.Item>
</Form>
</Col>
2026-05-15 13:28:19 +08:00
<Col xs={24} lg={10}>
<div style={{ padding: 20, background: '#fafafa', borderRadius: 12, border: '1px dashed #d9d9d9', display: 'flex', flexDirection: 'column', alignItems: 'center', minHeight: 400 }}>
2026-05-13 13:31:49 +08:00
{serialNumber && (
2026-05-15 09:37:22 +08:00
<div style={{ padding: 12, background: '#e6f7ff', borderRadius: 8, width: '100%', textAlign: 'center', marginBottom: 20 }}>
2026-05-15 13:09:28 +08:00
<Text strong>序列号:{serialNumber}</Text>
</div>
)}
{qrUrl ? (
2026-05-15 13:28:19 +08:00
<img src={qrUrl} style={{ width: 240, height: 240, maxWidth: '100%' }} alt="qrcode" />
) : (
<div style={{ color: '#999', padding: '60px 0' }}>请先生成二维码</div>
)}
<Space style={{ marginTop: 24 }}>
2026-05-15 13:09:28 +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>
2026-05-15 13:09:28 +08:00
</Modal>
2026-05-15 09:37:22 +08:00
2026-05-15 13:09:28 +08:00
{/* 查看二维码 */}
<Modal open={viewModal} title="二维码详情" onCancel={() => setViewModal(false)} footer={null} width={400}>
{viewRecord && (
2026-05-15 13:34:39 +08:00
<div style={{ textAlign: 'center', display: "flex", flexDirection: "column", justifyContent: "center", alignItems: 'center' }}>
<img src={viewRecord.qrCodeBase64} style={{ width: 200, height: 200, maxWidth: '100%' }} alt="qrcode" />
2026-05-15 13:09:28 +08:00
<div style={{ marginTop: 16 }}>序列号:{viewRecord.serialNumber}</div>
</div>
)}
</Modal>
2026-05-15 09:37:22 +08:00
2026-05-15 13:09:28 +08:00
{/* 数据管理 */}
2026-05-15 13:28:19 +08:00
<Modal open={manageVisible} title="数据管理" width="90%" style={{ maxWidth: 900 }} onCancel={() => setManageVisible(false)} footer={<Button onClick={() => setManageVisible(false)}>关闭</Button>} destroyOnClose>
2026-05-15 13:09:28 +08:00
<DictManager title="机器人类型" data={types} fields={[{ name: 'storeValue', label: '类型名称', required: true }, { name: 'displayName', label: '存储值', required: true }, { name: 'remark', label: '备注' }]} 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;