share.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var shares=null;
  2. var Intent=null,File=null,Uri=null,main=null;
  3. // H5 plus事件处理
  4. function plusReady(){
  5. updateSerivces();
  6. if(plus.os.name=="Android"){
  7. main = plus.android.runtimeMainActivity();
  8. Intent = plus.android.importClass("android.content.Intent");
  9. File = plus.android.importClass("java.io.File");
  10. Uri = plus.android.importClass("android.net.Uri");
  11. }
  12. }
  13. if(window.plus){
  14. plusReady();
  15. }else{
  16. document.addEventListener("plusready",plusReady,false);
  17. }
  18. /**
  19. *
  20. * 更新分享服务
  21. */
  22. function updateSerivces(){
  23. plus.share.getServices( function(s){
  24. shares={};
  25. for(var i in s){
  26. var t=s[i];
  27. shares[t.id]=t;
  28. }
  29. }, function(e){
  30. alert("获取分享服务列表失败:"+e.message );
  31. } );
  32. }
  33. /**
  34. * 分享操作
  35. * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
  36. * @param {Boolean} bh 是否分享链接
  37. */
  38. function shareAction(sb,bh) {
  39. if(!sb||!sb.s){
  40. alert("无效的分享服务!");
  41. return;
  42. }
  43. var msg={content:sharehrefDes.value,extra:{scene:sb.x}};
  44. if(bh){
  45. msg.href=sharehref.value;
  46. if(sharehrefTitle&&sharehrefTitle.value!=""){
  47. msg.title=sharehrefTitle.value;
  48. }
  49. if(sharehrefDes&&sharehrefDes.value!=""){
  50. msg.content=sharehrefDes.value;
  51. }
  52. msg.thumbs=["spzf_16.png"];
  53. msg.pictures=["spzf_19.png"];
  54. }else{
  55. if(pic&&pic.realUrl){
  56. msg.pictures=[pic.realUrl];
  57. }
  58. }
  59. // 发送分享
  60. if ( sb.s.authenticated ) {
  61. alert("---已授权---");
  62. shareMessage(msg,sb.s);
  63. } else {
  64. alert("---未授权---");
  65. sb.s.authorize( function(){
  66. shareMessage(msg,sb.s);
  67. },function(e){
  68. alert("认证授权失败:"+e.code+" - "+e.message );
  69. });
  70. }
  71. }
  72. /**
  73. * 发送分享消息
  74. * @param {JSON} msg
  75. * @param {plus.share.ShareService} s
  76. */
  77. function shareMessage(msg,s){
  78. alert(JSON.stringify(msg));
  79. s.send( msg, function(){
  80. alert("分享到\""+s.description+"\"成功! " );
  81. }, function(e){
  82. alert( "分享到\""+s.description+"\"失败: "+JSON.stringify(e) );
  83. } );
  84. }
  85. // 分析链接
  86. function shareHref(){
  87. var shareBts=[];
  88. // 更新分享列表
  89. var ss=shares['weixin'];
  90. ss&&ss.nativeClient&&(shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
  91. shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'}));
  92. ss=shares['qq'];
  93. ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});
  94. // 弹出分享列表
  95. shareBts.length>0?plus.nativeUI.actionSheet({title:'分享链接',cancel:'取消',buttons:shareBts},function(e){
  96. (e.index>0)&&shareAction(shareBts[e.index-1],true);
  97. }):plus.nativeUI.alert('当前环境无法支持分享链接操作!');
  98. }