gaoluyang
9 天以前 86966ec9a83b93decbd93fc8ce2be1882d4c9775
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<script lang="ts" setup>
import { inject, nextTick, onMounted, ref, toRaw, watch } from 'vue';
 
import { confirm, useVbenModal } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';
 
import {
  Button,
  Divider,
  Form,
  FormItem,
  Input,
  Select,
  SelectOption,
  Switch,
} from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getModelList } from '#/api/bpm/model';
 
interface FormData {
  processInstanceName: string;
  calledElement: string;
  inheritVariables: boolean;
  businessKey: string;
  inheritBusinessKey: boolean;
  calledElementType: string;
}
 
defineOptions({ name: 'CallActivity' });
const props = defineProps({
  id: { type: String, default: '' },
  type: { type: String, default: '' },
});
const prefix = inject('prefix');
 
const formData = ref<FormData>({
  processInstanceName: '',
  calledElement: '',
  inheritVariables: false,
  businessKey: '',
  inheritBusinessKey: false,
  calledElementType: 'key',
});
const inVariableList = ref<any[]>([]);
const outVariableList = ref<any[]>([]);
const variableType = ref<string>(); // 参数类型
const editingVariableIndex = ref<number>(-1); // 编辑参数下标
const varialbeFormRef = ref();
const varialbeFormData = ref<{
  source: string;
  target: string;
}>({
  source: '',
  target: '',
});
 
const bpmnInstances = () => (window as any)?.bpmnInstances;
const bpmnElement = ref<any>();
const otherExtensionList = ref<any[]>([]);
const childProcessOptions = ref<{ key: string; name: string }[]>([]);
 
const initCallActivity = () => {
  bpmnElement.value = bpmnInstances().bpmnElement;
 
  // 初始化所有配置项
  Object.keys(formData.value).forEach((key: string) => {
    // @ts-expect-error: form state is updated through dynamic schema keys
    formData.value[key] =
      bpmnElement.value.businessObject[key] ??
      formData.value[key as keyof FormData];
  });
 
  otherExtensionList.value = []; // 其他扩展配置
  inVariableList.value.length = 0;
  outVariableList.value.length = 0;
  // 初始化输入参数
  bpmnElement.value.businessObject?.extensionElements?.values?.forEach(
    (ex: any) => {
      if (ex.$type === `${prefix}:In`) {
        inVariableList.value.push(ex);
      } else if (ex.$type === `${prefix}:Out`) {
        outVariableList.value.push(ex);
      } else {
        otherExtensionList.value.push(ex);
      }
    },
  );
};
 
const updateCallActivityAttr = (attr: keyof FormData) => {
  bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
    [attr]: formData.value[attr],
  });
};
 
const [VariableModal, variableModalApi] = useVbenModal({
  title: '参数配置',
  onConfirm: () => {
    saveVariable();
  },
});
 
const openVariableForm = (type: string, data: any, index: number) => {
  editingVariableIndex.value = index;
  variableType.value = type;
  varialbeFormData.value = index === -1 ? {} : { ...data };
  variableModalApi.open();
};
 
const removeVariable = async (type: string, index: number) => {
  try {
    await confirm({
      title: '提示',
      content: '是否确认删除?',
    });
    if (type === 'in') {
      inVariableList.value.splice(index, 1);
    }
    if (type === 'out') {
      outVariableList.value.splice(index, 1);
    }
    updateElementExtensions();
  } catch (error: any) {
    console.error(`[removeVariable error ]: ${error.message || error}`);
  }
};
 
const saveVariable = async () => {
  try {
    await varialbeFormRef.value?.validate();
  } catch {
    // 验证失败直接返回
    return;
  }
 
  if (editingVariableIndex.value === -1) {
    if (variableType.value === 'in') {
      inVariableList.value.push(
        bpmnInstances().moddle.create(`${prefix}:In`, {
          ...varialbeFormData.value,
        }),
      );
    }
    if (variableType.value === 'out') {
      outVariableList.value.push(
        bpmnInstances().moddle.create(`${prefix}:Out`, {
          ...varialbeFormData.value,
        }),
      );
    }
    updateElementExtensions();
  } else {
    if (variableType.value === 'in') {
      inVariableList.value[editingVariableIndex.value].source =
        varialbeFormData.value.source;
      inVariableList.value[editingVariableIndex.value].target =
        varialbeFormData.value.target;
    }
    if (variableType.value === 'out') {
      outVariableList.value[editingVariableIndex.value].source =
        varialbeFormData.value.source;
      outVariableList.value[editingVariableIndex.value].target =
        varialbeFormData.value.target;
    }
  }
  variableModalApi.close();
};
 
const updateElementExtensions = () => {
  const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', {
    values: [
      ...inVariableList.value,
      ...outVariableList.value,
      ...otherExtensionList.value,
    ],
  });
  bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
    extensionElements: extensions,
  });
};
 
watch(
  () => props.id,
  (val) => {
    // oxlint-disable-next-line no-unused-expressions
    val &&
      val.length > 0 &&
      nextTick(() => {
        initCallActivity();
      });
  },
  { immediate: true },
);
 
