gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<script setup lang="ts">
import type { BpmCategoryApi } from '#/api/bpm/category';
import type { BpmProcessDefinitionApi } from '#/api/bpm/definition';
import type { BpmFormApi } from '#/api/bpm/form';
import type { SystemDeptApi } from '#/api/system/dept';
import type { SystemUserApi } from '#/api/system/user';
 
import { onBeforeUnmount, onMounted, provide, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
 
import { confirm, Page } from '..\..\..\..\packages\effects\common-ui\src';
import {
  BpmAutoApproveType,
  BpmModelFormType,
  BpmModelType,
} from '..\..\..\..\packages\constants\src';
import { useTabs } from '..\..\..\..\packages\effects\hooks\src';
import { IconifyIcon } from '..\..\..\..\packages\icons\src';
import { useUserStore } from '..\..\..\..\packages\stores\src';
 
import { Button, Card, message } from 'ant-design-vue';
 
import { getCategorySimpleList } from '#/api/bpm/category';
import { getProcessDefinition } from '#/api/bpm/definition';
import { getFormSimpleList } from '#/api/bpm/form';
import {
  createModel,
  deployModel,
  getModel,
  updateModel,
} from '#/api/bpm/model';
import { getSimpleDeptList } from '#/api/system/dept';
import { getSimpleUserList } from '#/api/system/user';
 
import BasicInfo from './modules/basic-info.vue';
import ExtraSetting from './modules/extra-setting.vue';
import FormDesign from './modules/form-design.vue';
import ProcessDesign from './modules/process-design.vue';
 
defineOptions({ name: 'BpmModelCreate' });
 
type BpmProcessDefinitionType = Omit<
  BpmProcessDefinitionApi.ProcessDefinition,
  'modelId' | 'modelType'
> & {
  id?: string;
  type?: number;
};
 
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const tabs = useTabs();
 
const basicInfoRef = ref<InstanceType<typeof BasicInfo>>(); // 基础信息组件引用
const formDesignRef = ref<InstanceType<typeof FormDesign>>(); // 表单设计组件引用
const processDesignRef = ref<InstanceType<typeof ProcessDesign>>(); // 流程设计组件引用
const extraSettingRef = ref<InstanceType<typeof ExtraSetting>>(); // 更多设置组件引用
 
const actionType = route.params.type as string; // 操作类型:create、copy、update
const currentStep = ref(-1); // 步骤控制。-1 用于,一开始全部不展示等当前页面数据初始化完成
const steps = [
  { title: '基本信息', validator: validateBasic },
  { title: '表单设计', validator: validateForm },
  { title: '流程设计', validator: validateProcess },
  { title: '更多设置', validator: validateExtra },
];
 
const formData: any = ref({
  id: undefined,
  name: '',
  key: '',
  category: undefined,
  icon: undefined,
  description: '',
  type: BpmModelType.SIMPLE,
  formType: BpmModelFormType.NORMAL,
  formId: '',
  formCustomCreatePath: '',
  formCustomViewPath: '',
  visible: true,
  startUserType: undefined,
  startUserIds: [],
  startDeptIds: [],
  managerUserIds: [],
  allowCancelRunningProcess: true,
  processIdRule: {
    enable: false,
    prefix: '',
    infix: '',
    postfix: '',
    length: 5,
  },
  autoApprovalType: BpmAutoApproveType.NONE,
  titleSetting: {
    enable: false,
    title: '',
  },
  summarySetting: {
    enable: false,
    summary: [],
  },
  allowWithdrawTask: false,
}); // 表单数据
const processData = ref<any>(); // 流程数据
 
const formList = ref<BpmFormApi.Form[]>([]);
const categoryList = ref<BpmCategoryApi.Category[]>([]);
const userList = ref<SystemUserApi.User[]>([]);
const deptList = ref<SystemDeptApi.Dept[]>([]);
 
provide('processData', processData);
provide('modelData', formData);
 
/** 步骤校验函数 */
async function validateBasic() {
  await basicInfoRef.value?.validate();
}
 
/** 表单设计校验 */
async function validateForm() {
  await formDesignRef.value?.validate();
}
 
/** 流程设计校验 */
async function validateProcess() {
  await processDesignRef.value?.validate();
}
 
/** 更多设置校验 */
async function validateExtra() {
  await extraSettingRef.value?.validate();
}
 
/** 初始化数据 */
async function initData() {
  if (actionType === 'definition') {
    // 情况一:流程定义场景(恢复)
    const definitionId = route.params.id as string;
    const data = await getProcessDefinition(definitionId);
    const processDefinition: BpmProcessDefinitionType = data;
    // 将 definition => model
    processDefinition.type = data.modelType;
    processDefinition.id = data.modelId;
    if (data.simpleModel) {
      processDefinition.simpleModel = JSON.parse(data.simpleModel);
    }
    formData.value = processDefinition;
 
    // 设置 startUserType
    if (formData.value.startUserIds?.length > 0) {
      formData.value.startUserType = 1;
    } else if (formData.value.startDeptIds?.length > 0) {
      formData.value.startUserType = 2;
    } else {
      formData.value.startUserType = 0;
    }
  } else if (['copy', 'update'].includes(actionType)) {
    // 情况二:修改场景/复制场景
    const modelId = route.params.id as string;
    formData.value = await getModel(modelId);
 
    // 设置 startUserType
    if (formData.value.startUserIds?.length > 0) {
      formData.value.startUserType = 1;
    } else if (formData.value.startDeptIds?.length > 0) {
      formData.value.startUserType = 2;
    } else {
      formData.value.startUserType = 0;
    }
 
    // 特殊:复制场景
    if (route.params.type === 'copy') {
      delete formData.value.id;
      if (formData.value.bpmnXml) {
        formData.value.bpmnXml = formData.value.bpmnXml.replaceAll(
          formData.value.name,
          `${formData.value.name}副本`,
        );
        formData.value.bpmnXml = formData.value.bpmnXml.replaceAll(
          formData.value.key,
          `${formData.value.key}_copy`,
        );
      }
      formData.value.name += '副本';
      formData.value.key += '_copy';
    }
  } else {
    // 情况三:新增场景
    formData.value.startUserType = 0; // 全体
    formData.value.managerUserIds.push(userStore.userInfo?.id);
  }
 
  // 获取表单列表
  formList.value = await getFormSimpleList();
  categoryList.value = await getCategorySimpleList();
  // 获取用户列表
  userList.value = await getSimpleUserList();
  // 获取部门列表
  deptList.value = await getSimpleDeptList();
 
  // 最终,设置 currentStep 切换到第一步
  currentStep.value = 0;
  // 以前未配置更多设置的流程
  extraSettingRef.value?.initData();
}
 
/** 根据类型切换流程数据 */
watch(
  async () => formData.value.type,
  () => {
    if (formData.value.type === BpmModelType.BPMN) {
      processData.value = formData.value.bpmnXml;
    } else if (formData.value.type === BpmModelType.SIMPLE) {
      processData.value = formData.value.simpleModel;
    }
  },
  {
    immediate: true,
  },
);
 
/** 校验所有步骤数据是否完整 */
async function validateAllSteps() {
  // 基本信息校验
  try {
    await validateBasic();
  } catch {
    currentStep.value = 0;
    message.warning('请完善基本信息');
    return false;
  }
 
  // 表单设计校验
  try {
    await validateForm();
  } catch {
    currentStep.value = 1;
    message.warning('请完善自定义表单信息');
    return false;
  }
 
  // 流程设计校验
  try {
    await validateProcess();
  } catch {
    currentStep.value = 2;
    return false;
  }
 
  // 更多设置校验
  try {
    await validateExtra();
  } catch {
    currentStep.value = 3;
    return false;
  }
 
  return true;
}
 
const saveLoading = ref<boolean>(false);
/** 保存操作 */
async function handleSave() {
  try {
    saveLoading.value = true;
    // 保存前校验所有步骤的数据
    const result = await validateAllSteps();
    if (!result) {
      return;
    }
 
    // 更新表单数据
    const modelData = {
      ...formData.value,
    };
 
    switch (actionType) {
      case 'copy': {
        // 情况三:复制场景
        formData.value.id = await createModel(modelData);
        // 提示成功
        message.success('复制成功,可点击【发布】按钮,进行发布模型');
        break;
      }
      case 'definition': {
        // 情况一:流程定义场景(恢复)
        await updateModel(modelData);
        // 提示成功
        message.success('恢复成功,可点击【发布】按钮,进行发布模型');
        break;
      }
      case 'update': {
        // 情况二:修改场景
        await updateModel(modelData);
        // 提示成功
        message.success('修改成功,可点击【发布】按钮,进行发布模型');
        break;
      }
      default: {
        // 情况四:新增场景
        formData.value.id = await createModel(modelData);
        // 提示成功
        message.success('新建成功,可点击【发布】按钮,进行发布模型');
      }
    }
 
    // 返回列表页(排除更新的情况)
    if (actionType !== 'update') {
      await router.push({ name: 'BpmModel' });
    }
  } catch (error: any) {
    console.error('保存失败:', error);
  } finally {
    saveLoading.value = false;
  }
}
// 发布加载中状态
const deployLoading = ref<boolean>(false);
/** 发布操作 */
async function handleDeploy() {
  try {
    // 1.1 修改场景下直接发布,新增场景下需要先确认
    if (!formData.value.id) {
      await confirm('是否确认发布该流程?');
    }
    deployLoading.value = true;
    // 1.2 校验所有步骤
    await validateAllSteps();
 
    // 2.1 更新表单数据
    const modelData = {
      ...formData.value,
    };
    // 2.2 先保存所有数据
    if (formData.value.id) {
      await updateModel(modelData);
    } else {
      const result = await createModel(modelData);
      formData.value.id = result.id;
    }
    // 2.3 发布
    await deployModel(formData.value.id);
 
    // 3. 路由并提示
    message.success('发布成功');
    await router.push({ name: 'BpmModel' });
  } catch (error: any) {
    console.error('发布失败:', error);
    message.warning(error.message || '发布失败');
  } finally {
    deployLoading.value = false;
  }
}
 
/** 步骤切换处理 */
async function handleStepClick(index: number) {
  try {
    if (index !== 0) {
      await validateBasic();
    }
    if (index !== 1) {
      await validateForm();
    }
    if (index !== 2) {
      await validateProcess();
    }
    if (index !== 3) {
      await validateExtra();
    }
    // 切换步骤
    currentStep.value = index;
  } catch (error) {
    console.error('步骤切换失败:', error);
    if (currentStep.value !== 2) {
      message.warning('请先完善当前步骤必填信息');
    }
  }
}
 
/** 返回列表页 */
function handleBack() {
  tabs.closeCurrentTab();
  router.push({ name: 'BpmModel' });
}
 
/** 初始化 */
onMounted(async () => {
  await initData();
});
 
/** 添加组件卸载前的清理 */
onBeforeUnmount(() => {
  // 清理所有的引用
  basicInfoRef.value = undefined;
  formDesignRef.value = undefined;
  processDesignRef.value = undefined;
});
</script>
 
<template>
  <Page auto-content-height>
    <div class="mx-auto">
      <!-- 头部导航栏 -->
      <div
        class="absolute inset-x-0 top-0 z-10 flex h-12 items-center border-b bg-card px-5"
      >
        <!-- 左侧标题 -->
        <div class="flex w-48 items-center overflow-hidden">
          <IconifyIcon
            icon="lucide:arrow-left"
            class="size-5 flex-shrink-0 cursor-pointer"
            @click="handleBack"
          />
          <span
            class="ml-2.5 truncate text-base"
            :title="formData.name || '创建流程'"
          >
            {{ formData.name || '创建流程' }}
          </span>
        </div>
 
        <!-- 步骤条 -->
        <div class="flex h-full flex-1 items-center justify-center">
          <div class="flex h-full w-auto items-center justify-center">
            <div
              v-for="(step, index) in steps"
              :key="index"
              class="relative mx-6 flex h-full cursor-pointer items-center"
              :class="[
                currentStep === index
                  ? 'border-b-2 border-solid border-blue-500 text-blue-500'
                  : 'text-gray-500',
              ]"
              @click="handleStepClick(index)"
            >
              <div
                class="mr-2 flex h-7 w-7 items-center justify-center rounded-full border-2 border-solid text-base"
                :class="[
                  currentStep === index
                    ? 'border-blue-500 bg-blue-500 text-white'
                    : 'border-gray-300 bg-white text-gray-500',
                ]"
              >
                {{ index + 1 }}
              </div>
              <span class="whitespace-nowrap text-base font-bold">
                {{ step.title }}
              </span>
            </div>
          </div>
        </div>
 
        <!-- 右侧按钮 -->
        <div class="flex w-48 items-center justify-end gap-2">
          <Button
            v-if="actionType === 'update'"
            type="primary"
            :loading="deployLoading"
            @click="handleDeploy"
          >
            发 布
          </Button>
          <Button type="primary" @click="handleSave" :loading="saveLoading">
            <span v-if="actionType === 'definition'">恢 复</span>
            <span v-else>保 存</span>
          </Button>
        </div>
      </div>
      <!-- 主体内容 -->
      <Card :body-style="{ padding: '10px' }" class="mb-4">
        <div class="mt-12">
          <!-- 第一步:基本信息 -->
          <div v-if="currentStep === 0" class="mx-auto w-4/6">
            <BasicInfo
              v-model="formData"
              :category-list="categoryList"
              :user-list="userList"
              :dept-list="deptList"
              ref="basicInfoRef"
            />
          </div>
          <!-- 第二步:表单设计  -->
          <div v-if="currentStep === 1" class="mx-auto w-4/6">
            <FormDesign
              v-model="formData"
              :form-list="formList"
              ref="formDesignRef"
            />
          </div>
 
          <!-- 第三步:流程设计 -->
          <ProcessDesign
            v-if="currentStep === 2"
            v-model="formData"
            ref="processDesignRef"
          />
 
          <!-- 第四步:更多设置 -->
          <div v-show="currentStep === 3" class="mx-auto w-4/6">
            <ExtraSetting v-model="formData" ref="extraSettingRef" />
          </div>
        </div>
      </Card>
    </div>
  </Page>
</template>