引入js文件:
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
判断网络状态:
<script>
function wx_share(){
var apiWx_jssdk = 'https://api.xxx.com/index/wx_jssdk/getSign';//请看
var _url = location.href.split('#')[0];
$.post(apiWx_jssdk, { url:_url }, function(data) {
if(data.code != 200) {
console.log('微信jssdk授权失败');
return false;
}
wx.config({
debug: false,//生产环境需要关闭debug模式
appId: data.data.appId,//appId通过微信服务号后台查看
timestamp: data.data.timestamp,//生成签名的时间戳
nonceStr: data.data.nonceStr,//生成签名的随机字符串
signature: data.data.signature,//签名
jsApiList: [ // 所有要调用的 API 都要加到这个列表中
'getNetworkType',
]
});
wx.ready(function() {
//获取当前网络状态
wx.getNetworkType({
success: function (res) {
var wifi = res.networkType;
if(wifi == 'wifi'){
return false;
}
var msg = '正在使用非WIFI网络,播放直播将产生流量费用';
if(/Android/i.test(navigator.userAgent)){
alert(msg);
}else{
alert(msg);
}
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
});
wx.error(function(res){
console.log("好像出错了!!");
});
},'json');
}
wx_share();
</script>
注:https://api.xxx.com/index/wx_jssdk/getSign返回数据格式为:
//以下为PHP代码:
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string,
"ticket" => $this->ticket,
"accessToken" => $this->accessToken,
"ticketData" => $this->ticketData,
"accessTokenData" => $this->accessTokenData,
);
ajaxOut(200, $signPackage, 'ok');
|