function toConfigure() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: '//www.4dage.com/wechat/jssdk/share',
            type: "get",
            data: {
                uri: location.href.split("#")[0],
                name: "厦门四维时代微信公众号"
            },
            dataType: "jsonp",
            //jsonpCallback: "success_jsonp",
            success: function(data, textStatus) {
                wx.config({
                    debug: false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    signature: data.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'
                    ]
                });
                resolve()
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                reject("jsonp.error:" + textStatus)
            }
        });
    })
}

function strToJSON(search) {
    let args = search.substr(1).split('&')
    let obj = {}
    args.forEach(arg => {
        let index = arg.indexOf('=')
        if (!~index) {
            obj[arg] = null
        } else {
            obj[arg.substr(0, index)] = arg.substr(index + 1)
        }
    })

    return obj
}

function jsonToStr(data) {
    let strs = []
    Object.keys(data).forEach(k => {
        if (data[k] !== null) {
            strs.push(`${k}=${data[k]}`)
        }
    })
    return '?' + strs.join('&')
}


function setup({ title, link, imgUrl, desc }) {
    let defaultFn = () => {}
    let defaultChar = ''
    let search = link.substr(link.indexOf('?'))
    let path = link.substr(0, link.indexOf('?'))
    let data = strToJSON(search)



    wx.ready(function() {
        // 微信朋友圈
        data.open = 'wx_friends'
        wx.onMenuShareTimeline({ title, link: path + jsonToStr(data), imgUrl, desc });
        // 微信好友
        data.open = 'wx_friend'
        wx.onMenuShareAppMessage({ title, desc, link: path + jsonToStr(data), imgUrl, type: defaultChar, dataUrl: defaultChar })
        // 微博
        data.open = 'weibo'
        wx.onMenuShareWeibo({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
        // 空间
        data.open = 'qq_zone'
        wx.onMenuShareQZone({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
        // QQ好友
        data.open = 'qq'
        wx.onMenuShareQQ({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
        wx.error(function(e) {})
    })
}

export default async (args) => {
    if (typeof wx != "undefined") { //需要引入 https://res.wx.qq.com/open/js/jweixin-1.2.0.js
        await toConfigure()
        setup(args)
    }
}