login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 !== undefined && 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 if(response.error === 2) {
  37. $('#check_ssoId').val(response.ssoId);
  38. $('#phone').modal('show');
  39. } else {
  40. let msg = response.msg !== undefined ? response.msg : '未知错误';
  41. showError(msg, $("input"));
  42. }
  43. },
  44. error: function (result) {
  45. showError('内部程序错误', null);
  46. }
  47. });
  48. });
  49. $("input").blur(function () {
  50. cleanError();
  51. cleanValidError($(this));
  52. });
  53. $(".form-control").on('input', function () {
  54. $('#hint').html('&nbsp;');
  55. });
  56. $("#get-code").click(function() {
  57. const mobile = $("#mobile").val();
  58. if(!validMobile(mobile)){
  59. return false;
  60. }
  61. const btn = $(this);
  62. if(!btn.hasClass('disabled')){
  63. $.ajax({
  64. url: '/sms/code',
  65. type: 'post',
  66. data: { mobile: mobile, type: 1},
  67. error: function() {
  68. showValidError('短信接口出错!',$('#mobile'));
  69. },
  70. beforeSend: function() {
  71. },
  72. success: function(response) {
  73. if (response.err === 0) {
  74. codeSuccess(btn);
  75. } else {
  76. showValidError(response.msg,$('#mobile'));
  77. }
  78. }
  79. });
  80. }
  81. });
  82. $('#check-code').click(function () {
  83. const mobile = $("#mobile").val();
  84. const ssoId = $("#check_ssoId").val();
  85. const code = $("#code").val();
  86. if(!validMobile(mobile)) {
  87. return false;
  88. }
  89. if(ssoId === undefined || ssoId === '') {
  90. showValidError('账号有误!', $('#code'));
  91. return false;
  92. }
  93. if($.trim(code) === '') {
  94. showValidError('验证码不能为空!', $('#code'));
  95. return false;
  96. }
  97. $.ajax({
  98. url: '/sms/mobile',
  99. type: 'post',
  100. data: {ssoId: ssoId, mobile: mobile, code: code},
  101. error: function() {
  102. showValidError('接口出错!',$('#code'));
  103. },
  104. beforeSend: function() {
  105. },
  106. success: function(response) {
  107. if (response.err === 0) {
  108. $("#login").click();
  109. $('#phone').modal('hide');
  110. } else {
  111. showValidError(response.msg,$('#code'));
  112. }
  113. }
  114. })
  115. });
  116. });
  117. /**
  118. * 获取成功后的操作
  119. *
  120. * @param {Object} btn - 点击的按钮
  121. * @return {void}
  122. */
  123. function codeSuccess(btn) {
  124. let counter = 60;
  125. btn.removeClass('btn-primary').addClass('btn-outline-secondary disabled').text(counter + '秒 重新获取');
  126. btn.parents().siblings('div').children('input').removeAttr('readonly');
  127. const countDown = setInterval(function() {
  128. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + '秒 ';
  129. // 倒数结束后
  130. if (countString === '') {
  131. clearInterval(countDown);
  132. btn.removeClass('btn-outline-secondary disabled').addClass('btn-primary').text('获取验证码');
  133. }
  134. const text = countString + '重新获取';
  135. btn.text(text);
  136. counter -= 1;
  137. }, 1000);
  138. }
  139. /**
  140. * 验证手机号是否正确
  141. *
  142. * @return {boolean}
  143. */
  144. function validMobile(mobile) {
  145. let result = true;
  146. if($.trim(mobile) === ''){
  147. showValidError('手机号不能为空!',$('#mobile'));
  148. return false;
  149. }
  150. let mobileValid = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
  151. if(!mobileValid.test(mobile)){
  152. showValidError('手机号码格式有误!',$('#mobile'));
  153. return false;
  154. }
  155. return result;
  156. }
  157. /**
  158. * 提示验证信息错误
  159. *
  160. * @param {string} msg
  161. * @param {object} element
  162. * @return {void}
  163. */
  164. function showValidError(msg, element) {
  165. if (element !== null) {
  166. element.addClass('is-invalid');
  167. element.siblings().text(msg);
  168. }
  169. }
  170. /**
  171. * 清除验证信息错误提示
  172. *
  173. * @return {void}
  174. */
  175. function cleanValidError(element) {
  176. element.removeClass('is-invalid');
  177. element.siblings().text('');
  178. }
  179. /**
  180. * 验证数据
  181. *
  182. * @return {boolean}
  183. */
  184. function valid() {
  185. let result = true;
  186. let account = $("#inputEmail").val();
  187. if (account === undefined || account === '') {
  188. showError('用户名不能为空!', $("#inputEmail"));
  189. return false;
  190. }
  191. let password = $("#inputPassword").val();
  192. if (password === undefined || password === '') {
  193. showError('密码不能为空!', $("#inputPassword"));
  194. return false;
  195. }
  196. return result;
  197. }
  198. /**
  199. * 提示错误
  200. *
  201. * @param {string} msg
  202. * @param {object} element
  203. * @return {void}
  204. */
  205. function showError(msg, element) {
  206. if (element !== null) {
  207. element.parent().addClass('has-danger');
  208. }
  209. $("#message").text(msg);
  210. $("#error-tips").show("fast");
  211. }
  212. /**
  213. * 清除错误提示
  214. *
  215. * @return {void}
  216. */
  217. function cleanError() {
  218. $("input").parent().removeClass('has-danger');
  219. $("#message").text('');
  220. $("#error-tips").hide("fast");
  221. }
  222. /**
  223. * 设置版本信息
  224. *
  225. * @param {Object} versionData
  226. * @return {void}
  227. */
  228. function setVersion(versionData) {
  229. let html = '';
  230. for (let version of versionData) {
  231. let tmpHtml = '<div class="col-sm-6">' +
  232. '<div class="card card-block">' +
  233. '<h3 class="card-title">'+ version.name +'</h3>' +
  234. '<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>' +
  235. '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
  236. '</div>' +
  237. '</div>';
  238. html += tmpHtml;
  239. }
  240. $("#version-area").html(html);
  241. }