var wxShareHref = window.location.href;

// if (/\?from=/ig.test(wxShareHref)) {
//     var noJump = /nojump/ig.test(wxShareHref);
//     if (noJump == false) {
//         window.location.href = wxShareHref.split("?from=")[0];
//     }
// }

var json_wx = {
    "jsonpurl": (function () {
        var loc = wxShareHref.substring(0, wxShareHref.indexOf("#") < 0 ? undefined : wxShareHref.indexOf("#"));
        var ishttps = "https:" == document.location.protocol ? true : false;
        var host = "http://api.hunan.gov.cn:8090";
        if (ishttps) {
            host = "https://api.hunan.gov.cn";
        }
        var str = host + "/trans-api/api/wx/getWxConfig?callback=wx_cb" + "&url=" + encodeURIComponent(loc);
        return str;
    })(),
    "jsonpurl2": function (type) {
        var loc = wxShareHref.substring(0, wxShareHref.indexOf("#") < 0 ? undefined : wxShareHref.indexOf("#"));
        //http://192.168.1.184:8300/ucap/api/znld/visits/saveOrGetVisits?url=http://gxt.hunan.gov.cn/gxt/xxgk_71033/gzdt/rdjj/202005/t20200529_12222779.html&appType=1
        // var str = "http://192.168.1.184:8300/ucap/api/znld/visits/saveOrGetVisits?url=" + loc + "&appType=" + type;
        // var res = loc.replace("//", "/");
        // var res2 = res.replace(":/", "://");
        // console.log(res2);
        var ishttps = "https:" == document.location.protocol ? true : false;
        var host = "http://api.hunan.gov.cn:8090";
        if (ishttps) {
            host = "https://api.hunan.gov.cn";
        }
        var str = host + "/trans-api/api/frraAdmin/saveOrGetVisits?url=" + loc + "&appType=" + type;
        return str;
    },
};

var wxUrls;

