share.js 2.05 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/7/29
 * Time: 16:55
 */

const $ = require('jquery');
const jsApiList = [
    'checkJsApi',
    'onMenuShareTimeline',
    'onMenuShareAppMessage',
    'onMenuShareQQ',
    'onMenuShareWeibo',
    'onMenuShareQZone'
];
let shareData = {
    title: document.title,
    link: location.href,
    desc: 'YOHO!BLK',
    imgUrl: 'http://img02.yohoboys.com/staticimg/2016/08/02/16/02fd39c0711f6ecb73d4a096a143e0771d.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: '/api/wechat/share/token',
                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 = (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);
    }
};