h5+分享到、朋友圈代码⽰例
(function(window) {
var Share={};
Share.info = {
id: '',
name: '',
head_image: "_www/images/icon/A/144.png",
introduce: ''
};
/**
* 更新分享服务
*/
var shares = null;
function getSerivces() {
Services(function(s) {
shares = {};
for (var i in s) {
var t = s[i];
shares[t.id] = t;
}
}, function(e) {
console.log("获取分享服务列表失败:" + e.message);
});
};
function shareAction(id, ex) {
var s = null;
if (!id || !(s = shares[id])) {
console.log("⽆效的分享服务!");
return;
}
if (s.authenticated) {
console.log("---已授权---");
shareMessage(s, ex);
} else {
代码转换console.log("---未授权---");
//TODO 授权⽆法回调,有bug
s.authorize(function() {
console.log('授权成功...')
shareMessage(s, ex);
}, function(e) {
console.log("认证授权失败:" + e.code + " - " + e.message);
});
}
};
var sharecount = 0;
/**
* 发送分享消息
* @param
*/
function shareMessage(s, ex) {
plus.nativeUI.showWaiting();
setTimeout(plus.nativeUI.closeWaiting,5000);//TODO 5秒后⾃动关闭等待,否则如果⽤户分享出去后选择‘留在’,再⼿动回到app的时候,waiting⽆法关闭var msg = {
extra: {
scene: ex
}
};
msg.href = "分享的⽹址" + "share?hid=" + Share.info.id;
msg.title = "我在xxxx等你——" + Share.info._name;
//取本地图⽚
var img = vertAbsoluteFileSystem(Share.info.place('file://', ''));
console.log(img);
msg.thumbs = [img];
if (sharecount > 0) {
//如果本地图⽚过⼤,导致分享失败,递归时重新分享获取默认图⽚
msg.thumbs = ["_www/images/icon/A/144.png"];
}
console.log(JSON.stringify(msg));
s.send(msg, function() {
plus.nativeUI.closeWaiting();
var strtmp = "分享到\"" + s.description + "\"成功! ";
console.log(strtmp);
ast(strtmp, {
verticalAlign: 'center'
});
sharecount = 0;
}, function(e) {
plus.nativeUI.closeWaiting();
if (e.code == -2) {
ast('已取消分享', {
verticalAlign: 'center'
});
sharecount = 0;
} else if (e.code == -3 || e.code == -8) {
console.de);
if (++sharecount < 2) {
//TODO 分享失败可能是图⽚过⼤的问题,递归取默认图⽚重新分享
shareMessage(s, ex);
} else {
sharecount = 0;
ast('分享失败', {
verticalAlign: 'center'
});
}
}else{
<('分享失败:'+JSON.stringify(e))
}
console.log("分享到\"" + s.description + "\"失败: " + e.code + " - " + e.message); });
};
function share() {
bhref = true;
var ids = [{
id: "weixin",
ex: "WXSceneSession"
}, {
id: "weixin",
ex: "WXSceneTimeline"
}],
bts = [{
title: "发送给好友"
}, {
title: "分享到朋友圈"
}];
plus.nativeUI.actionSheet({
cancel: "取消",
buttons: bts
},
function(e) {
var i = e.index;
if (i > 0) {
shareAction(ids[i - 1].id, ids[i - 1].ex);
}
}
);
};
Share.share=share;
window.Share = Share;
mui.plusReady(function() {
getSerivces();
});
})(window)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论