第一步:獲取ACCESS_TOKEN
為了安全性考慮這一步是由后台獲取
wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=AppSecret', method: 'get', header: { 'content-type': 'application/json' // 默認值 }, success:(res)=> { console.log(res.data.access_token); } })
取到access_toke后再向微信請求獲取二維碼
第二步:獲取小程序二維碼並渲染:
這一步為了方便我在前台獲取了
let that = this wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='+res.data.access_token, method: 'POST', header: { 'content-type': 'application/json' // 默認值 }, data:{ 'path': "/page/index/index?openid=11", // 攜參數openid "width":100//非必填 }, responseType: 'arraybuffer', // arraybuffer是以數組的語法處理二進制數據,稱為二進制數組。 success: function(res) { let data = wx.arrayBufferToBase64(res.data); console.log('data:image/png;base64,' +data); that.setData({ myCode: 'data:image/png;base64,' +data }) } })
最后把myCode渲染到頁面就可以了
<image src="{{myCode}}"></image>