Files
ebiz-h5/src/views/ebiz/generateImg/generateImg.vue
2024-10-24 10:38:32 +08:00

204 lines
5.3 KiB
Vue

<template>
<div>
<div id="capture" ref="generateImg" style="height: 100vh; overflow: hidden">
<img src="@/assets/images/list_img.jpg" alt="" />
<div class="box">
<div class="title">
<p class="organ">{{ orderList.manageComName }}</p>
<p class="name">{{ orderList.name }}</p>
</div>
<div class="pName">{{ orderList.productName }}</div>
<div class="type">{{ orderList.outOrderType == 'accept' ? '承保规保' : '预收规保' }}</div>
<div class="yuan">{{ orderList.prem }}<span></span></div>
<div class="time">{{ orderList.outOrderType == 'accept' ? '承保时间:' : '预收时间:' }}{{ orderList.appntDate }}</div>
</div>
</div>
<div class="flex justify-content-s bottom-btn border-color">
<van-button square type="default" size="large" @click="shareImg" v-no-more-click="1000"> 分享 </van-button>
<van-button square type="danger" size="large" @click="downLoadImagesFunc" v-no-more-click="1000"> 下载 </van-button>
</div>
</div>
</template>
<script>
import html2canvas from 'html2canvas'
export default {
name: 'generateImg',
data() {
return {
orderList: {}
}
},
mounted() {
this.getList()
},
methods: {
getList() {
this.orderList = JSON.parse(localStorage.getItem('orderList'))
},
// 将 Blob转base64
blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
})
},
// 分享
shareImg() {
// const element = document.getElementById('capture')
// const canvas = await html2canvas(element)
html2canvas(this.$refs.generateImg, {
width: this.$refs.generateImg.offsetWidth,
height: this.$refs.generateImg.offsetHeight,
backgroundColor: '#fff',
scale: 2,
dpi: 300
}).then((canvas) => {
// 将 canvas 转换为 Blob
canvas.toBlob((blob) => {
this.blobToBase64(blob).then((base64String) => {
let base64Manane = base64String.split(',')[1]
EWebBridge.webCallAppInJs('bridge', {
flag: 'share',
extra: {
shareType: '1',
img: base64Manane
}
})
})
}, 'image/png')
})
},
// 生成图片并下载
domToImage(domElement, quality, callback) {
html2canvas(domElement, {
width: domElement.offsetWidth,
height: domElement.offsetHeight,
backgroundColor: '#fff',
scale: 2,
dpi: 300
}).then((canvas) => {
// 转换canvas为PNG图片并压缩
canvas.toBlob(
(blob) => {
// const a = document.createElement('a')
// a.href = URL.createObjectURL(blob)
// a.download = '业绩贺报.png'
// a.click()
// let newimg = new Image()
// newimg.src = URL.createObjectURL(blob)
// newimg.onload = function () {
// URL.revokeObjectURL(newimg.src)
// callback(newimg)
// }
this.blobToBase64(blob).then((base64String) => {
console.log(base64String, '==')
EWebBridge.webCallAppInJs('download', {
name: '业绩贺报.png',
url: base64String
})
.then(() => {
console.log('下载完成')
})
.catch(() => {
this.$toast.fail('图片下载失败,请重新下载!')
})
})
},
'image/png'
// quality
)
})
},
downLoadImagesFunc() {
this.domToImage(this.$refs.generateImg)
}
}
}
</script>
<style lang="scss" scoped>
img {
height: 100%;
width: 100%;
position: relative;
}
.box {
position: absolute;
width: 80%;
height: 40%;
top: 38%;
left: 10%;
.title {
position: absolute;
width: 100%;
// left: 30%;
top: 24%;
font-weight: bold;
p {
display: flex;
justify-content: center;
}
.organ {
font-size: 12px;
color: #b7170b;
}
.name {
font-size: 17px;
color: #b7170b;
}
}
.pName {
position: absolute;
top: 44%;
font-size: 15px;
color: #efbc4f;
width: 100%;
display: flex;
justify-content: center;
}
.type {
font-size: 22px;
color: #efbc4f;
position: absolute;
top: 53%;
width: 100%;
display: flex;
justify-content: center;
}
.yuan {
font-size: 30px;
color: #efbc4f;
position: absolute;
top: 67%;
width: 100%;
text-align: center;
display: inline-block;
vertical-align: bottom;
span {
font-size: 14px;
margin-left: 5px;
}
}
.time {
font-size: 14px;
color: #efbc4f;
position: absolute;
top: 85%;
width: 100%;
display: flex;
justify-content: center;
}
}
.border-color {
z-index: 9999;
border: 2px solid;
border-image: radial-gradient(circle, rgba(255, 233, 124, 1), rgba(222, 144, 34, 1)) 2 2;
}
/deep/ .van-button--danger {
background: linear-gradient(to right, #f26e43, #ac0209) !important; /*设置按钮为渐变颜色*/
border: none !important;
}
</style>