login.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Ext.QuickTips.init();
  2. Ext.define('erp.controller.login', {
  3. extend : 'Ext.app.Controller',
  4. views : [ 'login.Login' ],
  5. init : function() {
  6. var me = this;
  7. this.control({
  8. 'loginView':{
  9. afterrender:function(){
  10. var username = me.getCookie('username');
  11. var password = me.getCookie('password');
  12. if(username){
  13. Ext.getCmp('username').setValue(username);
  14. }
  15. if(password){
  16. Ext.getCmp('password').setValue(password);
  17. }
  18. if(!username){
  19. Ext.getCmp('username').focus();
  20. }
  21. }
  22. },
  23. 'loginView #loginBtn' : {
  24. click : function(btn) {
  25. var form = btn.ownerCt.ownerCt;
  26. var values = form.getForm().getValues();
  27. if(!values.name||!values.password){
  28. Ext.Msg.alert('提示','请输入用户名和密码!');
  29. }else{
  30. Ext.Ajax.request({
  31. url : basePath + 'user/login',
  32. method : 'POST',
  33. params : values,
  34. callback : function(options, success, response) {
  35. var res = Ext.decode(response.responseText);
  36. if (res.success == false) {
  37. Ext.getCmp('errortip').show();
  38. } else {
  39. //设置cookie
  40. me.setCookie('username',values.name,'/',14);
  41. me.setCookie('password',values.password,'/',14);
  42. /**
  43. * 获取链接参数
  44. *
  45. * @param key
  46. * 参数的键
  47. * @returns 参数的值
  48. */
  49. var getParameter= function(key) {
  50. var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
  51. var r = window.location.search.substr(1).match(reg);
  52. if (r != null)
  53. return decodeURI(r[2]);
  54. return null;
  55. };
  56. var returnUrl=getParameter('returnUrl');
  57. if(returnUrl && decodeURIComponent(returnUrl) != window.location.href){
  58. // 回到登录前的页面
  59. window.location.href = decodeURIComponent(returnUrl);
  60. }else{
  61. window.location.reload();
  62. }
  63. }
  64. }
  65. });
  66. }
  67. }
  68. },
  69. 'loginView #username' : {
  70. focus:function(field){
  71. me.hideErrorTip();
  72. }
  73. },
  74. 'loginView #password' : {
  75. focus:function(field){
  76. me.hideErrorTip();
  77. }
  78. }
  79. });
  80. },
  81. hideErrorTip:function(){
  82. var tip = Ext.getCmp('errortip');
  83. if(!tip.hidden){
  84. tip.hide();
  85. }
  86. },
  87. setCookie:function(name,value,path,outDay){
  88. var date = new Date();
  89. date.setDate(date.getDate()+outDay);
  90. date.setTime(date.getTime() + (outDay*24*60*60*1000));
  91. var expires = "; expires=" + date.toUTCString();
  92. var path = path?';path='+path:'';
  93. document.cookie = name + '=' + value + ';expires=' + expires + path;
  94. },
  95. getCookie:function(name){
  96. var cookies = document.cookie.split('; ');
  97. var res = null;
  98. Ext.Array.each(cookies,function(cookie){
  99. var values = cookie.split('=');
  100. var cookieName = values[0];
  101. if(cookieName&&name){
  102. if(cookieName==name){
  103. res = values[1];
  104. return false;
  105. }
  106. }
  107. });
  108. return res;
  109. }
  110. });