<template>
|
<div>
|
<el-upload
|
class="avatar-uploader"
|
accept=".png,.jpg,.jpeg"
|
:style="customStyle"
|
action=""
|
:auto-upload="false"
|
:show-file-list="false"
|
:on-change='openUpdateIcon'>
|
<img v-if=" file.imgurlfull" style="width: 100%;" :src="file.imgurlfull" :style="customStyle" class="avatar">
|
<div v-else :style="customStyle">
|
<i class="el-icon-plus avatar-uploader-icon"></i>
|
<div class="tips-style">{{ tipsLabel }}</div>
|
</div>
|
</el-upload>
|
<el-dialog
|
append-to-body
|
:close-on-click-modal="false"
|
title="上传图片"
|
:visible.sync="updateImg"
|
width="1000px"
|
class="icon-dialog-wrapper dialong-com-style">
|
<ImageCropper ref="iconShot" v-if="updateImg" :imgSrc="img" >
|
</ImageCropper>
|
<span slot="footer" class="dialog-footer">
|
<el-button v-if="loading">取 消</el-button>
|
<el-button v-else @click="updateImg = false">取 消</el-button>
|
<el-button :loading="loading" type="primary" @click="uploadIcon">确 定</el-button>
|
</span>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import ImageCropper from '@/components/common/ImageCropper'
|
import { upload } from '@/api/system/common'
|
export default {
|
components: { ImageCropper },
|
props: {
|
file: {
|
type: Object,
|
default: () => {}
|
},
|
tipsLabel: '',
|
customStyle: {
|
type: String,
|
default: 'width: 90px; height: 90px;'
|
},
|
uploadData: Object
|
},
|
data () {
|
return {
|
loading: false,
|
fileInfo:{},
|
img: null,
|
updateImg: false,
|
imageSrc: null,
|
uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/visitsAdmin/cloudService/public/upload'
|
}
|
},
|
|
methods: {
|
uploadIcon () {
|
// 获取裁剪后的图片
|
this.$refs.iconShot.getImagecropper().getCropBlob((fileData) => { // 获取当前裁剪好的数据
|
// 注此时的data是一个Blob数据,部分接口接收的是File转化的FormData数据
|
console.log(fileData)
|
const formData = new FormData()
|
|
formData.append('folder', 'member')
|
formData.append(
|
'file',
|
new File(
|
[fileData], // 将Blob类型转化成File类型
|
this.fileInfo.name, // 设置File类型的文件名称
|
{ type: this.fileInfo.type } // 设置File类型的文件类型
|
)
|
)
|
this.loading = true
|
upload(formData).then(res => {
|
this.loading = false
|
console.log(res)
|
this.file.imgurl = res.imgaddr
|
this.file.imgurlfull = res.url
|
this.$message.success('上传成功')
|
this.imageSrc = res.url
|
this.updateImg = false
|
this.$emit('uploadSuccess', { imgurl: res.imgaddr, imgurlfull: res.url, name: res.originname })
|
this.$emit('uploadEnd')
|
}, () => {
|
this.loading = false
|
})
|
})
|
},
|
|
// 上传图片
|
openUpdateIcon (file, fileList) {
|
const isJPG = file.raw.type === 'image/jpeg' || file.raw.type === 'image/png'
|
const isLt2M = file.size / 1024 / 1024 < 5
|
if (!isJPG) {
|
this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
|
return false
|
}
|
if (!isLt2M) {
|
this.$message.error('上传头像图片大小不能超过 5MB!')
|
return false
|
}
|
// 上传成功后将图片地址赋值给裁剪框显示图片
|
this.$nextTick(async () => {
|
// base64方式
|
// this.option.img = await fileByBase64(file.raw)
|
this.fileInfo.name = file.name
|
this.fileInfo.type = file.type
|
console.log(file, fileList)
|
this.img = URL.createObjectURL(file.raw)
|
// this.loading = false
|
this.updateImg = true
|
})
|
},
|
handleAvatarSuccess (res, file) {
|
if (res.code == 200) {
|
const { data } = res
|
this.file.imgurl = data.imgaddr
|
this.file.imgurlfull = data.url
|
this.$message.success('上传成功')
|
this.imageSrc = data.url
|
this.updateImg = true
|
// this.$emit('uploadSuccess', { imgurl: data.imgaddr, imgurlfull: data.url, name: data.originname })
|
} else {
|
this.$message.error('上传失败')
|
}
|
this.$emit('uploadEnd')
|
},
|
uploadError () {
|
this.$message.error('上传失败')
|
this.$emit('uploadEnd')
|
},
|
// // 拦截
|
beforeAvatarUpload (file) {
|
this.$emit('uploadBegin')
|
return true
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss" scoped>
|
$image-width: 90px;
|
.avatar-uploader {
|
width: $image-width;
|
height: $image-width;
|
}
|
::v-deep .el-upload {
|
border: 1px dashed #d9d9d9;
|
border-radius: 6px;
|
cursor: pointer;
|
position: relative;
|
overflow: hidden;
|
}
|
.avatar-uploader .el-upload:hover {
|
border-color: #409EFF;
|
}
|
.avatar-uploader-icon {
|
line-height: 90px;
|
font-size: 28px;
|
color: #8c939d;
|
width: $image-width;
|
height: $image-width;
|
text-align: center;
|
}
|
.avatar {
|
width: 100% !important;
|
height: auto !important;
|
display: block;
|
}
|
.tips-style {
|
height: 13px;
|
font-size: 13px;
|
font-weight: 400;
|
color: #999999;
|
line-height: 13px;
|
}
|
</style>
|
<style lang="scss" scoped>
|
::v-deep .el-upload--picture-card{
|
width: 90px !important;
|
height: 90px !important;
|
}
|
::v-deep .el-upload-list__item {
|
width: 90px !important;
|
height: 90px !important;
|
}
|
.icon {
|
-webkit-transform: translate(-50%,-50%);
|
-ms-transform: translate(-50%,-50%);
|
transform: translate(0%, -85%);
|
}
|
::v-deep .el-upload-list__item {
|
width: 90px !important;
|
height: 90px !important;
|
}
|
</style>
|