gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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 type { FileType } from 'ant-design-vue/es/upload/interface';
 
import { ref } from 'vue';
 
import { useVbenModal } from '#/packages/effects/common-ui/src';
import { downloadFileFromBlobPart } from '#/packages/utils/src';
 
import { Button, message, Switch, Upload } from 'ant-design-vue';
 
import { importAttendanceRecord, importAttendanceTemplate } from '#/api/hrm/attendance/record';
 
const emit = defineEmits(['success']);
 
const fileList = ref<FileType[]>([]);
const updateSupport = ref(false);
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    if (fileList.value.length === 0) {
      message.warning('请选择要导入的文件');
      return;
    }
    modalApi.lock();
    try {
      await importAttendanceRecord(fileList.value[0] as File, updateSupport.value);
      await modalApi.close();
      emit('success');
      message.success('导入成功');
    } finally {
      modalApi.unlock();
    }
  },
});
 
function beforeUpload(file: FileType) {
  fileList.value = [file];
  return false;
}
 
function handleRemove() {
  fileList.value = [];
}
 
async function handleDownloadTemplate() {
  const data = await importAttendanceTemplate();
  downloadFileFromBlobPart({ fileName: '考勤导入模板.xls', source: data });
}
</script>
 
<template>
  <Modal title="导入考勤记录" class="w-[500px]">
    <div class="mx-4">
      <Upload
        :max-count="1"
        accept=".xls,.xlsx"
        :file-list="fileList"
        :before-upload="beforeUpload"
        @remove="handleRemove"
      >
        <Button type="primary">选择 Excel 文件</Button>
      </Upload>
      <div class="mt-4">
        <span class="mr-2">是否覆盖已存在的数据:</span>
        <Switch v-model:checked="updateSupport" />
      </div>
      <div class="mt-2 text-gray-500">
        仅允许导入 xls、xlsx 格式文件
      </div>
    </div>
    <template #prepend-footer>
      <Button @click="handleDownloadTemplate">下载导入模板</Button>
    </template>
  </Modal>
</template>