<image-cropper ref="iscropper" :imageFile='imageFile' @uploadSuccess='uploadSuccess'>
</image-cropper>
import imageCropper from '../component/imageCropper.vue'
export default {
data(){
imageFile: ""
},
methods: {
uploadSuccess(file) {
this.imageFile = file
},
}
}
<template>
<div>
<div class="user-info-head" @click="editCropper()">
<img v-if="imageFile" :src="imageFile" title="点击上传头像" class="img-circle img-lg" />
<div v-else>+</div>
<img
v-if="imageFile"
class="el-icon-error"
src="../images/close-btn.png"
style="
position: absolute;
left: 112px;
top: -6px;
cursor: pointer;
color: #b4b8bc;
width: 16px;
height: 16px;
"
@click.stop="deleteImage">
</div>
<el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="800px" append-to-body @opened="modalOpened" @close="closeDialog()">
<el-row>
<el-col :xs="24" :md="12" :style="{height: '350px'}">
<vue-cropper ref="cropper" :img="options.img" :info="true" :autoCrop="options.autoCrop" :autoCropWidth="options.autoCropWidth" :autoCropHeight="options.autoCropHeight" :fixedBox="options.fixedBox" @realTime="realTime" v-if="visible" :key="visible" />
</el-col>
<el-col :xs="24" :md="12" :style="{height: '350px'}">
<div class="avatar-upload-preview">
<img :src="previews.url" :style="previews.img" />
</div>
</el-col>
</el-row>
<br />
<el-row>
<el-col :lg="2" :md="2">
<el-upload action="#" :headers="uploadHeaders" :accept="'.jpg,.jpeg,.png'" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
<el-button size="small">
选择
<i class="el-icon-upload el-icon--right"></i>
</el-button>
</el-upload>
</el-col>
<el-col :lg="{span: 1, offset: 2}" :md="2">
<el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button>
</el-col>
<el-col :lg="{span: 1, offset: 1}" :md="2">
<el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button>
</el-col>
<el-col :lg="{span: 1, offset: 1}" :md="2">
<el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
</el-col>
<el-col :lg="{span: 1, offset: 1}" :md="2">
<el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
</el-col>
<el-col :lg="{span: 2, offset: 6}" :md="2">
<el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
</el-col>
</el-row>
</el-dialog>
</div>
</template>
<script>
import axios from 'axios'
import { getToken } from '@/base/cookie'
import {
VueCropper
} from 'vue-cropper'
export default {
components: {
VueCropper
},
props: {
imageFile: {
type: String,
default: ''
}
},
watch: {
imageFile: {
deep: true,
immediate: true,
handler(newValue, oldValue) {
this.options.img = newValue
}
}
},
data() {
return {
uploadHeaders: {
'X-Token': getToken()
},
open: false,
visible: false,
title: '修改头像',
options: {
img: '',
info: true,
outputSize: 1,
outputType: 'jpeg',
canScale: false,
autoCrop: true,
autoCropWidth: 300,
autoCropHeight: 300,
fixedBox: true,
fixed: true,
fixedNumber: [1, 1],
full: true,
canMoveBox: false,
original: false,
centerBox: true,
infoTrue: true
},
previews: {},
formDataData: {}
}
},
mounted() {
console.log(this.imageFile)
},
methods: {
editCropper() {
console.log(this.imageFile, 'console.log(this.imageFile)')
this.options.img = this.imageFile
this.open = true
},
modalOpened() {
this.visible = true
},
requestUpload() {},
rotateLeft() {
this.$refs.cropper.rotateLeft()
},
rotateRight() {
this.$refs.cropper.rotateRight()
},
changeScale(num) {
num = num || 1
this.$refs.cropper.changeScale(num)
},
beforeUpload(file) {
const types = ['image/jpeg', 'image/jpg', 'image/png']
const isJPG = types.includes(file.type)
const imgType = file.type.split('/')[1]
console.log('imgType', imgType)
const isLt2M = file.size / 1024 / 1024 < 20
if (!isJPG) {
this.$message.error('上传图片只能是 JPG 或 JPEG 或 png格式!')
return
}
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 20MB!')
return
}
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
this.options.img = reader.result
}
this.formDataData = {
file: file,
name: file.name
}
},
uploadImg() {
this.$refs.cropper.getCropBlob((data) => {
const cloneFile = new File([data], this.formDataData.name)
const formData = new FormData()
formData.append('file', cloneFile)
const config = {
headers: {
'X-Token': getToken()
}
}
axios.post(`${process.env.VUE_APP_OSS_API}/aliyunObs/uploadFile`, formData, config).then(res => {
this.$emit('uploadSuccess', res.data.data.filePath)
this.$set(this.options, 'img', res.data.data.filePath)
this.open = false
}).catch(e => {
this.$message.error(e.data.msg || '上传失败')
})
})
},
deleteImage() {
this.options.img = ''
this.$emit('uploadSuccess', '')
},
realTime(data) {
this.previews = data
},
closeDialog() {
this.options.img = ''
this.visible = false
}
}
}
</script>
<style lang="scss" scoped>
.user-info-head {
position: relative;
display: inline-block;
height: 120px;
width: 120px;
text-align: center;
font-size: 24px;
line-height: 120px;
background-color: rgb(242, 243, 245);
color: #86909c;
div{
font-size: 24px
}
}
// .user-info-head:hover:after {
// content: '+';
// position: absolute;
// left: 0;
// right: 0;
// top: 0;
// bottom: 0;
// color: #eee;
// background: rgba(0, 0, 0, 0.1);
// font-size: 24px;
// font-style: normal;
// -webkit-font-smoothing: antialiased;
// -moz-osx-font-smoothing: grayscale;
// cursor: pointer;
// line-height: 110px;
// border-radius: 8px;
// // border-radius: 50%;
// text-align: center
// }
.img-circle {
border-radius: 8px;
// border-radius: 50%;
}
.img-lg {
width: 120px;
height: 120px;
}
.avatar-upload-preview {
position: absolute;
top: 50%;
transform: translate(50%, -50%);
width: 200px;
height: 200px;
border-radius: 8px;
box-shadow: 0 0 4px #ccc;
overflow: hidden;
}
</style>