Hbuilder打包Vue app項目實現分享功能


<script type="text/javascript">

  var auths = null;

  // 監聽plusready事件  

  document.addEventListener("plusready", function() {

  // 擴展API加載完畢,現在可以正常調用擴展API

  plus.oauth.getServices(function(services) {

  auths = services;

  }, function(e) {

  alert("獲取分享服務列表失敗:" + e.message + " - " + e.code);

  });

  // 分享功能
  var Intent = null,
      File = null,
      Uri = null,
      main = null;
  var shares = null;
  var shareImageUrl = '';
  // updateSerivces方法中plus.share與上面的plus.oauth不知道有沒有重合,以防萬一還是加上了
  updateSerivces();
  if (plus.os.name == "Android") {
    Intent = plus.android.importClass("android.content.Intent");
    File = plus.android.importClass("java.io.File");
    Uri = plus.android.importClass("android.net.Uri");
    main = plus.android.runtimeMainActivity();
  }

  }, false);

  function updateSerivces() {
    plus.share.getServices(function(s) {
      shares = {};
      for (var i in s) {
        var t = s[i];
        shares[t.id] = t;
      }
      alert("獲取分享服務列表成功");
    }, function(e) {
      alert("獲取分享服務列表失敗:" + e.message);
    });
    }

 </script>

所有獲取APi功能的接口都得在plusready事件之后調用

在index.html header中調用完API后,在需要分享的頁面通過點擊事件調用share方法

// 微信分享
share () {
  var ids = [{
            id: "weixin", 
            ex: "WXSceneSession"  /*微信好友*/
        }, {
            id: "weixin",
            ex: "WXSceneTimeline" /*微信朋友圈*/
        }, {
            id: "qq"   /*QQ好友*/
        }, {
            id: "tencentweibo"   /*騰訊微博*/
        },{
            id: "sinaweibo"  /*新浪微博*/
        }],
        bts = [{
            title: "發送給微信好友"
        }, {
            title: "分享到微信朋友圈"
        }, {
            title: "分享到QQ"
        }, {
            title: "分享到騰訊微博"
        }, {
            title: "分享到新浪微博"
        }];
    plus.nativeUI.actionSheet({
            cancel: "取消",
            buttons: bts
        },function(e) {
            var i = e.index;
            alert('i:' + i)
            if (i > 0) {
                // this.shareAction(ids[i - 1].id, ids[i - 1].ex);
                var id = ids[i - 1].id;
                var ex = ids[i - 1].ex
                var s = null;
                alert('ex:'+ex)
                if (!id || !(s = shares[id])) {
                    alert("無效的分享服務!");
                    return;
                }
                if (s.authenticated) {
                    alert("---已授權---");
                    var msg = {
                      content: '分享-詳情',
                      href: '***',分享鏈接
                      title: '',標題
                      content: '',分享描述
                      thumbs: ['http://img3.3lian.com/2013/v10/4/87.jpg'],
                      pictures: ['http://img3.3lian.com/2013/v10/4/87.jpg'],
                      extra: {
                        scene: ex
                      }
                    };
                    s.send(msg, function() {
                        alert("分享成功!");
                    }, function(e) {
                        alert("分享失敗!");
                    });
                } else {
                    alert("---未授權---");
                    s.authorize(function() {
                      var msg = {
                          content: '分享-詳情',
                          href: '**',
                          title: '',
                          content: '',
                          thumbs: ['http://img3.3lian.com/2013/v10/4/87.jpg'],
                          pictures: ['http://img3.3lian.com/2013/v10/4/87.jpg'],
                          extra: {
                            scene: ex
                         }
                      };
                      s.send(msg, function() {
                          alert("分享成功!");
                      }, function(e) {
                          alert("分享失敗!");
                      });
                    }, function(e) {
                        alert("認證授權失敗");
                    });
                }
            }
        }
    );
}

在Hbuilder manifest.json文件中的SDK配置中的plus.share微信消息及朋友圈勾選並填寫appid與appsecret,在模塊權限配置中添加Share(分享)模塊后,打包即可

大概記錄功能,代碼有待完善


免責聲明!

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



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