zs
2025-04-28 1f32ea02c1910c417f159cba81a296e66ae7484c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { computed, defineComponent, ref } from 'vue'
import sdk from 'sdk'
import styles from './Variable.module.scss'
import Icon from '../Icon/Icon'
import { Base } from '@/libs/Base/Base'
import { useVModels } from '@vueuse/core'
const { openVariableDialog } = sdk.utils
 
interface CurrentVariableType {
  id?: string
  name?: string
}
 
export default defineComponent({
  name: '变量',
  props: {
    modelValue: {
      type: [Number, String],
      default: '',
    },
    isClose: {
      type: Boolean,
      default: false,
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    // 以下属性用来多选
    dataSource: {
      type: Array,
      default: () => [],
    },
    isMultiple: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: 0,
    },
    field: {
      type: String,
      default: '',
    },
    type: {
      type: String,
      default: '',
    },
  },
  emits: ['update:modelValue', 'update:dataSource', 'change'],
  setup(props, { attrs, slots, emit }) {
    const elementType = {
      input: 'input',
      select: 'select',
    }
    const variable = computed({
      get: () => {
        return props.modelValue === null ? '' : String(props.modelValue)
      },
      set: (val) => emit('update:modelValue', val),
    })
 
    const { dataSource } = useVModels(props, emit)
 
    /**
     * 多选
     */
    const onMultipleSelectVariable = (varData: any[]) => {
      if (props.field) {
        const index = props.index
        varData.forEach((variable, i: number) => {
          const row: any = dataSource.value[index + i]
          if (row) {
            if (
              typeof row[props.field] !== 'object' ||
              row[props.field] === null
            ) {
              row[props.field] = variable.name
            }
          }
        })
      }
    }
 
    const onSelectVariable = async () => {
      const currentVariable: CurrentVariableType = {}
      if (variable.value) {
        currentVariable.id = Base.getVariableIdByName(variable.value)
        currentVariable.name = variable.value
      }
      try {
        const varData = await openVariableDialog({
          currentVariable,
          isMultiple: props.isMultiple,
          defaultCheckKey: [],
          showConfig: false,
          configData: {},
        })
        if (!props.isMultiple) {
          variable.value = varData.name
          emit('change', varData.name)
        } else {
          onMultipleSelectVariable(varData)
        }
      } catch (error) {
        console.log(error)
      }
    }
    const onClear = () => {
      variable.value = ''
    }
    return () => {
      const type = props.type || attrs.type
      if (type === elementType.select) {
        return (
          <el-input
            v-model={variable.value}
            {...attrs}
            clearable={props.clearable || props.isClose}
            readonly={props.isClose || props.clearable}
            class={styles.selectVariable}
            placeholder="请输入"
            suffix-icon={
              <el-button
                link
                type="primary"
                size="small"
                style="margin-right: 10px;"
                onClick={onSelectVariable}
              >
                选择
              </el-button>
            }
          ></el-input>
        )
      }
      if (type === elementType.input) {
        return (
          <el-input
            v-model={variable.value}
            onClick={onSelectVariable}
            {...attrs}
            clearable={props.clearable || props.isClose}
            readonly={props.isClose || props.clearable}
            suffix-icon={
              attrs.disabled ? null : (
                <Icon
                  onClick={onClear}
                  style="cursor: pointer"
                  icon="close_x"
                ></Icon>
              )
            }
          ></el-input>
        )
      }
      return (
        <div class={styles.variable}>
          {variable.value ? (
            <div class={styles.content} onClick={onSelectVariable}>
              <span title={variable.value} class={styles.text}>
                {variable.value}
              </span>
              {props.isClose ? (
                <Icon
                  width={16}
                  height={16}
                  icon="close_x"
                  onClick={() => (variable.value = '')}
                />
              ) : null}
            </div>
          ) : (
            <span onClick={onSelectVariable} class={styles.select}>
              请选择
            </span>
          )}
        </div>
      )
    }
  },
})