h5+ 開發分享功能


h5+ 開發微信、QQ分享功能

此處只做微信網頁分享示例代碼

 

方式一、JS+HTML+h5Plus

1.html代碼

<div class="button" onclick="shareWeb()">分享網頁</div>

2.JS代碼

var shares=null;
var sweixin=null;
var buttons=[
{title:'我的好友',extra:{scene:'WXSceneSession'}},
{title:'朋友圈',extra:{scene:'WXSceneTimeline'}},
{title:'我的收藏',extra:{scene:'WXSceneFavorite'}}
];


// H5 plus事件處理
function plusReady(){
  updateSerivces();
}
if(window.plus){
  plusReady();
}else{
  document.addEventListener('plusready', plusReady, false);
}

/**
*1. 更新分享服務

* 根據授權,通過getServices方法獲取分享服務

*/

function updateSerivces(){
    plus.share.getServices(function(s){
        shares={};
        for(var i in s){
            var t=s[i];
            shares[t.id]=t;
        }
    sweixin=shares['weixin'];
    }, function(e){
        outSet('獲取分享服務列表失敗:'+e.message);
    });
}
/*
2.
*/
// 分享網頁
function shareWeb(){
  var msg={
    type:'web',
    thumbs:['_www/logo.png'],
    href:'分享網頁的鏈接',
    title:'分享網頁的標題',
    content:'分享網頁的描述'
  };

  sweixin?plus.nativeUI.actionSheet({title:'分享網頁到微信',cancel:'取消',buttons:buttons}, function(e){
    (e.index>0)&&share(sweixin, msg, buttons[e.index-1]);
  }):plus.nativeUI.alert('當前環境不支持微信分享操作!'); 
}
//

//3.分享

function share(srv, msg, button){
  outSet('分享操作:');
  if(!srv){
    outLine('無效的分享服務!');
    return;
  }
  button&&(msg.extra=button.extra);
  // 發送分享
  if(srv.authenticated){
    
    doShare(srv, msg);
  }else{
    srv.authorize(function(){
      doShare(srv, msg);
    }, function(e){
  
});

//4. 發送分享

function doShare(srv, msg){
  outLine(JSON.stringify(msg));
  srv.send(msg, function(){
    outLine('分享到"'+srv.description+'"成功!');
  }, function(e){
    outLine('分享到"'+srv.description+'"失敗: '+JSON.stringify(e));
  });
}

 

 

 

 

Tip:時間倉促,先寫這么多吧。詳細注解后期會補上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM