<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="../favicon.ico">
    <title>ibzuaa</title>

    <script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
    <script>

        /**
         * 初始化
         */
        $(function () {
            var code = getUrlParam('code');
            var state = getUrlParam('state');
            if (code && state) {
                // 通过授权code请求后台
                $.ajax({
                    type: "get",
                    url: "../uaa/open/wechat/sns/" + code + "?state=" + state,
                    dataType: "json",
                    contentType: 'application/json',
                    success: function (data) {
                        if (data) {
                            if (data.openid) {
                                localStorage.setItem('openid', data.openid);
                            }
                            if (data.access_token) {
                                localStorage.setItem('access_token',data.access_token);
                            }
                            if (data.refresh_token) {
                                localStorage.setItem('refresh_token',data.refresh_token);
                            }
                            if (data.unionid) {
                                localStorage.setItem('unionid',data.unionid);
                            }
                            if (data.token) {
                                localStorage.setItem('token', data.token);
                            }
                            if (data.user) {
                                var user = data.user;
                                localStorage.setItem('user', JSON.stringify(user));
                                // 设置cookie,保存账号7天
                                setCookie("loginname", user.loginname, 7);
                                // 跳转首页
                                window.location.href = "../index";
                            } else {
                                // 跳转微信绑定
                                window.location.href = "../#/weixinLoginRedirect?code=" + code + "&state=" + state;
                            }
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
                        // 回到登录页
                        window.location.href = "../";
                    }
                });
            } else {
                // 回到登录页
                window.location.href = "../";
            }

        });

        /**
         * 回去url中的参数
         * @param name
         * @returns {*}
         */
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg); //匹配目标参数
            if (r != null) return unescape(r[2]);
            return null; //返回参数值
        }

        /**
         * 设置Cookie
         * @param name
         * @param value
         * @param day
         */
        function setCookie(name, value, day) {
            if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
                var curDate = new Date();
                var curTamp = curDate.getTime();
                var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
                var passedTamp = curTamp - curWeeHours;
                var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
                var leftTime = new Date();
                leftTime.setTime(leftTamp + curTamp);
                document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toUTCString() + ";path=/";
            } else {
                document.cookie = name + "=" + escape(value);
            }
        }

    </script>

</head>
<body>

</body>
</html>