生成小程序二维码getWXACodeUnlimit,小程序怎么获取参数值

由于之前做的小程序带参数进入都是用芝麻小程序做的二维码,直接传参即可。
调用getWXACodeUnlimit生成二维码前端去取值。

不同之处:有sence=这个参数。取值略微不同。

1.先说后台调getWXACodeUnlimit生成二维码,用户从微信进去点击扫一扫

onLoad: function (options) { 
        if (this.getUrlParam("cabinet_id", decodeURIComponent(options.scene))) {//扫码进来带参数
            this.setData({ cabinet_id: this.getUrlParam("cabinet_id", decodeURIComponent(options.scene)) });//将id赋值给data里面的变量
        }  
    },
// 获取扫码后的字符串,因为考虑有多个参数,所以写了一个公共函数
    getUrlParam(name, str) {
        let regs = new RegExp("scene=", "g"); //场景值
        str = str.replace(regs, ""); 
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var array = str.match(reg);
        if (array != null)
            return array[2];
    },

关键点是得到的参数要先decodeURIComponent

2.进入小程序后点击扫一扫,扫getWXACodeUnlimit生成的二维码

wx.scanCode({
                success:(res)=> {
                    let str = decodeURIComponent(res.path ? res.path.substr(res.path.indexOf("?") + 1, res.path.length - 1) : "");
                    if (this.getUrlParam("cabinet_id", str)) {
                        this.setData({ "cabinet_id": this.getUrlParam("cabinet_id", str) });
                    }else{
                        wx.showModal({
                            title: '提示',
                            content: '没有找到相关设备,请找相关人员确认是否为有效二维码',
                            confirmText: "确定",
                            showCancel: false,
                            success: (res) => {if (res.confirm) {}}
                        })
                    }  
                }
            })

关键点是扫码获取的参数并不是result,参数在path里面,手机端自动解码了,但是小程序开发工具还是需要的。需要在小程序端解码。

3.小程序编辑器模拟getWXACodeUnlimit生成的二维码传参,根据小程序说明文档,说需要encodeURLComponent一下,然后scene=encodeURLComponent(cabinet_id=10001),是这样scene=cabinet_id%3D10001

要模拟需要点击编译,选择自定义编译条件。如下图:
https://img-blog.csdnimg.cn/20190416175517985.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NjcyOTA3,size_16,color_FFFFFF,t_70

nice!


版权声明:本文为weixin_40362806原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。