matrix_components 2.0.366 → 2.0.367

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,557 @@
1
+ <template>
2
+ <div class="demo-container">
3
+ <div class="control-panel">
4
+ <h3>动态表单组件演示</h3>
5
+ <!-- 配置文件选择 -->
6
+ <el-form :model="state" ref="formRef" label-position="left">
7
+ <el-button type="primary" @click="getFormData">获取表单数据</el-button>
8
+ <el-button type="default" @click="resetFormData">重置表单</el-button>
9
+ <br />
10
+ <span style="color: red"> 结果:{{ state.formData }} </span>
11
+ <br />
12
+ <br />
13
+ <NsFormTitle title="模型参数">
14
+ <NsForm
15
+ ref="row1Ref"
16
+ :readOnly="state.readOnly"
17
+ backgroundColor="#fff"
18
+ :model="state.model"
19
+ :rows="state.rows"
20
+ formPropKey="rows"
21
+ labelColor="#606266"
22
+ labelWidth="150"
23
+ gapH="20px"
24
+ gapV="10px"
25
+ ></NsForm>
26
+ </NsFormTitle>
27
+ <NsFormTitle title="视频配置">
28
+ <NsForm
29
+ ref="row2Ref"
30
+ :readOnly="state.readOnly"
31
+ backgroundColor="#fff"
32
+ :model="state.model"
33
+ :rows="state.rows2"
34
+ formPropKey="rows2"
35
+ labelColor="#606266"
36
+ labelWidth="150"
37
+ gapH="20px"
38
+ gapV="10px"
39
+ ></NsForm>
40
+ </NsFormTitle>
41
+ <NsFormTitle title="结果保存">
42
+ <NsForm
43
+ ref="row3Ref"
44
+ :readOnly="state.readOnly"
45
+ backgroundColor="#fff"
46
+ :model="state.model"
47
+ :rows="state.rows3"
48
+ formPropKey="rows3"
49
+ labelColor="#606266"
50
+ labelWidth="150"
51
+ gapH="20px"
52
+ gapV="10px"
53
+ ></NsForm>
54
+ </NsFormTitle>
55
+ </el-form>
56
+ </div>
57
+ </div>
58
+ </template>
59
+
60
+ <script setup lang="ts">
61
+ import { ElInput, ElMessage, ElRadioGroup } from 'element-plus'
62
+ import { reactive, ref } from 'vue'
63
+ import { cloneDeep } from 'lodash-es'
64
+ import { nextTick } from 'vue'
65
+
66
+ const props = defineProps({
67
+ readOnly: {
68
+ type: Boolean,
69
+ default: false,
70
+ },
71
+ row: {
72
+ type: Object,
73
+ default: () => ({}),
74
+ },
75
+ })
76
+
77
+ const formRef = ref()
78
+ const row1Ref = ref()
79
+ const row2Ref = ref()
80
+ const row3Ref = ref()
81
+
82
+ const state = reactive({
83
+ formData: {},
84
+ readOnly: props.readOnly,
85
+ model: props.readOnly ? '' : 'vertical',
86
+ // 使用 cloneDeep 进行深拷贝,保留函数
87
+ rows: [
88
+ [
89
+ {
90
+ key: 'confidence',
91
+ label: '置信度',
92
+ value: 'aaa',
93
+ component: ElInput,
94
+ params: {
95
+ rules: [
96
+ {
97
+ required: true,
98
+ message: '请输入',
99
+ },
100
+ ],
101
+ },
102
+ },
103
+ {
104
+ key: 'iou',
105
+ label: '交并比',
106
+ value: '',
107
+ component: ElInput,
108
+ params: {
109
+ 'v-length.range': {
110
+ min: 0,
111
+ max: 1,
112
+ maxLength: 3,
113
+ },
114
+ rules: [
115
+ {
116
+ required: true,
117
+ message: '请输入',
118
+ },
119
+ ],
120
+ },
121
+ },
122
+ ],
123
+ ],
124
+ rows2: [
125
+ [
126
+ {
127
+ key: 'timeInterval',
128
+ label: '时间间隔(秒)',
129
+ value: '',
130
+ component: ElInput,
131
+ params: {
132
+ 'v-length.range': {
133
+ min: 0,
134
+ max: 6000,
135
+ int: true,
136
+ },
137
+ rules: [
138
+ {
139
+ required: true,
140
+ message: '请输入',
141
+ },
142
+ ],
143
+ },
144
+ },
145
+ {
146
+ key: 'stuck_threshold',
147
+ label: '视频帧卡顿(秒)',
148
+ value: '',
149
+ component: ElInput,
150
+ params: {
151
+ 'v-length.range': {
152
+ min: 0,
153
+ max: 1000,
154
+ int: true,
155
+ },
156
+ rules: [
157
+ {
158
+ required: true,
159
+ message: '请输入',
160
+ },
161
+ ],
162
+ },
163
+ },
164
+ ],
165
+ [
166
+ {
167
+ key: 'max_retries',
168
+ label: '最大重连次数',
169
+ value: '',
170
+ component: ElInput,
171
+ params: {
172
+ 'v-length.range': {
173
+ min: 0,
174
+ max: 100,
175
+ int: true,
176
+ },
177
+ rules: [
178
+ {
179
+ required: true,
180
+ message: '请输入',
181
+ },
182
+ ],
183
+ },
184
+ },
185
+ {
186
+ value: ' ',
187
+ },
188
+ ],
189
+ ],
190
+ rows3: [
191
+ [
192
+ {
193
+ key: 'save_video',
194
+ label: '是否保存视频',
195
+ value: '1',
196
+ component: ElRadioGroup,
197
+ params: {
198
+ rules: [
199
+ {
200
+ required: true,
201
+ message: '请选择',
202
+ trigger: 'change',
203
+ },
204
+ ],
205
+ options: [
206
+ {
207
+ value: '1',
208
+ label: '是',
209
+ },
210
+ {
211
+ value: '0',
212
+ label: '否',
213
+ },
214
+ ],
215
+ },
216
+ },
217
+ {
218
+ key: 'pre_buffer_second',
219
+ label: '帧前缓存(秒)',
220
+ value: '',
221
+ component: ElInput,
222
+ params: {
223
+ 'v-length.range': {
224
+ min: 0,
225
+ max: 1000,
226
+ int: true,
227
+ },
228
+ rules: [
229
+ {
230
+ required: true,
231
+ message: '请输入',
232
+ },
233
+ ],
234
+ },
235
+ },
236
+ ],
237
+ [
238
+ {
239
+ key: 'det_area_mode',
240
+ label: '检测区域工作模式',
241
+ value: 'normal',
242
+ component: ElRadioGroup,
243
+ events: {
244
+ change: detAreaModeChange,
245
+ },
246
+ params: {
247
+ rules: [
248
+ {
249
+ required: true,
250
+ message: '请选择',
251
+ trigger: 'change',
252
+ },
253
+ ],
254
+ options: [
255
+ {
256
+ value: 'normal',
257
+ label: '常规检测(normal)',
258
+ },
259
+ {
260
+ value: 'abnormal',
261
+ label: '非常规检测(abnormal)',
262
+ },
263
+ ],
264
+ },
265
+ },
266
+ ],
267
+ ],
268
+ })
269
+
270
+ function detAreaModeChange(value: any) {
271
+ if (value === 'abnormal') {
272
+ if (state.rows3?.length && state.rows3[state.rows3.length - 1]?.[0]?.key === 'det_area_json') {
273
+ state.rows3.pop()
274
+ }
275
+ state.rows3.push([
276
+ {
277
+ key: 'det_area_json',
278
+ label: '感兴趣区域',
279
+ value: '',
280
+ component: ElInput,
281
+ span: 6,
282
+ events: {
283
+ change: (value) => getAreasHandler(value),
284
+ },
285
+ params: {
286
+ rules: [
287
+ {
288
+ required: true,
289
+ message: '请输入',
290
+ trigger: 'change',
291
+ },
292
+ ],
293
+ },
294
+ },
295
+ { value: ' ' },
296
+ ])
297
+ } else {
298
+ state.rows3.pop()
299
+ }
300
+ }
301
+
302
+ function getAreasHandler(value: any) {
303
+ state.rows3[state.rows3.length - 1][0].value = value
304
+ }
305
+
306
+ /**
307
+ * 保存
308
+ */
309
+ async function getFormData() {
310
+ try {
311
+ await formRef.value.validate()
312
+ } catch (error: any) {
313
+ console.log(error)
314
+ ElMessage.error('表单校验失败')
315
+ state.formData = {}
316
+ return false
317
+ }
318
+ const data1 = row1Ref.value?.getKeyValuePairs?.(state.rows)
319
+ const data2 = row2Ref.value?.getKeyValuePairs?.(state.rows2)
320
+ const data3 = row3Ref.value?.getKeyValuePairs?.(state.rows3)
321
+ const data = { ...data1, ...data2, ...data3 }
322
+ state.formData = data
323
+ ElMessage.success('表单校验成功')
324
+ return data
325
+ }
326
+
327
+ /**
328
+ * 重置表单
329
+ */
330
+ async function resetFormData() {
331
+ // 使用组件内置的 resetForm 方法
332
+ row1Ref.value?.resetForm?.()
333
+ row2Ref.value?.resetForm?.()
334
+ row3Ref.value?.resetForm?.()
335
+ // 重置表单验证状态
336
+ formRef.value?.clearValidate?.()
337
+ // 清空结果显示
338
+ state.formData = {}
339
+ ElMessage.success('表单重置成功')
340
+ }
341
+
342
+ /**
343
+ * 重置表单数据到指定值或默认值
344
+ * @param targetRows 要重置的rows数据
345
+ * @param defaultValues 默认值对象,key为字段key,value为默认值
346
+ */
347
+ function resetFormToDefaults(targetRows: any[], defaultValues: Record<string, any> = {}) {
348
+ for (let rowIndex = 0; rowIndex < targetRows.length; rowIndex++) {
349
+ const row = targetRows[rowIndex]
350
+ for (let colIndex = 0; colIndex < row.length; colIndex++) {
351
+ const item = row[colIndex]
352
+ if (item.key) {
353
+ // 如果提供了默认值,使用默认值,否则根据组件类型重置
354
+ if (defaultValues.hasOwnProperty(item.key)) {
355
+ item.value = cloneDeep(defaultValues[item.key])
356
+ } else {
357
+ // 根据组件类型或当前值类型设置默认值
358
+ if (Array.isArray(item.value)) {
359
+ item.value = []
360
+ } else if (typeof item.value === 'number') {
361
+ item.value = 0
362
+ } else if (typeof item.value === 'boolean') {
363
+ item.value = false
364
+ } else {
365
+ item.value = ''
366
+ }
367
+ }
368
+
369
+ // 重置删除的文件列表
370
+ if (item.delValue !== undefined) {
371
+ item.delValue = defaultValues[`${item.key}_delValue`]
372
+ ? cloneDeep(defaultValues[`${item.key}_delValue`])
373
+ : []
374
+ }
375
+ }
376
+
377
+ // 检查子项
378
+ if (item.children && Array.isArray(item.children)) {
379
+ for (let childIndex = 0; childIndex < item.children.length; childIndex++) {
380
+ const child = item.children[childIndex]
381
+ if (child.key) {
382
+ // 重置子项
383
+ if (defaultValues.hasOwnProperty(child.key)) {
384
+ child.value = cloneDeep(defaultValues[child.key])
385
+ } else {
386
+ if (Array.isArray(child.value)) {
387
+ child.value = []
388
+ } else if (typeof child.value === 'number') {
389
+ child.value = 0
390
+ } else if (typeof child.value === 'boolean') {
391
+ child.value = false
392
+ } else {
393
+ child.value = ''
394
+ }
395
+ }
396
+
397
+ // 重置子项删除的文件列表
398
+ if (child.delValue !== undefined) {
399
+ child.delValue = defaultValues[`${child.key}_delValue`]
400
+ ? cloneDeep(defaultValues[`${child.key}_delValue`])
401
+ : []
402
+ }
403
+ }
404
+ }
405
+ }
406
+ }
407
+ }
408
+ }
409
+
410
+ /**
411
+ * 批量设置表单值
412
+ * @param targetRows 要设置的rows数据
413
+ * @param values 值对象,key为字段key,value为要设置的值
414
+ */
415
+ function setFormValues(targetRows: any[], values: Record<string, any> = {}) {
416
+ for (let rowIndex = 0; rowIndex < targetRows.length; rowIndex++) {
417
+ const row = targetRows[rowIndex]
418
+ for (let colIndex = 0; colIndex < row.length; colIndex++) {
419
+ const item = row[colIndex]
420
+ if (item.key && values.hasOwnProperty(item.key)) {
421
+ item.value = cloneDeep(values[item.key])
422
+ }
423
+
424
+ // 检查子项
425
+ if (item.children && Array.isArray(item.children)) {
426
+ for (let childIndex = 0; childIndex < item.children.length; childIndex++) {
427
+ const child = item.children[childIndex]
428
+ if (child.key && values.hasOwnProperty(child.key)) {
429
+ child.value = cloneDeep(values[child.key])
430
+ }
431
+ }
432
+ }
433
+ }
434
+ }
435
+ }
436
+
437
+ /**
438
+ * 重置所有表单到默认值(新的重置方法)
439
+ * @param customDefaults 自定义默认值
440
+ */
441
+ function resetAllFormsToDefaults(customDefaults: Record<string, any> = {}) {
442
+ // 重置第一个表单
443
+ resetFormToDefaults(state.rows, customDefaults)
444
+
445
+ // 重置第二个表单
446
+ resetFormToDefaults(state.rows2, customDefaults)
447
+
448
+ // 重置第三个表单
449
+ resetFormToDefaults(state.rows3, customDefaults)
450
+
451
+ // 重置表单验证状态
452
+ formRef.value?.clearValidate?.()
453
+
454
+ // 清空结果显示
455
+ state.formData = {}
456
+
457
+ ElMessage.success('表单重置到默认值成功')
458
+ }
459
+ </script>
460
+
461
+ <style lang="scss" scoped>
462
+ .demo-container {
463
+ padding: 20px;
464
+ max-width: 1400px;
465
+ margin: 0 auto;
466
+ }
467
+
468
+ .control-panel {
469
+ background: #f5f5f5;
470
+ padding: 20px;
471
+ border-radius: 8px;
472
+ margin-bottom: 20px;
473
+
474
+ h3 {
475
+ margin: 0 0 20px 0;
476
+ color: #333;
477
+ }
478
+
479
+ h4 {
480
+ margin: 15px 0 10px 0;
481
+ color: #666;
482
+ font-size: 14px;
483
+ }
484
+ }
485
+
486
+ .config-section,
487
+ .upload-section,
488
+ .action-section {
489
+ margin-bottom: 20px;
490
+ padding: 15px;
491
+ background: white;
492
+ border-radius: 6px;
493
+ border: 1px solid #e0e0e0;
494
+
495
+ .config-select {
496
+ width: 300px;
497
+ margin-right: 10px;
498
+ }
499
+
500
+ input[type='file'] {
501
+ margin-right: 10px;
502
+ }
503
+
504
+ .el-button {
505
+ margin-right: 10px;
506
+ margin-bottom: 5px;
507
+ }
508
+ }
509
+
510
+ .form-container {
511
+ background: white;
512
+ padding: 20px;
513
+ border-radius: 8px;
514
+ border: 1px solid #e0e0e0;
515
+ margin-bottom: 20px;
516
+ }
517
+
518
+ .empty-state {
519
+ text-align: center;
520
+ padding: 60px 20px;
521
+ background: white;
522
+ border-radius: 8px;
523
+ border: 1px solid #e0e0e0;
524
+ }
525
+
526
+ .data-card {
527
+ margin-top: 20px;
528
+
529
+ .card-header {
530
+ display: flex;
531
+ justify-content: space-between;
532
+ align-items: center;
533
+ }
534
+
535
+ .form-data-display {
536
+ background-color: #f5f7fa;
537
+ padding: 15px;
538
+ border-radius: 4px;
539
+ font-size: 12px;
540
+ line-height: 1.5;
541
+ max-height: 400px;
542
+ overflow-y: auto;
543
+ }
544
+ }
545
+
546
+ @media (max-width: 768px) {
547
+ .config-select {
548
+ width: 100% !important;
549
+ margin-bottom: 10px;
550
+ }
551
+
552
+ .el-button {
553
+ width: 100%;
554
+ margin-bottom: 10px;
555
+ }
556
+ }
557
+ </style>