/* * 本时生活 */ Page({ /** * 页面的初始数据 */ data: { qrcode : '', //二维码 userInfo : '', //我的信息 isImgLay : false,//是否显示图片弹出层 parentId : '', //是否分享进入 shareState : false //显示分享朋友圈弹出层 }, /** * 生命周期函数--监听页面加载 */ onLoad (options) { // 获取二维码 wx.$api.user.publicCode('mini', 'recharge', 'pages/userStored/userStored?type=shareStored').then(res=>{ this.setData({ qrcode : res.data.qrcode, userInfo : res.data.user }) }).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-storedCode-down.png', 0, 0, 375, 650) // 绘制二维码 ctx.drawImage(res[0], 114, 400, 150, 150) // 文字 // ctx.setFontSize(16) // ctx.setFillStyle("#2f3245") // ctx.setTextAlign('center') // ctx.fillText(this.data.userInfo.nickname, 194, 350 , 270) // 文字 ctx.setFontSize(26) ctx.setFillStyle("#222222") ctx.setTextAlign('center') ctx.fillText("向重要的人分享生活的惊喜", 194, 350 , 350) 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 + "邀请您进入本时生活", path : "/pages/userStored/userStored?parent_id=" + this.data.userInfo.user_id + "&type=shareStored", imageUrl: "/static/img/storedCode.png" } }, /** * 用户点击右上角分享朋友圈 */ // onShareTimeline: function () { // return { // title : this.data.userInfo.nickname + "邀请您进入本时生活", // path : "/pages/userStored/userStored?parent_id=" + this.data.userInfo.user_id + "&type=shareStored", // imageUrl: "/static/img/storedCode.png" // } // } })