/* * 本时生活 */ Page({ /** * 页面的初始数据 */ data: { qrcode : '', //二维码 userInfo: '', //我的信息 isImgLay: false, //是否显示图片弹出层 parentId: '' //是否分享进入 }, /** * 生命周期函数--监听页面加载 */ onLoad (options) { // 判断是否由分享进入 if(options.type == "share"){ // 写入缓存 this.setData({ parentId: options.parent_id }) } wx.$api.user.myshare(this.data.parentId).then(res=>{ this.setData({ qrcode : res.data.qrcode, userInfo : res.data.user }) }) }, /** * 绘图 */ dowImg(){ wx.showLoading({ title: '加载中', }) let avatarImg = new Promise(success=>{ wx.getImageInfo({ src : this.data.userInfo.avatar, success : res => { success(res.path) } }) }) // 下载素材 let codeImg = new Promise(success => { wx.getImageInfo({ src : this.data.qrcode, success : res => { success(res.path) } }) }) Promise.all([codeImg, avatarImg]).then(res => { // 绘制海报 const ctx = wx.createCanvasContext('qrcodeCard') ctx.save() // 绘制背景 ctx.setFillStyle('#f7662d') ctx.fillRect(0, 0, 375, 603) // 绘制背景 ctx.drawImage('/static/img/user-codeImg-down.png', 0, 0, 375, 650) // 绘制二维码 ctx.drawImage(res[0], 114, 390, 150, 150) // 文字 ctx.setFontSize(16) ctx.setFillStyle("#2f3245") ctx.setTextAlign('center') ctx.fillText(this.data.userInfo.nickname, 194, 350 , 270) // 文字 ctx.setFontSize(14) ctx.setFillStyle("#af7700") ctx.setTextAlign('center') ctx.fillText(this.data.userInfo.nickname + " -- " + "邀请您进入本时生活", 188, 570 , 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 => { console.log(e) } }) } }) }) }).catch(err=>{ wx.showToast({ title: '海报下载,请检查网络', icon : 'none' }) }) }, /** * 图片弹出层 */ removeSaveImg(){ this.setData({ isImgLay: !this.data.isImgLay }) }, /** * 保存图片到本地 */ saveImg(){ this.dowImg(); this.setData({ isImgLay: !this.data.isImgLay }) }, /** * 分享 */ onShareAppMessage(){ return { title : this.data.userInfo.nickname + "邀请您进入本时生活", imageUrl: "", query : "/pages/code/code?parent_id=" + this.data.userInfo.user_id + "&type=share" } } })