<script lang="ts" setup>
|
import { useVbenModal } from '@vben/common-ui';
|
import { downloadFileFromBlobPart } from '@vben/utils';
|
|
import { ElButton, ElMessage, ElUpload } from 'element-plus';
|
|
import { useVbenForm } from '#/adapter/form';
|
import { import${simpleClassName}, import${simpleClassName}Template } from '#/api/${table.moduleName}/${table.businessName}';
|
import { $t } from '#/locales';
|
|
import { useImportFormSchema } from '../data';
|
|
const emit = defineEmits(['success']);
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
formItemClass: 'col-span-2',
|
labelWidth: 120,
|
},
|
layout: 'horizontal',
|
schema: useImportFormSchema(),
|
showDefaultActions: false,
|
});
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
modalApi.lock();
|
// 提交表单
|
const data = await formApi.getValues();
|
try {
|
await import${simpleClassName}(data.file);
|
// 关闭并提示
|
await modalApi.close();
|
emit('success');
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
|
/** 文件改变时 */
|
function handleChange(file: any) {
|
if (file.raw) {
|
formApi.setFieldValue('file', file.raw);
|
}
|
}
|
|
/** 下载模板 */
|
async function handleDownload() {
|
const data = await import${simpleClassName}Template();
|
downloadFileFromBlobPart({ fileName: '${table.classComment}导入模板.xls', source: data });
|
}
|
</script>
|
|
<template>
|
<Modal title="导入${table.classComment}" class="w-1/3">
|
<Form class="mx-4">
|
<template #file>
|
<div class="w-full">
|
<ElUpload
|
:limit="1"
|
accept=".xls,.xlsx"
|
:on-change="handleChange"
|
:auto-upload="false"
|
>
|
<ElButton type="primary"> 选择 Excel 文件 </ElButton>
|
</ElUpload>
|
</div>
|
</template>
|
</Form>
|
<template #prepend-footer>
|
<div class="flex flex-auto items-center">
|
<ElButton @click="handleDownload"> 下载导入模板 </ElButton>
|
</div>
|
</template>
|
</Modal>
|
</template>
|