微信小程序之创建包含参数的专属二维码

小程序前端获取带参二维码

微信文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

需求: 有时候我们需要生成咱们自己小程序的二维码,同时需要二维码带着参数

步骤:
1.获取token
2.获取二维码,在scene中传你要接收的参数

代码如下:

Page({
data: {
	token: '',
    imgUrl: '', // 小程序二维码
}
// 获取token
  getToken() {
    let that = this;
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你自己的appid&secret=自己的secret',  // 具体传参见文档
      success: res => {
        console.log(res)
        that.setData({
          token: res.data.access_token
        })
        that.getUnlimited(res.data.access_token)
      }
    })
  },

  getUnlimited(id) {
    const that = this;
    wx.request({
      url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + id, // 切记tooken连接在地址后面,不要放在data中,否则会导致报错,token过期
      data: {
        scene: wx.getStorageSync('thisCode'),
        page: 'pages/index/index',
        width: 280,
        is_hyaline: true
      },
      header: {
        'content-type': 'application/json;charset=utf-8'
      },
      responseType: 'arraybuffer',  // 很重要一定要传
      method: 'POST',  // 一定要用POST,用get报错
      success: res => {  // res中data这时看是个空数组,但经过下面解析就会出现数据
        let data = wx.arrayBufferToBase64(res.data)
        that.setData({
          imgUrl: 'data:image/PNG;base64,' + data  // 一定不要漏掉
        })
      }
    })
  }
})

3.然后在你的扫描进去的那个页面的onload生命周期中获取传参

var scene = decodeURIComponent(options.scene);
that.setData({
   scene
})
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值