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 微信掃一掃內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
