100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信小程序获取扫描二维码后携带的参数

微信小程序获取扫描二维码后携带的参数

时间:2021-04-02 22:39:42

相关推荐

微信小程序获取扫描二维码后携带的参数

微信小程序获取扫描二维码后携带的参数

1、decodeURIComponent解析生成二维码的链接。

/*** 生命周期函数--监听页面加载*/onLoad: function(options) {if (options.scene) {//获取二维码的携带的链接信息let qrUrl = decodeURIComponent(options.scene)console.log(qrUrl)this.setData({//获取链接中的参数信息actId: utils.getQueryString(qrUrl, 'actId'),shareUserId: utils.getQueryString(qrUrl, 'shareUserId'),})} },

2、utils中获取链接中所携带的参数

// 解析链接中的参数let getQueryString = function (url, name) {console.log("url = " + url)console.log("name = " + name)var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')var r = url.substr(1).match(reg)if (r != null) {console.log("r = " + r)console.log("r[2] = " + r[2])return r[2]}return null;}//导出方法,外部调用module.exports = {getQueryString: getQueryString,}

避坑:

onLoad (option) {console.log(option)}

这时可以接收到 拿着参数去请求数据等等操作~

假如你的小程序要发布了

这时候应该改变获取参数的方式,因为正式发布后的获取的参数和在开发者工具中是不一样的,这个坑!!!。下面代码是你获取正式发布小程序后的入口二维码中参数的代码,scene是微信生成二维码方法的一个参数,用来写你要在二维码中携带的参数

onLoad (option) {console.log(option)if (option.scene) {let obj = decodeURIComponent(option.scene)... // 这里就是你拿着参数obj进行操作}}

decodeURIComponent详解

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。