微信、支付宝扫码进入小程序,并接收参数

微信:微信中,扫码进入页面时,携带的参数依然在页面onLoad生命周期的options中,

onLoad(options){

const q = options.q;
                if (q && q != 'undefined') {
                    const qrUrl = decodeURIComponent(q);
                    let jsonUrl = this.getwxUrlParam(qrUrl);//jsonUrl为解析出的参数对象
                    console.log(jsonUrl, 'jsonUrl-')
                    that.shopId = jsonUrl.shopId
                }

}

methods:{

getwxUrlParam(url) {
                let theRequest = {};
                if (url.indexOf('#') != -1) {
                    const str = url.split('#')[1];
                    const strs = str.split('&');
                    for (let i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
                    }
                } else if (url.indexOf('?') != -1) {
                    const str = url.split('?')[1];
                    const strs = str.split('&');
                    for (let i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
                    }
                }
                return theRequest;
            }

}

支付宝:支付宝在扫码进入页面时,只能在App.vue的onLaunch中接收到参数

App.vue中:

globalData: {
            qrCode: '',
 },

onLaunch: function(options) {

if (options.query && options.query.qrCode) {
                this.globalData.qrCode = options.query.qrCode
            }

}

跳转到的页面中:

onLoad(options) {

// #ifdef MP-ALIPAY
                let qrCode = getApp().globalData.qrCode;
                if (qrCode) {
                    const zfbObj = this.getwxUrlParam(qrCode)//zfbObj为解析出的参数对象
                    this.shopId = zfbObj.shopId
 }
                // #endif

}

methods:{

getwxUrlParam(url) {
                let theRequest = {};
                if (url.indexOf('#') != -1) {
                    const str = url.split('#')[1];
                    const strs = str.split('&');
                    for (let i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
                    }
                } else if (url.indexOf('?') != -1) {
                    const str = url.split('?')[1];
                    const strs = str.split('&');
                    for (let i = 0; i < strs.length; i++) {
                        theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
                    }
                }
                return theRequest;
            }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值