您的位置:首页 > 移动开发

Hbuilder 云端打包app微信分享功能源码示例

2017-04-20 00:00 513 查看
<script type="text/javascript">

mui('.mui-scroll-wrapper').scroll({

indicators: true //是否显示滚动条

});

var shares = null;

mui.plusReady(function() {

var article_id = plus.webview.currentWebview().article_id;

var table = document.body.querySelector('.jays-content');

jays.ajaxPost(jays.api.shop_article_info, {

articleId: article_id

}, function(data) {

if(data.code == 1) {

var li = document.createElement('li');

li.className = 'jays-details';

li.innerHTML =

'<div class="jays-details-top">' +

data.data.title +

'</div>' +

'<div class="jays-author mui-clearfix">' +

'<div class="jays-name">' +

data.data.username +

'</div>' +

'<div class="jays-concern-number mui-icon iconfont icon-yanjing"> '+

data.data.views +

'</div>' +

'</div>' +

'<div class="jays-artical">' +

data.data.contents +

'</div>';

table.appendChild(li);

} else {

mui.toast(data.msg);

}

});

//分享功能

plus.share.getServices(function(s) {

shares = {};

for(var i in s) {

var t = s[i];

shares[t.id] = t;

}

}, function(e) {

mui.toast("无享服务!");

console.log("获取分享服务列表失败:" + e.message);

});

});

function shareAction(sb, ishref, msginfo) {

if(!sb || !sb.s) {

mui.toast("无效的分享服务!");

return;

}

var msg = { content: msginfo.content, extra: { scene: sb.x } };

if(ishref) {

msg.title = msginfo.title;

msg.href = msginfo.href;

msg.content = msginfo.content;

msg.thumbs = [msginfo.pic];

msg.pictures = [msginfo.pic];

}

// 发送分享

if(sb.s.authenticated) {

console.log("---已授权---");

shareMessage(msg, sb.s);

} else {

console.log("---未授权---");

sb.s.authorize(function() {

shareMessage(msg, sb.s);

}, function(e) {

console.log("认证授权失败:" + e.code + " - " + e.message);

});

}

}

/**

* 发送分享消息

* @param {JSON} msg

* @param {plus.share.ShareService} s

*/

function shareMessage(msg, s) {

s.send(

msg,

function() {

mui.toast("分享到\"" + s.description + "\"成功! ");

},

function(e) {

mui.toast()

mui.toast("分享到\"" + s.description + "\"失败! ");

// console.log("分享到\"" + s.description + "\"失败: " + JSON.stringify(e));

}

);

}

/**

* 分享内容或者链接

* @param {JSON} msgdata 分享数据的对象

* @param {Boolean} ishref 是否分享链接

*/

var msgdata = {

title: '哪种吃东西方式更健康',

href: 'http://www.baidu.com',

desc: '2017年4月23日',

pic: ''

};

var appshare = function (msgdata, ishref) {

// 分享参数

if(ishref){

var msginfo = { title: msgdata.title, href: msgdata.href, content: msgdata.desc, pic: msgdata.pic };

}else{

var msginfo = { content: msgdata.desc };

}

var shareBts = [];

// 更新分享列表

var ss = shares['weixin'];

ss && ss.nativeClient && (shareBts.push({ title: '微信朋友圈', s: ss, x: 'WXSceneTimeline' }), shareBts.push({ title: '微信好友', s: ss, x: 'WXSceneSession' }));

ss = shares['qq'];

ss && ss.nativeClient && shareBts.push({ title: 'QQ', s: ss });

if(!ishref){

ss = shares['sinaweibo'];

ss && shareBts.push({ title: '新浪微博', s: ss });

}

// 弹出分享列表

shareBts.length > 0 ? plus.nativeUI.actionSheet({

title: '分享',

cancel: '取消',

buttons: shareBts

},

function(e) {

(e.index > 0) && shareAction(shareBts[e.index - 1], ishref, msginfo);

}

) : plus.nativeUI.alert('当前环境无法支持分享操作!');

}

mui('body').on('tap', '.more', function(){

appshare(msgdata,true);

});

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