function wxShareV2() {
    var tags = [];
    for (var i = 0; i < wxUrls.length; i++) {
        var tag = document.createElement("script");
        tag.src = wxUrls[i];
        tags.push(tag);
    }
    // console.log(tags)
    for (var i = 0; i < tags.length; i++) {
        document.body.appendChild(tags[i]);
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload; //把现在有window.onload事件处理函数的值存入变量oldonload。
    if (typeof window.onload != 'function') { //如果这个处理函数还没有绑定任何函数，就像平时那样把新函数添加给它
        window.onload = func;
    } else { //如果在这个处理函数上已经绑定了一些函数。就把新函数追加到现有指令的末尾
        window.onload = function () {
            console.log(oldonload, func)
            oldonload();
            func();
        }
    }
}

function wx_cb(data) {
    // alert(data.code);
    // alert(data.content.appId);
    // alert(data.content.signature);
    // alert(data.content.nonceStr);
    // alert(data.content.timestamp);
    // alert(data.content.jsApiList);
    if (data.code !== 200) console.log("shibai");
    //console.log(data);
    wx.config({
        debug: false,
        appId: data.content.appId,
        timestamp: data.content.timestamp,
        nonceStr: data.content.nonceStr,
        signature: data.content.signature,
        jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "onMenuShareQZone"]
    });
    wx.ready(function () {

        wx.checkJsApi({
            jsApiList: [
                "getNetworkType",
                "previewImage"
            ],
            success: function (res) {
                //alert(JSON.stringify(res));
                console.log(JSON.stringify(res));
            }
        });
        var shareTit;
        var sharesum;
        var shareUrl = wxShareHref;
        var shareImg = "http://www.hunan.gov.cn/hnszf/xhtml/images/public/WechatShareIcon.png";
        try {
            var titleLayout = document.getElementById("sc_title");
            if (!titleLayout) {
                titleLayout = document.querySelector(".sp_title");
            }
            if (titleLayout) {
                // shareTit = titleLayout.innerHTML.replace(/[\r\n]/g, '').replace(/\s+/g, '');
                shareTit = titleLayout.innerText.trim().replace("<br>", "").replace("&nbsp;", "");
            }
            if (!shareTit) {
                shareTit = document.title;
            }
            var digestLayout = document.getElementById("sc_digest");
            if (digestLayout) {
                sharesum = digestLayout.innerText.trim().replace("<br>", "").replace("&nbsp;", "");
            }
            var ps = document.getElementsByTagName("p");
            if (!sharesum && ps && ps.length > 0) {
                for (var i = 0; i < ps.length; i++) {
                    // sharesum = ps[0].innerText.replace(/[\r\n]/g, '').replace(/\s+/g, '');
                    sharesum = ps[0].innerText.trim().replace("<br>", "").replace("&nbsp;", "");
                    if (sharesum === undefined || sharesum === null || sharesum === "") {
                        break;
                    }
                }
            }
            if (sharesum === undefined || sharesum === null || sharesum === "") {
                sharesum = shareTit;
            }
        } catch (e) {
            shareTit = "";
            sharesum = "";
        }
        wx.onMenuShareAppMessage({
            title: shareTit,
            desc: sharesum,
            link: shareUrl,
            imgUrl: shareImg,
            trigger: function (res) {
                //alert('onMenuShareAppMessage-trigger');
            },
            success: function (res) {
                //alert('onMenuShareAppMessage-success');
            },
            cancel: function (res) {
                //alert('onMenuShareAppMessage-cancel');
            },
            fail: function (res) {
                //alert(JSON.stringify(res));
            }
        });

        wx.onMenuShareTimeline({
            title: shareTit,
            link: shareUrl,
            imgUrl: shareImg,
            trigger: function (res) {
                //alert('onMenuShareTimeline trigger');
            },
            success: function (res) {
                //alert('onMenuShareTimeline-success');
            },
            cancel: function (res) {
                //alert('onMenuShareTimeline-cancel');
            },
            fail: function (res) {
                //alert('onMenuShareTimeline' + JSON.stringify(res));
            }
        });

        wx.onMenuShareQQ({
            title: shareTit,
            desc: sharesum,
            link: shareUrl,
            imgUrl: shareImg,
            trigger: function (res) {
                //alert('onMenuShareQQ');
            },
            complete: function (res) {
                //alert(JSON.stringify(res));
            },
            success: function (res) {
                //alert('onMenuShareQQ');
            },
            cancel: function (res) {
                //alert('onMenuShareQQ');
            },
            fail: function (res) {
                //alert(JSON.stringify(res));
            }
        });

        wx.onMenuShareWeibo({
            title: shareTit,
            desc: sharesum,
            link: shareUrl,
            imgUrl: shareImg,
            trigger: function (res) {
                //alert('onMenuShareWeibo trigger');
            },
            complete: function (res) {
                //alert(JSON.stringify(res));
            },
            success: function (res) {
                //alert('onMenuShareWeibo');
            },
            cancel: function (res) {
                //alert('onMenuShareWeibo');
            },
            fail: function (res) {
                //alert(JSON.stringify(res));
            }
        });

        wx.onMenuShareQZone({
            title: shareTit,
            desc: sharesum,
            link: shareUrl,
            imgUrl: shareImg,
            trigger: function (res) {
                // alert('onMenuShareQZone');
            },
            complete: function (res) {
                //alert(JSON.stringify(res));
            },
            success: function (res) {
                // alert('onMenuShareQZone');
            },
            cancel: function (res) {
                // alert('onMenuShareQZone');
            },
            fail: function (res) {
                // alert(JSON.stringify(res));
            }
        });
    });
    wx.error(function (res) {
        // alert(JSON.stringify(res));
    });

}

function cb(data) {
    // console.log(data);
    var show = false;
    if (!show || data.code !== 200 && data.content.pv < 1) {
        return;
    }
    var count = data.content.pv;
    var html = "<span>浏览次数: <i style='color: #006EAB;' >" + count + "</i></span>";
    $(".time").after(html);
}

(function (isM) {
    var _mob = isM || false;
    var urls = [];
    if (_mob) {
        urls.push(json_wx.jsonpurl);
    }
    //urls.push(json_wx.jsonpurl2(_mob ? 2 : 1));
    // alert(urls.toLocaleString())
    wxUrls = urls;
    addLoadEvent(wxShareV2);
})((function (w) {
    var ua = w.navigator.userAgent.toLowerCase(),
        isMatchMob = /phone|pad|pod|iphone|ipod|ios|ipad|android|mobile|blackberry|iemobile|mqqbrowser|juc|fennec|wosbrowser|browserng|webos|symbian|windows phone|micromessenger/gi.test(ua) ? true : false;
    ;
    return isMatchMob;
})(window));


