要實(shí)現(xiàn)的亞子:

image.png

image.png

image.png
廢話不說,上代碼,先封裝一個(gè)公用組件
<template>
<div class="main">
<div class="cropper" style="position: relative;overflow: hidden; text-align: center;width:100%;height:100%;" :style="obj">
<van-image v-if="show" :src="img" class="img" style="position: absolute;width: 100%;height:100%;left: 0;top: 0;" />
<van-loading v-else color="#65abff" style="" />
<!-- option是配置,格式是對(duì)象,getbase64Data是組件的一個(gè)方法獲取裁剪完的頭像 -->
<h5-cropper @getbase64="getbase64Data" :option="option" @getblob="getBlob" @get-file="getFile"></h5-cropper>
</div>
</div>
</template>
<script>
// import { getsign } from "@/api/common";
//import TcVod from 'vod-js-sdk-v6'
export default {
props: {
fixedNumber: {
type: Array,
default: () => [1, 1]
},
image: {
type: String,
default: "http://erkong.ybc365.com/36bc0202010231838302739.png"
},
radius: {
type: Number,
default: 0
}
},
data() {
return {
show: true,
obj: {
borderRadius: `${this.radius}px`
},
img: this.image,
option: {
ceilbutton: true,//操作按鈕是否在頂部
fixedNumber: this.fixedNumber,//截圖框的寬高比例
canMoveBox: true,//截圖框能否拖動(dòng)
fixedBox: false,//固定截圖框大小 不允許改變
centerBox: true,//截圖框是否被限制在圖片里面
fixed: false,//是否開啟截圖框?qū)捀吖潭ū壤?
}
}
},
methods: {
getbase64Data(data) {
this.img = data;
},
getBlob(blob) {
// console.log(blob)
this.$emit('getBlob')
},
getFile(file) {
// console.log(file)
// this.$emit('getFile')
// this.show = false
// this.uploader = this.tcVod.upload({
// mediaFile: file,
// })
// // 上傳進(jìn)度
// this.uploader.on('media_progress', (info) => {
// console.log('上傳進(jìn)度' + "=>" + info.percent * 100);
// })
// // 上傳完成時(shí)
// this.uploader.on('media_upload', (info) => {
// console.log('上傳完成時(shí)' + "=>" + info);
// this.$toast.success('上傳成功!')
// })
// this.uploader.done().then((doneResult) => {
// this.img = (doneResult.video.url);
// this.show = true
this.$emit('getImageUrl', file)
// })
}
},
// created() {
// //獲取簽名
// function getSignature() {
// return getsign(JSON.stringify({
// "Action": "GetUgcUploadSign"
// })).then(function(response) {
// return response.data.sign
// })
// };
// this.tcVod = new TcVod({
// getSignature: getSignature
// });
// },
}
</script>
<style lang="scss" scoped>
.main {
width: 100%;
height: 100%;
::v-deep .van-loading {
margin-top: 20%;
}
}
</style>
然后在其他頁面引入組件,直接用
<!-- 上傳圖片、視頻 -->
<h1 class="title">上傳圖片:</h1>
<van-field class="titleImg" readonly input-align="right">
<template #button>
<div style="display: flex;">
<div class="avatar" style="width:80px;height:80px;margin-right:20px;">
<CropperH5 ref="avatarImage" :fixedNumber="[1,1]" @getImageUrl='getAvatarImageUrl1' :image='avatarImage' />
</div>
<div class="avatar" style="width:80px;height:80px;">
<CropperH5 ref="avatarImage" :fixedNumber="[1,1]" @getImageUrl='getAvatarImageUrl2' :image='avatarImage' />
</div>
</div>
</template>
</van-field>
data() {
return {
avatarImage: "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=319223871,3949806150&fm=26&gp=0.jpg",
img1: '',
img2: ''
}
},
methods:{
//獲取頭像url
async getAvatarImageUrl1(file) {
var formData = new FormData();
formData.append('file', file); // 固定格式
const res = await upload(formData)
if (res) {
this.img1 = (res.data[0])
console.log(res);
}
},
async getAvatarImageUrl2(file) {
var formData = new FormData();
formData.append('file', file); // 固定格式
const res = await upload(formData)
if (res) {
this.img2 = (res.data[0])
console.log(res);
}
},
}
::v-deep .titleImg {
padding: 10px 0;
.van-cell__value {
display: flex;
}
.van-field__control {
display: none;
}
::v-deep .cropper-crop-box {
width: 300px;
height: 300px;
}
}