<template>
<div class="upload-file">
<el-upload
multiple
:action="uploadFileUrl"
:before-upload="handleBeforeUpload"
:file-list="fileList"
:limit="limit"
:on-preview="handlePreviewFile"
:on-error="handleUploadError"
:on-exceed="handleExceed"
:on-change="handleFileChange"
:on-success="handleUploadSuccess"
:on-remove="handleDelete"
:auto-upload="false"
:headers="headers"
class="upload-file-uploader"
ref="fileUpload"
>
<div style="text-align: left" v-show="!infoview">
<el-button size="mini" type="primary">选取文件</el-button>
</div>
<div style="text-align: left" v-show="!infoview">
<span class="el-upload__tip" slot="tip" v-if="showTip">
请上传
<template v-if="fileSize">
大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
</template>
<template v-if="fileType">
格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
</template>
的文件
</span>
</div>
</el-upload>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
name: "FileUpload",
props: {
value: [String, Object, Array],
limit: {
type: Number,
default: 5,
},
fileSize: {
type: Number,
default: 5,
},
infoview: {
type: Boolean,
default: false,
},
fileType: {
type: Array,
default: () => [
"doc",
"docx",
"xls",
"xlsx",
"ppt",
"pptx",
"txt",
"pdf",
],
},
isShowTip: {
type: Boolean,
default: true,
},
},
data() {
return {
number: 0,
uploadList: [],
uploadFileUrl: process.env.VUE_APP_BASE_API + "/file/upload",
headers: {
Authorization: "Bearer " + getToken(),
},
fileList: [],
unList: [],
};
},
watch: {
value: {
handler(val) {
if (val) {
this.fileList = val;
} else {
this.fileList = [];
return [];
}
},
deep: true,
immediate: true,
},
},
computed: {
showTip() {
return this.isShowTip && (this.fileType || this.fileSize);
},
},
methods: {
handleBeforeUpload(file) {
if (this.fileType) {
const fileName = file.name.split(".");
const fileExt = fileName[fileName.length - 1];
const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
if (!isTypeOk) {
this.$modal.msgError(
`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
);
return false;
}
}
if (this.fileSize) {
const isLt = file.size / 1024 / 1024 < this.fileSize;
if (!isLt) {
this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
return false;
}
}
this.$modal.loading("正在上传文件,请稍候...");
this.number++;
return true;
},
handleExceed() {
this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
},
handleUploadError(err) {
this.$modal.msgError("上传文件失败,请重试");
this.$modal.closeLoading();
},
handleUploadSuccess(res, file) {
if (res.code === 200) {
this.uploadList.push({
...res.data,
name: res.data.name,
tempurl: `http://${window.location.host}${process.env.VUE_APP_BASE_FILE_API}${res.data.url}`,
});
this.uploadedSuccessfully();
} else {
this.number--;
this.$modal.closeLoading();
this.$modal.msgError(res.msg);
this.$refs.fileUpload.handleRemove(file);
this.uploadedSuccessfully();
}
},
handleDelete(file, fileList) {
this.$emit("input", fileList);
},
handleFileChange(file, fileList) {
this.unList = fileList;
console.log(fileList, "22222222222222222222222");
},
uploadedSuccessfully() {
if (this.number > 0 && this.uploadList.length === this.number) {
this.fileList = this.fileList.concat(this.uploadList);
this.uploadList = [];
this.number = 0;
this.$emit("input", this.fileList);
this.$modal.closeLoading();
this.$emit("submitdata");
}
},
getFileName(name) {
if (name.lastIndexOf("/") > -1) {
return name.slice(name.lastIndexOf("/") + 1);
} else {
return name;
}
},
handlePreviewFile(file) {
if (file.tempurl) {
window.open(file.tempurl);
}
},
submitUpload() {
if (this.unList.length > 0) {
this.$refs.fileUpload.submit();
} else {
this.$emit("submitdata");
}
},
listToString(list, separator) {
let strs = "";
separator = separator || ",";
for (let i in list) {
strs += list[i].url + separator;
}
return strs != "" ? strs.substr(0, strs.length - 1) : "";
},
},
};
</script>
<style scoped lang="scss">
.upload-file-uploader {
margin-bottom: 5px;
}
.upload-file-list .el-upload-list__item {
border: 1px solid #e4e7ed;
line-height: 2;
margin-bottom: 10px;
position: relative;
}
.upload-file-list .ele-upload-list__item-content {
display: flex;
justify-content: space-between;
align-items: center;
color: inherit;
}
.ele-upload-list__item-content-action .el-link {
margin-right: 10px;
}
</style>