const app=getApp(); const utils = require("../../utils/http"); let winwidth = 0; Component({ /** * 组件的属性列表 */ properties: { showTask: { type: Boolean, value: true }, wshareshow: { type: Boolean, value:false }, posterimg:{ type:String, value:'' }, qrcode:{ type:String, value:'' }, title:{ type:String, value:'' }, islist: { type: Boolean, value:false }, imgurlflag: { type: Boolean, value:false,//默认是false, }, type: { type: String, value: "" }, contentid: { type: Number|String, value: "" } }, /** * 组件的初始数据 */ data: { imgUrl:app.globalData.imgUrl, imgflag:false, cangth: 100, shareImg:'' }, /** * 组件的方法列表 */ methods: { dothis:function(){}, cancletap:function(){ this.setData({ wshareshow:false }) }, // sharefriendtap:function(){ // var that=this; // that.triggerEvent("sfriend", '') // this.setData({ // wshareshow:false // }) // }, shareurltap:function(){ var that=this; that.triggerEvent("shareurl", '') this.setData({ wshareshow:false }) }, setposter:function(){ var that=this; wx.getSystemInfo({ success: (res) => { winwidth = res.windowWidth; } }) // that.setData({ // imgflag:true // }) // setTimeout(function(){ // that.createdCode(); // },500) that.triggerEvent("poster", '') this.setData({ wshareshow:false }) }, sendfriend:function(){ var that=this; that.triggerEvent("sendfriendcircle", '') this.setData({ wshareshow:false }) }, setTasktap: function() { const that = this; utils.$get({ url: app.globalData.webUrl + 'api/agentEmp/set_share_content', header: { 'Authorization': 'bearer ' + app.globalData.token }, data: { content_id: that.data.contentid, type: that.data.type }, success: function (res) { if (res.data.code == '0') { wx.showToast({ title: res.data.msg, icon: "none", duration: 1500 }) } } }) }, //开始绘图 createdCode: function () { let that = this; if(that.data.title.length>26){ that.setData({ title:that.data.title.substring(0,23)+'...' }) } var posterimg=that.data.posterimg; posterimg = posterimg.replace('http://o.nczyzs.com/',"https://o.nczyzs.com/"); var qrcode=that.data.qrcode; qrcode = qrcode.replace('http://o.nczyzs.com/',"https://o.nczyzs.com/"); let topwidth = winwidth * 590 / 750,topheight = ''; // const ctx = wx.createCanvasContext('myCanvas'); //绘图上下文 wx.createSelectorQuery().select('#myCanvas') .fields({ node: true, size: true }) .exec((rest) => { console.log(rest) const canvas = rest[0].node; const ctx = canvas.getContext('2d') wx.getImageInfo({ src: posterimg, success(res) { wx.getImageInfo({ src: qrcode, success(rs) { topheight = topwidth * res.height / res.width; that.setData({ cangth: topheight + 64 }) ctx.save(); ctx.drawImage(res.path, 0, 0, topwidth, topheight); ctx.rect(0, topheight, topwidth, 64); ctx.setFillStyle('#FFFFFF'); ctx.fill(); ctx.restore() ctx.setFontSize(14); ctx.fillText("@" + app.globalData.personMsg.nickname, 12, topheight + 24, topwidth - 70); // ctx.fillText(that.data.title, 12, topheight + 48, topwidth - 70); ctx.drawImage(rs.path, topwidth - 52, topheight + 12, 40, 40); ctx.draw(); wx.hideLoading(); setTimeout(() => { wx.canvasToTempFilePath({ x: 0, y: 0, canvasId: 'myCanvas', success: function (res) { let shareImg = res.tempFilePath; that.setData({ shareImg: shareImg, }) }, fail: function (res) {} }) },500) },fail(res){ console.log(res) }, }) },fail(res){ console.log(res) } }) }) }, saveimgtap: function () { let that = this; // 获取用户是否开启用户授权相册 wx.getSetting({ success(res) { // 如果没有则获取授权 if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success() { wx.saveImageToPhotosAlbum({ filePath: that.data.shareImg, success() { wx.showToast({ title: '保存成功' }) }, fail() { wx.showToast({ title: '保存失败', icon: 'none' }) } }) }, fail() { // 如果用户拒绝过或没有授权,则再次打开授权窗口 //(ps:微信api又改了现在只能通过button才能打开授权设置,以前通过openSet就可打开,下面有打开授权的button弹窗代码) that.setData({ openSet: true }) } }) } else { // 有则直接保存 wx.saveImageToPhotosAlbum({ filePath: that.data.shareImg, success() { wx.showToast({ title: '保存成功' }) }, fail() { wx.showToast({ title: '保存失败', icon: 'none' }) } }) } } }) }, } })