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

 

<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">&nbsp;'+

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>

转载于:https://my.oschina.net/af666/blog/883904

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值