login.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. const url = response.last_page !== null && response.last_page !== '' ?
  23. response.last_page : '/pm';
  24. if (response.login_ask === 0) {
  25. location.href = url;
  26. } else {
  27. response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
  28. null : JSON.parse(response.compilation_list);
  29. if (response.compilation_list === null || response.compilation_list.length <= 0) {
  30. location.href = url;
  31. return false;
  32. }
  33. setVersion(response.compilation_list);
  34. $('#ver').modal('show');
  35. }
  36. } else {
  37. let msg = response.msg !== undefined ? response.msg : '未知错误';
  38. showError(msg, $("input"));
  39. }
  40. },
  41. error: function (result) {
  42. showError('内部程序错误', null);
  43. }
  44. });
  45. });
  46. $("input").blur(function () {
  47. cleanError();
  48. });
  49. $(".form-control").on('input', function () {
  50. $('#hint').html('&nbsp;');
  51. });
  52. });
  53. /**
  54. * 验证数据
  55. *
  56. * @return {boolean}
  57. */
  58. function valid() {
  59. let result = true;
  60. let account = $("#inputEmail").val();
  61. if (account === undefined || account === '') {
  62. showError('用户名不能为空!', $("#inputEmail"));
  63. return false;
  64. }
  65. let password = $("#inputPassword").val();
  66. if (password === undefined || password === '') {
  67. showError('密码不能为空!', $("#inputPassword"));
  68. return false;
  69. }
  70. return result;
  71. }
  72. /**
  73. * 提示错误
  74. *
  75. * @param {string} msg
  76. * @param {object} element
  77. * @return {void}
  78. */
  79. function showError(msg, element) {
  80. if (element !== null) {
  81. element.parent().addClass('has-danger');
  82. }
  83. $("#message").text(msg);
  84. $("#error-tips").show("fast");
  85. }
  86. /**
  87. * 清除错误提示
  88. *
  89. * @return {void}
  90. */
  91. function cleanError() {
  92. $("input").parent().removeClass('has-danger');
  93. $("#message").text('');
  94. $("#error-tips").hide("fast");
  95. }
  96. /**
  97. * 设置版本信息
  98. *
  99. * @param {Object} versionData
  100. * @return {void}
  101. */
  102. function setVersion(versionData) {
  103. let html = '';
  104. for (let version of versionData) {
  105. let tmpHtml = '<div class="col-sm-6">' +
  106. '<div class="card card-block">' +
  107. '<h3 class="card-title">'+ version.name +'</h3>' +
  108. '<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>' +
  109. '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
  110. '</div>' +
  111. '</div>';
  112. html += tmpHtml;
  113. }
  114. $("#version-area").html(html);
  115. }