js中調(diào)用微信的掃描二維碼功能的實(shí)現(xiàn)代碼
關(guān)鍵代碼
<html><head> <title> js調(diào)用微信掃一掃功能測(cè)試 </title> <!--引用微信JS庫(kù)--> <script type='text/javascript' src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script> <!--引用jQuery庫(kù)--> <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.min.js'></script></head><body> <input type='button' value='掃一掃' id='scanQRCode'><script type='text/javascript'> //這里【url參數(shù)一定是去參的本網(wǎng)址】 $.get('獲取微信認(rèn)證參數(shù)的網(wǎng)址?url=當(dāng)前網(wǎng)頁的網(wǎng)址', function(data){ var jsondata=$.parseJSON(data); wx.config({ // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會(huì)通過log打出,僅在pc端時(shí)才會(huì)打印。 debug: false, // 必填,公眾號(hào)的唯一標(biāo)識(shí) appId: jsondata.model.appId, // 必填,生成簽名的時(shí)間戳 timestamp: '' + jsondata.model.timestamp, // 必填,生成簽名的隨機(jī)串 nonceStr: jsondata.model.nonceStr, // 必填,簽名 signature: jsondata.model.signature, // 必填,需要使用的JS接口列表 jsApiList: [’checkJsApi’, ’scanQRCode’] }); }); wx.error(function (res) { alert('出錯(cuò)了:' + res.errMsg);//這個(gè)地方的好處就是wx.config配置錯(cuò)誤,會(huì)彈出窗口哪里錯(cuò)誤,然后根據(jù)微信文檔查詢即可。 }); wx.ready(function () { wx.checkJsApi({ jsApiList: [’scanQRCode’], success: function (res) { } }); //點(diǎn)擊按鈕掃描二維碼 document.querySelector(’#scanQRCode’).onclick = function () { wx.scanQRCode({needResult: 1, // 默認(rèn)為0,掃描結(jié)果由微信處理,1則直接返回掃描結(jié)果,scanType: ['qrCode'], // 可以指定掃二維碼還是一維碼,默認(rèn)二者都有success: function (res) { var result = res.resultStr; // 當(dāng)needResult 為 1 時(shí),掃碼返回的結(jié)果 alert('掃描結(jié)果:'+result); window.location.href = result;//因?yàn)槲疫@邊是掃描后有個(gè)鏈接,然后跳轉(zhuǎn)到該頁面} }); }; });</script></body></html>
注意事項(xiàng):
“獲取微信認(rèn)證參數(shù)”這個(gè)的前提是您能夠有自己的微信開發(fā)資質(zhì),并能獲取到正確的參數(shù)
公眾號(hào)的唯一標(biāo)識(shí) 簽名的時(shí)間戳 簽名隨機(jī)串常見的錯(cuò)誤
config:invalid signature
解決辦法
“當(dāng)前網(wǎng)頁的地址”-----哈哈,一定是你寫的不對(duì),這里一定是去參的本網(wǎng)頁的地址最好是在服務(wù)器下去測(cè)試
總結(jié)
到此這篇關(guān)于在js中調(diào)用微信的掃描二維碼功能的文章就介紹到這了,更多相關(guān)js 微信掃描二維碼內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: