1. 程式人生 > >微信小程式 - 生成二維碼

微信小程式 - 生成二維碼

生成二維碼有兩種方式:

前端生成(僅展示用)

 

 1 onShow: function() {
 2 
 3     $.setTitle('推廣二維碼');
 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發過去,後端接收,並且生成帶參小程式圖片傳送到前端)