share.js 2.02 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/7/29
 * Time: 16:55
 */
var jsApiList = [
    'checkJsApi',
    'onMenuShareTimeline',
    'onMenuShareAppMessage',
    'onMenuShareQQ',
    'onMenuShareWeibo',
    'onMenuShareQZone'
];
var 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'
};

if (/MicroMessenger/i.test(navigator.userAgent)) {
    $.ajax({
        url: '//res.wx.qq.com/open/js/jweixin-1.1.0.js',
        dataType: 'script',
        cache: true,
        success: function() {
            $.ajax({
                url: '/activity/wechat/share',
                data: {
                    url: location.href
                },
                success: function(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() {
                            window.wx.onMenuShareAppMessage(shareData);
                            window.wx.onMenuShareTimeline(shareData);
                            window.wx.onMenuShareQQ(shareData);
                            window.wx.onMenuShareWeibo(shareData);
                            window.wx.onMenuShareQZone(shareData);
                        });
                    }
                }
            });
        }
    });
}
module.exports = function(data) {
    shareData = data;

    if (window.wx) {
        window.wx.onMenuShareAppMessage(shareData);
        window.wx.onMenuShareTimeline(shareData);
        window.wx.onMenuShareQQ(shareData);
        window.wx.onMenuShareWeibo(shareData);
        window.wx.onMenuShareQZone(shareData);
    }
};