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}`
    });
}
相关推荐
特别橙的橙汁1 分钟前
Node.js 调用可执行文件时的 stdout 缓冲区问题
前端·node.js·swift
yiranlater1 分钟前
点云八叉树处理
前端
榴莲CC7 分钟前
VK1620 抗噪数显LED驱动芯片数码管显示IC内置 RC振荡器/8级整体亮度可调
前端
@Autowire11 分钟前
Layout-box-sizing是 CSS 中控制元素盒模型计算方式的核心属性,直接决定了元素的 width/height 是否包含内边距和边框
前端
alamhubb12 分钟前
反感pnpm的全链路污染?可以了解下这个对原项目零侵入,零修改完全兼容npm的monorepo工具
前端·javascript·node.js
叁两13 分钟前
“死了么”用户数翻800倍,估值近1亿,那我来做个“活着呢”!
前端·人工智能·产品
2501_9481949834 分钟前
RN for OpenHarmony AnimeHub项目实战:正在热播页面开发
javascript·react native·react.js
2501_9445210035 分钟前
rn_for_openharmony商城项目app实战-语言设置实现
javascript·数据库·react native·react.js·harmonyos
AdleyTales41 分钟前
vscode识别不了@提示找不到路径解决
前端·javascript·vscode
去哪儿技术沙龙1 小时前
去哪儿网前端代码自动生成技术实践
前端·ai编程