login_startup.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * 登录相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/8
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. $("input").blur(function () {
  10. cleanError();
  11. cleanValidError($(this));
  12. });
  13. $("#get-code").click(function() {
  14. const mobile = $("#proMobile").val();
  15. const btn = $(this);
  16. if(!btn.hasClass('disabled')){
  17. $.ajax({
  18. url: '/sms/code',
  19. type: 'post',
  20. data: { mobile: mobile, type: 3},
  21. error: function() {
  22. showValidError('短信接口出错!',$('#smsCode'));
  23. },
  24. beforeSend: function() {
  25. },
  26. success: function(response) {
  27. if (response.err === 0) {
  28. codeSuccess(btn);
  29. } else {
  30. showValidError(response.msg,$('#smsCode'));
  31. }
  32. }
  33. });
  34. }
  35. });
  36. });
  37. function login() {
  38. let ssoID = $("#ssoID").val();
  39. let token = $("#token").val();
  40. let code = $("#smsCode").val();
  41. $.ajax({
  42. url: '/login',
  43. type: 'post',
  44. data: {
  45. "ssoID": ssoID,
  46. "token": token,
  47. "code": code,
  48. },
  49. success: function (response) {
  50. if (response.error === 0) {
  51. $('#phonepass').modal('hide');
  52. const url = response.last_page !== null && response.last_page !== undefined && response.last_page !== '' ?
  53. response.last_page : '/pm';
  54. if (response.login_ask === 0) {
  55. location.href = url;
  56. } else {
  57. response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
  58. null : JSON.parse(response.compilation_list);
  59. if (response.compilation_list === null || response.compilation_list.length <= 0) {
  60. location.href = url;
  61. return false;
  62. }
  63. console.log(response.compilation_list);
  64. setVersion(response.compilation_list);
  65. $('#ver').modal('show');
  66. }
  67. } else if(response.error === 2) {
  68. $('#phonepass').modal('hide');
  69. captchaObj.reset();
  70. $('#check_ssoId').val(response.ssoId);
  71. $('#phone').modal('show');
  72. } else if(response.error === 3) {
  73. showValidError(response.msg,$('#smsCode'));
  74. } else {
  75. $('#phonepass').modal('hide');
  76. let msg = response.msg !== undefined ? response.msg : '未知错误';
  77. showError(msg, $("input"));
  78. captchaObj.reset();
  79. }
  80. },
  81. error: function (result) {
  82. showError('内部程序错误', null);
  83. }
  84. });
  85. }
  86. /**
  87. * 获取成功后的操作
  88. *
  89. * @param {Object} btn - 点击的按钮
  90. * @return {void}
  91. */
  92. function codeSuccess(btn) {
  93. let counter = 60;
  94. btn.removeClass('btn-primary').addClass('btn-outline-secondary disabled').text(counter + '秒 重新获取');
  95. btn.parents().siblings('div').children('input').removeAttr('readonly');
  96. const countDown = setInterval(function() {
  97. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + '秒 ';
  98. // 倒数结束后
  99. if (countString === '') {
  100. clearInterval(countDown);
  101. btn.removeClass('btn-outline-secondary disabled').addClass('btn-primary').text('获取验证码');
  102. }
  103. const text = countString + '重新获取';
  104. btn.text(text);
  105. counter -= 1;
  106. }, 1000);
  107. }
  108. /**
  109. * 提示验证信息错误
  110. *
  111. * @param {string} msg
  112. * @param {object} element
  113. * @return {void}
  114. */
  115. function showValidError(msg, element) {
  116. if (element !== null) {
  117. element.addClass('is-invalid');
  118. element.siblings().text(msg);
  119. }
  120. }
  121. /**
  122. * 清除验证信息错误提示
  123. *
  124. * @return {void}
  125. */
  126. function cleanValidError(element) {
  127. element.removeClass('is-invalid');
  128. element.siblings().text('');
  129. }
  130. /**
  131. * 提示错误
  132. *
  133. * @param {string} msg
  134. * @param {object} element
  135. * @return {void}
  136. */
  137. function showError(msg, element) {
  138. if (element !== null) {
  139. element.parent().addClass('has-danger');
  140. }
  141. $("#message").html(msg);
  142. $("#error-tips").show("fast");
  143. }
  144. /**
  145. * 清除错误提示
  146. *
  147. * @return {void}
  148. */
  149. function cleanError() {
  150. $("input").parent().removeClass('has-danger');
  151. $("#message").text('');
  152. $("#error-tips").hide("fast");
  153. }
  154. /**
  155. * 设置版本信息
  156. *
  157. * @param {Object} versionData
  158. * @return {void}
  159. */
  160. function setVersion(versionData) {
  161. let html = '';
  162. for (let version of versionData) {
  163. let description = version.description ? version.description : '介绍内容';
  164. let tmpHtml = '<div class="col-sm-6">' +
  165. '<div class="card card-block">' +
  166. '<div class="card-body">' +
  167. '<h3 class="card-title">'+ version.name +'</h3>' +
  168. '<p class="card-text">' + description + '</p>' +
  169. '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
  170. '</div>' +
  171. '</div>' +
  172. '</div>';
  173. html += tmpHtml;
  174. }
  175. $("#version-area").html(html);
  176. }