config.ts
4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { validateTCPCustomCommand } from '.'
import type { Specs, StructJSON } from '@/api/device/model'
import { type FormSchema } from '@/components/Form'
import { ComponentEnum } from '@/components/Form/src/enum'
import { TransportTypeEnum } from '@/enums/deviceEnum'
import { DataTypeEnum } from '@/enums/objectModelEnum'
export enum FormFieldsEnum {
SERVICE_COMMAND = 'serviceCommand',
}
export const getFormSchemas = ({ structJSON: structJson, required, transportType }: { structJSON: StructJSON[]; required?: boolean; transportType?: string }): FormSchema[] => {
const createInputNumber = ({
identifier,
functionName,
dataType,
}: StructJSON): FormSchema => {
const { specs, type } = dataType || {}
const { valueRange } = specs! as Specs
const { max = Number.MAX_SAFE_INTEGER, min = Number.MIN_SAFE_INTEGER } = valueRange || {}
return {
field: identifier,
label: functionName,
component: ComponentEnum.INPUT_NUMBER,
rules: [
{
required,
message: `${functionName}是必填项`,
},
],
componentProps: {
max,
min,
precision: type === DataTypeEnum.NUMBER_INT ? 0 : 2,
},
} as FormSchema
}
const createInput = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
const { specs } = dataType || {}
const { length = 10240 } = specs! as Partial<Specs>
return {
field: identifier,
label: functionName,
component: ComponentEnum.INPUT,
rules: [
{
required,
message: `${functionName}是必填项`,
},
{
type: 'string',
trigger: 'change',
validator: (_rule, value) => {
if ((value?.length || 0) > length)
return Promise.reject(new Error(`${functionName}数据长度应该小于${length}`))
return Promise.resolve(value)
},
},
],
componentProps: {
maxLength: length,
},
} as FormSchema
}
const createSelect = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
const { specs } = dataType || {}
const { boolClose, boolOpen } = specs! as Partial<Specs>
return {
field: identifier,
label: functionName,
component: ComponentEnum.SELECT,
rules: [
{
required,
message: `${functionName}是必填项`,
type: 'number',
},
],
componentProps: {
options: [
{ label: `${boolClose}-0`, value: 0 },
{ label: `${boolOpen}-1`, value: 1 },
],
},
}
}
const createEnumSelect = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
const { specsList } = dataType || {}
return {
field: identifier,
label: functionName,
component: ComponentEnum.SELECT,
rules: [
{
required,
message: `${functionName}是必填项`,
type: 'number',
},
],
componentProps: {
options: specsList,
fieldNames: { label: 'name', value: 'value' },
},
}
}
const createStructJson = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
return {
field: identifier,
label: functionName,
component: ComponentEnum.THINGS_MODEL_FORM,
changeEvent: 'update:value',
componentProps: () => {
return {
inputData: dataType?.specs || [],
}
},
colSlot: identifier,
}
}
const createTCPServiceCommandInput = ({ serviceCommand }: StructJSON): FormSchema => {
return {
field: FormFieldsEnum.SERVICE_COMMAND,
label: '服务命令',
component: ComponentEnum.INPUT,
required,
rules: [{ validator: validateTCPCustomCommand }],
componentProps: {
defaultValue: serviceCommand,
},
}
}
const schemas: FormSchema[] = []
for (const item of structJson) {
const { dataType } = item
const { type } = dataType || {}
if (transportType === TransportTypeEnum.TCP) {
item.serviceCommand && schemas.push(createTCPServiceCommandInput(item))
break
}
if (type === DataTypeEnum.BOOL)
schemas.push(createSelect(item))
else if (type === DataTypeEnum.ENUM)
schemas.push(createEnumSelect(item))
else if (type === DataTypeEnum.NUMBER_INT)
schemas.push(createInputNumber(item))
else if (type === DataTypeEnum.NUMBER_DOUBLE)
schemas.push(createInputNumber(item))
else if (type === DataTypeEnum.STRING)
schemas.push(createInput(item))
else if (type === DataTypeEnum.STRUCT)
schemas.push(createStructJson(item))
}
return schemas
}