login.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * 登录相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/8
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. let referer = scUrlUtil.GetQueryString('referer');
  10. $("#login").click(function () {
  11. if (!valid()) {
  12. return false;
  13. }
  14. let account = $("#inputEmail").val();
  15. let pw = $("#inputPassword").val();
  16. $.ajax({
  17. url: '/login',
  18. type: 'post',
  19. data: {"account": account, "pw": pw},
  20. success: function (response) {
  21. if (response.error === 0) {
  22. if (response.login_ask === 0) {
  23. location.href = '/pm';
  24. } else {
  25. $('#ver').modal('show');
  26. }
  27. // if (referer) {
  28. // location.href = referer;
  29. // } else {
  30. // location.href = '/';
  31. // }
  32. } else {
  33. let msg = response.msg !== undefined ? response.msg : '未知错误';
  34. showError(msg, $("input"));
  35. }
  36. },
  37. error: function (result) {
  38. showError('内部程序错误', null);
  39. }
  40. });
  41. });
  42. $("input").blur(function () {
  43. cleanError();
  44. });
  45. $(".form-control").on('input', function () {
  46. $('#hint').html(' ');
  47. });
  48. });
  49. /**
  50. * 验证数据
  51. *
  52. * @return {boolean}
  53. */
  54. function valid() {
  55. let result = true;
  56. let account = $("#inputEmail").val();
  57. if (account === undefined || account === '') {
  58. showError('用户名不能为空!', $("#inputEmail"));
  59. return false;
  60. }
  61. let password = $("#inputPassword").val();
  62. if (password === undefined || password === '') {
  63. showError('密码不能为空!', $("#inputPassword"));
  64. return false;
  65. }
  66. return result;
  67. }
  68. /**
  69. * 提示错误
  70. *
  71. * @param {string} msg
  72. * @param {object} element
  73. * @return {void}
  74. */
  75. function showError(msg, element) {
  76. if (element !== null) {
  77. element.parent().addClass('has-danger');
  78. }
  79. $("#message").text(msg);
  80. $("#error-tips").show("fast");
  81. }
  82. /**
  83. * 清除错误提示
  84. *
  85. * @return {void}
  86. */
  87. function cleanError() {
  88. $("input").parent().removeClass('has-danger');
  89. $("#message").text('');
  90. $("#error-tips").hide("fast");
  91. }