uniapp框架使用vue2实现微信扫二维码跳转小程序指定页面

前言:最新有一个需求就是在后台系统中添加一个二维码,然后扫描二维码跳转到小程序的指定页面

后台系统中安装插件qrcode
markdown 复制代码
> cnpm install qrcodejs2 --save 
> // 或者 
> npm install qrcodejs2 --save

引入插件

// 引入二维码插件 import QRCode from "qrcodejs2";

html 复制代码
 <div
       style="
                  justify-content: center;
                  align-items: center;
                  display: flex;
                "
                ref="qrcode"
              ></div>
          
js 复制代码
new QRCode(this.$refs.qrcode, {
          text: `https://xxxx/xxxx/xxxx/#/pages/index/index/id=${this.eid}&consUserType=2`, //二维码跳转
          width: 80, //二维码设置宽高
          height: 80,
        });

在微信开发者工具中配置二维码参数

开发管理->开发设置->扫普通链接二维码打开小程序

上图在配置项在前缀占用规则可以详细配置参数 下图在微信开发者工具中配置可以获取二维码传递来的数据,注意启动参数直接写q="xxxxxxxxxxxxxxxx",因为会默认变成对象包裹,扫描二维码跳转到指定页面所以启动页面填写对应的地址。

下面链接微信小程序配置二维码规则

developers.weixin.qq.com/miniprogram...

js 复制代码
//页面加载时接收二维码传递来的参数q链接,将其放进store中,之前登录逻辑会判断小程序是否已经登录(小程序在之前登录逻辑不展示了),当未登录会跳转到login页面进行登录
onLoad(option) {
    if (!option.q) {
        this.Id = option.id;
        this.consUserType = option.consUserType;
    } else {
        this.$store.commit("CHANGE_QR_ROUTE", option)
    }
},
js 复制代码
//login登录逻辑
//从仓库中获取store的值
let qrCode = store.getters.qrCodeRoute
if (qrCode && qrCode.q) {
    let q = decodeURIComponent(decodeURIComponent(qrCode.q))
    const startIndex = q.indexOf("#/") + 2;
    const endIndex = q.indexOf("/id=");
    const parameter = q.substring(startIndex, endIndex);
    // 使用正则匹配id和consUserType的值
    const idRegex = /id=(\d+)/;
    const consUserTypeRegex = /consUserType=(\d+)/;
    const idMatch = q.match(idRegex);
    const consUserTypeMatch = q.match(consUserTypeRegex);
    const id = idMatch ? idMatch[1] : null;
    const consUserType = consUserTypeMatch ? consUserTypeMatch[1] : null;
    // console.log("parameter", parameter, "id", id, "consUserType", consUserType)
    //跳转到指定页面
    uni.reLaunch({
        url: `../../${parameter}?id=${id}&consUserType=${consUserType}`
    });
}
相关推荐
excel5 分钟前
前端必备:从能力检测到 UA-CH,浏览器客户端检测的完整指南
前端
前端小巷子12 分钟前
Vue 3全面提速剖析
前端·vue.js·面试
悟空聊架构18 分钟前
我的网站被攻击了,被干掉了 120G 流量,还在持续攻击中...
java·前端·架构
CodeSheep20 分钟前
国内 IT 公司时薪排行榜。
前端·后端·程序员
尖椒土豆sss24 分钟前
踩坑vue项目中使用 iframe 嵌套子系统无法登录,不报错问题!
前端·vue.js
遗悲风24 分钟前
html二次作业
前端·html
江城开朗的豌豆28 分钟前
React输入框优化:如何精准获取用户输入完成后的最终值?
前端·javascript·全栈
CF14年老兵28 分钟前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae
画月的亮31 分钟前
前端处理导出PDF。Vue导出pdf
前端·vue.js·pdf
江城开朗的豌豆37 分钟前
拆解Redux:从零手写一个状态管理器,彻底搞懂它的魔法!
前端·javascript·react.js