办学质量监测教学评价系统
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<script setup lang="ts">
import type { Ref } from 'vue';
 
import type { Column, GenInfo } from '#/api/tool/gen/model';
 
import { inject, onMounted } from 'vue';
 
import { useVbenForm } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { addFullName, listToTree } from '@vben/utils';
 
import { Col, Row } from 'ant-design-vue';
 
import { menuList } from '#/api/system/menu';
 
import { formSchema } from './basic';
 
/**
 * 从父组件注入
 */
const genInfoData = inject('genInfoData') as Ref<GenInfo['info']>;
 
const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
      formItemClass: 'col-span-1',
    },
    labelWidth: 150,
  },
  schema: formSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
 
/**
 * 树表需要用到的数据
 */
async function initTreeSelect(columns: Column[]) {
  const options = columns.map((item) => {
    const label = `${item.columnName} | ${item.columnComment}`;
    return { label, value: item.columnName };
  });
  formApi.updateSchema([
    {
      componentProps: {
        options,
      },
      fieldName: 'treeCode',
    },
    {
      componentProps: {
        options,
      },
      fieldName: 'treeParentCode',
    },
    {
      componentProps: {
        options,
      },
      fieldName: 'treeName',
    },
  ]);
}
 
/**
 * 加载菜单选择
 */
async function initMenuSelect() {
  const list = await menuList();
  // support i18n
  list.forEach((item) => {
    item.menuName = $t(item.menuName);
  });
  const tree = listToTree(list, { id: 'menuId', pid: 'parentId' });
  const treeData = [
    {
      fullName: $t('menu.root'),
      menuId: 0,
      menuName: $t('menu.root'),
      children: tree,
    },
  ];
  addFullName(treeData, 'menuName', ' / ');
 
  formApi.updateSchema([
    {
      componentProps: {
        fieldNames: {
          label: 'menuName',
          value: 'menuId',
        },
        // 设置弹窗滚动高度 默认256
        listHeight: 300,
        treeData,
        treeDefaultExpandAll: false,
        // 默认展开的树节点
        treeDefaultExpandedKeys: [0],
        treeLine: { showLeafIcon: false },
        treeNodeLabelProp: 'fullName',
      },
      fieldName: 'parentMenuId',
    },
  ]);
}
 
onMounted(async () => {
  const info = genInfoData.value;
  await formApi.setValues(info);
  // 弹出框类型需要手动赋值
  if (info.options) {
    const { popupComponent, formComponent } = JSON.parse(info.options);
    if (popupComponent) {
      formApi.setFieldValue('popupComponent', popupComponent);
    }
    if (formComponent) {
      formApi.setFieldValue('formComponent', formComponent);
    }
  }
  await Promise.all([initTreeSelect(info.columns), initMenuSelect()]);
});
 
/**
 * 校验表单
 */
async function validateForm() {
  const { valid } = await formApi.validate();
  if (!valid) {
    return false;
  }
  return true;
}
 
/**
 * 获取表单值
 */
async function getFormValues() {
  return await formApi.getValues();
}
 
defineExpose({
  validateForm,
  getFormValues,
});
</script>
 
<template>
  <Row justify="center">
    <Col v-bind="{ xs: 24, sm: 24, md: 20, lg: 16, xl: 16 }">
      <BasicForm />
    </Col>
  </Row>
</template>