schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
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
import { _t } from '@/libs/Language/Language'
import { injectStore } from '../../core/store'
import { WidgetNameType } from './WidgetTypeByEnum'
 
const { flowMap } = injectStore()
 
export class CreateFormItem {
  category: string = ''
  label: string = ''
  prop: string = ''
  el: any
  rules: any
  readonly: boolean = false
  tip: string = ''
  icon: string = ''
  options: any
  defaultValue: any
  clearable: boolean = true
  controlsPosition: string = ''
  step: number = 1
  min: number = 0
  propertyType: string = ''
  elementAttributes: any
  elementType: any
  type: string = ''
  placeholder: string = ''
  valueOnClear: number | any
  nullable: boolean = false
  pattern: string = ''
  rows: string | number = 10
  constructor(item: any, currentNode: string) {
    this.init(item, currentNode)
  }
 
  init(item: any, currentNode: string) {
    const el =
      WidgetNameType[item.propertyType]?.el ||
      WidgetNameType[item.propertyType]?.type
    this.category = item.category
    this.label = item.name
    this.prop = item.propertyKey
    this.el = el
    this.readonly = item.readonly
    this.tip = item.description
    this.icon = 'wen'
    this.options = item.options
    this.defaultValue = item.propertyValue
    this.clearable = item.nullable ? true : false
    this.controlsPosition = 'right'
    this.valueOnClear = item.nullable ? undefined : item.propertyValue
    this.step = 1
    this.nullable = item.nullable
    this.min = WidgetNameType[item.propertyType]?.type?.min ?? undefined
    this.propertyType = item.propertyType
    this.elementAttributes = item?.elementAttributes
    this.elementType = item.elementType
    this.placeholder = item.placeholder
    this.type = WidgetNameType[item.propertyType]?.el
      ? WidgetNameType[item.propertyType]?.type
      : 'select'
    this.rules = this.getRules(item.propertyKey, currentNode)
    this.rows = item.rows || WidgetNameType[item.propertyType]?.rows
 
    const requiredRule = {
      required: true,
      message: this.options.length ? _t('请选择') : _t('请输入') + item.name,
      trigger: 'change',
    }
    if (item.required && !this.rules) {
      this.rules = [requiredRule]
    }
    if (item.pattern) {
      this.rules.push({
        pattern: new RegExp(item.pattern),
        message: item.ruleMessage || _t('格式错误'),
        trigger: 'change',
      })
    }
  }
 
  getRules(key: string, currentNode: string) {
    // 根据实际情况实现获取规则的逻辑
    const ruleMap: Record<string, any> = {
      Name: [
        {
          required: true,
          message: _t('请输入步骤名称'),
          trigger: 'change',
        },
 
        {
          validator: (rule: any, value: string, callback: any) => {
            let count = 0
            let isSameValue = false
            flowMap.forEach((item: any) => {
              if (item?.properties?.name === value) {
                if (currentNode !== item.id) {
                  count++
                  if (count >= 1) {
                    return (isSameValue = true)
                  }
                }
              }
            })
 
            if (isSameValue) {
              callback(_t('节点名称重复,请检查后重试'))
            } else {
              callback()
            }
          },
          trigger: 'blur',
        },
      ],
    }
    return ruleMap[key]
  }
}