微信:微信中,扫码进入页面时,携带的参数依然在页面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;
}
}