jdjm
Version:
金融移动端 - jm
238 lines (230 loc) • 10.1 kB
JavaScript
/**
* @Author: Created By McChen
* @Date: 2016/5/24
* @Mail: chenjiahao@jd.com
* @Version: V0.0.0
*/
;(function (jm) {
/**============================
private
==============================*/
var browser = "default";
for(i in jm.getBrowserInfo().appType){
if (jm.getBrowserInfo().appType[i]) {
browser = i;
}
}
//金融app
if (browser == "jrApp" && window.jsBridgeV3 != undefined) {
window.jrBridge = window.jrBridge || jsBridgeV3.onReady();
}
//京东app
if (browser == "jdApp") {
var jdAppVersion = jdAppVersion = function () {
var uaArr = navigator.userAgent.toLowerCase().split(';');
return uaArr[2] ? uaArr[2].replace(/\./g, '') : ''
}();
var isJdAppGt500 = jdAppVersion >= 500
}
//各个渠道的分享方法
var shareFn = {
wxShare: function (opts) {
var onBridgeReady = function () {
// 发送给好友;
WeixinJSBridge.on('menu:share:appmessage', function (argv) {
WeixinJSBridge.invoke('sendAppMessage', {
"appid": opts.appId,
"img_url": opts.imgUrl,
"link": opts.link,
"desc": opts.desc,
"title": opts.title
}, function (res) {
opts.shareSuccess && opts.shareSuccess();
});
});
// 分享到朋友圈;
WeixinJSBridge.on('menu:share:timeline', function (argv) {
WeixinJSBridge.invoke('shareTimeline', {
"img_url": opts.imgUrl,
"link": opts.link,
"desc": opts.desc,
"title": opts.title
}, function (res) {
opts.shareSuccess && opts.shareSuccess();
});
});
};
if (typeof WeixinJSBridge == "object") {
onBridgeReady();
} else {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}
},
jrShare: function (opts) {
if (window.jsBridgeV3) {
window.jrBridge = window.jrBridge || jsBridgeV3.onReady();
jrBridge.then(function () {
this.jsToNaWeiXin({
isShow: true, //true 显示btn,false 隐藏btn,默认隐藏
optionType: 1, //1:分享,2:连接跳转类型,3:跳原生模块
btnText: '分享', //原生按钮显示的文字
shareDate: { //optionType=1时候传递,分享信息
appId: '', //微信公共号id,可选填
img: opts.imgUrl, //分享图标
link: opts.link, //分享连接地址
desc: opts.desc, //分享描述
title: opts.title, //分享标题
friendesc: opts.title, //分享朋友圈的文字内容
type: "" //需要回调时候传递,这个type值,杨国亮给,有则传这个值,没有,type 都不要传
}
});
})
}
},
jdShare: function (opts) {
try {
if (!jm.getBrowserInfo().systemType.iOS) { //安卓
// 仅当 shareHelper对象存在时才能下发分享配置,更老版本不支持;未来如果该对象删除了也不出错
if (window.shareHelper) {
// jdApp 5.0版本新增方法
if (typeof shareHelper.initShare === 'function') {
/**
* channel 参数可设置,配置后,分享面板仅出现配置过的项目; 如果为空,则显示默认全部
*/
shareHelper.initShare(JSON.stringify({
"title": opts.title,
"content": opts.desc,
"shareUrl": opts.link,
"iconUrl": opts.imgUrl,
"shareActionType": "S",
"channel": "",
"callback": "N",
"eventId": ""
}));
}
else if (typeof shareHelper.setShareInfo === 'function') {
shareHelper.setShareInfo(opts.title, opts.desc, opts.link, opts.imgUrl)
}
}
} else if (jm.getBrowserInfo().systemType.iOS) {
var link;
// jdApp 5.0及以上版本
if (isJdAppGt500) {
jsonObj = {
category: "jump",
des: "share",
type: "111",
title: opts.title,
content: opts.desc,
shareUrl: opts.link,
//分享的图片url,自定义, V 5.0 之前,使用该字段下发分享icon url
imageUrl: opts.imgUrl,
//分享的图片url,自定义,V 5.0 之后,使用该字段下发分享 icon url
iconUrl: opts.imgUrl,
channel: "",
isCallBack: "N",
shareActionType: "S"
}
link = 'openApp.jdmobile://virtual?params=' + JSON.stringify(jsonObj);
} else {
// 包括 jdApp5.0以下,以及非 jdApp
link = 'openapp.jdmobile://communication?params={' +
'"action":"syncShareData",' +
'"title":"' + opts.title + '",' +
'"content":"' + opts.desc + '",' +
'"shareUrl":"' + opts.link + '",' +
'"iconUrl":"' + opts.imgUrl + '",' +
'"isCallBack":"N"' +
'}';
}
location.href = link;
}
} catch (e) {
}
},
sqShare: function (opts) {
var siv = setInterval(function () {
if (typeof(mqq) != "undefined" && typeof(mqq.ui) != "undefined") {
mqq.ui.setActionButton({
iconID: 4,
callback: function () {
mqq.ui.showShareMenu();
}
});
mqq.ui.setOnShareHandler(function (type) {
if (typeof(type) == "undefined")mqq.ui.showShareMenu();
else {
mqq.ui.shareMessage(
{
title: opts.title,
desc: opts.desc,
share_type: type,
image_url: opts.imgUrl
}
);
}
});
clearInterval(siv);
} else {
arguments.callee.call(this);
}
}, 500);
},
wyShare: function(opts) {
window.walletShare = function() {
jdp.shareUrl({
url: opts.link,
title: opts.title,
desc: opts.desc,
channel: "WX|QQ",
callback: function(status) {}
});
};
if (jm.getBrowserInfo().systemType.iOS) {
var timer = setInterval(function() {
if (window.WebViewJavascriptBridge) {
jdp.setMenu([ {
menuTitle: "分享",
menuAction: "walletShare"
} ]);
clearInterval(timer);
}
}, 100);
} else {
jdp.setMenu([ {
menuTitle: "分享",
menuAction: "walletShare"
} ]);
}
}
};
/**============================
public
==============================*/
jm.share = {
setShare: function (opts) {
switch (browser) {
case "wx": //微信里
shareFn.wxShare(opts);
break;
case "jrApp":
shareFn.jrShare(opts);
break;
case "jdApp":
shareFn.jdShare(opts);
break;
case "mqq":
shareFn.sqShare(opts);
break;
case "wyApp":
shareFn.wyShare(opts);
break;
}
}
}
})(window.jm);