<template>
|
<div>
|
<el-upload
|
ref="upload"
|
:class="{disButtonSty: noneBtnImg}"
|
:headers="headers"
|
:action="imagesUploadApi + '?name=' + new Date().getTime()"
|
:file-list="fileList"
|
list-type="picture-card"
|
:disabled="disabled"
|
:auto-upload="false"
|
:before-upload="beforeAvatarUpload"
|
:on-change="handleChange"
|
:on-preview="handlePictureCardPreview"
|
:on-remove="handleRemove"
|
:on-success="handleSuccess"
|
:on-error="handleError"
|
>
|
<i slot="default" class="el-icon-plus" />
|
</el-upload>
|
<!-- 放大事件显示框 -->
|
<el-dialog :visible.sync="dialogVisible" :modal="false">
|
<img width="100%" :src="dialogImageUrl" alt="">
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import { mapGetters } from 'vuex'
|
import { getToken } from '@/utils/auth'
|
export default {
|
name: 'ImageUpload',
|
props: {
|
imageUrl: {
|
type: String,
|
default() {
|
return null
|
}
|
},
|
limit: {
|
type: Number,
|
default() {
|
return 1
|
}
|
},
|
disabled: {
|
type: Boolean,
|
default() {
|
return false
|
}
|
}
|
},
|
data() {
|
return {
|
// 控制图片上传按钮是否隐藏
|
noneBtnImg: false,
|
// 请求头
|
headers: { 'Authorization': getToken() },
|
// 放大事件显示框图片路径
|
dialogImageUrl: '',
|
// 放大事件显示框显示状态
|
dialogVisible: false,
|
// 图片墙列表
|
fileList: []
|
}
|
},
|
computed: {
|
...mapGetters([
|
'imagesUploadApi',
|
'baseApi'
|
])
|
},
|
mounted() {
|
// 判断图片路径参数是否为空
|
if (this.imageUrl !== null) {
|
// 图片路径数组
|
const imageUrlList = this.imageUrl.split(',')
|
// 循环
|
for (let i = 0; i < imageUrlList.length; i++) {
|
// 临时集合
|
const itemOption = {}
|
// 临时集合添加图片路径参数
|
itemOption.url = imageUrlList[i].indexOf('https:') === -1 ? this.baseApi + imageUrlList[i] : imageUrlList[i]
|
// 图片墙列表添加临时集合
|
this.fileList.push(itemOption)
|
// 判断图片列表数量
|
if (this.fileList.length >= this.limit) {
|
// 上传按钮隐藏
|
this.noneBtnImg = true
|
}
|
}
|
}
|
},
|
methods: {
|
// 提交前校验
|
beforeAvatarUpload(file) {
|
const isJPG = file.type === 'image/jpg'
|
const isJPEG = file.type === 'image/jpeg'
|
const isPNG = file.type === 'image/png'
|
const isGIF = file.type === 'image/gif'
|
if (!isJPG && !isJPEG && !isPNG && !isGIF) {
|
this.$message.error('上传头像图片只能是 JPG/JPEG/PNG/GIF 格式!')
|
}
|
return isJPG || isJPEG || isPNG || isGIF
|
},
|
// 改变事件
|
handleChange(file, fileList) {
|
// 图片墙列表赋值
|
this.fileList = fileList
|
// 判断图片列表数量
|
if (fileList.length >= this.limit) {
|
// 上传按钮隐藏
|
this.noneBtnImg = true
|
}
|
},
|
// 放大事件
|
handlePictureCardPreview(file) {
|
// 放大事件显示框图片路径赋值
|
this.dialogImageUrl = file.url
|
// 放大事件显示框显示
|
this.dialogVisible = true
|
},
|
// 移除事件
|
handleRemove(file, fileList) {
|
// 图片墙列表赋值
|
this.fileList = fileList
|
// 判断图片列表数量
|
if (fileList.length < this.limit) {
|
// 上传按钮隐藏
|
this.noneBtnImg = false
|
}
|
},
|
// 成功事件
|
handleSuccess(response, file, fileList) {
|
// 循环图片墙列表
|
for (let i = 0; i < fileList.length; i++) {
|
// 判断图片状态
|
if (fileList[i].status === 'ready' || fileList[i].status === 'uploading') {
|
// 终止
|
return
|
}
|
// 判断循环是否结束
|
if (i === fileList.length - 1) {
|
// 向父页面发送参数:“imageInfo”是参数名,“fileList”是参数值。
|
this.$emit('imageInfo', fileList)
|
}
|
}
|
},
|
// 失败事件
|
handleError(err) {
|
console.log(err)
|
},
|
// 上传图片
|
submit() {
|
// 判断图片墙长度
|
if (this.fileList.length === 0) {
|
// 向父页面发送参数:“imageInfo”是参数名,“this.fileList”是参数值。
|
this.$emit('imageInfo', this.fileList)
|
// 终止
|
return
|
}
|
// 循环图片墙列表
|
for (let i = 0; i < this.fileList.length; i++) {
|
// 判断图片状态
|
if (this.fileList[i].status === 'ready') {
|
// 将指定组件别名进行提交
|
this.$refs.upload.submit()
|
// 终止
|
return
|
}
|
// 判断循环是否结束
|
if (i === this.fileList.length - 1) {
|
// 向父页面发送参数:“imageInfo”是参数名,“this.fileList”是参数值。
|
this.$emit('imageInfo', this.fileList)
|
}
|
}
|
}
|
}
|
}
|
</script>
|
|
<style>
|
.disButtonSty .el-upload--picture-card{
|
display: none; /* 上传按钮隐藏 */
|
}
|
</style>
|