function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
    if (r != null) return unescape(r[2]); return null;
}
(function ($,learun) {
    if (GetQueryString("code") != undefined && GetQueryString("code") != "") {
        $.ajax({
            url: config.webapi + "weixinapi/getweixinaccess_token?code=" + GetQueryString("code"),
            type: "post",
            data: {
                token: '',
                loginMark: learun.deviceId()
            },
            success: function (res) {
                $("#waiting").hide();
                if (res.code == 200) {
                    if (!!res.data.logined) {
                        var logininfo = {
                            account: account,
                            token: res.data.baseinfo.token,
                            date: learun.date.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
                        };
                        learun.storage.set('logininfo', logininfo);
                        learun.storage.set('userinfo', res.data);
                        //learun.tab.go('workspace');
                        location.href ="http://"+ window.location.host;
                    } else {
                        $(".login-content").show();
                        $("#accountopenid").val(res.data.openid);
                        $("#loginBtn").on("click", function () {
                            var account = $('#account').val();
                            var password = $('#password').val();
                            if (account === "") {
                                learun.layer.warning('用户名不能为空！', function () { }, '提示', '关闭');
                            } else if (password === "") {
                                learun.layer.warning('密码不能为空！', function () { }, '提示', '关闭');
                            } else {
                                var data = {
                                    username: account,
                                    password: $.md5(password),
                                    openid: $("#accountopenid").val()
                                };
                                var postdata = {
                                    token: '',
                                    loginMark: learun.deviceId(),
                                    data: JSON.stringify(data)
                                };

                                learun.layer.loading(true, "正在登录，请稍后");
                                learun.http.post(config.webapi  + "weixinapi/login", postdata, (res) => {
                                    learun.layer.loading(false);
                                    if (res === null) {
                                        learun.layer.warning('无法连接服务器,请检测网络！', function () { }, '提示', '关闭');
                                        return;
                                    }
                                    if (res.code === 200) {
                                        console.log(res);
                                        var logininfo = {
                                            account: account,
                                            token: res.data.baseinfo.token,
                                            date: learun.date.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
                                        };
                                        learun.storage.set('logininfo', logininfo);
                                        learun.storage.set('userinfo', res.data);
                                        $('#account').val('');
                                        $('#password').val('');
                                        location.href = "http://" + window.location.host;
                                    } else {
                                        learun.layer.warning(res.info, function () { }, '提示', '关闭');
                                    }
                                });
                            }
                        });
                    }
                } else {
                    console.log(res.info);
                    learun.layer.warning(res.info);
                    location.href = "http://" + window.location.host;
                }
            }
        });
    } else {
        $(".login-content").hide();
        $.ajax({
            url: config.webapi + "weixinapi/weixinconfig",
            type: "get",
            success: function (res) {
                location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + res.data.appid + "&redirect_uri=" + encodeURIComponent('http://' + window.location.host + '/weixinlogin.html') + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
            }
        });
    }
})(window.jQuery, window.lrmui);