login.js 7.4 KB

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