办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
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
<script setup lang="ts">
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { addFullName, cloneDeep, getPopupContainer } from '@vben/utils';
 
import { useVbenForm } from '#/adapter/form';
import { categoryTree } from '#/api/workflow/category';
import {
  workflowDefinitionAdd,
  workflowDefinitionInfo,
  workflowDefinitionUpdate,
} from '#/api/workflow/definition';
 
import { modalSchema } from './data';
 
const emit = defineEmits<{ reload: [type: 'add' | 'update'] }>();
 
const isUpdate = ref(false);
const title = computed(() => {
  return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
 
const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 90,
  },
  schema: modalSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
 
async function setupCategorySelect() {
  // menu
  const tree = await categoryTree();
  addFullName(tree, 'label', ' / ');
 
  formApi.updateSchema([
    {
      componentProps: {
        fieldNames: {
          label: 'label',
          value: 'id',
        },
        getPopupContainer,
        // 设置弹窗滚动高度 默认256
        listHeight: 300,
        showSearch: true,
        treeData: tree,
        treeDefaultExpandAll: true,
        // 默认展开的树节点
        // treeDefaultExpandedKeys: [0],
        treeLine: { showLeafIcon: false },
        // 筛选的字段
        treeNodeFilterProp: 'label',
        treeNodeLabelProp: 'fullName',
      },
      fieldName: 'category',
    },
  ]);
}
 
const [BasicDrawer, modalApi] = useVbenModal({
  onCancel: handleCancel,
  onConfirm: handleConfirm,
  async onOpenChange(isOpen) {
    if (!isOpen) {
      return null;
    }
    modalApi.modalLoading(true);
 
    const { id } = modalApi.getData() as { id?: number | string };
    isUpdate.value = !!id;
 
    // 加载分类树选择
    await setupCategorySelect();
    if (isUpdate.value && id) {
      const record = await workflowDefinitionInfo(id);
      await formApi.setValues(record);
    }
 
    modalApi.modalLoading(false);
  },
});
 
async function handleConfirm() {
  try {
    modalApi.modalLoading(true);
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    const data = cloneDeep(await formApi.getValues());
    if (isUpdate.value) {
      await workflowDefinitionUpdate(data);
      emit('reload', 'update');
    } else {
      await workflowDefinitionAdd(data);
      emit('reload', 'add');
    }
    await handleCancel();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.modalLoading(false);
  }
}
 
async function handleCancel() {
  modalApi.close();
  await formApi.resetForm();
}
</script>
 
<template>
  <BasicDrawer
    :close-on-click-modal="false"
    :fullscreen-button="false"
    :title="title"
    class="w-[550px]"
  >
    <div class="min-h-[400px]">
      <BasicForm />
    </div>
  </BasicDrawer>
</template>