2026-08-12 302e8457c8d34a35d7eabbd474d98ebc16120ed2
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
<script lang="ts" setup>
import type { FileType } from 'ant-design-vue/es/upload/interface';
 
import { h } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { downloadFileFromBlobPart } from '@vben/utils';
 
import { Button, message, notification, Upload } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { importDept, importDeptTemplate } from '#/api/system/dept';
import { $t } from '#/locales';
 
import { useImportFormSchema } from '../data';
 
const emit = defineEmits(['success']);
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    formItemClass: 'col-span-2',
    labelWidth: 120,
  },
  layout: 'horizontal',
  schema: useImportFormSchema(),
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = await formApi.getValues();
    try {
      const response = (await importDept(data.file, data.updateSupport)) as any;
      // 关闭并提示
      await modalApi.close();
      emit('success');
 
      const resultData = response?.data || response || {};
      const failureNames = resultData.failureDeptNames || {};
 
      if (Object.keys(failureNames).length > 0) {
        const errorNodes = Object.entries(failureNames).map(([name, reason]) => {
          return h('div', `${name}: ${reason}`);
        });
 
        notification.warning({
          message: '导入完成,部分部门导入失败',
          description: () => h('div', errorNodes),
          duration: null,
        });
      } else {
        message.success($t('ui.actionMessage.operationSuccess'));
      }
    } finally {
      modalApi.unlock();
    }
  },
});
 
/** 上传前 */
function beforeUpload(file: FileType) {
  formApi.setFieldValue('file', file);
  return false;
}
 
/** 下载模版 */
async function handleDownload() {
  const data = await importDeptTemplate();
  downloadFileFromBlobPart({ fileName: '部门导入模板.xls', source: data });
}
</script>
 
<template>
  <Modal title="导入部门" class="w-1/3">
    <Form class="mx-4">
      <template #file>
        <div class="w-full">
          <Upload
            :max-count="1"
            accept=".xls,.xlsx"
            :before-upload="beforeUpload"
          >
            <Button type="primary"> 选择 Excel 文件 </Button>
          </Upload>
        </div>
      </template>
    </Form>
    <div class="mx-4 mt-2 mb-4 text-gray-500 text-xs">
      提示:模板里的“上级部门”请填写全路径(如:总公司/北京分公司)
      <div>上级部门空着即为顶级部门。</div>
    </div>
    <template #prepend-footer>
      <div class="flex flex-auto items-center">
        <Button @click="handleDownload"> 下载导入模板 </Button>
      </div>
    </template>
  </Modal>
</template>