车间能级提升-智能设备管理系统
朱桂飞
2025-01-09 3e8f7f239bedae0b4f04a1ac6bd443ba6298f73c
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
<script setup lang="ts">
import type { Column, GenInfo } from '#/api/tool/gen/model';
 
import { inject, onMounted, type Ref } 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 { toCurrentStep } from '../mitt';
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 = JSON.parse(info.options)?.popupComponent;
    if (popupComponent) {
      await formApi.setFieldValue('popupComponent', popupComponent);
    }
  }
  await Promise.all([initTreeSelect(info.columns), initMenuSelect()]);
});
 
async function handleNext() {
  try {
    const { valid } = await formApi.validate();
    if (!valid) {
      return null;
    }
    const data = await formApi.getValues();
    Object.assign(genInfoData.value, data);
    toCurrentStep(1);
  } catch (error) {
    console.error(error);
  }
}
</script>
 
<template>
  <Row justify="center">
    <Col v-bind="{ xs: 24, sm: 24, md: 20, lg: 16, xl: 16 }">
      <BasicForm />
      <div class="flex justify-center">
        <a-button type="primary" @click="handleNext">下一步</a-button>
      </div>
    </Col>
  </Row>
</template>