h5+ 開發分享功能

  • 2020 年 5 月 23 日
  • 筆記

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

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

 

方式一、JS+HTML+h5Plus

1.html程式碼

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

2.JS程式碼

// 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:時間倉促,先寫這麼多吧。詳細註解後期會補上