mui.init();
var shares = null;
// H5 plus事件处理
function plusReady() {
updateSerivces();
}
if(window.plus) {
plusReady();
} else {
document.addEventListener('plusready', plusReady, false);
}
/**
-
更新分享服务
*/
function updateSerivces() {
plus.share.getServices(function(s) {
shares = {};
for(var i in s) {
var t = s[i];
shares[t.id] = t;} logDebug("------>" + JSON.stringify(shares));
}, function(ex) {
logError('获取分享服务列表失败:' + ex);
});
}
function share(share_id) {
logDebug(share_id);
if(share_id == "qq") {
shareAction(shares.qq)
}
if(share_id == "weixin") {
shares.weixin.x = 'WXSceneSession';
shareAction(shares.weixin)
}
if(share_id == "weixin_p") {
shares.weixin.x = 'WXSceneTimeline';
shareAction(shares.weixin)
}
if(share_id == "sinaweibo")
{
shareAction(shares.sinaweibo)
}
}
/**
- 分享操作
- @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
-
@param {Boolean} bh 是否分享链接
*/
function shareAction(sb) {
var curView = plus.webview.currentWebview();if(!sb) {
logDebug('无效的分享服务!');
return;
}var share_content ='';
if(sb.id == "sinaweibo")
{
share_content=curView.title+"http://"+curView.url;
}
else
{
//分享时content太长会报错,此处加上截断处理,保留80个字符
if(curView.content.length > 80) {
share_content=curView.content.substr(0,80);
} else {
share_content=curView.content;
}
}
var msg = {
title : curView.title,
content:share_content,
// href : curView.url,
pictures: ['http://10.133.64.34:8091/static/img/code.jpg'],
sharetype : curView.sharetype,
sharepkey : curView.sharepkey,
thumbs : [curView.thumbs],
extra: {
scene: sb.x
}
};
console.log('<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
console.log(JSON.stringify(msg));
// 发送分享
if(sb.authenticated) {
logDebug('---已授权---');
shareMessage(msg, sb);
} else {
logDebug('---未授权---');
sb.authorize(function() {
shareMessage(msg, sb);
}, function(e) {
logDebug('认证授权失败:' + e.code + ' - ' + e.message);
});
}
}
/** 发送分享消息
@param {JSON} msg
@param {plus.share.ShareService} shareServiceIns
*/
function shareMessage(msg, shareIns) {
logDebug(JSON.stringify(msg));
console.log("--------->"+msg.href);
console.log("--------->"+msg.title);
console.log("--------->"+msg.sharetype);
console.log("--------->"+msg.artcleid);
var post_data = {
'NSHAREPKEY': msg.sharepkey,
'VSHARETYPE': msg.sharetype,
'VSHAREHREF': msg.href,
'uuid': plus.device.uuid
};
callAjaxService('/SysCommon/SaveUserShare', post_data, function(){},0);
console.log(msg.content);
shareIns.send(msg, function() {
logDebug('分享到"' + shareIns.description + '"成功!');
}, function(e) {
logDebug('分享到"' + shareIns.description + '"失败: ' + JSON.stringify(e));
});
}
function closeShare() {
plus.webview.currentWebview().close();
}