|
|
1
|
+<script lang="ts" setup>
|
|
|
2
|
+ import { Card } from 'ant-design-vue';
|
|
|
3
|
+ import { nextTick, onBeforeUpdate, onUpdated } from 'vue';
|
|
|
4
|
+ import { DataTypeEnum } from '../StructForm/config';
|
|
|
5
|
+ import { BasicCreateFormParams } from './type';
|
|
|
6
|
+ import { DynamicProps } from '/#/utils';
|
|
|
7
|
+ import { Specs, StructJSON } from '/@/api/device/model/modelOfMatterModel';
|
|
|
8
|
+ import { BasicForm, FormProps, FormSchema, useForm } from '/@/components/Form';
|
|
|
9
|
+
|
|
|
10
|
+ const props = withDefaults(
|
|
|
11
|
+ defineProps<{
|
|
|
12
|
+ inputData?: StructJSON[];
|
|
|
13
|
+ formProps?: FormProps;
|
|
|
14
|
+ value?: Recordable;
|
|
|
15
|
+ }>(),
|
|
|
16
|
+ {
|
|
|
17
|
+ inputData: () => [] as StructJSON[],
|
|
|
18
|
+ formProps: () => ({}),
|
|
|
19
|
+ value: () => ({}),
|
|
|
20
|
+ }
|
|
|
21
|
+ );
|
|
|
22
|
+
|
|
|
23
|
+ const emit = defineEmits(['update:value']);
|
|
|
24
|
+
|
|
|
25
|
+ const [register, { setProps, getFieldsValue, setFieldsValue }] = useForm({
|
|
|
26
|
+ schemas: [],
|
|
|
27
|
+ showActionButtonGroup: false,
|
|
|
28
|
+ layout: 'inline',
|
|
|
29
|
+ labelWidth: 80,
|
|
|
30
|
+ wrapperCol: { span: 12 },
|
|
|
31
|
+ ...(props.formProps || ({} as unknown as Partial<DynamicProps<FormProps>>)),
|
|
|
32
|
+ });
|
|
|
33
|
+
|
|
|
34
|
+ const syncValue = (key: string, value: any) => {
|
|
|
35
|
+ const record = getFieldsValue();
|
|
|
36
|
+ emit('update:value', { ...record, [key]: value });
|
|
|
37
|
+ };
|
|
|
38
|
+
|
|
|
39
|
+ const createInputNumber = ({
|
|
|
40
|
+ identifier,
|
|
|
41
|
+ functionName,
|
|
|
42
|
+ specs,
|
|
|
43
|
+ }: BasicCreateFormParams): FormSchema => {
|
|
|
44
|
+ const { valueRange } = specs! as Partial<Specs>;
|
|
|
45
|
+ const { max = 2147483647, min = -2147483648 } = valueRange || {};
|
|
|
46
|
+ return {
|
|
|
47
|
+ field: identifier,
|
|
|
48
|
+ label: functionName,
|
|
|
49
|
+ component: 'InputNumber',
|
|
|
50
|
+ rules: [
|
|
|
51
|
+ {
|
|
|
52
|
+ type: 'number',
|
|
|
53
|
+ trigger: 'change',
|
|
|
54
|
+ validator: (_rule, value) => {
|
|
|
55
|
+ if (value < min || value > max) {
|
|
|
56
|
+ return Promise.reject(`${functionName}取值范围在${min}~${max}之间`);
|
|
|
57
|
+ }
|
|
|
58
|
+ return Promise.resolve(value);
|
|
|
59
|
+ },
|
|
|
60
|
+ },
|
|
|
61
|
+ ],
|
|
|
62
|
+ componentProps: {
|
|
|
63
|
+ max,
|
|
|
64
|
+ min,
|
|
|
65
|
+ // step: step,
|
|
|
66
|
+ // formatter: (value: string) => value,
|
|
|
67
|
+ // parser: (string: string) => {
|
|
|
68
|
+ // if (dataType === DataTypeEnum.IS_NUMBER_INT) {
|
|
|
69
|
+ // return Number(Number(string).toFixed());
|
|
|
70
|
+ // }
|
|
|
71
|
+ // return Number(string);
|
|
|
72
|
+ // },
|
|
|
73
|
+ onChange: (value: string) => {
|
|
|
74
|
+ syncValue(identifier, value);
|
|
|
75
|
+ },
|
|
|
76
|
+ },
|
|
|
77
|
+ } as FormSchema;
|
|
|
78
|
+ };
|
|
|
79
|
+
|
|
|
80
|
+ const createInput = ({ identifier, functionName, specs }: BasicCreateFormParams): FormSchema => {
|
|
|
81
|
+ const { length = 10240 } = specs! as Partial<Specs>;
|
|
|
82
|
+ return {
|
|
|
83
|
+ field: identifier,
|
|
|
84
|
+ label: functionName,
|
|
|
85
|
+ component: 'Input',
|
|
|
86
|
+ rules: [
|
|
|
87
|
+ {
|
|
|
88
|
+ type: 'string',
|
|
|
89
|
+ trigger: 'change',
|
|
|
90
|
+ validator: (_rule, value) => {
|
|
|
91
|
+ if (value.length > length) {
|
|
|
92
|
+ return Promise.reject(`${functionName}数据长度应该小于${length}`);
|
|
|
93
|
+ }
|
|
|
94
|
+ return Promise.resolve(value);
|
|
|
95
|
+ },
|
|
|
96
|
+ },
|
|
|
97
|
+ ],
|
|
|
98
|
+ componentProps: {
|
|
|
99
|
+ maxLength: length,
|
|
|
100
|
+ onChange: (value: InputEvent) => {
|
|
|
101
|
+ syncValue(identifier, (value.target as HTMLInputElement).value);
|
|
|
102
|
+ },
|
|
|
103
|
+ },
|
|
|
104
|
+ } as FormSchema;
|
|
|
105
|
+ };
|
|
|
106
|
+
|
|
|
107
|
+ const createSelect = ({ identifier, functionName, specs }: BasicCreateFormParams): FormSchema => {
|
|
|
108
|
+ const { boolClose, boolOpen } = specs! as Partial<Specs>;
|
|
|
109
|
+ return {
|
|
|
110
|
+ field: identifier,
|
|
|
111
|
+ label: functionName,
|
|
|
112
|
+ component: 'Select',
|
|
|
113
|
+ componentProps: {
|
|
|
114
|
+ options: [
|
|
|
115
|
+ { label: `${boolClose}-0`, value: 0 },
|
|
|
116
|
+ { label: `${boolOpen}-0`, value: 1 },
|
|
|
117
|
+ ],
|
|
|
118
|
+ onChange: (value: string) => {
|
|
|
119
|
+ syncValue(identifier, value);
|
|
|
120
|
+ },
|
|
|
121
|
+ },
|
|
|
122
|
+ };
|
|
|
123
|
+ };
|
|
|
124
|
+
|
|
|
125
|
+ const createInputJson = ({ identifier, functionName }: BasicCreateFormParams): FormSchema => {
|
|
|
126
|
+ return {
|
|
|
127
|
+ field: identifier,
|
|
|
128
|
+ label: functionName,
|
|
|
129
|
+ component: 'InputTextArea',
|
|
|
130
|
+ componentProps: {
|
|
|
131
|
+ onChange: (value: InputEvent) => {
|
|
|
132
|
+ syncValue(identifier, (value.target as HTMLInputElement).value);
|
|
|
133
|
+ },
|
|
|
134
|
+ },
|
|
|
135
|
+ };
|
|
|
136
|
+ };
|
|
|
137
|
+
|
|
|
138
|
+ const transformToFormSchema = (inputData: StructJSON[]) => {
|
|
|
139
|
+ const schemas: FormSchema[] = [];
|
|
|
140
|
+ for (const item of inputData) {
|
|
|
141
|
+ const { dataType, identifier, functionName } = item;
|
|
|
142
|
+ const { type, specs } = dataType! || {};
|
|
|
143
|
+
|
|
|
144
|
+ const params: BasicCreateFormParams = {
|
|
|
145
|
+ identifier: identifier!,
|
|
|
146
|
+ functionName: functionName!,
|
|
|
147
|
+ dataType: dataType! as unknown as DataTypeEnum,
|
|
|
148
|
+ specs: specs as Partial<Specs>,
|
|
|
149
|
+ };
|
|
|
150
|
+ if (type === DataTypeEnum.IS_NUMBER_INT || type === DataTypeEnum.IS_NUMBER_DOUBLE) {
|
|
|
151
|
+ schemas.push(createInputNumber(params));
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ if (type === DataTypeEnum.IS_BOOL) {
|
|
|
155
|
+ schemas.push(createSelect(params));
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ if (type === DataTypeEnum.IS_STRING) {
|
|
|
159
|
+ schemas.push(createInput(params));
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ if (type === DataTypeEnum.IS_STRUCT) {
|
|
|
163
|
+ schemas.push(createInputJson(params));
|
|
|
164
|
+ }
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ return schemas;
|
|
|
168
|
+ };
|
|
|
169
|
+
|
|
|
170
|
+ onBeforeUpdate(() => {
|
|
|
171
|
+ if (props.inputData && props.inputData.length) {
|
|
|
172
|
+ const schemas = transformToFormSchema(props.inputData);
|
|
|
173
|
+ setProps({ schemas });
|
|
|
174
|
+ }
|
|
|
175
|
+ });
|
|
|
176
|
+
|
|
|
177
|
+ onUpdated(async () => {
|
|
|
178
|
+ if (props.inputData && props.inputData.length) {
|
|
|
179
|
+ await nextTick();
|
|
|
180
|
+ setFieldsValue(props.value);
|
|
|
181
|
+ }
|
|
|
182
|
+ });
|
|
|
183
|
+</script>
|
|
|
184
|
+
|
|
|
185
|
+<template>
|
|
|
186
|
+ <Card bordered class="!border-dashed !rounded-lg !border-2px">
|
|
|
187
|
+ <!-- <Alert class="!mb-4 w-32" message="服务参数配置" type="info" /> -->
|
|
|
188
|
+ <BasicForm class="object-model-validate-form" @register="register" />
|
|
|
189
|
+ </Card>
|
|
|
190
|
+</template>
|
|
|
191
|
+
|
|
|
192
|
+<style lang="less">
|
|
|
193
|
+ .object-model-validate-form {
|
|
|
194
|
+ .ant-input-number {
|
|
|
195
|
+ width: 100% !important;
|
|
|
196
|
+ }
|
|
|
197
|
+ }
|
|
|
198
|
+</style> |
...
|
...
|
|