//处理登录页面的相关动作 //weidy@2013-08-01 var path = "";//后台路径 var configData = {}; var browserInfo = null; var processStep = 0; var processInterval = null; var usedTime = 0; var projectName = ""; var pathName = ""; var jq = null; $(document).ready(function() { jq = $; // 先判断浏览器版本 browserInfo = checkBrowser(); pathName = window.document.location.pathname; projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1) + "/"; //处理页面的内容 getConfig(function(){ initDefaultConfig(); //checkIsLogin(function(){ initPage();//获取配置后,检查是否已经登录过,如果没有登录则在初始化页面的内容 //}); }); }); function getConfig(callback){ $.getJSON(projectName + "config.json?v=" + (new Date()).getTime(),function(data){ configData = data; if(configData.isDebug == "true" || configData.isDebug == true){ path = configData.backPath; }else{ path = projectName; if(configData.compatibility) { if (pathName.indexOf("/") > -1) { if(""!=configData.unCorsPath){ path = path.substring(0, path.lastIndexOf("/")) + "/" + configData.unCorsPath + "/"; }else{ path = path.substring(0, path.lastIndexOf("/")) + "/"; } } } } if(callback){ callback(); } }); } function initDefaultConfig(){ if(!configData.backgroundimg ){ configData.backgroundimg = "style/images/base/login/background-classic.png"; } if(!configData.defaultPhoto){ configData.defaultPhoto = "style/images/base/login/userphotoman.png"; } if(!configData.loginUrl){ configData.loginUrl = "framework/loginController/login"; } if(!configData.sessionInfoUrl){ configData.sessionInfoUrl = "framework/loginController/getSessionInfo"; } if(!configData.logoutUrl){ configData.logoutUrl = "framework/loginController/logout"; } } function initPage(){ $(document).attr("title",configData.title + "--监控中心服务"); $("#background img").attr("src", projectName + configData.backgroundimg ); if ($.cookie('useridcookie') != null && $.cookie('useridcookie') != "" && $.cookie('useridcookie') != "null") { $("#userid").val($.cookie('useridcookie')); } //清除锁定窗口的cookie $.cookie("isLockWindow", "false"); $.cookie('AuthorizationToken','',{path:"/"}); //设置用户的头像 if($.cookie('userphotocookie') != null && $.cookie('userphotocookie') != "" && $.cookie('userphotocookie') != "null"){ $("#userPhoto").attr("src",projectName + $.cookie('userphotocookie')); }else{ $("#userPhoto").attr("src",projectName + configData.defaultPhoto); } $("#loginbt").click(function() { if (checkEmpty())// 先检查是否为空 login(); }); // 在密码框上点击了回车键 $("#password").bind("keydown", function(e) { var key = e.which; if (key == 13){// 按下回车键{ if (checkEmpty())// 先检查是否为空 login(); } }); if(configData.unSecret){ $("#secretFlag").show(); $("#secretFlag").html(configData.unSecretText); } } function checkIsLogin(callback){ crosPost(configData.sessionInfoUrl,{},function(result){ //如果在线,那直接提示用户是否要继续 if(result.success){ $webUtil.deptOid = result.obj.deptOid; // 保存用户部门Oid var sessionInfo = result.obj; if (window.confirm('当前浏览器中已经使用[' + sessionInfo.userName + ']登录了,如果您现在要使用别的账户登录,' + sessionInfo.userName + '将会自动注销,您是否要继续?')) { crosPost(configData.logoutUrl,{"userid":sessionInfo.userId},function(result){ window.location.href = window.location.href; },function(xhr,error){ window.location.href = window.location.href; }); } else { if(browserInfo.browser == "IE"){ window.open(",’_parent’,"); window.close(); }else{ try{ window.opener = null; window.open('', '_self'); }catch(e){ window.close(); } } } }else{ if(callback){ callback(); } } },function(xhr,error){ if(callback){ callback(); } }); } function crosPost(url1,data,callback,errorCallback){ var url = ""; if(configData.isDebug == "true" || configData.isDebug == true){ url = configData.adminServicePath + url1; }else{ url = path + url1; } var ajaxOperation ={ type:'post', url:url, data:data, success:function(data){ var result = data; if(callback){ callback(result); } }, error:function(xhr,error){ if(errorCallback){ errorCallback(xhr,error); } } }; if(configData.isDebug){ jQuery.support.cors=true; } jq.ajax(ajaxOperation); } // 检查用户名和密码是否为空 function checkEmpty() { if (($("#userid").val() == "") && ($("#password").val() == "")) { showError("登录账号和密码不能为空"); return false; } else if ($("#userid").val() == "") { showError("登录账号不能为空"); return false; } else if ($("#password").val() == "") { showError("密码不能为空"); return false; } else return true; } // 显示错误 function showError(text) { $("#msg").html( text); hideError(5000); } // 错误显示div隐藏值 function hideError(time) { window.setTimeout(function() { $("#msg").html(""); }, time); } // 登录 function login() { $("#loginbt").attr("disabled", true); var userid = $("#userid").val(); if($("#isForceLogin").attr("checked") == "checked"){ $.cookie('useridcookie',userid,{expires:30}); }else{ $.cookie('useridcookie',null); } //var password = MD5($("#password").val()); //新平台已经支持md5加密了 var password = MD5($("#password").val()); var isForceLogin = $("#isForceLogin").is(":checked"); showLoading(); var loginData = { userId : userid, password : password, forceLogin : isForceLogin, browser:browserInfo.browser, browserversion:browserInfo.browserversion, osversion: browserInfo.osversion }; //因为每个项目,在登录后获取的内容可能会有不一样,所以登录的路径支持配置configData.loginUrl crosPost("loginForAdmin/login",loginData,function(result){ hideLoading(); if(result == undefined || result == null){ showError("登录出现了错误!请查看服务器是否开启"); $("#loginbt").attr("disabled", false); } if (result.success){ $.cookie('AuthorizationToken',result.obj.sessionInfo.token,{path:"/"}); createCookie('AuthorizationToken',result.obj.sessionInfo.token); if(configData.mainDisplay == "open" && browserInfo.browser == 'IE'){ window.open(projectName + "mainAdmin.html",'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,status=no'); window.close(); }else { window.location.href = projectName + "mainAdmin.html"; } }else { showError(result.msg); $("#loginbt").attr("disabled", false); } },function(xhr,error){ hideLoading(); showError(error); }); } function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else{ expires = ""; } document.cookie = name+"="+value+expires+"; path=/"; } function showLoading(){ $(".loadbg").show(); $(".processbar").show(); $(".loading").show(); var preWidth = $(".loading").css("width"); if(preWidth.indexOf("px")>-1){ preWidth = preWidth.replace("px",""); } preWidth = preWidth*1/10; processInterval = window.setInterval(function(){ $("#useTime").html(usedTime + 1); $(".processbar").css("width",preWidth*(processStep+1)); usedTime ++; processStep ++ ; if(processStep == 10){ processStep = 0; } },1000); } function hideLoading(){ $(".loadbg").hide(); $(".processbar").hide(); $(".loading").hide(); processStep = 0; usedTime =0; if(processInterval != null){ window.clearInterval(processInterval); } }