liu
2 天以前 5a322d24b59c4b70c08e792f21162be0f05d0199
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
<script lang="ts" setup>
import { ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { downloadFileFromBlobPart } from '@vben/utils';
 
import { DatePicker, message } from 'ant-design-vue';
 
import { getRangePickerDefaultProps } from '#/utils';
 
/** 导出参数(后端 PageReqVO 以数组解析 inspectDate 起止) */
interface ExportParams {
  inspectDate?: [string, string];
}
 
interface ExportModalData {
  /** 弹窗标题 */
  title?: string;
  /** 模块导出函数:requestClient.download(...) 返回 Blob */
  exportApi: (params: ExportParams) => Promise<Blob>;
  /** 导出文件名 */
  fileName: string;
}
 
defineOptions({ name: 'QcExportRangeModal' });
 
const rangePickerProps = getRangePickerDefaultProps();
 
const modalData = ref<ExportModalData>();
const range = ref<[string, string] | undefined>(undefined);
const exporting = ref(false);
const title = ref('导出');
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    if (exporting.value) {
      return;
    }
    const data = modalData.value;
    if (!data) {
      return;
    }
    exporting.value = true;
    modalApi.lock();
    try {
      const params: ExportParams =
        range.value && range.value.length === 2
          ? { inspectDate: range.value }
          : {};
      const blob = await data.exportApi(params);
      downloadFileFromBlobPart({ fileName: data.fileName, source: blob });
      message.success('导出成功');
      await modalApi.close();
    } finally {
      exporting.value = false;
      modalApi.unlock();
    }
  },
  onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      modalData.value = undefined;
      range.value = undefined;
      return;
    }
    const data = modalApi.getData<ExportModalData>();
    modalData.value = data;
    range.value = undefined;
    title.value = data?.title ?? '导出';
    modalApi.setState({ confirmText: '导出' });
  },
});
</script>
 
<template>
  <Modal :title="title" class="w-2/5" :mask-closable="false">
    <div class="px-1 py-3">
      <div class="mb-2 text-sm font-medium text-gray-700">检验时间</div>
      <DatePicker.RangePicker
        v-model:value="range"
        v-bind="rangePickerProps"
        allow-clear
        class="w-full"
      />
      <div class="mt-2 text-xs text-gray-400">
        不选时间则导出全部数据;导出按「检测日期」过滤
      </div>
    </div>
  </Modal>
</template>