const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px' var config = require("../../config/config.js"); var app = getApp(); const Http = require("../../utils/HttpBasics"); const util = require("../../utils/util"); const imgurl = require("../../utils/imgurl"); Page({ /** * 页面的初始数据 */ data: { shareDefaultTp: imgurl.shareDefaultTp.url, navigationBarHeight, windowWidth: wx.getSystemInfoSync().windowWidth, windowHeight: wx.getSystemInfoSync().screenHeight, totalHeight: 0, isdisable: true, mallname: '', //商场名字 qrCodeL: "", //小程序码 couponList: [], //活动劵列表 canvasScale: 1.0 // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图 }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { this.setCanvas() }, onShow: function() { }, //绘制微信头像 drawAvatarimg(ctx, scale) { var that = this; //商户详情海报 console.log(that.data.avatarUrl, "???") let src1 = that.data.avatarUrl + `?imageView/2/w/${100}/h/${100}`; let src2 = util.getProxyImgUrl(src1); wx.getImageInfo({ src: src2, success: function(res) { console.log(res.path) that.darwAvatarArc(ctx, res.path, 10, 18, 35, 35); } }); }, setCanvas() { wx.showLoading({ title: "生成中..." }); var tha // 适配屏幕 let scale = this.data.windowWidth / 375.0 console.log(scale) this.setData({ totalHeight: 667 * scale }) // 获取Canvas let ctx = wx.createCanvasContext("myCanvas"); this.getQrcodeFx(ctx, scale) this.getMallInfo(ctx, scale) this.getUserInfo(ctx, scale) ctx.setFillStyle("#fff"); ctx.fillRect(0, 0, this.data.windowWidth, this.data.windowHeight); this.drawNormalText(ctx, "长按二维码识别", 180 * scale, 360 * scale, 16 * scale, "#000", "left", "middle") this.drawNormalText(ctx, "尽享更多优惠活动!", 170 * scale, 390 * scale, 16 * scale, "#000", "left", "middle") ctx.draw(true); this.setData({ isdisable: false, }) setTimeout(function() { wx.hideLoading() }, 2000) }, // 绘制只有一行的文字 drawNormalText(ctx, str, x, y, font, style, align, baseLine) { ctx.setFontSize(font); ctx.setFillStyle(style); ctx.setTextAlign(align); ctx.setTextBaseline(baseLine); ctx.fillText(str, x, y); ctx.draw(true); }, //商场信息 getMallInfo: function(ctx, scale) { let that = this; // 获取用户信息 Http.get({ url: config.api.getMallInfo, data: {} }).then(res => { console.log(res); let src1 = "" if (res.data.img) { src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`; } else { src1 = that.data.shareDefaultTp + `?imageView/2/w/${100}/h/${100}`; //默认图片地址 } let src2 = util.getProxyImgUrl(src1); wx.getImageInfo({ src: src2, success: function(res) { ctx.drawImage(res.path, ((that.data.windowWidth -320 )/4 )* scale, 80 * scale, 320 * scale, 200 * scale); ctx.draw(true); } }); that.setData({ mallname: res.data.name }); if (res.data.addr){ that.canvasTextAutoLine('详细地址:' + res.data.addr, ctx, 20 * scale, 480 * scale, 14 * scale, "#888", "left", "middle", 26 * scale, 330 * scale, 2) } if (res.data.servicePhone){ that.drawNormalText(ctx, '客服电话:' + res.data.servicePhone, 20 * scale, 540 * scale, 16 * scale, "#888", "left", "middle", scale); } that.drawNormalText(ctx, "---" + that.data.mallname + "---", 90 * scale, 62 * scale, 16 * scale, "#000", "left", "middle", scale); }); }, //获取分享小程序二维码 getQrcodeFx(ctx, scale) { let that = this let coImageWidth = parseInt(120 * scale); let coImageHeight = parseInt(120 * scale); Http.get({ url: config.api.getQrcodeFx, }).then(res => { console.log(res.data) let src1 = res.data + `?imageView/2/w/${100}/h/${100}`; let src2 = util.getProxyImgUrl(src1); wx.getImageInfo({ src: src2, success: function(res) { console.log(res.path) that.darwAvatarArc(ctx, res.path, 30 * scale, 310 * scale, coImageWidth, coImageHeight, scale); } }); }) }, //获取头像和昵称 getUserInfo: function(ctx, scale) { let that = this; // 获取用户信息 Http.get({ url: config.api.getScore, data: {} }).then(res => { let nickName = ''; if (res.data.nickName == undefined || res.data.nickName == undefined) { wx.getUserInfo({ success: function(data) { nickName = data.userInfo.nickName; that.setData({ nickName: nickName, //用户名 avatarUrl: data.userInfo.avatarUrl //用户头像 }); console.log(data.userInfo.avatarUrl, "????--**") this.drawNormalText(ctx, nickName, 50 * scale, 34 * scale, 16 * scale, "#000", "left", "middle"); this.drawNormalText(ctx, " 向您推荐", (ctx.measureText(this.data.nickName).width + 60) * scale * scale, 32 * scale, 16 * scale, "#848484", "left", "middle", scale); that.drawAvatarimg(ctx, scale) } }) } else { nickName = res.data.nickName; that.setData({ nickName: nickName, avatarUrl: res.data.avatarUrl }); this.drawNormalText(ctx, nickName, 48 * scale, 34 * scale, 16 * scale, "#000", "left", "middle"); this.drawNormalText(ctx, " 向您推荐", (ctx.measureText(this.data.nickName).width + 60) * scale * scale, 32 * scale, 16 * scale, "#848484", "left", "middle", scale); that.drawAvatarimg(ctx, scale) } }); }, canvasTextAutoLine: function(str, ctx, initX, initY, font, style, align, baseLine, lineHeight, maxWidth, row = 1) { var lineWidth = 0; var lastSubStrIndex = 0; var currentRow = 1; for (let i = 0; i < str.length; i++) { lineWidth += ctx.measureText(str[i]).width; if (lineWidth > maxWidth) { currentRow++; let newStr = str.substring(lastSubStrIndex, i); if (currentRow > row && str.length > i) { newStr = str.substring(lastSubStrIndex, i - 2) + "..."; } ctx.setFontSize(font); ctx.setFillStyle(style); ctx.setTextAlign(align); ctx.setTextBaseline(baseLine); ctx.fillText(newStr, initX, initY); initY += lineHeight; lineWidth = 0; lastSubStrIndex = i; if (currentRow > row) { break; } } if (i == str.length - 1) { ctx.setFontSize(font); ctx.setFillStyle(style); ctx.setTextAlign(align); ctx.setTextBaseline(baseLine); ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY); } } }, //绘制圆角图像 darwAvatarArc: function(ctx, src, x, y, w, h = w) { /* ctx: 画布对象 src: 头像缓存路径 x: 头像起始位置 横坐标 y: 头像起始位置 纵坐标 w: 头像宽度 h: 头像高度,不传为w */ // 保存绘图上下文。 ctx.save(); // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边 ctx.beginPath(); // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。 // 用 stroke 或者 fill 方法来在 canvas 中画弧线。 ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false); /* 从原始画布中剪切任意形状和尺寸。 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内( 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。 */ ctx.clip(); // 画头像 ctx.drawImage(src, x, y, w, h); // 恢复之前保存的绘图上下文。 ctx.restore(); // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。 ctx.draw(true); }, /** * 保存图片 */ saveImage() { let that = this; wx.canvasToTempFilePath({ x: 0, y: 0, width: this.data.windowWidth * this.data.canvasScale, height: this.data.totalHeight * this.data.canvasScale, canvasId: "myCanvas", success: function(res) { that.saveImageToPhotos(res.tempFilePath); }, fail: function(res) { wx.showToast({ title: "图片生成失败", icon: "none", duration: 2000 }); } }); }, //保存到相册 saveImageToPhotos: function(tempFilePath) { wx.saveImageToPhotosAlbum({ filePath: tempFilePath, success(result) { wx.showToast({ title: "保存成功,从相册中分享到朋友圈吧", icon: "none", duration: 4000 }); }, fail: function(err) { if ( err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" ) { // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用 wx.showModal({ title: "提示", content: "需要您授权保存相册", showCancel: false, success: modalSuccess => { wx.openSetting({ success(settingdata) { console.log("settingdata", settingdata); if (settingdata.authSetting["scope.writePhotosAlbum"]) { wx.showModal({ title: "提示", content: "获取权限成功,再次点击图片即可保存", showCancel: false }); } else { wx.showModal({ title: "提示", content: "获取权限失败,将无法保存到相册哦~", showCancel: false }); } }, fail(failData) { console.log("failData", failData); }, complete(finishData) { console.log("finishData", finishData); } }); } }); } } }); } });