微信JSSDK調(diào)用微信掃一掃功能的方法
如何利用微信JSSDK調(diào)用微信掃一掃功能?具體內(nèi)容如下
1. 確保有 調(diào)起微信掃一掃接口 權(quán)限,測試號可能不行;
2. 導(dǎo)入相關(guān)JS
<script type="text/javascript" http://test.com/zepto_touch.js"></script> <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3. 頁面觸發(fā)掃碼元素
<img src="../../../images/right.jpg" onclick="scanCode()" class="img">
4. 相關(guān)JS代碼
<script type="text/javascript">
var _appId = "wxz88dbd30e5580e59";
var _data = {
appId : _appId,
url : location.href,
t : Math.random()
};
var _getWechatSignUrl = 'http://test.com/getWechatSign.do';
// 獲取微信簽名
$.ajax({
url : _getWechatSignUrl,
data : _data,
success : function(o) {
console.log(o);
if (o.returnCode == "00") {
wxConfig(o.detail[0].timestamp, o.detail[0].nonceStr, o.detail[0].signature);
}
}
});
function wxConfig(_timestamp, _nonceStr, _signature) {
//alert('獲取數(shù)據(jù):'+_timestamp+'\n'+_nonceStr+'\n'+_signature);
console.log('獲取數(shù)據(jù):' + _timestamp + '\n' + _nonceStr + '\n' + _signature);
wx.config({
debug : true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時(shí)才會打印。
appId : _appId, // 必填,公眾號的唯一標(biāo)識
timestamp : _timestamp, // 必填,生成簽名的時(shí)間戳
nonceStr : _nonceStr, // 必填,生成簽名的隨機(jī)串
signature : _signature,// 必填,簽名,見附錄1
jsApiList : [ 'onMenuShareTimeline', 'onMenuShareAppMessage',
'onMenuShareQQ', 'onMenuShareWeibo', 'scanQRCode' ]
// 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
});
}
function scanCode() {
wx.scanQRCode({
needResult : 1,
scanType : [ "qrCode", "barCode" ],
success : function(res) {
console.log(res)
alert(JSON.stringify(res));
var result = res.resultStr;
},
fail : function(res) {
console.log(res)
alert(JSON.stringify(res));
}
});
}
</script>
5. 獲取簽名接口getWechatSign.do各值生成方式
timestamp
Long timestamp = System.currentTimeMillis() / 1000;
nonceStr
String nonceStr = RandomStringUtils.randomAlphanumeric(16);
signature
public static String getSign(String jsapi_ticket, String noncestr, Long timestamp, String url)
throws NoSuchAlgorithmException {
String shaStr = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url="
+ url;
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(shaStr.getBytes());
StringBuffer signature = new StringBuffer();
for (int i = 0; i < result.length; i++) {
signature.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return signature.toString();
}
6. 微信參考文檔
獲取access_token https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
獲取jsapi_ticket https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript的基礎(chǔ)語法和數(shù)據(jù)類型詳解
這篇文章主要介紹了JavaScript的基礎(chǔ)語法和數(shù)據(jù)類型,保姆級的詳細(xì)教程,萬字長文詳細(xì)的列出了JavaScript的各種語法,建議收藏系列,希望可以有所幫助2021-09-09
javascript getElementById 使用方法及用法
顧明思義,get-Element-By-Id,就是通過ID來設(shè)置/返回HTML標(biāo)簽的屬性及調(diào)用其事件與方法。用這個(gè)方法基本上可以控制頁面所有標(biāo)簽,條件很簡單就是給每個(gè)標(biāo)簽分配一個(gè)ID號2008-11-11
微信小程序遍歷Echarts圖表實(shí)現(xiàn)多個(gè)餅圖
這篇文章主要介紹了微信小程序遍歷Echarts圖表實(shí)現(xiàn)多個(gè)餅圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

