<script lang="ts" setup>
|
import type { FileType } from 'ant-design-vue/es/upload/interface';
|
|
import { useVbenModal } from '@vben/common-ui';
|
import { downloadFileFromBlobPart } from '@vben/utils';
|
|
import { Button, message, Upload } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import {
|
getSupplierImportTemplate,
|
importSupplierExcel,
|
} from '#/api/srm/supplier';
|
|
import { useImportFormSchema } from '../data';
|
|
const emit = defineEmits(['success']);
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
formItemClass: 'col-span-2',
|
labelWidth: 110,
|
},
|
layout: 'horizontal',
|
schema: useImportFormSchema(),
|
showDefaultActions: false,
|
});
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
const data = await formApi.getValues();
|
modalApi.lock();
|
try {
|
const res = (await importSupplierExcel({
|
file: data.file,
|
updateSupport: data.updateSupport,
|
})) as any;
|
// 兼容响应结构:业务数据直接返回或包在 { code, data } 中
|
const result = res?.data ?? res ?? {};
|
const createList = result.createList ?? [];
|
const updateList = result.updateList ?? [];
|
const failureList = result.failureList ?? {};
|
const failureCount = Object.keys(failureList).length;
|
let tip = `导入完成:新增 ${createList.length} 条、更新 ${updateList.length} 条、失败 ${failureCount} 条。`;
|
if (failureCount > 0) {
|
const sample = Object.entries(failureList)
|
.slice(0, 5)
|
.map(([name, reason]) => `${name}:${reason}`)
|
.join(';');
|
tip += `失败原因示例:${sample}`;
|
}
|
if (failureCount > 0) {
|
message.warning(tip);
|
} else {
|
message.success(tip);
|
}
|
// 关闭并刷新
|
await modalApi.close();
|
emit('success');
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
|
/** 上传前 */
|
function beforeUpload(file: FileType) {
|
formApi.setFieldValue('file', file);
|
return false;
|
}
|
|
/** 下载模板 */
|
async function handleDownload() {
|
const data = await getSupplierImportTemplate();
|
downloadFileFromBlobPart({ fileName: '供应商导入模板.xlsx', 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>
|
<template #prepend-footer>
|
<div class="flex flex-auto items-center">
|
<Button @click="handleDownload"> 下载导入模板 </Button>
|
</div>
|
</template>
|
</Modal>
|
</template>
|