2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
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
<script lang="ts" setup>
import { ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { downloadFileFromBlobPart } from '@vben/utils';
 
import { ElButton, ElMessage, ElUpload } from 'element-plus';
 
import { importStudent, importStudentTemplate } from '#/api/infra/demo';
 
defineOptions({ name: 'StudentImport' });
 
const emit = defineEmits(['success']);
const fileRef = ref<File | null>(null);
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    if (!fileRef.value) {
      ElMessage.error('请上传文件');
      return;
    }
    modalApi.lock();
    try {
      const formData = new FormData();
      formData.append('file', fileRef.value);
      const response: any = await importStudent(formData);
      const data = response?.data ?? response ?? {};
      let text = '导入成功数量:' + (data.successCount || 0) + ';导入失败数量:' + (data.failureCount || 0) + ';';
      if (data.failureRows) {
        Object.keys(data.failureRows).forEach((rowNo) => {
          text += '< 第' + rowNo + '行: ' + data.failureRows[rowNo] + ' >';
        });
      }
      ElMessage.info(text);
      await modalApi.close();
      emit('success');
    } finally {
      modalApi.unlock();
    }
  },
});
 
/** 文件改变 */
function handleChange(file: any) {
  if (file.raw) {
    fileRef.value = file.raw;
  }
}
 
/** 下载导入模板 */
async function handleDownload() {
  const data = await importStudentTemplate();
  downloadFileFromBlobPart({ fileName: '学生导入模板.xls', source: data });
}
</script>
 
<template>
  <Modal title="导入学生" class="w-1/3">
    <div class="mx-4">
      <ElUpload
        :limit="1"
        accept=".xls,.xlsx"
        :on-change="handleChange"
        :auto-upload="false"
      >
        <ElButton type="primary"> 选择 Excel 文件 </ElButton>
      </ElUpload>
    </div>
    <template #prepend-footer>
      <div class="flex flex-auto items-center">
        <ElButton @click="handleDownload"> 下载导入模板 </ElButton>
      </div>
    </template>
  </Modal>
</template>