<template>
<view class="page">
<uni-popup ref="popup" type="center">
<view class="invitePoster">
<image @click="cacel"
class="closeIcon" src="../../static/common/Btn_Close.png" mode=""></image>
<!-- #ifdef H5 -->
<!-- h5端部分机型用uniappimg标签无法长按保存,用回原生img标签 -->
<img class="strategyImgs" :src="data.imgURL" />
<!-- #endif -->
<!-- #ifdef APP -->
<image class="strategyImgs" :src="data.imgURL" mode="widthFix"></image>
<!-- #endif -->
<!-- #ifdef APP -->
<view class="popupInfo">
<view class="buttons">
<view class="button1" @click="download">
<text>下载海报</text>
</view>
</view>
</view>
<!-- #endif -->
</view>
</uni-popup>
<!-- 组件方式生成海报和二维码,需在页面添加标签,并且生成后会直接在页面显示出来,(放在页面下方,并且阻止用户滑动下去发现。如果标签没有生成是不会画图的,只能这样取巧挡住用户发现)因此生成海报后隐藏两个标签 也可以把标签放在弹窗里面直接当做海报生成出来,但需自己调试 -->
<canvas ref="myCanvas" canvas-id="myCanvas" v-show="data.showcanvas"
style="width: 563rpx;height: 1001rpx;margin-top: 100px;"></canvas>
<uqrcode ref="uqrcodeRef" style="margin-top: 100px;" v-show="data.showcanvas"
canvas-id="uqrcode" value="123" :options="{ margin: 2 }"></uqrcode>
</view>
</template>
<script lang='ts' setup>
import { ref ,computed,onMounted} from "vue"
import UQRCode from '../../uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';
const popup = ref(null)
const uqrcodeRef = ref(null)
const data = ref({
isShowPopup:false,
qrCodeUrl:"",
imgURL:'',
showcanvas:true
})
const APPsetImage = () => {
if (data.value.imgURL) {
return
}
const codeData = "二维码需要的内容"
const qr = new UQRCode()
console.log(qr);
// 设置二维码内容
qr.data = codeData;
// 设置二维码大小,必须与canvas设置的宽高一致
qr.size = 200;
// 调用制作二维码方法
qr.make();
// 获取canvas上下文
var canvasContext = uni.createCanvasContext('uqrcode', this);
qr.canvasContext = canvasContext;
qr.drawCanvas().then((res)=>{
console.log(res);
})
setTimeout(() => {
uni.canvasToTempFilePath(
{
canvasId: 'uqrcode',
success: res => {
console.log(res.tempFilePath);
let imgUrl = '../../static/invite/Bg_Sharepop.png';
let posterQrCodeImg = res.tempFilePath;
const ctx = uni.createCanvasContext('myCanvas', this);
console.log(posterQrCodeImg,22222);
uni.getImageInfo({
src: imgUrl,
success(res) {
ctx.drawImage(res.path, 0, 0, 310, 556)
ctx.drawImage(posterQrCodeImg, 38, 174,135,135);
ctx.fillStyle = "#f4ce8d";
ctx.font = '16px "my-font-bangbang"';
ctx.fillText('要显示的文字', 16,322)
ctx.draw(false, (ret) => {
console.log(ret);
uni.canvasToTempFilePath({
x: 0,
y: 0,
width: 563,
height: 1001,
destWidth: 563,
destHeight: 1001,
canvasId: 'myCanvas',
quality: 1,
complete: function (res) {
console.log(res.tempFilePath);
data.value.imgURL=res.tempFilePath;
data.value.showcanvas = false
}
});
});
}
})
},
fail: err => {
console.log(err);
}
},
);
}, 300);
}
const show = () => {
if (!token) {
uni.navigateTo({
url:"/pages/login/login"
})
}else {
APPsetImage()
popup.value.open('center')
}
}
const copy = () => {
uni.setClipboardData({
data:userInfo.info.inviteCode,
success:() => {
data.value.isShowPopup=true
}
})
}
const download = () => {
popup.value.close('center')
let a = document.createElement('a')
a.download = ''
a.href = data.value.imgURL
document.body.appendChild(a)
a.click()
a.remove()
// #endif
// #ifdef APP
uni.saveImageToPhotosAlbum({
filePath:data.value.imgURL,
success: () => {
uni.showToast({
title: '保存成功',
duration:1000
});
},
fail: () => {
uni.showToast({
title: '保存失败',
icon:"error",
duration:2000
});
}
})
// #endif
}
const cacel = () => {
popup.value.close('center')
}
</script>
<style scoped lang='scss'>
.invite{
min-height: 100vh;
display: flex;
flex-flow: column;
align-items: center;
overflow-y: auto;
background: url(../../static/invite/Bg_Basemap.png) ;
background-size: 100% 100%;
.topImgs{
width: 153px;
color: #fff;
}
}
</style>