<template>
|
<div>
|
<el-upload :http-request="uploadHttp" :before-upload="(file)=>beforeAvatarUpload(file)" :multiple="options.multiple" :on-preview="(file)=>handlePicPreview(file)" :on-remove="handlePicRemove" :disabled="options.disabled" :file-list="picList" :list-type="options.listType" :class="{'hide-button': options.readonly}" action="">
|
<i v-if="options.listType=='picture-card'" class="el-icon-plus"></i>
|
<el-button v-else :type="options.buttonType" size="small">点击上传</el-button>
|
</el-upload>
|
|
<!--预览图片-->
|
<el-dialog v-dialogDrag :visible.sync="dialogPicVisible" :append-to-body="true">
|
<img :src="dialogImageUrl" width="100%" alt="">
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import ossClient from "@/utils/aliyun.oss.client";
|
|
export default {
|
props: {
|
// 下拉框绑定值
|
value: {
|
type: Array,
|
default: () => {
|
return [];
|
}
|
},
|
// 参数配置
|
options: {
|
type: Object,
|
default: () => {
|
return {
|
multiple: true,
|
disabled: false,
|
listType: "text", // text/picture/picture-card
|
buttonType: "text"
|
};
|
}
|
}
|
},
|
data() {
|
return {
|
dialogImageUrl: null,
|
dialogPicVisible: false,
|
BASE_API: process.env.BASE_API
|
};
|
},
|
computed: {
|
// 图片列表
|
picList: {
|
get: function() {
|
return this.value;
|
},
|
set: function(val) {
|
this.$emit("input", val);
|
}
|
}
|
},
|
methods: {
|
// 图片删除
|
handlePicRemove(file, fileList) {
|
this.picList = fileList;
|
this.$emit("on-upload-deleted", file, fileList);
|
},
|
// 预览图片
|
handlePicPreview(file) {
|
if (this.options.listType === "text") {
|
window.open(file.url);
|
} else {
|
this.dialogImageUrl = file.url;
|
this.dialogPicVisible = true;
|
}
|
},
|
// 阿里云图片上传
|
uploadHttp({ file }) {
|
// 定义唯一的文件名
|
const contentType = file.type;
|
const name = file.name;
|
const fileName = `juhe/file/${this.common.formatDate(new Date(), "YYYY-MM-DD")}/${this.common.getGUID()}-${name}`;
|
ossClient(this.common.uploadConf)
|
.put(fileName, file, {
|
ContentType: contentType
|
})
|
.then(({ res, url, name }) => {
|
if (res && res.status === 200) {
|
const url = res.requestUrls[0];
|
const list = this.picList;
|
const f = url.split("/");
|
const name = decodeURI(f[f.length - 1]).substr(37);
|
list.push({
|
name: name,
|
url: url
|
});
|
this.picList = list;
|
this.$emit("on-upload-finished", this.picList);
|
}
|
})
|
.catch(err => {
|
this.$message.error(`上传图片失败` + err.message);
|
});
|
},
|
/**
|
* 图片限制
|
*/
|
beforeAvatarUpload(file) {
|
if (!this.options.multiple) {
|
if (this.picList.length >= 1) {
|
this.$message.error("已存在文件,请先删除然后重新上传");
|
return false;
|
}
|
}
|
if (this.options.listType === "text") {
|
const names = file.name.split(".");
|
const extName = names[names.length - 1];
|
const isLt5MB = file.size / 1024 / 1024 < 5;
|
const type = ["xls", "xlsx", "doc", "docx", "pdf", "jpeg", "jpg", "png"];
|
if (type.indexOf(extName) < 0) {
|
this.$message.error(`只能上传${type.join("/")}格式文档!`);
|
}
|
if (!isLt5MB) {
|
this.$message.error("大小不能超过 5MB!");
|
}
|
return type.indexOf(extName) >= 0 && isLt5MB;
|
} else {
|
const names = file.name.split(".");
|
const extName = names[names.length - 1];
|
const isLt5MB = file.size / 1024 / 1024 < 5;
|
const type = ["jpeg", "jpg", "png"];
|
if (type.indexOf(extName) < 0) {
|
this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
|
}
|
if (!isLt5MB) {
|
this.$message.error("单张图片大小不能超过 5MB!");
|
}
|
return type.indexOf(extName) >= 0 && isLt5MB;
|
}
|
}
|
}
|
};
|
</script>
|