成大事不在于力量的大小,而在于能坚持多久。

小程序分享朋友圈

1、在HTML页面写上

1 <view class="mask_hb" wx:if="{{haibao}}"></view>
2 <view class="canvas-box" hidden='{{haibao == false}}'>
3 <canvas class="canvas" canvas-id="mycanvas" disable-scroll catchtouchmove='{{true}}' bindlongpress="handleLongPress" />
4 <view class="close_hb" wx:if="{{haibao}}">
5 <image src="{{hostUrl}}/icon/vip_close_Pop-ups.png" catchtap='close_hb'></image>
6 </view>
7 </view>
8 <!-- 使用canvas将分享图片和申请0元购图片合成一张图 -->
9 <canvas canvas-id="canvas" style="width:400px;height:400px;position: absolute;left:-1000px;top:-1000px;"></canvas>

2、在css样式上写样式

 1 /* 海报弹框 */
 2 
 3 .mask_hb {
 4   width: 100%;
 5   height: 100%;
 6   background-color: rgba(0, 0, 0, 0.6);
 7   position: fixed;
 8   top: 0;
 9   z-index: 99;
10 }
11 
12 .canvas-box {
13   width: 0;
14   height: 0;
15   position: fixed;
16   top: 3.9%;
17   left: 6%;
18   z-index: 99;
19 }
20 
21 .canvas {
22   width: 88%;
23   height: 89%;
24   position: fixed;
25   top: 3.9%;
26   left: 6%;
27   z-index: 99;
28 }
29 
30 .close_hb {
31   width: 100%;
32   display: flex;
33   justify-content: center;
34   position: fixed;
35   left: 0;
36   bottom: 2%;
37   z-index: 99;
38 }
39 
40 .close_hb image {
41   width: 50rpx;
42   height: 50rpx;
43 }

