//c_开头的js变量是服务器传过来的
var c_access_token = "<?=$access_token?>";
var c_log_url = ‘<?=site_url("log/index")?>‘;
var c_download_url = ‘<?=site_url("download/index")?>‘;
var c_timestamp = ‘<?=$timestamp?>‘;
var c_signature = ‘<?=$signature?>‘;
var c_noncestr = ‘<?=$noncestr?>‘;
var log_msg = function(msg){
$.post(c_log_url, {msg:msg});
}
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端log_msg出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ‘wx693f4c620712348b‘, // 必填,公众号的唯一标识
timestamp: c_timestamp, // 必填,生成签名的时间戳
nonceStr: c_noncestr, // 必填,生成签名的随机串
signature: c_signature,// 必填,签名,见附录1
jsApiList: [// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
‘checkJsApi‘,
‘hideMenuItems‘,
‘showMenuItems‘,
‘hideAllNonBaseMenuItem‘,
‘showAllNonBaseMenuItem‘,
‘translateVoice‘,
‘startRecord‘,
‘stopRecord‘,
‘onRecordEnd‘,
‘playVoice‘,
‘pauseVoice‘,
‘stopVoice‘,
‘uploadVoice‘,
‘downloadVoice‘,
‘chooseImage‘,
‘previewImage‘,
‘uploadImage‘,
‘downloadImage‘,
‘getNetworkType‘,
‘openLocation‘,
‘getLocation‘,
‘hideOptionMenu‘,
‘showOptionMenu‘,
‘closeWindow‘,
‘scanQRCode‘
]
});
// 微信
wx.error(function(res){
log_msg(‘验证失败‘);
alert(‘验证失败‘);
});
wx.ready(function(){
//1.检查扫一扫接口
document.querySelector(‘#scanQRCode‘).onclick = function() {
log_msg(‘检查api‘);
unvaild_api = [];
wx.checkJsApi({
jsApiList:[
‘scanQRCode‘
],
success:function(res){
log_msg(‘返回检查api的结果‘+JSON.stringify(res));
$.each(res.checkResult, function(k, v) {
if (!v) {
unvaild_api.push(k);
return false;
}
});
if (unvaild_api.length > 0) {
log_msg(‘存在无效api‘+unvaild_api.join(‘, ‘));
log_msg(‘存在无效api的结果‘+unvaild_api.join(‘, ‘));
return false;
}
//2.扫一扫接口
wx.scanQRCode({
needResult:1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
success: function (res) {
//结果
alert(res.resultStr);
}
});
}
});
}
});