如何利用微信jssdk调用微信扫一扫功能?具体内容如下
1. 确保有 调起微信扫一扫接口 权限,测试号可能不行;
2. 导入相关js
3. 页面触发扫码元素
4. 相关js代码
var _appid = "wxz88dbd30e5580e59";
var _data = {
appid : _appid,
url : location.href,
t : math.random()
};
var _getwechatsignurl = '/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('获取数据:'+_timestamp+'\n'+_noncestr+'\n'+_signature);
console.log('获取数据:' + _timestamp + '\n' + _noncestr + '\n' + _signature);
wx.config({
debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appid : _appid, // 必填,公众号的唯一标识
timestamp : _timestamp, // 必填,生成签名的时间戳
noncestr : _noncestr, // 必填,生成签名的随机串
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));
}
});
}
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
获取jsapi_ticket
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。