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

const $ = require('yoho-jquery');

$.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: [
                            'checkJsApi',
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo',
                            'hideMenuItems',
                            'showMenuItems',
                            'hideAllNonBaseMenuItem',
                            'showAllNonBaseMenuItem',
                            'translateVoice',
                            'startRecord',
                            'stopRecord',
                            'onRecordEnd',
                            'playVoice',
                            'pauseVoice',
                            'stopVoice',
                            'uploadVoice',
                            'downloadVoice',
                            'chooseImage',
                            'previewImage',
                            'uploadImage',
                            'downloadImage',
                            'getNetworkType',
                            'openLocation',
                            'getLocation',
                            'hideOptionMenu',
                            'showOptionMenu',
                            'closeWindow',
                            'scanQRCode',
                            'chooseWXPay',
                            'openProductSpecificView',
                            'addCard',
                            'chooseCard',
                            'openCard'
                        ]
                    });
                }
            }
        });
    }
});

module.exports = (shareData) => {
    if (window.wx) {
        window.wx.ready(function() {
            window.wx.onMenuShareAppMessage(shareData);
            window.wx.onMenuShareTimeline(shareData);
            window.wx.onMenuShareQQ(shareData);
            window.wx.onMenuShareWeibo(shareData);
        });
    }
};