var apiBaseUrl = window.location.protocol + '//' + window.location.host +"/api/"; var localUrl = encodeURIComponent(window.location.href); //console.log(apiBaseUrl,localUrl); // 统计访问/分享次数 function syncUpdateCount(title, desc, type) { $.getJSON(apiBaseUrl + "wechat_visit.php?t=1&callback=?", { "url": localUrl, "title": title ,"desc":desc, "type": type }, function (json) { //to do console.info(json); if(json.visit_count){ $(function(){ $("#visit_count").html(""+json.visit_count+""); $(".visit_count").html(json.visit_count); window['visit_count'] = json.visit_count; console.log('访问量:'+ window['visit_count']) }) } }); } $.weixinShare = function (options) { var defaults = { debug: true, title: "亲,忘记写标题了!", desc: "亲,忘记写内容了!", link: localUrl, imgUrl: "https://www.baidu.com/img/baidu_jgylogo3.gif", nocount: false, success: function () { _hmt.push(['_trackEvent', '分享', '分享成功']); syncUpdateCount(this.title,this.desc, "Share"); }, share_success:false, cancel: function () { console.log("用户取消分享"); _hmt.push(['_trackEvent', '分享', '分享取消']); }, fail:function(){ _hmt.push(['_trackEvent', '分享', '分享失败']); } }; // 参数合并 // var opts = $.extend(true, defaults, options); var opts = Object.assign({},defaults, options, { success: function () { // 在新合并的成功回调函数中执行默认的和自定义的回调函数 defaults.success.call(this); options.success.call(this); // console.log('分享成功'); } }); // console.log(opts) if(opts.nocount==true){ }else{ //访问 syncUpdateCount(opts.title,opts.desc, "Visit"); //console.log(opts.success); } // 分享 $.getJSON(apiBaseUrl + "wechat_token.php?t=1&callback=?", {"url": encodeURIComponent(window.location.href)}, function (json) { if (json) { wx.config({ debug: opts.debug, appId: json.appId, timestamp: json.timestamp, nonceStr: json.nonceStr, signature: json.signature, //jsApiList: new Array('onMenuShareTimeline', 'onMenuShareAppMessage','showMenuItems','hideMenuItems','hideAllNonBaseMenuItem','hideOptionMenu','startRecord','stopRecord','onVoiceRecordEnd','playVoice','pauseVoice','stopVoice','onVoicePlayEnd','uploadVoice','downloadVoice','chooseImage','previewImage','uploadImage','downloadImage','translateVoice','openLocation','getLocation') jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'translateVoice', 'startRecord', 'stopRecord', 'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard' ], openTagList: ['wx-open-launch-weapp','wx-open-launch-app','wx-open-subscribe','wx-open-audio'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app'] }); wx.ready(function () { // 分享给朋友 wx.onMenuShareAppMessage(opts); // 分享到QQ wx.onMenuShareQQ(opts); // 分享到腾讯微博 wx.onMenuShareWeibo(opts); // 分享到朋友圈 var optsx = $.extend({},opts); optsx.success = optsx.share_success||optsx.success; wx.onMenuShareTimeline(optsx); }); } }); }