share.js 3.48 KB
let shareData = {
  title: '来YO!社区,一起玩潮流!',
  link: '',
  desc: 'Yoho!buy有货App',
  imgUrl: 'http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png'
};

let jsApiList = [
  'checkJsApi',
  'onMenuShareTimeline',
  'onMenuShareAppMessage',
  'onMenuShareQQ',
  'onMenuShareWeibo',
  'onMenuShareQZone'
];

const setWxShareData = function(shareInfo) {
  shareInfo = shareInfo || shareData;
  window.wx.onMenuShareTimeline(shareInfo);
  window.wx.onMenuShareAppMessage(shareInfo);
  window.wx.onMenuShareQQ(shareInfo);
  window.wx.onMenuShareQZone(shareInfo);
  window.wx.onMenuShareWeibo(shareInfo);
};

function loadScript(url, success) {
  const head = document.getElementsByTagName('head')[0];
  let script = document.createElement('script');

  script.type = 'text/javascript';
  script.onload = script.onreadystatechange = function() {
    if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
      success && success();
      script.onload = script.onreadystatechange = null;
    }
  };
  script.src = url;
  head.appendChild(script);
}

function jsonp(url, data = {}) {
  return new Promise((resolve) => {
    let JSONP = document.createElement('script');
    let query = [];

    data.callback = 'jsonCallBack';
    Object.keys(data).forEach(key => {
      query.push(key + '=' + data[key]);
    });

    window.jsonCallBack = (result) => {
      resolve(result);
    };

    JSONP.type = 'text/javascript';
    JSONP.src = `${url}?${query.join('&')}`;
    const head = document.getElementsByTagName('head')[0];

    head.appendChild(JSONP);

    setTimeout(() => {
      head.removeChild(JSONP);
    }, 500);
  });
}

function init(qs) {
  if (/QQ/i.test(navigator.userAgent)) {
    loadScript('//qzonestyle.gtimg.cn/qzone/qzact/common/share/share.js', function() {
      window.setShareInfo && window.setShareInfo({
        title: shareData.title,
        summary: shareData.desc,
        pic: shareData.imgUrl,
        url: shareData.link || location.href
      });
    });
  }

  if (/MicroMessenger/i.test(navigator.userAgent)) {
    loadScript('//res.wx.qq.com/open/js/jweixin-1.3.2.js', () => {
      jsonp(location.protocol + '//m.yohobuy.com/activity/wechat/share', {
        url: encodeURIComponent(location.href.split('#')[0])
      }).then(res => {
        if (window.wx) {
          window.wx.config({
            debug: false,
            appId: res.appId,
            timestamp: res.timestamp,
            nonceStr: res.nonceStr,
            signature: res.signature,
            jsApiList: jsApiList
          });

          window.wx.ready(function() {
            setWxShareData();
          });
        }
      });
    });
  }

  if (qs && qs.share_id) {
    jsonp(location.protocol + '//m.yohobuy.com/activity/share', {
      shareId: qs.share_id
    }).then(res => {
      if (res && res.code === 200 && res.data) {
        shareData.desc = res.data.content || res.data.title;
        shareData.imgUrl = res.data.pic;
        shareData.title = res.data.title;
      }
    });
  }
}

export default {
  init,
  setShareInfo(data) {
    setTimeout(() => {
      let shareInfo = Object.assign({}, shareData, data || {});

      shareInfo.link = shareInfo.link || location.href;

      if (window.wx) {
        setWxShareData(shareInfo);
      }

      window.setShareInfo && window.setShareInfo({
        title: shareInfo.title,
        summary: shareInfo.desc,
        pic: shareInfo.imgUrl,
        url: shareInfo.link
      });
    }, 300);
  }
};