feat: 用户导入
This commit is contained in:
108
apps/web-antd/src/views/system/user/user-import-modal.vue
Normal file
108
apps/web-antd/src/views/system/user/user-import-modal.vue
Normal file
@@ -0,0 +1,108 @@
|
||||
<script setup lang="ts">
|
||||
import type { UploadFile } from 'ant-design-vue/es/upload/interface';
|
||||
|
||||
import { h, ref, unref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { Modal, Switch, Upload } from 'ant-design-vue';
|
||||
|
||||
import { downloadImportTemplate, userImportData } from '#/api/system/user';
|
||||
import { downloadExcel } from '#/utils/file/download';
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
const UploadDragger = Upload.Dragger;
|
||||
|
||||
const [BasicModal, modalApi] = useVbenModal({
|
||||
onCancel: handleCancel,
|
||||
onConfirm: handleSubmit,
|
||||
});
|
||||
|
||||
const fileList = ref<UploadFile[]>([]);
|
||||
const checked = ref(false);
|
||||
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
modalApi.modalLoading(true);
|
||||
if (fileList.value.length !== 1) {
|
||||
handleCancel();
|
||||
return;
|
||||
}
|
||||
const data = {
|
||||
file: fileList.value[0]!.originFileObj as Blob,
|
||||
updateSupport: unref(checked),
|
||||
};
|
||||
const { code, msg } = await userImportData(data);
|
||||
let modal = Modal.success;
|
||||
if (code === 200) {
|
||||
emit('reload');
|
||||
} else {
|
||||
modal = Modal.error;
|
||||
}
|
||||
handleCancel();
|
||||
modal({
|
||||
content: h('div', {
|
||||
class: 'max-h-[260px] overflow-y-auto',
|
||||
innerHTML: msg,
|
||||
}),
|
||||
title: '提示',
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn(error);
|
||||
modalApi.close();
|
||||
} finally {
|
||||
modalApi.modalLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
modalApi.close();
|
||||
fileList.value = [];
|
||||
checked.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicModal
|
||||
:close-on-click-modal="false"
|
||||
:fullscreen-button="false"
|
||||
class="w-[550px]"
|
||||
title="用户导入"
|
||||
>
|
||||
<!-- z-index不设置会遮挡模板下载loading -->
|
||||
<!-- 手动处理 而不是放入文件就上传 -->
|
||||
<UploadDragger
|
||||
v-model:file-list="fileList"
|
||||
:before-upload="() => false"
|
||||
:max-count="1"
|
||||
:show-upload-list="true"
|
||||
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
|
||||
>
|
||||
<p class="ant-upload-drag-icon">
|
||||
<span
|
||||
class="icon-[ant-design--inbox-outlined] text-primary size-[36px]"
|
||||
></span>
|
||||
</p>
|
||||
<p class="ant-upload-text">点击或者拖拽到此处上传文件</p>
|
||||
</UploadDragger>
|
||||
<div class="mt-2 flex flex-col gap-2">
|
||||
<div class="flex items-center gap-3">
|
||||
<span>允许导入xlsx, xls文件</span>
|
||||
<a-button
|
||||
link
|
||||
type="link"
|
||||
@click="downloadExcel(downloadImportTemplate, '用户导入模板')"
|
||||
>
|
||||
下载模板
|
||||
</a-button>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span :class="{ 'text-red-500': checked }">
|
||||
是否更新/覆盖已存在的用户数据
|
||||
</span>
|
||||
<Switch v-model:checked="checked" />
|
||||
</div>
|
||||
</div>
|
||||
</BasicModal>
|
||||
</template>
|
Reference in New Issue
Block a user