Ext.QuickTips.init(); Ext.define('erp.controller.login', { extend : 'Ext.app.Controller', views : [ 'login.Login' ], init : function() { var me = this; this.control({ 'loginView':{ afterrender:function(){ var username = me.getCookie('username'); var password = me.getCookie('password'); if(username){ Ext.getCmp('username').setValue(username); } if(password){ Ext.getCmp('password').setValue(password); } if(!username){ Ext.getCmp('username').focus(); } } }, 'loginView #loginBtn' : { click : function(btn) { var form = btn.ownerCt.ownerCt; var values = form.getForm().getValues(); var viewport = Ext.getCmp('loginViewport'); var el = viewport.getEl(); if(!values.name||!values.password){ showErrorMsg('提示','请输入用户名和密码!'); }else{ el.mask('登录中...'); Ext.Ajax.request({ url : basePath + 'user/login', method : 'POST', params : values, callback : function(options, success, response) { el.unmask(); var res = Ext.decode(response.responseText); if (res.success == false) { Ext.getCmp('errortip').show(); } else { //设置cookie me.setCookie('username',res.name,'/',14); me.setCookie('password',values.password); me.setCookie('role',res.role,'/',14); /** * 获取链接参数 * * @param key * 参数的键 * @returns 参数的值 */ var getParameter= function(key) { var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }; var returnUrl=getParameter('returnUrl'); if(returnUrl && decodeURIComponent(returnUrl) != window.location.href){ // 回到登录前的页面 window.location.href = decodeURIComponent(returnUrl); }else{ window.location.reload(); } } } }); } } } }); }, setCookie:function(name,value,path,outDay){ var date = new Date(); date.setDate(date.getDate()+outDay); date.setTime(date.getTime() + (outDay*24*60*60*1000)); var expires = "; expires=" + date.toUTCString(); var path = path?';path='+path:''; document.cookie = name + '=' + value + ';expires=' + expires + path; }, getCookie:function(name){ var cookies = document.cookie.split('; '); var res = null; Ext.Array.each(cookies,function(cookie){ var values = cookie.split('='); var cookieName = values[0]; if(cookieName&&name){ if(cookieName==name){ res = values[1]; return false; } } }); return res; } });