login.js 7.8 KB

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