1. 程式人生 > 程式設計 >vue使用微信掃一掃功能的實現程式碼

vue使用微信掃一掃功能的實現程式碼

第一步: 安裝weixin-js-sdk 和 jquery 包 npm install weixin-js-sdk jquery

第二部: 配置wx.config (配置都是後端返回來的,菜鳥前端只需要按需傳值過去就可)

程式碼如下

import wx from “weixin-js-sdk”;
import $ from “jquery”;
goSao() {

 //這裡【url引數一定是去參的本網址】,請求後端介面換取signature
 //(相容安卓和ios)
 let url = "傳值";
 let ua = navigator.userAgent.toLowerCase();
 if (/iphone|ipad|ipod/.test(ua)) {
  this.newUrl = window.location.href.split("#")[0];
 } else if (/android/.test(ua)) {
  this.newUrl = window.location.href;
 }
 //傳值為了去掉#
 
 $.get(`後臺需要的路徑=${this.newUrl}`,function(response) {
   wx.config({
    // 開啟除錯模式,呼叫的所有api的返回值會在客戶端alert出來,若要檢視傳入的引數,可以在pc端開啟,引數資訊會通過log打出,僅在pc端時才會列印。
    debug: false,// 必填,公眾號的唯一標識
    appId: response.data.appId,// 必填,生成簽名的時間戳
    timestamp: response.data.timestamp,// 必填,生成簽名的隨機串
    nonceStr: response.data.nonceStr,// 必填,簽名
    signature: response.data.signature,// 必填,需要使用的JS介面列表,所有JS介面列表
    jsApiList: ["scanQRCode"]
   });
   console.log(response)
  }
 );
 wx.error(function(res) {
  alert("出錯了:" + res.errMsg); //這個地方的好處就是wx.config配置錯誤,會彈出視窗哪裡錯誤,然後根據微信文件查詢即可。
 });
 let _t = this
  wx.ready(function () {
    wx.checkJsApi({
      jsApiList: ['scanQRCode'],success: function (res) {

      }
    });
    wx.scanQRCode({
      needResult: 1,// 預設為0,掃描結果由微信處理,1則直接返回掃描結果,
      scanType: ["qrCode"],// 可以指定掃二維碼還是一維碼,預設二者都有
      success: async (res)=>{
       var result = res.resultStr; // 當needResult 為 1 時,掃碼返回的結果
      alert(result )
      }
    });
  });
},

已測,可以使用

注:只能微信瀏覽器使用,其他瀏覽器不可

總結

到此這篇關於vue使用微信掃一掃功能的實現程式碼的文章就介紹到這了,更多相關vue 微信掃一掃內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!