Files
cardtest/pages/code/code.js
zhangmanman 8f7cc986c2 [更新]
2021-08-13 13:30:35 +08:00

202 lines
5.6 KiB
JavaScript

/*
* 本时生活
*/
Page({
/**
* 页面的初始数据
*/
data: {
qrcode : '', //二维码
userInfo : '', //我的信息
isImgLay : false, //是否显示图片弹出层
shareState : false, //显示分享朋友圈弹出层
sharesData : '' //分享携带参数
},
/**
* 生命周期函数--监听页面加载
*/
onLoad (options) {
if(getApp().globalData.scene === 1155 && options.type === "shareLogin"){
wx.switchTab({
url: '/pages/index/index',
})
}
// 获取用户信息和二维码
wx.$api.user.myshare('mini','','pages/index/index?type=shareLogin').then(res=>{
this.setData({
qrcode : res.data.qrcode,
userInfo : res.data.user
})
}).catch(err=>{})
// 获取分享好友标题
wx.$api.user.wechatShares().then(res=>{
this.setData({
sharesData: res.data
})
}).catch(err=>{})
},
/**
* 绘图
*/
dowImg(){
wx.showLoading({
title: '加载中',
})
let avatarImg = new Promise(success=>{
wx.getImageInfo({
src : '',
success : res => {
success(res.path)
}
})
})
// 下载素材
let codeImg = new Promise(success => {
wx.getImageInfo({
src : this.data.qrcode,
success : res => {
success(res.path)
}
})
})
Promise.all([codeImg]).then(res => {
// 绘制海报
const ctx = wx.createCanvasContext('qrcodeCard')
ctx.save()
// 绘制背景
ctx.setFillStyle('#e2e2e2')
ctx.fillRect(0, 0, 375, 603)
// 绘制背景
ctx.drawImage('/static/img/user-codeImg-down.png', 0, 0, 375, 650)
// 绘制二维码
ctx.drawImage(res[0], 114, 470, 150, 150)
// 文字
// ctx.setFontSize(16)
// ctx.setFillStyle("#2f3245")
// ctx.setTextAlign('center')
// ctx.fillText(this.data.userInfo.nickname, 194, 350 , 270)
// 文字
ctx.setFontSize(40)
ctx.setFillStyle("#222222")
ctx.setTextAlign('center')
ctx.fillText("扫码享福利", 188, 400 , 270)
// 文字
ctx.setFontSize(18)
ctx.setFillStyle("#222222")
ctx.setTextAlign('center')
ctx.fillText("扫描二维码加入本时生活", 188, 440 , 270)
ctx.save();
ctx.beginPath(); //开始绘制
ctx.arc(70 / 2 + 156, 70 / 2 + 250, 70 / 2, 0, Math.PI * 2, false);
ctx.clip();
// ctx.drawImage(res[1], 156, 250, 70, 70);
// 保存图片
ctx.draw(true, () => {
wx.hideLoading()
wx.canvasToTempFilePath({
canvasId: 'qrcodeCard',
x: 0,
y: 0,
success: res => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success : res=>{
wx.showToast({
title: '分享海报已保存至相册',
icon : 'none'
})
},
fail : err=>{
wx.showModal({
title : '提示',
content : '暂未授权小程序写入您的相册,无法存储二维码海报',
confirmColor: '#d82526',
confirmText : '去设置',
success : info=>{
if (info.confirm){
wx.openSetting()
}
}
})
},
complete: e => {}
})
}
})
})
}).catch(err=>{
wx.showToast({
title: '海报下载,请检查网络',
icon : 'none'
})
})
},
/**
* 图片弹出层
*/
removeSaveImg(){
this.setData({
isImgLay: !this.data.isImgLay
})
},
/**
* 保存图片到本地
*/
saveImg(){
this.dowImg();
this.setData({
isImgLay: !this.data.isImgLay
})
},
/**
* 分享朋友圈弹出层状态
*/
shareShow() {
this.setData({
shareState: !this.data.shareState
})
},
/**
* 分享
*/
onShareAppMessage(){
return {
title : this.data.userInfo.nickname + this.data.sharesData.title,
path : "/pages/index/index?parent_id=" + this.data.userInfo.user_id + "&type=shareLogin",
imageUrl: this.data.sharesData.image
}
},
/**
* 用户点击右上角分享朋友圈
*/
// onShareTimeline () {
// return {
// title : this.data.userInfo.nickname + this.data.sharesData.title,
// query : "type=shareLogin",
// imageUrl: this.data.sharesData.image
// }
// }
})