<template>
|
<div>
|
<el-dialog
|
:visible.sync="fileDialogVisible"
|
ref="formDialogRef"
|
width="35%"
|
append-to-body
|
close-on-click-modal
|
@close="closeDialog"
|
>
|
<template slot="header">
|
<span style="padding-bottom: 18px">
|
{{ isImportOrExport ? '请按照需求导出目标内容' : '请按照模板样式上传项目文件' }}
|
</span>
|
</template>
|
<template slot="default">
|
<div v-if="!isImportOrExport" class="dialog-content">
|
<el-upload
|
ref="uploadRef"
|
class="upload-demo"
|
:action="upload.url"
|
:limit="1"
|
:accept="accept"
|
:headers="upload.headers"
|
:disabled="upload.isUploading"
|
:on-progress="handleFileUploadProgress"
|
:on-success="handleFileSuccess"
|
:auto-upload="false"
|
drag
|
>
|
<el-icon class="el-icon--upload"><upload-filled /></el-icon>
|
<div class="el-upload__text">请上传<em>Zip</em>文件,大小在<em>100M</em>以内</div>
|
</el-upload>
|
<span>仅允许导入zip格式文件。</span>
|
<el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="handleDownloadFile">下载模板</el-link>
|
</div>
|
<div v-else-if="isImportOrExport" class="dialog-content">
|
<el-button class="export-button" @click="handleDownloadTargetList">导出目标列表内容</el-button>
|
<el-button class="export-button">导出所有列表内容</el-button>
|
<el-button class="export-button">导出目标项目详情</el-button>
|
<el-button class="export-button">导出所有项目详情</el-button>
|
</div>
|
</template>
|
<template slot="footer">
|
<div v-if="!isImportOrExport" class="dialog-footer">
|
<el-button type="primary" @click="submitFileForm">确 定</el-button>
|
<el-button @click="closeDialog">取 消</el-button>
|
</div>
|
<div v-else-if="isImportOrExport"></div>
|
</template>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
// import UploadFilled from '@element/icons-vue/es/icons/UploadFilled.vue';
|
import { globalHeaders } from '@/utils/request';
|
import { FileTypeEnum } from '@/enums/fileType';
|
|
export default {
|
name: 'FileDialog',
|
// components: {
|
// UploadFilled
|
// },
|
props: {
|
isImportOrExport: {
|
type: Boolean,
|
default: false
|
},
|
fileDialogVisible: {
|
type: Boolean,
|
default: false
|
},
|
currentColumns: {
|
type: Array,
|
default: function () {
|
return [];
|
}
|
}
|
},
|
data() {
|
return {
|
uploadRef: null,
|
targetColumn: [],
|
accept: `${FileTypeEnum.ZIP}`,
|
upload: {
|
open: false,
|
title: '',
|
isUploading: false,
|
updateSupport: 0,
|
headers: globalHeaders(),
|
url: process.env.VITE_APP_BASE_API + '/project/import'
|
}
|
};
|
},
|
computed: {
|
// In Vue 2, computed properties don't use setters and getters as objects.
|
// Instead, you define them as functions for reading and use methods for writing.
|
// However, in this case, we can use a watcher to update the prop.
|
fileDialogVisible: {
|
get() {
|
return this.isFileDialogVisible;
|
},
|
set(value) {
|
this.$emit('update:fileDialogVisible', value);
|
}
|
}
|
// Note: In Vue 2, you cannot directly define a computed property with a setter for a prop.
|
// Instead, you should use a watcher or a method to handle changes.
|
// Since `fileDialogVisible` is a prop, we'll use a watcher to sync it with the internal state.
|
// However, for simplicity, we'll rename `fileDialogVisible` to `isFileDialogVisible` in data and use a watcher.
|
},
|
watch: {
|
fileDialogVisible(newVal) {
|
this.isFileDialogVisible = newVal;
|
},
|
isFileDialogVisible(newVal) {
|
this.$emit('update:fileDialogVisible', newVal);
|
}
|
},
|
methods: {
|
handleFileUploadProgress() {
|
this.upload.isUploading = true;
|
},
|
handleFileSuccess(response, file) {
|
this.upload.open = false;
|
this.upload.isUploading = false;
|
if (this.uploadRef) {
|
this.uploadRef.handleRemove(file);
|
}
|
if (response.code === 200) {
|
this.$emit('fileDialogCancel');
|
this.$message({
|
message: response.msg,
|
type: 'success'
|
});
|
} else {
|
this.$message.error(response.msg);
|
}
|
},
|
handleDownloadFile() {
|
const self = this;
|
fetch(`${process.env.VITE_APP_BASE_API}/project/export/template`, {
|
method: 'GET',
|
headers: self.upload.headers
|
})
|
.then(response => response.blob())
|
.then(blob => {
|
const url = window.URL.createObjectURL(blob);
|
const a = document.createElement('a');
|
a.style.display = 'none';
|
a.href = url;
|
a.download = `项目文件模板_${new Date().getTime()}.zip`;
|
document.body.appendChild(a);
|
a.click();
|
window.URL.revokeObjectURL(url);
|
})
|
.catch(error => {
|
console.error('文件下载失败:', error);
|
});
|
},
|
submitFileForm() {
|
if (this.uploadRef) {
|
this.uploadRef.submit();
|
}
|
},
|
closeDialog() {
|
this.$emit('fileDialogCancel');
|
},
|
handleDownloadTargetList() {
|
console.log('导出目标列表内容', this.currentColumns);
|
this.targetColumn = this.currentColumns.filter(item => item.visible);
|
}
|
},
|
created() {
|
// Initialize the computed property as a data property in Vue 2
|
this.isFileDialogVisible = this.fileDialogVisible;
|
}
|
};
|
</script>
|
|
<style scoped lang="scss">
|
.dialog-content {
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
flex-direction: column;
|
height: 300px;
|
.export-button {
|
margin-left: 0;
|
margin-bottom: 10px;
|
}
|
}
|
.dialog-footer {
|
display: flex;
|
justify-content: center;
|
}
|
.upload-demo {
|
width: 100%;
|
}
|
</style>
|