<script lang="ts" setup>
|
import type { FileType } from 'ant-design-vue/es/upload/interface';
|
import type { InvoiceImportResult } from '#/api/crm/invoice';
|
|
import { computed, ref } from 'vue';
|
|
import { useVbenModal } from '#/packages/effects/common-ui/src';
|
import { downloadFileFromBlobPart } from '#/packages/utils/src';
|
|
import { Button, message, Progress, Upload } from 'ant-design-vue';
|
|
import { importInvoice, importInvoiceTemplate } from '#/api/crm/invoice';
|
|
const emit = defineEmits(['success']);
|
|
const fileList = ref<FileType[]>([]);
|
const importResult = ref<InvoiceImportResult>();
|
const importing = ref(false);
|
const progressPercent = ref(0);
|
let progressTimer: ReturnType<typeof setInterval> | undefined;
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
let shouldClose = false;
|
if (fileList.value.length === 0) {
|
message.warning('请选择要导入的文件');
|
return;
|
}
|
if (importing.value) return;
|
modalApi.lock();
|
importing.value = true;
|
startMockProgress();
|
try {
|
importResult.value = await importInvoice(fileList.value[0] as File);
|
// 完成:进度条走满
|
stopMockProgress();
|
progressPercent.value = 100;
|
await delay(300);
|
emit('success');
|
const failedCount = Object.keys(
|
importResult.value?.failureList ?? {},
|
).length;
|
if (failedCount > 0) {
|
message.warning(
|
`导入完成:新增 ${createCount.value} 条,失败 ${failedCount} 条,请查看下方失败明细`,
|
);
|
} else {
|
message.success('导入完成');
|
shouldClose = true;
|
}
|
} finally {
|
stopMockProgress();
|
importing.value = false;
|
modalApi.unlock();
|
if (shouldClose) {
|
await modalApi.close();
|
}
|
}
|
},
|
});
|
|
/** 模拟进度条:请求期间从 0 走到 90% */
|
function startMockProgress() {
|
progressPercent.value = 0;
|
progressTimer = setInterval(() => {
|
if (progressPercent.value < 90) {
|
progressPercent.value = Math.min(
|
90,
|
progressPercent.value + Math.random() * 12,
|
);
|
}
|
}, 300);
|
}
|
|
function stopMockProgress() {
|
if (progressTimer) {
|
clearInterval(progressTimer);
|
progressTimer = undefined;
|
}
|
}
|
|
function delay(ms: number) {
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
}
|
|
function beforeUpload(file: FileType) {
|
fileList.value = [file];
|
importResult.value = undefined;
|
return false;
|
}
|
|
function handleRemove() {
|
fileList.value = [];
|
importResult.value = undefined;
|
}
|
|
async function handleDownloadTemplate() {
|
const data = await importInvoiceTemplate();
|
downloadFileFromBlobPart({ fileName: '开票导入模板.xls', source: data });
|
}
|
|
const createCount = computed(() => importResult.value?.createList?.length ?? 0);
|
const failureCount = computed(
|
() => Object.keys(importResult.value?.failureList ?? {}).length,
|
);
|
</script>
|
|
<template>
|
<Modal title="导入开票" class="w-[720px]">
|
<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-2 rounded-md bg-gray-50 p-2 text-xs leading-5 text-gray-500">
|
<p>仅允许导入 xls、xlsx 格式文件;必填列:合同编号、发票类型、开票金额(元)。“合同编号”需与系统中已审核通过的合同编号一致(编号需完全一致),客户由合同自动带入,无需填写;“发票类型”按字典名称填写(增值税专用发票 / 增值税普通发票 / 电子发票);“开票金额(元)”需大于 0 且不超过该合同剩余可开票金额。</p>
|
<p>“开票编号”留空则由系统自动生成,填写时不可与系统中已有开票编号重复;“开票日期”格式为 2026-01-01 10:00:00 或 2026-01-01,留空取当前时间。</p>
|
<p>逐行导入,失败行不影响成功行;导入的开票负责人 = 当前登录用户,状态为“未提交”,可在列表中继续编辑、上传发票附件后提交审批。</p>
|
</div>
|
<!-- 导入进度条 -->
|
<div v-if="importing" class="mt-4">
|
<div class="mb-2 text-sm text-gray-600">
|
正在导入开票数据,请稍候...
|
</div>
|
<Progress :percent="progressPercent" status="active" />
|
</div>
|
<!-- 导入结果 -->
|
<div
|
v-if="importResult"
|
class="mt-4 rounded-md border border-gray-200 bg-gray-50 p-3"
|
>
|
<div class="mb-2 font-medium">导入结果</div>
|
<div class="mb-2 flex gap-4">
|
<span class="text-green-600">新增:{{ createCount }} 条</span>
|
<span class="text-red-600">失败:{{ failureCount }} 条</span>
|
</div>
|
<div v-if="failureCount > 0" class="max-h-40 overflow-y-auto">
|
<div
|
v-for="(reason, name) in importResult.failureList"
|
:key="name"
|
class="text-sm leading-6 text-red-500"
|
>
|
{{ name }}:{{ reason }}
|
</div>
|
</div>
|
</div>
|
</div>
|
<template #prepend-footer>
|
<Button @click="handleDownloadTemplate">下载导入模板</Button>
|
</template>
|
</Modal>
|
</template>
|