|
@@ -0,0 +1,305 @@
|
|
|
|
+<template>
|
|
|
|
+ <el-dialog v-model="dialogVisible" title="批量导入" width="780px" destroy-on-close append-to-body @close="handleClose">
|
|
|
|
+ <div class="describe-content">
|
|
|
|
+ <div class="title">操作说明:</div>
|
|
|
|
+ <div class="step-container">
|
|
|
|
+ <el-steps direction="vertical">
|
|
|
|
+ <el-step status="process" title="首先点击“模板下载,下载录入模板”。" />
|
|
|
|
+ <el-step status="process" :title="'在模板空白处中填写' + stepsText + '。'" />
|
|
|
|
+ <el-step status="process" title="通过点击“导入按钮,浏览选择导入文件”,完成导入。" />
|
|
|
|
+ <el-step
|
|
|
|
+ status="process"
|
|
|
|
+ :title="'导入成功,对于系统中没有的' + stepsText + ',进行新增操作对于原本系统已存在的' + stepsText + ',进行信息合并操作。'"
|
|
|
|
+ />
|
|
|
|
+ </el-steps>
|
|
|
|
+ </div>
|
|
|
|
+ </div>
|
|
|
|
+ <template #footer>
|
|
|
|
+ <div class="dialog-footer">
|
|
|
|
+ <el-upload
|
|
|
|
+ ref="fileUploadRef"
|
|
|
|
+ multiple
|
|
|
|
+ action="#"
|
|
|
|
+ :before-upload="handleBeforeUpload"
|
|
|
|
+ :file-list="fileList"
|
|
|
|
+ :limit="limit"
|
|
|
|
+ :on-error="handleUploadError"
|
|
|
|
+ :on-exceed="handleExceed"
|
|
|
|
+ :on-success="handleUploadSuccess"
|
|
|
|
+ :show-file-list="false"
|
|
|
|
+ :headers="headers"
|
|
|
|
+ :http-request="uploadFile"
|
|
|
|
+ class="upload-file-uploader"
|
|
|
|
+ >
|
|
|
|
+ <el-button type="primary" :icon="Upload">导入</el-button>
|
|
|
|
+ </el-upload>
|
|
|
|
+ <el-button type="primary" :icon="Download" style="margin-left: 16px" @click="handleDownload">模板下载</el-button>
|
|
|
|
+ </div>
|
|
|
|
+ </template>
|
|
|
|
+ </el-dialog>
|
|
|
|
+</template>
|
|
|
|
+
|
|
|
|
+<script lang="ts" setup name="DataImport">
|
|
|
|
+import { Upload, Download } from '@element-plus/icons-vue';
|
|
|
|
+import { download2, globalHeaders } from '@/utils/request';
|
|
|
|
+import { propTypes } from '@/utils/propTypes';
|
|
|
|
+import { v1 as uuidv1 } from 'uuid';
|
|
|
|
+import axios from 'axios';
|
|
|
|
+import { getToken } from '@/utils/auth';
|
|
|
|
+import { importDataManagerInfo } from '@/api/dataManagement/dataManagement';
|
|
|
|
+
|
|
|
|
+const baseUrl = import.meta.env.VITE_APP_BASE_API;
|
|
|
|
+const props = defineProps({
|
|
|
|
+ modelValue: Boolean,
|
|
|
|
+ // 数量限制
|
|
|
|
+ limit: propTypes.number.def(1),
|
|
|
|
+ // 大小限制(MB)
|
|
|
|
+ fileSize: propTypes.number.def(100),
|
|
|
|
+ fileType: propTypes.array.def(['xls', 'xlsx']),
|
|
|
|
+ stepsText: String,
|
|
|
|
+ url: String,
|
|
|
|
+ fileName: String,
|
|
|
|
+ dataType: String
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const { proxy } = getCurrentInstance() as ComponentInternalInstance;
|
|
|
|
+const emits = defineEmits(['update:modelValue', 'success']);
|
|
|
|
+const dialogVisible = computed({
|
|
|
|
+ get() {
|
|
|
|
+ return props.modelValue;
|
|
|
|
+ },
|
|
|
|
+ set(newValue) {
|
|
|
|
+ emits('update:modelValue', newValue);
|
|
|
|
+ }
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const handleClose = () => {
|
|
|
|
+ emits('update:modelValue', false);
|
|
|
|
+};
|
|
|
|
+const number = ref(0);
|
|
|
|
+const uploadList = ref<any[]>([]);
|
|
|
|
+
|
|
|
|
+const headers = ref(globalHeaders());
|
|
|
|
+
|
|
|
|
+const fileList = ref<any[]>([]);
|
|
|
|
+const fileUploadRef = ref<ElUploadInstance>();
|
|
|
|
+// 隐藏清空数据
|
|
|
|
+watch(
|
|
|
|
+ () => props.modelValue,
|
|
|
|
+ async (val) => {
|
|
|
|
+ if (!val) {
|
|
|
|
+ fileList.value = [];
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ { deep: true, immediate: true }
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+// 进入导入界面
|
|
|
|
+const handleImport = () => {};
|
|
|
|
+// 上传前校检格式和大小
|
|
|
|
+const handleBeforeUpload = (file: any) => {
|
|
|
|
+ // 校检文件类型
|
|
|
|
+ if (props.fileType.length) {
|
|
|
|
+ const fileName = file.name.split('.');
|
|
|
|
+ const fileExt = fileName[fileName.length - 1];
|
|
|
|
+ const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
|
|
|
|
+ if (!isTypeOk) {
|
|
|
|
+ proxy?.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join('/')}格式文件!`);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 校检文件大小
|
|
|
|
+ if (props.fileSize) {
|
|
|
|
+ const isLt = file.size / 1024 / 1024 < props.fileSize;
|
|
|
|
+ if (!isLt) {
|
|
|
|
+ proxy?.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ proxy?.$modal.loading('正在上传文件,请稍候...');
|
|
|
|
+ number.value++;
|
|
|
|
+ return true;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// 文件个数超出
|
|
|
|
+const handleExceed = () => {
|
|
|
|
+ proxy?.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// 上传失败
|
|
|
|
+const handleUploadError = () => {
|
|
|
|
+ proxy?.$modal.msgError('上传文件失败');
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const uploadFile = async ({ data, file }) => {
|
|
|
|
+ // data是上传时附带的额外参数,file是文件
|
|
|
|
+ let url = baseUrl + '/file/upload/uploadfile'; //上传文件接口
|
|
|
|
+ try {
|
|
|
|
+ // 如果文件大于等于5MB,分片上传
|
|
|
|
+ data.file = file;
|
|
|
|
+ const res = await uploadByPieces(url, data);
|
|
|
|
+ // 分片上传后操作
|
|
|
|
+ return res;
|
|
|
|
+ } catch (e) {
|
|
|
|
+ return e;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+//分片上传
|
|
|
|
+const uploadByPieces = async (url, { fileName, file }) => {
|
|
|
|
+ // 上传过程中用到的变量
|
|
|
|
+ const chunkSize = 5 * 1024 * 1024; // 5MB一片
|
|
|
|
+ const chunkCount = Math.ceil(file.size / chunkSize); // 总片数
|
|
|
|
+ // 获取当前chunk数据
|
|
|
|
+ const identifier = uuidv1();
|
|
|
|
+ const getChunkInfo = (file, index) => {
|
|
|
|
+ let start = index * chunkSize;
|
|
|
|
+ let end = Math.min(file.size, start + chunkSize);
|
|
|
|
+ let chunknumber = file.slice(start, end);
|
|
|
|
+ const fileName = file.name;
|
|
|
|
+ return { chunknumber, fileName };
|
|
|
|
+ };
|
|
|
|
+ // 分片上传接口
|
|
|
|
+ const uploadChunk = (data, params) => {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ axios({
|
|
|
|
+ url,
|
|
|
|
+ method: 'post',
|
|
|
|
+ data,
|
|
|
|
+ params,
|
|
|
|
+ headers: {
|
|
|
|
+ 'Content-Type': 'multipart/form-data',
|
|
|
|
+ 'Authorization': 'Bearer ' + getToken()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ .then((res) => {
|
|
|
|
+ return resolve(res.data);
|
|
|
|
+ })
|
|
|
|
+ .catch((err) => {
|
|
|
|
+ return reject(err);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
+ // 针对单个文件进行chunk上传
|
|
|
|
+ const readChunk = (index) => {
|
|
|
|
+ const { chunknumber } = getChunkInfo(file, index);
|
|
|
|
+ let fetchForm = new FormData();
|
|
|
|
+ fetchForm.append('file', chunknumber);
|
|
|
|
+ return uploadChunk(fetchForm, {
|
|
|
|
+ chunknumber: index,
|
|
|
|
+ identifier: identifier
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
+ // 针对每个文件进行chunk处理
|
|
|
|
+ const promiseList = [];
|
|
|
|
+ try {
|
|
|
|
+ for (let index = 0; index < chunkCount; ++index) {
|
|
|
|
+ promiseList.push(readChunk(index));
|
|
|
|
+ }
|
|
|
|
+ await Promise.all(promiseList);
|
|
|
|
+ // 文件分片上传完成后,调用合并接口
|
|
|
|
+
|
|
|
|
+ const res = await mergeChunks(identifier, file.name);
|
|
|
|
+ res.originalName = file.name;
|
|
|
|
+ const res2 = await importDataManagerInfo(props.dataType, res.filename);
|
|
|
|
+ // const res3 = await customUpload(identifier, file.name);
|
|
|
|
+ return res;
|
|
|
|
+ } catch (e) {
|
|
|
|
+ return e;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+const mergeChunks = async (identifier: string, filename: string) => {
|
|
|
|
+ try {
|
|
|
|
+ const response = await axios.post(baseUrl + '/file/upload/mergefile', null, {
|
|
|
|
+ params: {
|
|
|
|
+ identifier: identifier,
|
|
|
|
+ filename: filename,
|
|
|
|
+ chunkstar: 0 // 假设所有分片的开始序号为0
|
|
|
|
+ },
|
|
|
|
+ headers: {
|
|
|
|
+ 'Authorization': 'Bearer ' + getToken()
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return response.data;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ throw new Error('合并请求失败');
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+// 上传成功回调
|
|
|
|
+const handleUploadSuccess = (res: any, file: UploadFile) => {
|
|
|
|
+ if (res && res.code === 200) {
|
|
|
|
+ uploadList.value.push({
|
|
|
|
+ name: res.originalName,
|
|
|
|
+ url: res.filename
|
|
|
|
+ });
|
|
|
|
+ uploadedSuccessfully();
|
|
|
|
+ proxy.$modal.msgSuccess('导入成功');
|
|
|
|
+ emits('update:modelValue', false);
|
|
|
|
+ emits('success');
|
|
|
|
+ } else {
|
|
|
|
+ number.value--;
|
|
|
|
+ proxy?.$modal.closeLoading();
|
|
|
|
+ proxy?.$modal.msgError(res.msg);
|
|
|
|
+ fileUploadRef.value?.handleRemove(file);
|
|
|
|
+ uploadedSuccessfully();
|
|
|
|
+ proxy.$modal.msgError('导入失败');
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// 上传结束处理
|
|
|
|
+const uploadedSuccessfully = () => {
|
|
|
|
+ if (number.value > 0 && uploadList.value.length === number.value) {
|
|
|
|
+ fileList.value = fileList.value.filter((f) => f.url !== undefined).concat(uploadList.value);
|
|
|
|
+ uploadList.value = [];
|
|
|
|
+ number.value = 0;
|
|
|
|
+ proxy?.$modal.closeLoading();
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// 下载模板
|
|
|
|
+const handleDownload = () => {
|
|
|
|
+ if (!props.url) return false;
|
|
|
|
+ let downLoadUrl = baseUrl + props.url;
|
|
|
|
+ download2(downLoadUrl, props.fileName);
|
|
|
|
+};
|
|
|
|
+</script>
|
|
|
|
+
|
|
|
|
+<style lang="scss" scoped>
|
|
|
|
+.title {
|
|
|
|
+ font-size: 18px;
|
|
|
|
+ line-height: 36px;
|
|
|
|
+ margin-bottom: 10px;
|
|
|
|
+ color: rgba(0, 0, 0, 0.85);
|
|
|
|
+}
|
|
|
|
+.step-container {
|
|
|
|
+ width: 700px;
|
|
|
|
+ height: 180px;
|
|
|
|
+ :deep(.el-step) {
|
|
|
|
+ .el-step__icon.is-text {
|
|
|
|
+ border-color: #a8abb2;
|
|
|
|
+ color: rgba(0, 0, 0, 0.85);
|
|
|
|
+ }
|
|
|
|
+ .el-step__title.is-process {
|
|
|
|
+ font-size: 16px;
|
|
|
|
+ color: rgba(0, 0, 0, 0.85);
|
|
|
|
+ font-weight: normal;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+.upload-file-list .el-upload-list__item {
|
|
|
|
+ line-height: 22px;
|
|
|
|
+ margin-bottom: 10px;
|
|
|
|
+ position: relative;
|
|
|
|
+ padding: 3px 5px;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+.upload-file-list .ele-upload-list__item-content {
|
|
|
|
+ display: flex;
|
|
|
|
+ align-items: center;
|
|
|
|
+ color: inherit;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+.ele-upload-list__item-content-action .el-link {
|
|
|
|
+ margin-right: 10px;
|
|
|
|
+}
|
|
|
|
+</style>
|