微信小程序 - 生成二維碼


生成二維碼有兩種方式:

前端生成(僅展示用)

 

 1 onShow: function() {
 2 
 3    4 
 5     let _this = this;
 6     wx.request({
 7       url: 'https://api.weixin.qq.com/cgi-bin/token',
 8       data: {
 9         grant_type: 'client_credential',
10         appid: '填寫appid', //不能缺少
11         secret: '填寫app秘鑰' //不能缺少
12       },
13       success: function(res) {
14 
15         wx.request({
16           url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + res.data.access_token,
17           data: {
18             // "path": "pages/index/index", 默認跳轉到主頁:pages/index/index,可指定
19             "width": 430,
20             "scene": wx.getStorageSync('uid')
21           },
22           responseType: 'arraybuffer', // 這行很重要,轉為二進制數組
23           header: {
24             'content-type': 'application/json;charset=utf-8'
25           },
26           method: 'POST',
27           success(res) {
28             //轉為base64
29             let bin64 = wx.arrayBufferToBase64(res.data);
30 
31             _this.setData({
32           //base 64設置到頁面上
33               img: "data:image/png;base64," + bin64
34             });
35           }
36         })
37       }
38     })
39   },

 

 

后端生成:調接口傳遞參數(uid發過去,后端接收,並且生成帶參小程序圖片發送到前端)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM