vue使用微信掃一掃功能的實(shí)現(xiàn)代碼
第一步: 安裝weixin-js-sdk 和 jquery 包 npm install weixin-js-sdk jquery
第二部: 配置wx.config (配置都是后端返回來的,菜鳥前端只需要按需傳值過去就可)
代碼如下
import wx from “weixin-js-sdk”;import $ from “jquery”;goSao() { //這里【url參數(shù)一定是去參的本網(wǎng)址】,請求后端接口換取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(`后臺(tái)需要的路徑=${this.newUrl}`, function(response) { wx.config({ // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會(huì)通過log打出,僅在pc端時(shí)才會(huì)打印。 debug: false, // 必填,公眾號的唯一標(biāo)識(shí) appId: response.data.appId, // 必填,生成簽名的時(shí)間戳 timestamp: response.data.timestamp, // 必填,生成簽名的隨機(jī)串 nonceStr: response.data.nonceStr, // 必填,簽名 signature: response.data.signature, // 必填,需要使用的JS接口列表,所有JS接口列表 jsApiList: ['scanQRCode'] }); console.log(response) } ); wx.error(function(res) { alert('出錯(cuò)了:' + res.errMsg); //這個(gè)地方的好處就是wx.config配置錯(cuò)誤,會(huì)彈出窗口哪里錯(cuò)誤,然后根據(jù)微信文檔查詢即可。 }); let _t = this wx.ready(function () { wx.checkJsApi({ jsApiList: [’scanQRCode’], success: function (res) { } }); wx.scanQRCode({ needResult: 1, // 默認(rèn)為0,掃描結(jié)果由微信處理,1則直接返回掃描結(jié)果, scanType: ['qrCode'], // 可以指定掃二維碼還是一維碼,默認(rèn)二者都有 success: async (res)=>{ var result = res.resultStr; // 當(dāng)needResult 為 1 時(shí),掃碼返回的結(jié)果 alert(result ) } }); });},
已測,可以使用
注:只能微信瀏覽器使用,其他瀏覽器不可
總結(jié)
到此這篇關(guān)于vue使用微信掃一掃功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)vue 微信掃一掃內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
