PC端生成二維碼,微信掃碼 帶參數跳轉微信小程序指定頁面


1、首先在 登錄 微信公眾號-小程序-開發-開發設置 找到 ‘掃普通鏈接二維碼打開小程序’,然后添加配置。

 

 

2、配置成功后,在PC端寫一個按鈕,點擊按鈕根據鏈接與參數生成二維碼圖片

<button type="default" @click="transmit">點擊生成二維碼</button>
<div class="erweima" id="qrcode" ref="qrcode"></div>

 

3、鏈接生成二維碼使用的是qrcodejs2,需要安裝並引入,同時引入jquery

npm install qrcodejs2 --save
import QRCode from "qrcodejs2";
import $ from "jquery"

 

4、JS 中

methods: {
        transmit(){
            if (this.$refs.qrcode.title != '') {
                $('#qrcode').html("")
            }this.qrcode('小紅', 18);
        },
        qrcode(e, n) {
            let qrcode = new QRCode("qrcode", {
                width: 200, // 二維碼寬度,單位像素
                height: 200, // 二維碼高度,單位像素
                text: "https://api.zhiyedang.cn/a?=" + e + "&" + n // 生成二維碼的鏈接
            });
        },
    }

到這里,點擊按鈕就能生成二維碼了,用微信掃二維碼也能跳到小程序指定頁面了

注意(這個功能默認跳轉到線上的小程序,如果想要跳轉到體驗版,第一步 測試鏈接里面的二維碼規則,和PC端二維碼鏈接的格式需要一致,否則跳進去報錯400)

 

微信小程序中

app.js 中

onLaunch: function () {
    var that = this ;
    if(JSON.stringify(options.referrerInfo)!='{}'){
      that.globalData.name=options.referrerInfo.extraData.name;
      that.globalData.age=options.referrerInfo.extraData.age;
    }
  },
globalData: {
    name:'',
    age:'',
  }

 

掃描后跳轉的小程序頁面 中 獲取參數

 

data: {
    name: '',
    age: ''
  },

 

onLoad: function (options) {
    var name
    var age
    if (JSON.stringify(options)=='{}') {
      name = app.globalData.name
      age = app.globalData.age
      this.setData({
        name: name,
        age: age
      })
      return
    }

    let url = decodeURIComponent(options.q)
    let data = url.split('=')[1]
    name = data.split('&')[0]
    age = data.split('&')[1]
    this.setData({
      name: name,
      age: age
    })

  },

到這里就算完成了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM