share.js 3.91 KB
import $ from 'jquery';
import utils from './utils';
import jsonp from './jsonp';

let jsApiList = [
    'checkJsApi',
    'onMenuShareTimeline',
    'onMenuShareAppMessage',
    'onMenuShareQQ',
    'onMenuShareWeibo',
    'onMenuShareQZone'
];
let shareData = {
    title: document.title,
    link: location.href,
    desc: 'YOHO!BUY',
    imgUrl: 'http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png'
};

let _getShareDataById = function() {
    let shareId = utils.queryString().share_id;
    if (shareId) {
        return jsonp({
            url: '//m.yohobuy.com/activity/share?callback=?',
            data: {
                shareId: shareId
            }
        }).then(function(res) {
            if (res && res.code === 200 && res.data) {
                shareData.title = res.data.title;
                shareData.link = res.data.link || location.href;
                shareData.desc = res.data.content || res.data.title;
                shareData.imgUrl = res.data.pic;
            }
            return $.Deferred().resolve();
        }, function(){
            return $.Deferred().resolve();
        });
    } else {
        let shareTitle = $('#shareTitle').val();
        let shareImg = $('#shareImg').val();
        let shareDesc = $('#shareDesc').val();
        let shareLink = $('#shareLink').val();

        shareData.title = shareTitle ? shareTitle : shareData.title;
        shareData.imgUrl = shareImg ? shareImg : shareData.imgUrl;
        shareData.desc = shareDesc ? shareDesc : shareData.desc;
        shareData.link = shareLink ? shareLink : shareData.link;
        return $.Deferred().resolve();
    }
};

export default {
    init() {
        if (/QQ/i.test(navigator.userAgent)) {
            $.ajax({
                url: '//qzonestyle.gtimg.cn/qzone/qzact/common/share/share.js',
                dataType: 'script',
                cache: true,
                success: function() {
                    _getShareDataById().then(function(){
                        window.setShareInfo && window.setShareInfo({
                            title: shareData.title,
                            summary: shareData.desc,
                            pic: shareData.imgUrl,
                            url: shareData.link
                        });
                    });
                }
            });
        }

        if (/MicroMessenger/i.test(navigator.userAgent)) {
            $.ajax({
                url: '//res.wx.qq.com/open/js/jweixin-1.3.2.js',
                dataType: 'script',
                cache: true,
                success: function() {
                    jsonp({
                        url: '//m.yohobuy.com/activity/wechat/share?callback=?',
                        data: {
                            url: location.href
                        }
                    }).then(function(res) {
                        if (window.wx) {
                            window.wx.config({
                                debug: false,
                                appId: res.appId,
                                timestamp: res.timestamp,
                                nonceStr: res.nonceStr,
                                signature: res.signature,
                                jsApiList: jsApiList
                            });

                            _getShareDataById().then(function() {
                                window.wx.ready(function() {
                                    window.wx.onMenuShareAppMessage(shareData);
                                    window.wx.onMenuShareTimeline(shareData);
                                    window.wx.onMenuShareQQ(shareData);
                                    window.wx.onMenuShareWeibo(shareData);
                                    window.wx.onMenuShareQZone(shareData);
                                });
                            });
                        }
                    });
                }
            });
        }
    }
};