生活在大數據互聯網時代下的咱們,天天都在不停的刷新朋友圈、微博、QQ空間,看到好的文章、圖片等都會不禁自主的想分享。使用者能夠一秒鐘完成內容分享,但是開發者要跳過很多坑後,才能開發出這麼便捷的分享功能。api
![ 195713gggujro3j2q2h0j3.jpg ](/uploads/attachments/457950/20180119/2aaabede4560a9419e1191e3fb9bc256.jpg)微信
那麼,開發者在開發微博、微信、QQ分享功能時,怎麼作才能跳過這些坑,順利完成分享功能?今天,柚子君直接分享源碼給你們,幫大家縮短開發時間。session
------ 這部分能夠放在公共的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);
}ui
------ 這部分能夠放在config.xml ------url
<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>3d
注意:這裏有個坑,就是QQPlus區分安卓和iOS,否則在調用QQ空間分享的時候是能進入到QQ空間的界面的,可是一點提交,就會提示應用不存在。xml
------ 調用方法 ------圖片
zShare.dialog('標題','文本','連接','圖片')ip
------ 圖片附件 ------
![ 2.png ](/uploads/attachments/457950/20180119/0164491b36a8cfe8a136d0f532249e5a.png)
![ ![ 3.png ](/uploads/attachments/457950/20180119/f8a8a9d056df17c420c63fa0e66944bf.png) ](/uploads/attachments/457950/20180119/de6661c3d123e1c27dd3a4eec3d138d6.png)
![ 4.png ](/uploads/attachments/457950/20180119/4fc22e68cbdc28e6fcc76cd13e6a8edf.png)
![ 5.png ](/uploads/attachments/457950/20180119/1de37963e3adc89df0b66da15863eadb.png)
![ 6.png ](/uploads/attachments/457950/20180119/af38ca2acefa552638c0d2249673296a.png)
另外:若是你在使用mobShare,想從mobShare換成獨立的模塊分享,你可能會遇到一個問題,就是不刪除mobShare又加入weibo模塊在編譯的時候會提示模塊衝突,那就要刪掉mobShare,要刪除的話你得先把config.xml中的mobShare代碼刪除,而後提交,再到APICloud模塊管理裏面刪除,若是不刪除代碼的話APICloud模塊管理是不能刪除的。