login.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. // 载入时先获取相关参数
  11. $.ajax({
  12. url: '/captcha',
  13. type: 'get',
  14. data: '',
  15. timeout: 5000,
  16. error: function() {
  17. $("#captcha-box").html('验证码加载失败');
  18. },
  19. beforeSend: function() {
  20. $("#captcha-box").html('正在加载验证码');
  21. },
  22. success: function(response) {
  23. $("#captcha-box").html('');
  24. if (response.success === 0) {
  25. alert('验证码初始化失败!');
  26. return false;
  27. }
  28. initGeetest({
  29. // 以下配置参数来自服务端 SDK
  30. gt: response.gt,
  31. challenge: response.challenge,
  32. offline: !response.success,
  33. new_captcha: response.new_captcha,
  34. width: '100%'
  35. }, handler);
  36. }
  37. });
  38. const handler = function(captchaObj) {
  39. captchaObj.appendTo('#captcha-box');
  40. captchaObj.onSuccess(function () {
  41. $(".btn-area").slideDown("fast");
  42. $('#login').click();
  43. captchaObj.getValidate();
  44. });
  45. $("#login").click(function () {
  46. if (!valid()) {
  47. return false;
  48. }
  49. let account = $("#inputEmail").val();
  50. let pw = $("#inputPassword").val();
  51. // 判断输入的邮箱/手机是否格式正确
  52. if(/^1[3456789]\d{9}$/.test(account) || /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(account)) {
  53. // 先判断是否是专业版用户,是的话弹出短信验证
  54. $.ajax({
  55. url: '/accountIsPro',
  56. type: 'post',
  57. async: true,
  58. data: {"account": account, "pw": pw},
  59. success: function (response) {
  60. if (response.error === 0) {
  61. const ispro = response.result;
  62. if (!ispro) {
  63. login(captchaObj);
  64. } else {
  65. $('#phonepass').modal('show');
  66. $('#proMobile').val(response.data);
  67. }
  68. } else {
  69. let msg = response.msg !== undefined ? response.msg : '未知错误';
  70. showError(msg, $("input"));
  71. }
  72. }
  73. });
  74. } else {
  75. $('#emailHelp').text('您输入的 邮箱/手机 格式不对');
  76. }
  77. });
  78. $('#loginPro').click(function () {
  79. if ($('#smsCode').val() === '') {
  80. showValidError('请输入验证码',$('#smsCode'));
  81. } else {
  82. login(captchaObj);
  83. }
  84. });
  85. $('#check-code').click(function () {
  86. const mobile = $("#mobile").val();
  87. const ssoId = $("#check_ssoId").val();
  88. const code = $("#code").val();
  89. if(!validMobile(mobile)) {
  90. return false;
  91. }
  92. if(ssoId === undefined || ssoId === '') {
  93. showValidError('账号有误!', $('#code'));
  94. return false;
  95. }
  96. if($.trim(code) === '') {
  97. showValidError('验证码不能为空!', $('#code'));
  98. return false;
  99. }
  100. $.ajax({
  101. url: '/sms/mobile',
  102. type: 'post',
  103. data: {ssoId: ssoId, mobile: mobile, code: code},
  104. error: function() {
  105. showValidError('接口出错!',$('#code'));
  106. },
  107. beforeSend: function() {
  108. },
  109. success: function(response) {
  110. if (response.err === 0) {
  111. $("#login").click();
  112. $('#phone').modal('hide');
  113. } else {
  114. showValidError(response.msg,$('#code'));
  115. }
  116. }
  117. })
  118. });
  119. };
  120. $("input").blur(function () {
  121. cleanError();
  122. cleanValidError($(this));
  123. });
  124. $(".form-control").on('input', function () {
  125. $('#hint').html(' ');
  126. });
  127. $("#get-code").click(function() {
  128. const mobile = $("#mobile").val();
  129. if(!validMobile(mobile)){
  130. return false;
  131. }
  132. const btn = $(this);
  133. if(!btn.hasClass('disabled')){
  134. $.ajax({
  135. url: '/sms/code',
  136. type: 'post',
  137. data: { mobile: mobile, type: 1},
  138. error: function() {
  139. showValidError('短信接口出错!',$('#mobile'));
  140. },
  141. beforeSend: function() {
  142. },
  143. success: function(response) {
  144. if (response.err === 0) {
  145. codeSuccess(btn);
  146. } else {
  147. showValidError(response.msg,$('#mobile'));
  148. }
  149. }
  150. });
  151. }
  152. });
  153. $("#get-code2").click(function() {
  154. const mobile = $("#proMobile").val();
  155. if(!validMobile(mobile)){
  156. return false;
  157. }
  158. const btn = $(this);
  159. if(!btn.hasClass('disabled')){
  160. $.ajax({
  161. url: '/sms/code',
  162. type: 'post',
  163. data: { mobile: mobile, type: 3},
  164. error: function() {
  165. showValidError('短信接口出错!',$('#smsCode'));
  166. },
  167. beforeSend: function() {
  168. },
  169. success: function(response) {
  170. if (response.err === 0) {
  171. codeSuccess(btn);
  172. } else {
  173. showValidError(response.msg,$('#smsCode'));
  174. }
  175. }
  176. });
  177. }
  178. });
  179. });
  180. function login(captchaObj) {
  181. let account = $("#inputEmail").val();
  182. let pw = $("#inputPassword").val();
  183. let geetest_challenge = $('input[name="geetest_challenge"]').val();
  184. let geetest_validate = $('input[name="geetest_validate"]').val();
  185. let geetest_seccode = $('input[name="geetest_seccode"]').val();
  186. let code = $("#smsCode").val();
  187. $.ajax({
  188. url: '/login',
  189. type: 'post',
  190. data: {
  191. "account": account,
  192. "pw": pw,
  193. "geetest_challenge": geetest_challenge,
  194. "geetest_validate": geetest_validate,
  195. "geetest_seccode": geetest_seccode,
  196. "code": code,
  197. },
  198. success: function (response) {
  199. if (response.error === 0) {
  200. $('#phonepass').modal('hide');
  201. const url = response.last_page !== null && response.last_page !== undefined && response.last_page !== '' ?
  202. response.last_page : '/pm';
  203. if (response.login_ask === 0) {
  204. location.href = url;
  205. } else {
  206. response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
  207. null : JSON.parse(response.compilation_list);
  208. if (response.compilation_list === null || response.compilation_list.length <= 0) {
  209. location.href = url;
  210. return false;
  211. }
  212. console.log(response.compilation_list);
  213. setVersion(response.compilation_list);
  214. $('#ver').modal('show');
  215. }
  216. } else if(response.error === 2) {
  217. $('#phonepass').modal('hide');
  218. captchaObj.reset();
  219. $('#check_ssoId').val(response.ssoId);
  220. $('#phone').modal('show');
  221. } else if(response.error === 3) {
  222. showValidError(response.msg,$('#smsCode'));
  223. } else {
  224. $('#phonepass').modal('hide');
  225. let msg = response.msg !== undefined ? response.msg : '未知错误';
  226. showError(msg, $("input"));
  227. captchaObj.reset();
  228. }
  229. },
  230. error: function (result) {
  231. showError('内部程序错误', null);
  232. }
  233. });
  234. }
  235. /**
  236. * 获取成功后的操作
  237. *
  238. * @param {Object} btn - 点击的按钮
  239. * @return {void}
  240. */
  241. function codeSuccess(btn) {
  242. let counter = 60;
  243. btn.removeClass('btn-primary').addClass('btn-outline-secondary disabled').text(counter + '秒 重新获取');
  244. btn.parents().siblings('div').children('input').removeAttr('readonly');
  245. const countDown = setInterval(function() {
  246. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + '秒 ';
  247. // 倒数结束后
  248. if (countString === '') {
  249. clearInterval(countDown);
  250. btn.removeClass('btn-outline-secondary disabled').addClass('btn-primary').text('获取验证码');
  251. }
  252. const text = countString + '重新获取';
  253. btn.text(text);
  254. counter -= 1;
  255. }, 1000);
  256. }
  257. /**
  258. * 验证手机号是否正确
  259. *
  260. * @return {boolean}
  261. */
  262. function validMobile(mobile) {
  263. let result = true;
  264. if($.trim(mobile) === ''){
  265. showValidError('手机号不能为空!',$('#mobile'));
  266. return false;
  267. }
  268. let mobileValid = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
  269. if(!mobileValid.test(mobile)){
  270. showValidError('手机号码格式有误!',$('#mobile'));
  271. return false;
  272. }
  273. return result;
  274. }
  275. /**
  276. * 提示验证信息错误
  277. *
  278. * @param {string} msg
  279. * @param {object} element
  280. * @return {void}
  281. */
  282. function showValidError(msg, element) {
  283. if (element !== null) {
  284. element.addClass('is-invalid');
  285. element.siblings().text(msg);
  286. }
  287. }
  288. /**
  289. * 清除验证信息错误提示
  290. *
  291. * @return {void}
  292. */
  293. function cleanValidError(element) {
  294. element.removeClass('is-invalid');
  295. element.siblings().text('');
  296. }
  297. /**
  298. * 验证数据
  299. *
  300. * @return {boolean}
  301. */
  302. function valid() {
  303. let result = true;
  304. let account = $("#inputEmail").val();
  305. if (account === undefined || account === '') {
  306. showError('用户名不能为空!', $("#inputEmail"));
  307. return false;
  308. }
  309. let password = $("#inputPassword").val();
  310. if (password === undefined || password === '') {
  311. showError('密码不能为空!', $("#inputPassword"));
  312. return false;
  313. }
  314. return result;
  315. }
  316. /**
  317. * 提示错误
  318. *
  319. * @param {string} msg
  320. * @param {object} element
  321. * @return {void}
  322. */
  323. function showError(msg, element) {
  324. if (element !== null) {
  325. element.parent().addClass('has-danger');
  326. }
  327. $("#message").html(msg);
  328. $("#error-tips").show("fast");
  329. }
  330. /**
  331. * 清除错误提示
  332. *
  333. * @return {void}
  334. */
  335. function cleanError() {
  336. $("input").parent().removeClass('has-danger');
  337. $("#message").text('');
  338. $("#error-tips").hide("fast");
  339. }
  340. /**
  341. * 设置版本信息
  342. *
  343. * @param {Object} versionData
  344. * @return {void}
  345. */
  346. function setVersion(versionData) {
  347. let html = '';
  348. for (let version of versionData) {
  349. let description = version.description ? version.description : '介绍内容';
  350. let tmpHtml = '<div class="col-sm-6">' +
  351. '<div class="card card-block">' +
  352. '<div class="card-body">' +
  353. '<h3 class="card-title">'+ version.name +'</h3>' +
  354. '<p class="card-text">' + description + '</p>' +
  355. '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
  356. '</div>' +
  357. '</div>' +
  358. '</div>';
  359. html += tmpHtml;
  360. }
  361. $("#version-area").html(html);
  362. }