<template>
<view class="pop_up" @touchmove.stop.prevent="" v-if="showDialog">
<!-- 生成海报的canvas -->
<view class="my-canvas-box" :class="posterInfo.status ? 'show' : 'show'" @click="posterInfo.status = false">
<canvas class="my-canvas" :style="{width:screen_width*375+'px',height:screen_height*0.65+'px'}"
canvas-id="myCanvas" @longpress.stop="download()"></canvas>
<view class="share_icon">
<view class="icons df al jcsb">
<button open-type="share" class="df al jc" @click="shareToFriend()">
<image src="@/static/images/fxhy.png" open-type="share"></image>
</button>
<button open-type="share" class="df al jc">
<image src="@/static/images/pyq.png" @click="shareToMoment()" class="shareq"></image>
</button>
<image src="@/static/images/fzlj.png" @click="copyLink()"></image>
<image src="@/static/images/xzbd.png" @click="download()"></image>
</view>
<view class="esc df al jc" @click="closeShow()">
取消
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
showDialog: {
type: Boolean,
default: false
},
datas: {
type: String,
default: ""
}
},
data() {
return {
showDialogs: false,
modalType: false, //弹框默认不显示
// 获取小程序码
maskData: '',
newMaskData: '',
context: '',
cyno: uni.getStorageSync('userInfo'),
imgUrl: this.datas,
posterInfo: {
status: false,
},
logoImg: '../static/images/天空之原LOGO.png',
radarImg: ''
}
},
onReady() {
this.getData()
this.context = uni.createCanvasContext('myCanvas', this);
},
created() {
this.getData()
},
methods: {
//1、先获取重要参数access_token
getData() {
//获取accessToken
let that = this;
// 小程序appid
const APP_ID = 'wxbcf3d7c72991ede8';
// 小程序app_secret
const APP_SECRET = '5460f25da2a921e27bd75281c490b97b';
let access_token = '';
uni.request({
//固定链接,不用改
url: "https://api.weixin.qq.com/cgi-bin/token",
data: {
grant_type: 'client_credential',
appid: APP_ID,
secret: APP_SECRET
},
success: function(res) {
access_token = res.data.access_token;
// 接口B:适用于需要的码数量极多的业务场景 生成的是小程序码
that.getQrCode(access_token);
}
})
},
//2、根据自己的逻辑调用获取
getQrCode(token) {
var that = this;
uni.request({
//固定链接,不用改
url: "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token,
method: 'POST',
//设置响应类型
responseType: 'arraybuffer',
data: {
// 必须是已经发布的小程序存在的页面(否则报错)
path: 'pages/index/index?custNo=' + that.cyno.custNo,
width: 280,
// 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
auto_color: false,
line_color: {
"r": "232",
"g": "232",
"b": "196"
} // auto_color 为 false 时生效,使用 rgb 设置颜色
},
success: function(res) {
//以图片的形式展示 image标签要转为base64格式
that.maskData = "data:image/PNG;BASE64," + uni.arrayBufferToBase64(res.data);
//我是海报需求 canvas绘制 所以还需要转
//声明文件系统
const fs = wx.getFileSystemManager();
//随机定义路径名称
var times = new Date().getTime();
var codeimg = wx.env.USER_DATA_PATH + '/' + times + '.png';
//将base64图片写入
fs.writeFile({
filePath: codeimg,
data: that.maskData.slice(22),
encoding: 'base64',
success: () => {
//newMaskData 就是转之后的路径
that.newMaskData = codeimg
that.generatePoster()
uni.setStorageSync("newMaskData", that.newMaskData)
}
});
}
})
},
shareToFriend() {
console.log(111);
// 在此处添加分享好友的逻辑
uni.share({
provider: "weixin",
scene: this.cyno.custNo,
type: 1,
summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!",
success: function(res) {
console.log("success:" + JSON.stringify(res));
},
fail: function(err) {
console.log("fail:" + JSON.stringify(err));
}
});
// return {
// title: '这是分享的标题',
// path: '/pages/index/index',
// imageUrl: '/static/share-image.jpg'
// }
},
shareToMoment() {
// 在此处添加分享朋友圈的逻辑
let that = this
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
let curRoute = routes[routes.length - 1].$page.fullPath // 获取当前页面路由,也就是最后一个打开的页面路由
uni.share({
provider: "weixin", //分享服务提供商(即weixin|qq|sinaweibo)
scene: 'WXSceneSession', //场景,可取值参考下面说明。
type: 0, //分享形式
href: "http://192.168.0.19:8889/platform-api", //跳转链接
title: "分享内容的标题", //分享内容的标题
summary: "分享内容的摘要", //分享内容的摘要
imageUrl: "封面图片地址", //图片地址
success: function(res) {
//成功后操作
},
fail: function(err) {
// 失败后操作
}
});
},
copyLink() {
// 在此处添加复制链接的逻辑
var pages = getCurrentPages() // 获取栈实例
let domain = 'http://xxxxxx.com/' //项目的域名
let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
let currentPage = domain + pages[pages.length - 1]['$page']['fullPath'] //当前页面路径(带参数)
uni.setClipboardData({
data: currentPage,
success: () => {
setTimeout(() => {
uni.showToast({
title: '已复制到粘贴板',
icon: 'none'
});
})
}
});
uni.hideToast();
},
getDowndimg(dataimg) {
uni.saveImageToPhotosAlbum({ //保存图片到系统相册。
filePath: dataimg, //图片文件路径
success: function() {
wx.hideLoading(); //隐藏 loading 提示框
wx.showModal({
title: '提示',
content: '保存成功',
modalType: false,
})
},
// 接口调用失败的回调函数
fail: function(err) {
if (err.errMsg ===
"saveImageToPhotosAlbum:fail:auth denied" ||
err
.errMsg ===
"saveImageToPhotosAlbum:fail auth deny" ||
err
.errMsg ===
"saveImageToPhotosAlbum:fail authorize no response"
) {
wx.showModal({
title: '提示',
content: '需要您授权保存相册',
modalType: false,
success: modalSuccess => {
wx.openSetting({
success(
settingdata
) {
if (settingdata
.authSetting[
'scope.writePhotosAlbum'
]
) {
wx.showModal({
title: '提示',
content: '获取权限成功,再次点击图片即可保存',
modalType: false,
})
} else {
wx.showModal({
title: '提示',
content: '获取权限失败,将无法保存到相册哦~',
modalType: false,
})
}
},
fail(
failData
) {
console
.log(
"failData",
failData
)
},
complete(
finishData
) {
console
.log(
"finishData",
finishData
)
}
})
}
})
}
},
complete(res) {
wx.hideLoading(); //隐藏 loading 提示框
}
})
},
download() {
var that = this;
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
success: function(res) {
console.log('成功', '对对对对');
that.getDowndimg(res.tempFilePath)
},
fail: function(res) {
console.log('失败', '3333');
}
}, that);
// 在此处添加下载本地的逻辑
wx.showLoading({
title: '加载中...'
});
//wx.downloadFile方法:下载文件资源到本地
},
// 弹窗
closeShow() {
this.$emit('getshow', this.showDialogs)
},
// 生成海报**
// 生成海报
generatePoster() {
// 这里是创建 canvas 绘图上下文
let context = this.context;
// 这里可以根据自己的需求显示加载动画
// 小程序码图片,下面可以放一个请求,来保存小程序码。然后赋值到qrImg这个变量上面
let qrImg = this.newMaskData;
// 给整个canvas填充白色背景(这个如果不加上的话,在APP端生成的海报没有白色背景)
const x = 0;
const y = 0;
const width = 600;
const height = 900;
const radius = 8;
context.save();
context.beginPath();
context.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
context.lineTo(x + width - radius, y);
context.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);
context.lineTo(x + width, y + height - radius);
context.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);
context.lineTo(x + radius, y + height);
context.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);
context.lineTo(x, y + radius);
context.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
context.closePath();
context.clip();
context.setFillStyle('#ffffff');
context.fillRect(0, 0, width, height);
context.draw();
// 绘制用户昵称
context.setFontSize(14);
context.setFillStyle('#000000');
// 这里根据自己的项目填写用户昵称的字段
context.fillText('来自YUAN的分享', 100, 38);
context.setFontSize(12);
// 绘制商品名称,这里是调用一个方法,按字符串长度进行分割换行。【可以做一个优化】
context.drawImage(this.logoImg, 20, 400, 130, 40);
// 绘制商品详情
uni.downloadFile({
url: this.imgUrl,
success: function(res) {
const x = 20;
const y = 55;
const width = 250;
const height = 320;
const radius = 8;
context.save();
context.beginPath();
context.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
context.lineTo(x + width - radius, y);
context.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);
context.lineTo(x + width, y + height - radius);
context.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);
context.lineTo(x + radius, y + height);
context.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);
context.lineTo(x, y + radius);
context.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
context.closePath();
context.clip();
context.drawImage(res.tempFilePath, x, y, width, height);
context.restore();
context.draw(true);
}
});
// 绘制小程序码
context.drawImage(qrImg, 200, 380, 70, 70);
// 绘制完成,让canvas显示【这里看自己项目,是否有loading动画】
this.posterInfo.status = true;
},
//*****
},
onShareAppMessage(res) {
if (res.from === 'button') { // 来自页面内分享按钮
return {
title: "hhhh", //分享标题
path: '/pages/tzss/tzss?ids=' + JSON.stringify(this.productList), //分享页面路径
imageUrl: '', //分享图标
desc: '', //自定义分享描述
}
this.search()
} else {
return {
title: "哈哈哈", //分享标题
path: '/pages/tzss/tzss?id=' + this.nr, //分享页面路径
imageUrl: '', //分享图标
desc: '', //自定义分享描述
}
}
},
}
</script>
<style scoped lang="scss">
.cs {
position: fixed;
top: 200rpx;
}
// 弹窗
.pop_up {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
}
// 功能按钮
.share_icon {
height: 280rpx;
width: 100%;
background-color: #ededed;
border-radius: 10rpx 10rpx 0 0;
position: fixed;
bottom: 0;
box-shadow: 4rpx 0rpx 49rpx 0rpx #5a5a5a;
.icons {
background-color: #fff;
height: 70%;
width: 100%;
padding: 0 60rpx;
box-sizing: border-box;
border-radius: 10rpx 10rpx 0 0;
image {
width: 95rpx;
height: 121rpx;
font-size: 24rpx;
color: #ccc;
}
button {
background-color: rgba(0, 0, 0, 0);
padding: 0;
margin: 0;
}
.shareq {
width: 80rpx;
height: 121rpx;
}
}
}
.esc {
height: 30%;
width: 100%;
background-color: #fff;
margin-top: 10rpx;
color: #848484;
}
// 生成海报
.my-canvas-box {
width: 750rpx;
height: 100%;
position: fixed;
background-color: rgba(0, 0, 0, 0.6);
z-index: 99;
&.hide {
display: none;
}
&.show {
display: block;
}
.canvas-tip {
color: #ffffff;
font-size: 24rpx;
margin-top: 30rpx;
text-align: center;
}
/* #ifdef MP-WEIXIN */
.my-canvas {
width: 600rpx;
height: 950rpx;
border-radius: 10rpx;
background-color: #ffffff;
margin: 80rpx auto;
}
/* #endif */
/* #ifdef APP-PLUS || H5 */
.my-canvas {
width: 600rpx;
height: 950rpx;
border-radius: 10rpx;
background-color: #ffffff;
margin-top: 220rpx auto;
}
/* #endif */
}
</style>