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}`
    });
}
相关推荐
豆豆41 分钟前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
JUNAI_Strive_ving1 小时前
番茄小说逆向爬取
javascript·python
看到请催我学习1 小时前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
twins35202 小时前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky2 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~2 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
哪 吒2 小时前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
安冬的码畜日常2 小时前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
l1x1n03 小时前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
Q_w77423 小时前
一个真实可用的登录界面!
javascript·mysql·php·html5·网站登录