机器人序列号 接口初始化
This commit is contained in:
@@ -1,22 +1,34 @@
|
||||
import React, { useState } from 'react';
|
||||
import {
|
||||
Table, Input, Button, Form, Space, Card, Modal
|
||||
Table, Input, Button, Form, Space, Card, Modal, message
|
||||
} from 'antd';
|
||||
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
||||
|
||||
|
||||
// 🔥 组件
|
||||
const DictManager = ({
|
||||
title,
|
||||
data,
|
||||
fields = [
|
||||
{ name: 'storeValue', label: '存储值', required: true },
|
||||
{ name: 'typeName', label: '名称', required: true },
|
||||
{ name: 'remark', label: '备注', required: false }
|
||||
],
|
||||
onAdd,
|
||||
onUpdate,
|
||||
onDelete,
|
||||
refresh,
|
||||
loading,
|
||||
}) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [editing, setEditing] = useState(null);
|
||||
const [messageApi, contextHolder] = message.useMessage();
|
||||
|
||||
// 打开新增
|
||||
const handleAddNew = () => {
|
||||
setEditing(null);
|
||||
form.resetFields();
|
||||
setVisible(true);
|
||||
};
|
||||
|
||||
// 编辑
|
||||
const handleEdit = (record) => {
|
||||
@@ -25,24 +37,38 @@ const DictManager = ({
|
||||
setVisible(true);
|
||||
};
|
||||
|
||||
// 提交
|
||||
const handleSubmit = () => {
|
||||
form.validateFields().then(values => {
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
let res;
|
||||
|
||||
if (editing) {
|
||||
onUpdate({ ...editing, ...values });
|
||||
res = await onUpdate({ ...editing, ...values });
|
||||
} else {
|
||||
onAdd({ id: Date.now(), ...values });
|
||||
res = await onAdd(values);
|
||||
}
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
setEditing(null);
|
||||
});
|
||||
|
||||
if (res?.code == 200) {
|
||||
messageApi.success(editing ? "编辑成功" : '新增成功');
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
setEditing(null);
|
||||
refresh?.();
|
||||
} else {
|
||||
messageApi.error(res?.msg || '操作失败');
|
||||
}
|
||||
} catch (err) {
|
||||
messageApi.error('提交失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 表格列
|
||||
const columns = [
|
||||
{ title: '存储值', dataIndex: 'value', key: 'value' },
|
||||
{ title: '显示名', dataIndex: 'label', key: 'label' },
|
||||
...fields.map(item => ({
|
||||
title: item.label,
|
||||
dataIndex: item.name,
|
||||
key: item.name,
|
||||
})),
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
@@ -51,7 +77,24 @@ const DictManager = ({
|
||||
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>
|
||||
编辑
|
||||
</Button>
|
||||
<Button type="link" danger icon={<DeleteOutlined />} onClick={() => onDelete(record.id)}>
|
||||
<Button
|
||||
type="link"
|
||||
danger
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={async () => {
|
||||
try {
|
||||
const res = await onDelete(record.id);
|
||||
if (res?.code === 200) {
|
||||
messageApi.success('删除成功');
|
||||
refresh?.();
|
||||
} else {
|
||||
messageApi.error(res?.msg || '删除失败');
|
||||
}
|
||||
} catch (err) {
|
||||
messageApi.error('删除失败');
|
||||
}
|
||||
}}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
@@ -61,15 +104,8 @@ const DictManager = ({
|
||||
|
||||
return (
|
||||
<Card size="small" title={title} style={{ marginBottom: 16 }}>
|
||||
<Button
|
||||
type="primary"
|
||||
size="small"
|
||||
onClick={() => {
|
||||
setEditing(null);
|
||||
form.resetFields();
|
||||
setVisible(true);
|
||||
}}
|
||||
>
|
||||
{contextHolder}
|
||||
<Button type="primary" size="small" onClick={handleAddNew}>
|
||||
新增
|
||||
</Button>
|
||||
|
||||
@@ -90,15 +126,20 @@ const DictManager = ({
|
||||
confirmLoading={loading}
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item name="value" label="存储值" rules={[{ required: true, message: '请输入' }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="label" label="显示名称" rules={[{ required: true, message: '请输入' }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
{fields.map(item => (
|
||||
<Form.Item
|
||||
key={item.name}
|
||||
name={item.name}
|
||||
label={item.label}
|
||||
rules={[{ required: item.required, message: `请输入${item.label}` }]}
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
))}
|
||||
</Form>
|
||||
</Modal>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default DictManager;
|
||||
|
||||
Reference in New Issue
Block a user