小程序接收二维码参数的问题

首先看一下官网:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

这是什么意思呢?  我来解释一下。

意思是:在别人通过扫面你的小程序码,并且你的小程序码是带参数的,那么在线上的版本就要加一个此方法进行转码;

decodeURIComponent(query.scene)

这是我在线上版本打印出来的,这是微信自动给参数转码了。 

这里和我们在微信开发工具是不一样的,所以在线上就要多一个判断了。

 

app.js  为    options.query.scene 

正常页面     options.scene 

if (options.scene){ //这里为线上操作
      //此处的二维码  page/index/index?brokerId=123
      let scene = decodeURIComponent(options.scene);
      console.log('index scene: ', scene)   //brokerId=123  为字符串,需要我们去分割
      params.brokerId = scene.split("=")[1];
    } else if (options.brokerId && options.brokerId !== 'undefined' ){//这里为开发环境
      params.brokerId = options.brokerId;
    }
if (options.scene){
//此时参数为两个   二维码为   page/index/index?A=123&B=456
      let scene = decodeURIComponent(options.scene);
      console.log('detail scene: ', scene)
      let arr = scene.split('&');
      let A = arr[0].split('=')[1];
      let B = arr[1].split('=')[1];
      wx.getStorage({
        key: 'phoneNumber',
        success(res) {
          _this.getHouseDetail(A, B, res.data);
        }
      })
    }else{
      wx.getStorage({
        key: 'phoneNumber',
        success(res) {
          _this.getHouseDetail(options.A, options.B, res.data);
        }
      })
    }

自己调试可以添加编译模式去模拟

注意:前面要加scene={你的参数转码}

放一个转码的网站:https://meyerweb.com/eric/tools/dencoder/

发布了149 篇原创文章 · 获赞 51 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/caoyan0829/article/details/95354462