需求背景:当用户直接用微信扫描小程序码,需要传入不同参数进行相应的页面操作,即需要动态生成带不同参数的小程序码
wxacode.getUnlimited相关资料:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
js代码如下
wx.request({
//注意:下面的access_token值可以不可以直接复制使用,需要自己请求获取
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='+token.access_token,
data: {
scene: scene,
page: "pages/index/index" //这里按照需求设置值和参数
},
method: "POST",
responseType: 'arraybuffer', //设置响应类型
arraybuffer: true, //转码,注意别忘了这个属性,不然会乱码
success(res) {
console.log(res)
var imgurl ='data:image/jpeg;base64,'+ wx.arrayBufferToBase64(res.data); //对数据进行转换操作
z.setData({
imgurl
})
},
fail(e) {
console.log(e)
}
})
wxml代码显示二维码
<image style="width:430rpx;height:430rpx" src="{{imgurl}}"></image>
将生成的二维码用微信开发工具进行测试:选择“通过二维码编译”
在自己传入的页面"pages/index/index"用options.scene接收
onLoad: function (options) {
if (options.scene) {
//options.scene必须要decodeURIComponent处理才能拿到
let scene = decodeURIComponent(options.scene);
//&是我们定义的参数链接方式
console.log(scene)
}
},