目录

微信小程序扫码识别二维码跳转指定页面

目录

【微信小程序】扫码识别二维码跳转指定页面

最近有个需求,扫码获取在微信中识别二维码直接跳转到指定的打卡圈,下面来介绍下如何实现这个功能。

https://i-blog.csdnimg.cn/blog_migrate/8990e75b0ed248f303848da1820c0aa5.jpeg

一、微信小程序后台配置二维码规则

https://i-blog.csdnimg.cn/blog_migrate/7ddf498ece36c5f9ef0a4ebda1023373.jpeg

https://i-blog.csdnimg.cn/blog_migrate/a06ebaff19b0641bb580f92501de25f2.jpeg

1、如果你想在开发环境测试的话,按照上图的’测试链接’中进行配置,配置测试链接后扫码或者长按识别二维码是可以进入开发版小程序,否则会跳转线上环境的小程序

2、线上版本的测试链接无需配置。

二、微信小程序内获取二维码信息

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.q) {
     //获取二维码的携带的链接信息
      let qrUrl = decodeURIComponent(options.q)
      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,
}