MUI Android分享功能

来源:互联网 发布:学网络编程 编辑:程序博客网 时间:2024/06/05 16:36



/** 更新分享服务 */

/*   更新分享服务需要在 mui.plusReady里执行 

*/

function updateSerivces() {
    plus.share.getServices(
        function(data) {
            shares = {};
            for (var i in data) {
                var t = data[i];
                shares[t.id] = t;
            }
           },
        function(data) {
            mui.toast("获取分享服务列表失败:" + data.message);
        }
    );
}

/** 分享事件 */

/*两个参数


index  要分享的下标


msg                                   分享信息
        href:        msg.url,               连接地址

        title:         msg.title,             标题名称
        content:    msg.content,    内容名称
        thumbs:     [msg.image],  图标
        pictures:     [msg.image]  图标


*/


function share(index,msg) {
    var ids =
       [{id: "qq"},                                                              /*QQ好友*/
        {id: "weixin",ex: "WXSceneSession"},         /*微信好友*/
        {id: "weixin",ex: "WXSceneTimeline"},        /*微信朋友圈*/
        {id: "sinaweibo"}                                                /*新浪微博*/
       ];                
    shareAction(ids[index].id, ids[index].ex,msg);
}   

/** 分享操作 */

function shareAction(id, ex, msg) {
    var sharedServices = null;
    if (!id || !(sharedServices = shares[id])) {
        mui.toast("无效的分享服务!");
        return;
    }
    if (sharedServices.authenticated) {
        sendShare(sharedServices, ex,msg);
    } else {
        sharedServices.authorize(
            function() {
                sendShare(sharedServices, ex,msg);
            },
            function(e) {
                mui.toast("认证授权失败");
            }
        );
    }
}

/** 发送分享消息 */

function sendShare(sharedServices, ex,msg) {
    /*信息*/
    var message= {
        href:        msg.url,

        title:         msg.title,
        content:    msg.content,
        thumbs:     [msg.image],
        pictures:     [msg.image],
        extra: {
            scene: ex
        }
    };
    sharedServices.send(
        message,
        function() {
            mui.toast("分享成功!");
        },
        function(e) {
        }
    );
}

原创粉丝点击