gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
<script lang="ts" setup>
import type { WmsApprovalApi } from '#/api/mes/wm/approval';
 
import { computed, ref, watch } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  getAvailableProcessList,
  getApprovalConfig,
  saveApprovalConfig,
} from '#/api/mes/wm/approval';
 
import { BIZ_TYPE_OPTIONS, useProcessFormSchema } from '../data';
 
const emit = defineEmits<{ success: [] }>();
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  layout: 'horizontal',
  schema: useProcessFormSchema(),
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const values = await formApi.getValues();
    if (!values.bizType) {
      message.error('请选择业务类型');
      return;
    }
    if (!values.processDefinitionKey) {
      message.error('请选择审批流程');
      return;
    }
 
    await saveApprovalConfig({
      warehouseId: values.warehouseId,
      bizType: values.bizType,
      approvalEnabled: true,
      processDefinitionKey: values.processDefinitionKey,
    });
    message.success('配置保存成功');
    emit('success');
    modalApi.close();
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = modalApi.getData() as {
        warehouseId: number;
        warehouseName: string;
      };
      if (data) {
        formApi.setValues({
          warehouseId: data.warehouseId,
          warehouseName: data.warehouseName,
          bizType: undefined,
          processDefinitionKey: undefined,
        });
      }
    }
  },
});
 
/** 当前选中的业务类型 */
const currentBizType = computed(() => {
  const values = formApi.getValues();
  return values.bizType;
});
 
/** 监听业务类型变化,加载对应的流程列表 */
watch(currentBizType, async (bizType) => {
  if (bizType) {
    const data = modalApi.getData() as { warehouseId: number };
    if (data?.warehouseId) {
      try {
        const config = await getApprovalConfig(data.warehouseId, bizType);
        if (config) {
          formApi.setValues({
            processDefinitionKey: config.processDefinitionKey,
          });
        }
      } catch {
        // 忽略错误
      }
 
      formApi.updateSchema([
        {
          fieldName: 'processDefinitionKey',
          componentProps: {
            api: async () => {
              return await getAvailableProcessList(data.warehouseId, bizType);
            },
          },
        },
      ]);
    }
  }
});
</script>
 
<template>
  <Modal title="配置审批流程" class="w-[500px]">
    <Form />
  </Modal>
</template>