3、在js画canvas

  1  shareFriend: function() {
  2     var that = this
  3     var width
  4     var height
  5     wx.getSystemInfo({
  6       success(res) {
  7         width = res.screenWidth
  8         height = res.screenHeight
  9       }
 10     })
 11     var ctx = wx.createCanvasContext('mycanvas');
 12     var path_bg = '/assets/images/icon/bg.png'; //背景图片
 13     var path_bg2 = '/assets/images/icon/canvas_title.png';
 14     var path_logo = '/assets/images/icon/xuncaoji_icon.png'
 15     var path_partner = '/assets/images/icon/partner.png'
 16     var title = '"Free Buy",自由买,免费拿'
 17     var inviterCode = that.data.shareData.inviterCode
 18     //绘制图片模板的背景图片
 19     ctx.drawImage(path_bg, 0, 0, 0.88 * width, 0.89 * height);
 20     //绘制红色背景
 21     ctx.drawImage(path_bg2, 0, 0, 0.885 * width, 0.224 * height);
 22     // 绘制标题
 23     ctx.setFontSize(13);
 24     ctx.setFillStyle('#fff');
 25     ctx.setTextAlign("center")
 26     ctx.fillText(title, 0.442 * width, 25);
 27     ctx.stroke();
 28     // 绘制中间矩形
 29     ctx.beginPath()
 30     ctx.setFillStyle('#fff')
 31     ctx.setShadow(0, 0, 2, '#eee')
 32     ctx.fillRect(0.057 * width, 0.08 * height, 0.76 * width, 0.522 * height - 2)
 33     ctx.closePath()
 34     //绘制合伙人图标
 35     ctx.beginPath()
 36     ctx.drawImage(path_partner, 0.35 * width, 44, 64, 51);
 37     ctx.closePath()
 38     // 绘制邀请码
 39     if (inviterCode) {
 40       ctx.beginPath()
 41       ctx.setFontSize(19);
 42       ctx.setFillStyle('#F85A53');
 43       ctx.fillText(`我的邀请码:${inviterCode}`, 0.442 * width, 120);
 44       ctx.stroke();
 45       ctx.closePath()
 46     }
 47     // 绘制最小矩形
 48     ctx.beginPath()
 49     ctx.setFillStyle('#fff')
 50     ctx.setShadow(0, 0, 2, '#eee')
 51     ctx.fillRect(0.1308 * width, 130, 0.617 * width, 0.3 * height)
 52     ctx.closePath()
 53     // 绘制商品图片
 54     ctx.beginPath()
 55     ctx.drawImage(that.data.path_img, 0.1308 * width + 7, 137, 0.617 * width - 14, 0.3 * height - 14);
 56     ctx.closePath()
 57     // 绘制已抢数量
 58     ctx.beginPath()
 59     var number = `已抢数量:${that.data.shareData.grabbed}`
 60     var textWidth = inviterCode ? ctx.measureText(number).width : ctx.measureText(number).width + 50
 61     ctx.setFillStyle('#F5BA2C');
 62     ctx.moveTo(0.1308 * width, 174)
 63     ctx.lineTo(0.1308 * width, 180)
 64     ctx.lineTo(0.1308 * width - 10, 174)
 65     ctx.lineTo(0.1308 * width - 10, 150)
 66     ctx.lineTo(0.1308 * width + textWidth / 2 + 22, 150)
 67     ctx.lineTo(0.1308 * width + textWidth / 2 + 22, 174)
 68     ctx.lineTo(0.1308 * width - 10, 174)
 69     ctx.arc(0.1308 * width + textWidth / 2 + 22, 162, 12, 1.5 * Math.PI, 0.5 * Math.PI, false)
 70     ctx.closePath()
 71     ctx.fill()
 72     ctx.beginPath()
 73     ctx.setFontSize(12);
 74     ctx.setFillStyle('#fff');
 75     ctx.setTextAlign("left")
 76     ctx.fillText(number, 0.1308 * width, 167);
 77     ctx.closePath()
 78     ctx.stroke();
 79     // 绘制0元购图标
 80     var path_zero = "/assets/images/icon/apply free.png"
 81     ctx.beginPath()
 82     ctx.drawImage(path_zero, 0.52 * width, 0.36 * height, 0.2 * width, 0.2 * width);
 83     ctx.closePath()
 84     // 绘制价格
 85     ctx.beginPath()
 86     var price = `¥${that.data.shareData.price}`
 87     ctx.setFontSize(16);
 88     ctx.setFillStyle('#F85A53');
 89     ctx.setTextAlign("left")
 90     ctx.fillText(price, 0.1308 * width, 0.525 * height - 4);
 91     ctx.stroke();
 92     ctx.closePath()
 93     ctx.beginPath()
 94     // 绘制参与返
 95     ctx.setFillStyle('#F85A53');
 96     ctx.setStrokeStyle('#F85A53')
 97     var textWidth = ctx.measureText(`最高返¥ ${that.data.shareData.cashBack}`).width
 98     var textWidth2 = ctx.measureText(price).width
 99     ctx.moveTo(0.1 * width + textWidth2 + 24 - 6, 0.525 * height - 8)
100     ctx.lineTo(0.1 * width + textWidth2 + 24, 0.525 * height - 12)
101     ctx.lineTo(0.1 * width + textWidth2 + 24, 0.525 * height - 20)
102     ctx.lineTo(0.1 * width + textWidth2 + 24 + textWidth / 2 + 32, 0.525 * height - 20)
103     ctx.lineTo(0.1 * width + textWidth2 + 24 + textWidth / 2 + 32, 0.525 * height + 4)
104     ctx.lineTo(0.1 * width + textWidth2 + 24, 0.525 * height + 4)
105     ctx.lineTo(0.1 * width + textWidth2 + 24, 0.525 * height - 2)
106     ctx.lineTo(0.1 * width + textWidth2 + 24 - 6, 0.525 * height - 8)
107     ctx.closePath()
108     ctx.fill()
109     // 绘制参与返价格
110     var cashBack = `最高返¥ ${that.data.shareData.cashBack}`
111     ctx.beginPath()
112     ctx.setFontSize(12);
113     ctx.setFillStyle('#fff');
114     ctx.setTextAlign("left")
115     ctx.fillText(cashBack, 0.1 * width + textWidth2 + 28, 0.525 * height - 4);
116     ctx.stroke();
117     ctx.closePath()
118     // 绘制广告语
119     ctx.beginPath()
120     var adTips = '我是合伙人,上0元购,自由买免费拿,推荐此商品!'
121     ctx.setFontSize(14);
122     ctx.setFillStyle('#333333');
123     ctx.setTextAlign("left")
124     let chr = adTips.split('') // 分割为字符串数组
125     let temp = ''
126     let row = []
127     for (let a = 0; a < chr.length; a++) {
128       if (ctx.measureText(temp).width < 0.65 * width) {
129         temp += chr[a]
130       } else {
131         a--
132         row.push(temp)
133         temp = ''
134       }
135     }
136     row.push(temp)
137     for (var b = 0; b < row.length; b++) {
138       ctx.fillText(row[b], 0.1308 * width - 6, 0.565 * height - 4 + b * 20);
139     }
140     ctx.stroke();
141     ctx.closePath()
142     // 绘制二维码
143     ctx.setShadow(0, 0, 0, '#fff')
144     ctx.beginPath()
145     ctx.drawImage(that.data.appletQrCodeUrl, 0.3 * width, 0.6075 * height - 2, 0.3 * width, 0.3 * width);
146     ctx.closePath()
147     // 绘制扫码提示
148     ctx.beginPath()
149     var codeTips = '长按图片识别二维码查看领取'
150     ctx.setFontSize(12);
151     ctx.setFillStyle('#999999');
152     ctx.setTextAlign("center")
153     ctx.fillText(codeTips, 0.44 * width, 0.787 * height - 2);
154     ctx.stroke();
155     ctx.closePath()
156     ctx.draw()
157     setTimeout(function() {
158       wx.canvasToTempFilePath({
159         canvasId: 'mycanvas',
160         success: function(res) {
161           that.data.haibaoImg = res.tempFilePath
162         }
163       })
164     }, 1000)
165     that.setData({
166       showModalStatus1: false,
167       haibao: true
168     })
169   },
170   // 长按保存到相册
171   handleLongPress: function() {
172     var that = this
173     var tempFilePath = that.data.haibaoImg
174     wx.getSetting({
175       success(res) {
176         if (!res.authSetting['scope.writePhotosAlbum']) {
177           wx.authorize({
178             scope: 'scope.writePhotosAlbum',
179             success() {
180               console.log('授权相册')
181               wx.saveImageToPhotosAlbum({
182                 filePath: tempFilePath,
183                 success(res) {
184                   wx.hideLoading()
185                   console.log('保存图片成功回调')
186                   wx.showToast({
187                     title: '保存成功',
188                     icon: 'none'
189                   });
190                   that.setData({
191                     haibao: false
192                   })
193                 },
194                 fail(res) {
195                   wx.hideLoading()
196                   console.log('保存图片失败回调')
197                   console.log(res);
198                 }
199               })
200             },
201             fail() {
202               wx.hideLoading();
203               wx.showModal({
204                 title: '温馨提示',
205                 content: '您已拒绝授权,是否去设置打开?',
206                 confirmText: "确认",
207                 cancelText: "取消",
208                 success: function(res) {
209                   console.log(res);
210                   if (res.confirm) {
211                     console.log('用户点击确认')
212                     wx.openSetting({
213                       success: (res) => {
214                         console.log(res)
215                         res.authSetting = {
216                           "scope.writePhotosAlbum": true,
217                         }
218                         console.log("openSetting: success");
219                         wx.saveImageToPhotosAlbum({
220                           filePath: tempFilePath,
221                           success(res) {
222                             wx.hideLoading()
223                             wx.showToast({
224                               title: '保存成功',
225                               icon: 'none'
226                             });
227                             that.setData({
228                               haibao: false
229                             })
230                           },
231                           fail(res) {
232                             wx.hideLoading()
233                             console.log(res);
234                           }
235                         })
236                       }
237                     });
238                   } else {
239                     console.log('用户点击取消')
240                   }
241                 }
242               });
243 
244             }
245           })
246         } else {
247           console.log('保存图片')
248           wx.saveImageToPhotosAlbum({
249             filePath: tempFilePath,
250             success(res) {
251               wx.hideLoading()
252               console.log('保存图片成功回调')
253               wx.showToast({
254                 title: '保存成功',
255                 icon: 'none'
256               });
257 
258               that.setData({
259                 haibao: false
260               })
261             },
262             fail(res) {
263               wx.hideLoading()
264               console.log('saveImageToPhotosAlbum 失败回调')
265               console.log(res);
266             }
267           })
268         }
269       },
270       fail(res) {
271         wx.hideLoading()
272         console.log('wx.getSetting 失败回调')
273         console.log(res);
274       }
275     })
276 
277   },

 

posted @ 2020-03-20 19:10  雪绒花1124  阅读(434)  评论(0)    收藏  举报