login.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. $(function() {
  2. validateKickout();
  3. validateRule();
  4. $('.imgcode').click(function() {
  5. var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
  6. $(".imgcode").attr("src", url);
  7. });
  8. });
  9. function login() {
  10. var username = $.common.trim($("input[name='username']").val());
  11. var password = $.common.trim($("input[name='password']").val());
  12. var validateCode = $("input[name='validateCode']").val();
  13. var rememberMe = $("input[name='rememberme']").is(':checked');
  14. if($.common.isEmpty(validateCode) && captchaEnabled) {
  15. $.modal.msg("请输入验证码");
  16. return false;
  17. }
  18. $.ajax({
  19. type: "post",
  20. url: ctx + "login",
  21. data: {
  22. "username": username,
  23. "password": password,
  24. "validateCode": validateCode,
  25. "rememberMe": rememberMe
  26. },
  27. beforeSend: function () {
  28. $.modal.loading($("#btnSubmit").data("loading"));
  29. },
  30. success: function(r) {
  31. if (r.code == web_status.SUCCESS) {
  32. location.href = ctx + 'index';
  33. } else {
  34. $('.imgcode').click();
  35. $(".code").val("");
  36. $.modal.msg(r.msg);
  37. }
  38. $.modal.closeLoading();
  39. }
  40. });
  41. }
  42. function validateRule() {
  43. var icon = "<i class='fa fa-times-circle'></i> ";
  44. $("#signupForm").validate({
  45. rules: {
  46. username: {
  47. required: true
  48. },
  49. password: {
  50. required: true
  51. }
  52. },
  53. messages: {
  54. username: {
  55. required: icon + "请输入您的用户名",
  56. },
  57. password: {
  58. required: icon + "请输入您的密码",
  59. }
  60. },
  61. submitHandler: function(form) {
  62. login();
  63. }
  64. })
  65. }
  66. function validateKickout() {
  67. if (getParam("kickout") == 1) {
  68. layer.alert("<font color='red'>您已在别处登录,请您修改密码或重新登录</font>", {
  69. icon: 0,
  70. title: "系统提示"
  71. },
  72. function(index) {
  73. //关闭弹窗
  74. layer.close(index);
  75. if (top != self) {
  76. top.location = self.location;
  77. } else {
  78. var url = location.search;
  79. if (url) {
  80. var oldUrl = window.location.href;
  81. var newUrl = oldUrl.substring(0, oldUrl.indexOf('?'));
  82. self.location = newUrl;
  83. }
  84. }
  85. });
  86. }
  87. }
  88. function getParam(paramName) {
  89. var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
  90. var r = window.location.search.substr(1).match(reg);
  91. if (r != null) return decodeURI(r[2]);
  92. return null;
  93. }