2026-01-20 14:31:20 +08:00
|
|
|
|
import React, { useState, useEffect, useMemo } from 'react';
|
2026-02-10 11:04:01 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Table,
|
|
|
|
|
|
Button,
|
|
|
|
|
|
Modal,
|
|
|
|
|
|
Form,
|
|
|
|
|
|
Input,
|
|
|
|
|
|
Select,
|
|
|
|
|
|
message,
|
|
|
|
|
|
Card,
|
|
|
|
|
|
Space,
|
|
|
|
|
|
InputNumber,
|
|
|
|
|
|
Switch,
|
|
|
|
|
|
Tag,
|
|
|
|
|
|
Statistic,
|
|
|
|
|
|
Tooltip,
|
|
|
|
|
|
} from 'antd';
|
|
|
|
|
|
import {
|
|
|
|
|
|
PlusOutlined,
|
|
|
|
|
|
EditOutlined,
|
|
|
|
|
|
DeleteOutlined,
|
|
|
|
|
|
AppstoreOutlined,
|
|
|
|
|
|
FontSizeOutlined,
|
|
|
|
|
|
NumberOutlined,
|
|
|
|
|
|
CheckCircleOutlined,
|
|
|
|
|
|
CalendarOutlined,
|
|
|
|
|
|
FileTextOutlined,
|
|
|
|
|
|
LockOutlined,
|
2026-03-23 14:07:40 +08:00
|
|
|
|
PlusCircleOutlined,
|
|
|
|
|
|
MinusCircleOutlined,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
} from '@ant-design/icons';
|
2025-12-18 10:45:13 +08:00
|
|
|
|
import axios from 'axios';
|
2026-03-10 15:49:02 +08:00
|
|
|
|
import { designTokens } from '../config/theme';
|
2026-03-12 12:40:12 +08:00
|
|
|
|
import CloseButton from '../components/CloseButton';
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
2026-01-20 11:28:53 +08:00
|
|
|
|
const { Option = Select.Option } = Select;
|
|
|
|
|
|
|
2026-03-23 14:07:40 +08:00
|
|
|
|
const OptionsEditor = ({ value = [], onChange }) => {
|
|
|
|
|
|
const handleAdd = () => {
|
|
|
|
|
|
onChange([...value, { value: '', label: '' }]);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleRemove = index => {
|
|
|
|
|
|
onChange(value.filter((_, i) => i !== index));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleUpdate = (index, field, fieldValue) => {
|
2026-03-27 19:12:16 +08:00
|
|
|
|
const newOptions = value.map((opt, i) => (i === index ? { ...opt, [field]: fieldValue } : opt));
|
2026-03-23 14:07:40 +08:00
|
|
|
|
onChange(newOptions);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-27 19:12:16 +08:00
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
border: '1px solid #e8e8e8',
|
|
|
|
|
|
borderRadius: '12px',
|
|
|
|
|
|
padding: '20px',
|
|
|
|
|
|
background: 'linear-gradient(135deg, #fafbfc 0%, #f5f7fa 100%)',
|
|
|
|
|
|
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.04)',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
marginBottom: '16px',
|
|
|
|
|
|
gap: '8px',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
width: '4px',
|
|
|
|
|
|
height: '16px',
|
|
|
|
|
|
background: 'linear-gradient(180deg, #667eea 0%, #764ba2 100%)',
|
|
|
|
|
|
borderRadius: '2px',
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span style={{ color: '#333', fontSize: '14px', fontWeight: '600' }}>选项配置</span>
|
|
|
|
|
|
<span style={{ color: '#999', fontSize: '12px' }}>(值用于提交,标签用于显示)</span>
|
2026-03-23 14:07:40 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{value.length === 0 ? (
|
2026-03-27 19:12:16 +08:00
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
|
padding: '24px',
|
|
|
|
|
|
background: '#fff',
|
|
|
|
|
|
borderRadius: '8px',
|
|
|
|
|
|
border: '1px dashed #d9d9d9',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div style={{ color: '#bbb', fontSize: '14px', marginBottom: '12px' }}>暂无选项</div>
|
2026-03-23 14:07:40 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
icon={<PlusCircleOutlined />}
|
|
|
|
|
|
onClick={handleAdd}
|
|
|
|
|
|
style={{
|
|
|
|
|
|
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
|
|
|
|
|
|
border: 'none',
|
|
|
|
|
|
borderRadius: '6px',
|
|
|
|
|
|
height: '36px',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
添加第一个选项
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
2026-03-27 19:12:16 +08:00
|
|
|
|
<div
|
|
|
|
|
|
style={{ display: 'flex', flexDirection: 'column', gap: '10px', marginBottom: '16px' }}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
gap: '12px',
|
|
|
|
|
|
padding: '0 4px',
|
|
|
|
|
|
marginBottom: '4px',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span style={{ width: '160px', color: '#666', fontSize: '12px', fontWeight: '500' }}>
|
|
|
|
|
|
值(value)
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span style={{ width: '160px', color: '#666', fontSize: '12px', fontWeight: '500' }}>
|
|
|
|
|
|
标签(label)
|
|
|
|
|
|
</span>
|
2026-03-23 14:07:40 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
{value.map((opt, index) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
key={index}
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
gap: '12px',
|
|
|
|
|
|
padding: '12px',
|
|
|
|
|
|
background: '#fff',
|
|
|
|
|
|
borderRadius: '8px',
|
|
|
|
|
|
border: '1px solid #e8e8e8',
|
|
|
|
|
|
transition: 'all 0.2s ease',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2026-03-27 19:12:16 +08:00
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
width: '24px',
|
|
|
|
|
|
height: '24px',
|
|
|
|
|
|
borderRadius: '50%',
|
|
|
|
|
|
background: 'linear-gradient(135deg, #667eea20 0%, #764ba220 100%)',
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
|
color: '#667eea',
|
|
|
|
|
|
fontSize: '12px',
|
|
|
|
|
|
fontWeight: '600',
|
|
|
|
|
|
flexShrink: 0,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2026-03-23 14:07:40 +08:00
|
|
|
|
{index + 1}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="值"
|
|
|
|
|
|
value={opt.value}
|
|
|
|
|
|
onChange={e => handleUpdate(index, 'value', e.target.value)}
|
|
|
|
|
|
style={{ width: '160px', borderRadius: '6px' }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="标签"
|
|
|
|
|
|
value={opt.label}
|
|
|
|
|
|
onChange={e => handleUpdate(index, 'label', e.target.value)}
|
|
|
|
|
|
style={{ width: '160px', borderRadius: '6px' }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
danger
|
|
|
|
|
|
icon={<MinusCircleOutlined />}
|
|
|
|
|
|
onClick={() => handleRemove(index)}
|
|
|
|
|
|
style={{ flexShrink: 0 }}
|
|
|
|
|
|
>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{value.length > 0 && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="dashed"
|
|
|
|
|
|
icon={<PlusCircleOutlined />}
|
|
|
|
|
|
onClick={handleAdd}
|
|
|
|
|
|
style={{
|
|
|
|
|
|
width: '100%',
|
|
|
|
|
|
height: '40px',
|
|
|
|
|
|
borderRadius: '8px',
|
|
|
|
|
|
borderColor: '#d9d9d9',
|
|
|
|
|
|
color: '#666',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
添加选项
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-20 11:28:53 +08:00
|
|
|
|
const pageContainerStyle = {
|
|
|
|
|
|
minHeight: '100vh',
|
|
|
|
|
|
background: designTokens.colors.background.secondary,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
padding: designTokens.spacing.lg,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const titleRowStyle = {
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'space-between',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
marginBottom: designTokens.spacing.lg,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const titleStyle = {
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
gap: designTokens.spacing.sm,
|
|
|
|
|
|
fontSize: '20px',
|
|
|
|
|
|
fontWeight: '600',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
color: designTokens.colors.text.primary,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const actionButtonStyle = {
|
|
|
|
|
|
height: '36px',
|
|
|
|
|
|
padding: `0 ${designTokens.spacing.md}px`,
|
|
|
|
|
|
borderRadius: designTokens.borderRadius.small,
|
|
|
|
|
|
fontSize: '13px',
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
alignItems: 'center',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
gap: designTokens.spacing.xs,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const primaryActionStyle = {
|
|
|
|
|
|
...actionButtonStyle,
|
|
|
|
|
|
background: designTokens.colors.primary.gradient,
|
|
|
|
|
|
border: 'none',
|
|
|
|
|
|
color: '#ffffff',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
boxShadow: designTokens.shadows.small,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const tableCardStyle = {
|
|
|
|
|
|
background: designTokens.colors.background.primary,
|
|
|
|
|
|
borderRadius: designTokens.borderRadius.large,
|
|
|
|
|
|
boxShadow: designTokens.shadows.small,
|
|
|
|
|
|
border: `1px solid ${designTokens.colors.border.light}`,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
overflow: 'hidden',
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const tableStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
background: designTokens.colors.background.primary,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
2026-01-20 14:31:20 +08:00
|
|
|
|
const titleIconStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
color: designTokens.colors.primary.main,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const modalTitleStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
fontWeight: '600',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const formLabelStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
fontWeight: '500',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const tableCellStyle = {
|
|
|
|
|
|
fontWeight: '500',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
color: designTokens.colors.text.primary,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const typeTagStyle = {
|
|
|
|
|
|
border: 'none',
|
|
|
|
|
|
borderRadius: designTokens.borderRadius.small,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
fontWeight: '500',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const orderBadgeStyle = {
|
|
|
|
|
|
background: designTokens.colors.background.tertiary,
|
|
|
|
|
|
padding: '2px 8px',
|
|
|
|
|
|
borderRadius: designTokens.borderRadius.small,
|
|
|
|
|
|
fontSize: '12px',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
fontWeight: '500',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const editButtonStyle = {
|
|
|
|
|
|
color: designTokens.colors.primary.main,
|
|
|
|
|
|
height: '28px',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
padding: '0 8px',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const deleteButtonStyle = {
|
|
|
|
|
|
height: '28px',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
padding: '0 8px',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const formRowStyle = {
|
|
|
|
|
|
display: 'flex',
|
2026-02-10 11:04:01 +08:00
|
|
|
|
gap: designTokens.spacing.md,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const formItemFlexStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
flex: 1,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const textAreaStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
fontFamily: 'monospace',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const modalBodyStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
padding: designTokens.spacing.lg,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const formActionsStyle = {
|
|
|
|
|
|
marginBottom: 0,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
textAlign: 'right',
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const modalStyle = {
|
2026-02-10 11:04:01 +08:00
|
|
|
|
borderRadius: designTokens.borderRadius.large,
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const FIELD_TYPE_MAP = {
|
|
|
|
|
|
string: { text: '文本', color: designTokens.colors.fieldType.string },
|
|
|
|
|
|
number: { text: '数字', color: designTokens.colors.fieldType.number },
|
|
|
|
|
|
boolean: { text: '布尔值', color: designTokens.colors.fieldType.boolean },
|
|
|
|
|
|
select: { text: '下拉选择', color: designTokens.colors.fieldType.select },
|
|
|
|
|
|
date: { text: '日期', color: designTokens.colors.fieldType.date },
|
2026-02-10 11:04:01 +08:00
|
|
|
|
textarea: { text: '多行文本', color: designTokens.colors.fieldType.textarea },
|
2026-01-20 14:31:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const FIELD_TYPE_OPTIONS = [
|
|
|
|
|
|
{ value: 'string', label: '文本' },
|
|
|
|
|
|
{ value: 'number', label: '数字' },
|
|
|
|
|
|
{ value: 'boolean', label: '布尔值' },
|
|
|
|
|
|
{ value: 'select', label: '下拉选择' },
|
|
|
|
|
|
{ value: 'date', label: '日期' },
|
2026-02-10 11:04:01 +08:00
|
|
|
|
{ value: 'textarea', label: '多行文本' },
|
2026-01-20 14:31:20 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
2025-12-18 10:45:13 +08:00
|
|
|
|
function DeviceFieldManagement() {
|
|
|
|
|
|
const [fields, setFields] = useState([]);
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
const [modalVisible, setModalVisible] = useState(false);
|
|
|
|
|
|
const [editingField, setEditingField] = useState(null);
|
2026-03-23 14:07:40 +08:00
|
|
|
|
const [selectedFieldType, setSelectedFieldType] = useState('string');
|
2025-12-18 10:45:13 +08:00
|
|
|
|
const [form] = Form.useForm();
|
2026-02-05 08:34:12 +08:00
|
|
|
|
const [pagination, setPagination] = useState({
|
|
|
|
|
|
current: 1,
|
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
|
pageSizeOptions: ['10', '20', '50', '100'],
|
|
|
|
|
|
showSizeChanger: true,
|
|
|
|
|
|
showQuickJumper: true,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条 / 共 ${total} 条`,
|
2026-02-05 08:34:12 +08:00
|
|
|
|
});
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
|
|
|
|
|
const fetchFields = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
const response = await axios.get('/api/deviceFields');
|
|
|
|
|
|
setFields(response.data.sort((a, b) => a.order - b.order));
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
message.error('获取字段列表失败');
|
|
|
|
|
|
console.error('获取字段列表失败:', error);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
fetchFields();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const showModal = (field = null) => {
|
|
|
|
|
|
setEditingField(field);
|
|
|
|
|
|
if (field) {
|
|
|
|
|
|
const fieldData = {
|
|
|
|
|
|
...field,
|
2026-03-23 14:07:40 +08:00
|
|
|
|
options: field.options || [],
|
2025-12-18 10:45:13 +08:00
|
|
|
|
};
|
2026-03-23 14:07:40 +08:00
|
|
|
|
setSelectedFieldType(field.fieldType || 'string');
|
2025-12-18 10:45:13 +08:00
|
|
|
|
form.setFieldsValue(fieldData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
form.resetFields();
|
2026-03-23 14:07:40 +08:00
|
|
|
|
setSelectedFieldType('string');
|
2025-12-18 10:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
setModalVisible(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
|
setModalVisible(false);
|
|
|
|
|
|
setEditingField(null);
|
2026-03-23 14:07:40 +08:00
|
|
|
|
setSelectedFieldType('string');
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleFieldTypeChange = value => {
|
|
|
|
|
|
setSelectedFieldType(value);
|
|
|
|
|
|
if (value !== 'select') {
|
|
|
|
|
|
form.setFieldsValue({ options: [] });
|
|
|
|
|
|
}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:04:01 +08:00
|
|
|
|
const handleSubmit = async values => {
|
2025-12-18 10:45:13 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const fieldData = {
|
|
|
|
|
|
...values,
|
2026-03-23 14:07:40 +08:00
|
|
|
|
options: values.options && values.options.length > 0 ? values.options : null,
|
2025-12-18 10:45:13 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (editingField) {
|
|
|
|
|
|
await axios.put(`/api/deviceFields/${editingField.fieldId}`, fieldData);
|
|
|
|
|
|
message.success('字段更新成功');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await axios.post('/api/deviceFields', fieldData);
|
|
|
|
|
|
message.success('字段创建成功');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setModalVisible(false);
|
|
|
|
|
|
fetchFields();
|
|
|
|
|
|
setEditingField(null);
|
2026-03-23 14:07:40 +08:00
|
|
|
|
setSelectedFieldType('string');
|
2025-12-18 10:45:13 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
message.error(editingField ? '字段更新失败' : '字段创建失败');
|
|
|
|
|
|
console.error(editingField ? '字段更新失败:' : '字段创建失败:', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:04:01 +08:00
|
|
|
|
const handleDelete = async fieldId => {
|
2025-12-18 10:45:13 +08:00
|
|
|
|
Modal.confirm({
|
|
|
|
|
|
title: '确认删除',
|
|
|
|
|
|
content: '确定要删除这个字段吗?',
|
|
|
|
|
|
okText: '删除',
|
|
|
|
|
|
okType: 'danger',
|
|
|
|
|
|
cancelText: '取消',
|
|
|
|
|
|
onOk: async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await axios.delete(`/api/deviceFields/${fieldId}`);
|
|
|
|
|
|
message.success('字段删除成功');
|
|
|
|
|
|
fetchFields();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
message.error('字段删除失败');
|
|
|
|
|
|
console.error('字段删除失败:', error);
|
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
},
|
2025-12-18 10:45:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:04:01 +08:00
|
|
|
|
const getFieldTypeIcon = type => {
|
2026-01-20 11:28:53 +08:00
|
|
|
|
const iconMap = {
|
|
|
|
|
|
string: <FontSizeOutlined />,
|
|
|
|
|
|
number: <NumberOutlined />,
|
|
|
|
|
|
boolean: <CheckCircleOutlined />,
|
|
|
|
|
|
select: <AppstoreOutlined />,
|
|
|
|
|
|
date: <CalendarOutlined />,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
textarea: <FileTextOutlined />,
|
2026-01-20 11:28:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
return iconMap[type] || <FontSizeOutlined />;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:04:01 +08:00
|
|
|
|
const columns = useMemo(
|
|
|
|
|
|
() => [
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '字段名称',
|
|
|
|
|
|
dataIndex: 'fieldName',
|
|
|
|
|
|
key: 'fieldName',
|
|
|
|
|
|
width: 150,
|
|
|
|
|
|
render: (text, record) => (
|
|
|
|
|
|
<Space>
|
|
|
|
|
|
<span style={tableCellStyle}>{text}</span>
|
|
|
|
|
|
{record.isSystem && (
|
|
|
|
|
|
<Tooltip title="系统字段,不可删除">
|
|
|
|
|
|
<LockOutlined style={{ color: '#f59e0b', fontSize: '14px' }} />
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '显示名称',
|
|
|
|
|
|
dataIndex: 'displayName',
|
|
|
|
|
|
key: 'displayName',
|
|
|
|
|
|
width: 120,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '字段类型',
|
|
|
|
|
|
dataIndex: 'fieldType',
|
|
|
|
|
|
key: 'fieldType',
|
|
|
|
|
|
width: 110,
|
|
|
|
|
|
render: type => {
|
|
|
|
|
|
const config = FIELD_TYPE_MAP[type] || {
|
|
|
|
|
|
text: type,
|
|
|
|
|
|
color: designTokens.colors.text.tertiary,
|
|
|
|
|
|
};
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag style={{ ...typeTagStyle, background: `${config.color}15`, color: config.color }}>
|
|
|
|
|
|
{getFieldTypeIcon(type)}
|
|
|
|
|
|
<span style={{ marginLeft: '4px' }}>{config.text}</span>
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '必填',
|
|
|
|
|
|
dataIndex: 'required',
|
|
|
|
|
|
key: 'required',
|
|
|
|
|
|
width: 80,
|
|
|
|
|
|
render: required => (
|
|
|
|
|
|
<span
|
|
|
|
|
|
style={{
|
|
|
|
|
|
color: required
|
|
|
|
|
|
? designTokens.colors.success.main
|
|
|
|
|
|
: designTokens.colors.text.tertiary,
|
|
|
|
|
|
...tableCellStyle,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{required ? '是' : '否'}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '可见',
|
|
|
|
|
|
dataIndex: 'visible',
|
|
|
|
|
|
key: 'visible',
|
|
|
|
|
|
width: 80,
|
|
|
|
|
|
render: visible => (
|
|
|
|
|
|
<span
|
|
|
|
|
|
style={{
|
|
|
|
|
|
color: visible ? designTokens.colors.primary.main : designTokens.colors.text.tertiary,
|
|
|
|
|
|
...tableCellStyle,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{visible ? '是' : '否'}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '顺序',
|
|
|
|
|
|
dataIndex: 'order',
|
|
|
|
|
|
key: 'order',
|
|
|
|
|
|
width: 80,
|
|
|
|
|
|
render: order => <span style={orderBadgeStyle}>{order}</span>,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
title: '操作',
|
|
|
|
|
|
key: 'action',
|
|
|
|
|
|
width: 160,
|
|
|
|
|
|
fixed: 'right',
|
|
|
|
|
|
render: (_, record) => (
|
|
|
|
|
|
<Space size="small">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
icon={<EditOutlined />}
|
|
|
|
|
|
onClick={() => showModal(record)}
|
2026-03-10 13:12:29 +08:00
|
|
|
|
className="table-action-btn"
|
2026-02-10 11:04:01 +08:00
|
|
|
|
>
|
|
|
|
|
|
编辑
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
{record.isSystem ? (
|
|
|
|
|
|
<Tooltip title="系统字段不可删除">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
danger
|
|
|
|
|
|
icon={<DeleteOutlined />}
|
|
|
|
|
|
disabled
|
2026-03-10 13:12:29 +08:00
|
|
|
|
className="table-action-btn-disabled"
|
2026-02-10 11:04:01 +08:00
|
|
|
|
>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
danger
|
|
|
|
|
|
icon={<DeleteOutlined />}
|
|
|
|
|
|
onClick={() => handleDelete(record.fieldId)}
|
2026-03-10 13:12:29 +08:00
|
|
|
|
className="table-action-btn"
|
2026-02-04 17:10:58 +08:00
|
|
|
|
>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
2026-02-10 11:04:01 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
[]
|
|
|
|
|
|
);
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-20 11:28:53 +08:00
|
|
|
|
<div style={pageContainerStyle}>
|
|
|
|
|
|
<div style={titleRowStyle}>
|
|
|
|
|
|
<div style={titleStyle}>
|
2026-01-20 14:31:20 +08:00
|
|
|
|
<AppstoreOutlined style={titleIconStyle} />
|
2026-01-20 11:28:53 +08:00
|
|
|
|
设备字段管理
|
|
|
|
|
|
</div>
|
2026-02-10 11:04:01 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
|
|
onClick={() => showModal()}
|
2026-03-10 13:12:29 +08:00
|
|
|
|
className="page-primary-btn"
|
2026-02-10 11:04:01 +08:00
|
|
|
|
>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
添加字段
|
|
|
|
|
|
</Button>
|
2026-01-20 11:28:53 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div style={tableCardStyle}>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
<Table
|
|
|
|
|
|
columns={columns}
|
|
|
|
|
|
dataSource={fields}
|
|
|
|
|
|
rowKey="fieldId"
|
|
|
|
|
|
loading={loading}
|
2026-02-05 08:34:12 +08:00
|
|
|
|
pagination={pagination}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
onChange={newPagination => {
|
2026-02-05 08:34:12 +08:00
|
|
|
|
setPagination({
|
|
|
|
|
|
...pagination,
|
|
|
|
|
|
current: newPagination.current,
|
2026-02-10 11:04:01 +08:00
|
|
|
|
pageSize: newPagination.pageSize,
|
2026-02-05 08:34:12 +08:00
|
|
|
|
});
|
2026-01-20 11:28:53 +08:00
|
|
|
|
}}
|
|
|
|
|
|
scroll={{ x: 900 }}
|
|
|
|
|
|
style={tableStyle}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
/>
|
2026-01-20 11:28:53 +08:00
|
|
|
|
</div>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
|
|
|
|
|
<Modal
|
2026-01-20 14:31:20 +08:00
|
|
|
|
title={<span style={modalTitleStyle}>{editingField ? '编辑字段' : '添加字段'}</span>}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
open={modalVisible}
|
2026-03-12 12:40:12 +08:00
|
|
|
|
closeIcon={<CloseButton />}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
onCancel={handleCancel}
|
|
|
|
|
|
footer={null}
|
|
|
|
|
|
width={600}
|
2026-01-20 14:31:20 +08:00
|
|
|
|
styles={{ body: modalBodyStyle }}
|
|
|
|
|
|
style={modalStyle}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
>
|
2026-01-20 14:31:20 +08:00
|
|
|
|
<Form form={form} layout="vertical" onFinish={handleSubmit}>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="fieldName"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>字段名称</span>}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
rules={[{ required: true, message: '请输入字段名称' }]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Input placeholder="请输入字段名称(英文,如:deviceId)" />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="displayName"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>显示名称</span>}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
rules={[{ required: true, message: '请输入显示名称' }]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Input placeholder="请输入显示名称(中文,如:设备ID)" />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="fieldType"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>字段类型</span>}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
rules={[{ required: true, message: '请选择字段类型' }]}
|
|
|
|
|
|
>
|
2026-03-23 14:07:40 +08:00
|
|
|
|
<Select placeholder="请选择字段类型" onChange={handleFieldTypeChange}>
|
2026-01-20 14:31:20 +08:00
|
|
|
|
{FIELD_TYPE_OPTIONS.map(opt => (
|
2026-02-10 11:04:01 +08:00
|
|
|
|
<Option key={opt.value} value={opt.value}>
|
|
|
|
|
|
{opt.label}
|
|
|
|
|
|
</Option>
|
2026-01-20 14:31:20 +08:00
|
|
|
|
))}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
</Select>
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
2026-01-20 14:31:20 +08:00
|
|
|
|
<div style={formRowStyle}>
|
2026-01-20 11:28:53 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="required"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>必填</span>}
|
2026-01-20 11:28:53 +08:00
|
|
|
|
valuePropName="checked"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
style={formItemFlexStyle}
|
2026-01-20 11:28:53 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Switch />
|
|
|
|
|
|
</Form.Item>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
2026-01-20 11:28:53 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="visible"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>可见</span>}
|
2026-01-20 11:28:53 +08:00
|
|
|
|
valuePropName="checked"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
style={formItemFlexStyle}
|
2026-01-20 11:28:53 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Switch defaultChecked />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
</div>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="order"
|
2026-01-20 14:31:20 +08:00
|
|
|
|
label={<span style={formLabelStyle}>显示顺序</span>}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
rules={[{ required: true, message: '请输入显示顺序' }]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<InputNumber placeholder="请输入显示顺序" min={0} style={{ width: '100%' }} />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
2026-03-23 14:07:40 +08:00
|
|
|
|
{selectedFieldType === 'select' ? (
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="options"
|
|
|
|
|
|
label={<span style={formLabelStyle}>选项配置</span>}
|
|
|
|
|
|
tooltip="为下拉选择类型添加选项,值(value)用于提交数据,标签(label)用于显示"
|
|
|
|
|
|
>
|
|
|
|
|
|
<OptionsEditor />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Form.Item
|
|
|
|
|
|
name="options"
|
|
|
|
|
|
label={<span style={formLabelStyle}>选项配置</span>}
|
|
|
|
|
|
tooltip="仅下拉选择类型需要配置选项"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Input.TextArea
|
|
|
|
|
|
rows={2}
|
|
|
|
|
|
placeholder="仅下拉选择类型需要配置,此处不可编辑"
|
|
|
|
|
|
disabled
|
|
|
|
|
|
style={{ background: '#f5f5f5' }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
)}
|
2025-12-18 10:45:13 +08:00
|
|
|
|
|
2026-01-20 14:31:20 +08:00
|
|
|
|
<Form.Item style={formActionsStyle}>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
<Space>
|
|
|
|
|
|
<Button onClick={handleCancel}>取消</Button>
|
2026-02-10 11:04:01 +08:00
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
|
|
确定
|
|
|
|
|
|
</Button>
|
2025-12-18 10:45:13 +08:00
|
|
|
|
</Space>
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
</Form>
|
|
|
|
|
|
</Modal>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 11:28:53 +08:00
|
|
|
|
export default DeviceFieldManagement;
|