javascript實現的微信分享外掛

antzone發表於2017-03-21

下面分享一段來自於網路上的javascript實現的微信分享外掛,希望能夠給需要的朋友帶來幫助。

程式碼如下:

[JavaScript] 純文字檢視 複製程式碼
/*******************************
 * Author:Mr.Think
 * Description:微信分享通用程式碼
 * 使用方法:_WXShare('分享顯示的LOGO','LOGO寬度','LOGO高度','分享標題','分享描述','分享連結','微信APPID(一般不用填)');
 *******************************/
function _WXShare(img,width,height,title,desc,url,appid){
  //初始化引數
  img=img||'http://a.zhixun.in/plug/img/ico-share.png';
  width=width||100;
  height=height||100;
  title=title||document.title;
  desc=desc||document.title;
  url=url||document.location.href;
  appid=appid||'';
  //微信內建方法
  function _ShareFriend() {
    WeixinJSBridge.invoke('sendAppMessage',{
      'appid': appid,
      'img_url': img,
      'img_width': width,
      'img_height': height,
      'link': url,
      'desc': desc,
      'title': title
    }, function(res){
      _report('send_msg', res.err_msg);
    })
  }
  function _ShareTL() {
    WeixinJSBridge.invoke('shareTimeline',{
      'img_url': img,
      'img_width': width,
      'img_height': height,
      'link': url,
      'desc': desc,
      'title': title
    }, function(res) {
      _report('timeline', res.err_msg);
    });
  }
  function _ShareWB() {
    WeixinJSBridge.invoke('shareWeibo',{
      'content': desc,
      'url': url,
    }, function(res) {
      _report('weibo', res.err_msg);
    });
  }
  // 當微信內建瀏覽器初始化後會觸發WeixinJSBridgeReady事件。
  document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
    // 傳送給好友
    WeixinJSBridge.on('menu:share:appmessage', function(argv){
      _ShareFriend();
    });
 
    // 分享到朋友圈
    WeixinJSBridge.on('menu:share:timeline', function(argv){
      _ShareTL();
    });
 
    // 分享到微博
    WeixinJSBridge.on('menu:share:weibo', function(argv){
      _ShareWB();
    });
  }, false);
}

相關文章