分享

/*分享模块,使用百度分享*/
function shareFn(json){
this.weixinUrl = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js";
this.baiduUrl="http://bdimg.share.baidu.com/static/api/js/share.js";
this.ua = navigator.userAgent.toLowerCase();
this.config={
title :json.title, // 分享标题
desc :json.desc, // 分享链接
link :json.link, // 分享链接
imgUrl :json.imgUrl, // 分享图标
}

//初始化方法
this.init();
};


shareFn.prototype.init=function(){
var _this=this;
if (this.isWeixin()){
var oScript = document.createElement("script");
oScript.setAttribute("src",this.weixinUrl);
document.getElementsByTagName("head")[0].appendChild(oScript);
wx.ready(function(){
wx.onMenuShareAppMessage(_this.config);
wx.onMenuShareTimeline(_this.config);
wx.onMenuShareQQ(_this.config);
wx.onMenuShareWeibo(_this.config);
wx.onMenuShareQZone(_this.config);
});
} else {
var oScript = document.createElement("script");
oScript.setAttribute("src",this.baiduUrl);
document.getElementsByTagName("head")[0].appendChild(oScript);

window._bd_share_config = {
common : {
bdText: _this.config.title,
bdDesc: _this.config.desc,
bdUrl : _this.config.link,
bdPic : _this.config.imgUrl
},
share : [{
"bdSize" : 32
}]
}


}
}

shareFn.prototype.isWeixin = function() {
return this.ua.match(/MicroMessenger/i) == "micromessenger";
}


function share(json){
return new shareFn(json)
}

支持微信分享和浏览器分享