Commonly used sharing source code (including Weibo, WeChat, QQ sharing...)

Living in the era of big data Internet, we are constantly refreshing the circle of friends, Weibo, and QQ space every day. When we see good articles, pictures, etc., we can't help but want to share. Users can complete content sharing in one second, but developers have to jump through many pits before they can develop such a convenient sharing function.

![ 195713gggujro3j2q2h0j3.jpg ](/uploads/attachments/457950/20180119/2aaabede4560a9419e1191e3fb9bc256.jpg)

So, what should developers do to skip these pits and successfully complete the sharing function when developing the sharing functions of Weibo, WeChat and QQ? Today, Grapefruit Jun directly shares the source code with you to help you shorten the development time.

------ This part can be placed in public JS ------

var zShare = {};
zShare.dialog = function(title,text,url,img){
   var shareItems = [
           {text:' WeChat friends',icon:'widget://image/wxsession.png'},
           {text:'WeChat Moments',icon:'widget://image/wxtimeline.png'},
           {text:'Sina Weibo ',icon:'widget://image/sinaWb.png'},
           {text:'QQ',icon:'widget://image/qq.png'},
           {text:'
       ]
   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:'Sharing, please wait',duration:2000,location:"middle"});
           } else {
               api.toast({msg:'Not installed Wechat , unable to share',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: 'successful sharing',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: 'Sharing, please wait',duration:2000,location:"middle"});
       } else {
           api.toast({msg: 'No QQ installed, unable to share',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>

Note: There is a pit here, that is, QQPlus distinguishes between Android and iOS, otherwise you can enter QQ when calling QQ space to share The interface of the space, but a little submission will prompt that the application does not exist.

------ Call method ------

zShare.dialog('title', 'text', 'link', 'picture')

------ picture attachment ------
![ 2.
! [! [3.png] (/ uploads / Attachments / 457 950 / 20,180,119 / f8a8a9d056df17c420c63fa0e66944bf.png)] (/ uploads / Attachments / 457 950 / 20,180,119 / de6661c3d123e1c27dd3a4eec3d138d6.png)
! [4.png] (/ uploads / Attachments / 457 950 /20180119/4fc22e68cbdc28e6fcc76cd13e6a8edf.png)

! [5.png] (/ uploads / Attachments / 457 950 / 20,180,119 / 1de37963e3adc89df0b66da15863eadb.png)
[6.png] (/ uploads / Attachments / 457 950 / 20,180,119 / af38ca2acefa552638c0d2249673296a.png)!

Also: if If you are using mobShare and want to change from mobShare to independent module sharing, you may encounter a problem, that is, adding the weibo module without deleting mobShare will prompt a module conflict when compiling, then delete mobShare, if you want to delete it You have to delete the mobShare code in config.xml first, then submit it, and then delete it in the APICloud module management. If the code is not deleted, the APICloud module management cannot be deleted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326178615&siteId=291194637
Recommended