const gridOptions = {
  columns: [
    { title: '源', field: 'source', minWidth: 100 },
    { title: '目标', field: 'target', minWidth: 100 },
    {
      title: '操作',
      width: 130,
      slots: { default: 'action' },
      fixed: 'right' as const,
    },
  ],
  border: true,
  showOverflow: true,
  height: 'auto',
  toolbarConfig: { enabled: false },
  pagerConfig: { enabled: false },
};
 
const [InVariableGrid, inVariableGridApi] = useVbenVxeGrid({
  gridOptions,
});
 
const [OutVariableGrid, outVariableGridApi] = useVbenVxeGrid({
  gridOptions,
});
 
// 使用浅层监听,避免无限循环
watch(
  () => [...inVariableList.value],
  (val) => {
    inVariableGridApi.setGridOptions({ data: val });
  },
);
 
watch(
  () => [...outVariableList.value],
  (val) => {
    outVariableGridApi.setGridOptions({ data: val });
  },
);
 
/** 选择子流程, 更新 bpmn callActivity calledElement 和 processInstanceName 属性 */
const handleChildProcessChange = (key: any) => {
  if (!key) return;
  const selected = childProcessOptions.value.find((item) => item.key === key);
  if (selected) {
    formData.value.calledElement = selected.key;
    formData.value.processInstanceName = selected.name;
    updateCallActivityAttr('calledElement');
    updateCallActivityAttr('processInstanceName');
  }
};
 
onMounted(async () => {
  try {
    // 获取流程模型列表
    const list = await getModelList(undefined);
    childProcessOptions.value = list.map((item) => ({
      key: item.key,
      name: item.name,
    }));
  } catch (error) {
    console.error('获取子流程列表失败', error);
  }
});
</script>
 
<template>
  <div class="-mx-2">
    <Form :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <FormItem label="被调用子流程">
        <Select
          v-model:value="formData.calledElement"
          placeholder="请选择子流程"
          allow-clear
          @change="handleChildProcessChange"
        >
          <SelectOption
            v-for="item in childProcessOptions"
            :key="item.key"
            :value="item.key"
            :label="item.name"
          >
            {{ item.name }}
          </SelectOption>
        </Select>
      </FormItem>
 
      <FormItem label="继承变量">
        <Switch
          v-model:checked="formData.inheritVariables"
          @change="updateCallActivityAttr('inheritVariables')"
        />
      </FormItem>
 
      <FormItem label="继承业务键">
        <Switch
          v-model:checked="formData.inheritBusinessKey"
          @change="updateCallActivityAttr('inheritBusinessKey')"
        />
      </FormItem>
 
      <FormItem v-if="!formData.inheritBusinessKey" label="业务键表达式">
        <Input
          v-model:value="formData.businessKey"
          allow-clear
          placeholder="请输入业务键表达式"
          @change="updateCallActivityAttr('businessKey')"
        />
      </FormItem>
 
      <div
        class="mb-1 mt-2 flex items-center justify-between border-t border-gray-200 pt-2"
      >
        <span class="flex items-center text-sm font-medium"> 输入参数 </span>
        <Button
          class="flex items-center"
          size="small"
          type="link"
          @click="openVariableForm('in', null, -1)"
        >
          <template #icon>
            <IconifyIcon icon="ep:plus" />
          </template>
          添加参数
        </Button>
      </div>
      <InVariableGrid class="-mx-2 mb-4">
        <template #action="{ row, rowIndex }">
          <Button
            size="small"
            type="link"
            @click="openVariableForm('in', row, rowIndex)"
          >
            编辑
          </Button>
          <Divider type="vertical" />
          <Button
            size="small"
            type="link"
            danger
            @click="removeVariable('in', rowIndex)"
          >
            移除
          </Button>
        </template>
      </InVariableGrid>
 
      <div
        class="mb-1 mt-2 flex items-center justify-between border-t border-gray-200 pt-2"
      >
        <span class="flex items-center text-sm font-medium"> 输出参数 </span>
        <Button
          class="flex items-center"
          size="small"
          type="link"
          @click="openVariableForm('out', null, -1)"
        >
          <template #icon>
            <IconifyIcon icon="lucide:plus" class="size-4" />
          </template>
          添加参数
        </Button>
      </div>
      <OutVariableGrid class="-mx-2">
        <template #action="{ row, rowIndex }">
          <Button
            size="small"
            type="link"
            @click="openVariableForm('out', row, rowIndex)"
          >
            编辑
          </Button>
          <Divider type="vertical" />
          <Button
            size="small"
            type="link"
            danger
            @click="removeVariable('out', rowIndex)"
          >
            移除
          </Button>
        </template>
      </OutVariableGrid>
    </Form>
 
    <VariableModal>
      <Form
        :model="varialbeFormData"
        ref="varialbeFormRef"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 18 }"
      >
        <FormItem
          label="源"
          name="source"
          :rules="[
            {
              required: true,
              message: '源不能为空',
              trigger: ['blur', 'change'],
            },
          ]"
        >
          <Input v-model:value="varialbeFormData.source" allow-clear />
        </FormItem>
        <FormItem
          label="目标"
          name="target"
          :rules="[
            {
              required: true,
              message: '目标不能为空',
              trigger: ['blur', 'change'],
            },
          ]"
        >
          <Input v-model:value="varialbeFormData.target" allow-clear />
        </FormItem>
      </Form>
    </VariableModal>
  </div>
</template>