帖子
帖子
用户
博客
课程
123下一页
返回列表 发新帖
显示全部楼层
5
帖子
0
勋章
115
Y币

常用的分享源码(含微博分享,微信分享,QQ分享,QQ空间...

  [复制链接]
发表于 2017-7-18 19:57:57
分享的功能花了一些时间,中间有不少坑,这里总结一下方便大家:样子是这样的:


先上源码:
---------------- 这部分可以放在公共的JS里面 ----------------
var zShare = {};
zShare.dialog = function(title,text,url,img){
    var shareItems = [
            {text:'微信好友',icon:'widget://image/wxsession.png'},
            {text:'微信朋友圈',icon:'widget://image/wxtimeline.png'},
            {text:'新浪微博',icon:'widget://image/sinaWb.png'},
            {text:'QQ',icon:'widget://image/qq.png'},
            {text:'QQ空间',icon:'widget://image/qZone.png'}
        ]
    var shareColumn = 5;
    var dialogBox = api.require('dialogBox');
    dialogBox.actionMenu ({
        rect:{h:150},
        texts:{cancel:'取消'},
        items:shareItems,
        styles:{
            bg:'#FFF',
            column:shareColumn,
            itemText: {color:'#000',size: 12,marginT:8},
            itemIcon: {size:50},
            cancel: {color:'#000',h: 40,size: 14}
        }
    }, function(ret){
        if(ret.eventType=='cancel'){
            dialogBox.close({dialogName:'actionMenu'});
        } else if(ret.eventType=='click'){
            if(ret.index==0){
                zShare.wxNews('session',title,text,url,img);
            } else if(ret.index==1){
                zShare.wxNews('timeline',title,text,url,img);
            } else if(ret.index==2){
                zShare.weiboNews('sinaWb',title,text,url,img);
            } else if(ret.index==3){
                zShare.qqNews('QFriend',title,text,url,img);
            } else if(ret.index==4){
                zShare.qqNews('QZone',title,text,url,img);
            }
        }
    });
}
zShare.wxNews = function(tar,title,text,url,img){
    filename = (new Date()).valueOf()+'.'+zShare.ext(img);
    api.download({
        url: img,
        savePath: 'fs://'+filename,
        report: false,
        cache: true,
        allowResume: true
    }, function(ret, err) {
        var wx = api.require('wx');
        wx.isInstalled(function(ret){
            if(ret.installed) {
                api.toast({msg:'分享中,请稍候',duration:2000,location:"middle"});
            } else {
                api.toast({msg:'没有安装微信,无法进行分享',duration:2000,location:"middle"});
            }
        });
        wx.shareWebpage({
            apiKey: '',
            scene: tar,
            title: title,
            description: text,
            thumb: 'fs://'+filename,
            contentUrl: url
        }, function(ret, err) {
            if (ret.status) {
                api.toast({msg: '分享成功',duration:2000, location: "middle"});
            }
        });
    });
}
zShare.qqNews = function(tar,title,text,url,img){
    var qq = api.require('QQPlus');
    qq.installed(function(ret){
        if(ret.status) {
            api.toast({msg:'分享中,请稍候',duration:2000,location:"middle"});
        } else {
            api.toast({msg:'没有安装QQ,无法进行分享',duration:2000,location:"middle"});
        }
    });
    qq.shareNews({
        url: url,
        title: title,
        description: text,
        imgUrl: img,
        type: tar
    },function(ret,err){
        if (ret.status){
            api.toast({msg: '分享成功',duration:2000, location: "botoom"});
        }
    });
}
zShare.weiboNews = function(tar,title,text,url,img){
    filename = (new Date()).valueOf()+'.'+zShare.ext(img);
    api.download({
        url: img,
        savePath: 'fs://'+filename,
        report: false,
        cache: true,
        allowResume: true
    }, function(ret, err) {
        var weibo = api.require('weibo');
        weibo.shareImage({
            text: title+text+url,
            imageUrl: 'fs://'+filename
        }, function(ret, err) {
            if (ret.status) {
                api.toast({msg:'分享成功',duration:2000,location:"middle"});
            }
        });
    });
}
zShare.ext = function(fileName) {
    return fileName.substring(fileName.lastIndexOf('.') + 1);
}

---------------- 这部分可以放在config.xml ----------------
  <feature name="QQPlus">
    <param name="urlScheme" value="tencent123456789"/>
    <param name="apiKey" value="123456789"/>
  </feature>
  <feature name="wx">
    <param name="urlScheme" value="wx1**********2e"/>
    <param name="apiKey" value="wx1**********2e"/>
    <param name="apiSecret" value="6a9*****************43c"/>
  </feature>
  <feature name="weibo">
    <param name="urlScheme" value="wb123456789"/>
    <param name="apiKey" value="123456789"/>
    <param name="registUrl" value="http://www.apicloud.com"/>
  </feature>

  注意:这里有个坑,就是QQPlus区分安卓和IOS,不然在调用QQ空间分享的时候是能进入到QQ空间的界面的,但是一点提交,就会提示应用不存在
---------------- 调用方法 ----------------
zShare.dialog('标题','文本','链接','图片')
---------------- 图片附件 ----------------


另外:如果你在使用mobShare的话,我不知道怎么弄,因为我没有能够用mobShare测试成功,如果你想从mobShare换成独立的模块分享,那你可能会遇到一个问题,就是不删除mobShare又加入weibo模块在编译的时候会提示模块冲突,那就要删掉mobShare,要删除的话你得先把config.xml中的mobShare代码删除,然后提交,再到APICloud模块管理里面删除,如果不删除代码的话APICloud模块管理是不能删除的。

本帖子中包含更多资源,您需要 登录 才可以下载或查看,没有帐号?立即注册

X
1682
帖子
10
勋章
115
Y币
感谢分享
30
帖子
0
勋章
123
Y币
mark 一下
1
帖子
0
勋章
13
Y币
谢谢分享
22
帖子
1
勋章
204
Y币
这个分享的url怎么取得
5
帖子
0
勋章
115
Y币
froth 发表于 2017-9-24 10:58
这个分享的url怎么取得

URL地址是对应的服务端的一个网页地址。就是说分享后其他用户点击这个分享实际上看到的是一个网页。
12
帖子
0
勋章
3
Y币
把你做好的那个例子二维码发来一下,借鉴一下
2
帖子
0
勋章
9
Y币
感谢分享
63
帖子
0
勋章
78
Y币
好东西,收藏一下。
63
帖子
0
勋章
78
Y币
     
123下一页
您需要登录后才可以回帖 登录

本版积分规则