login.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. //
  29. // initGeetest({
  30. // // 以下配置参数来自服务端 SDK
  31. // gt: response.gt,
  32. // challenge: response.challenge,
  33. // offline: !response.success,
  34. // new_captcha: response.new_captcha,
  35. // width: '100%'
  36. // }, handler);
  37. // }
  38. // });
  39. // const handler = function(captchaObj) {
  40. // captchaObj.appendTo('#captcha-box');
  41. // captchaObj.onSuccess(function () {
  42. // $(".btn-area").slideDown("fast");
  43. // // $('#login').click();
  44. // // captchaObj.getValidate();
  45. // });
  46. $("#login").click(function () {
  47. if (!valid()) {
  48. return false;
  49. }
  50. if ($('#changeLogin').attr('data-status') === 'user') {
  51. let account = $("#inputEmail").val();
  52. if(/^1[3456789]\d{9}$/.test(account) || /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(account)) {
  53. login();
  54. } else {
  55. $('#emailHelp').text('您输入的 邮箱/手机 格式不对');
  56. }
  57. } else {
  58. let account = $("#mobileLogin").val();
  59. if(/^1[3456789]\d{9}$/.test(account)) {
  60. login();
  61. } else {
  62. $('#phoneHelp').text('您输入的 手机 格式不对');
  63. }
  64. }
  65. // let account = $("#inputEmail").val();
  66. // let pw = $("#inputPassword").val();
  67. // 判断输入的邮箱/手机是否格式正确
  68. // if(/^1[3456789]\d{9}$/.test(account) || /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(account)) {
  69. // // 先判断是否是专业版用户,是的话弹出短信验证
  70. // $.ajax({
  71. // url: '/accountIsPro',
  72. // type: 'post',
  73. // async: true,
  74. // data: {"account": account, "pw": pw},
  75. // success: function (response) {
  76. // if (response.error === 0) {
  77. // const ispro = response.result;
  78. // if (!ispro) {
  79. // login(captchaObj);
  80. // } else {
  81. // $('#phonepass').modal('show');
  82. // $('#proMobile').val(response.data);
  83. // $('#pro_mobile').text(response.data.substr(0, 3) + '****' + response.data.substr(7, 11));
  84. // }
  85. // } else if(response.error === 2) {
  86. // $('#check_ssoId').val(response.ssoId);
  87. // $('#phone').modal('show');
  88. // } else {
  89. // let msg = response.msg !== undefined ? response.msg : '未知错误';
  90. // showError(msg, $("input"));
  91. // }
  92. // }
  93. // // });
  94. // } else {
  95. // $('#emailHelp').text('您输入的 邮箱/手机 格式不对');
  96. // }
  97. });
  98. // $('#loginPro').click(function () {
  99. // if ($('#smsCode').val() === '') {
  100. // showValidError('请输入验证码',$('#smsCode'));
  101. // } else {
  102. // login(captchaObj);
  103. // }
  104. // });
  105. $('#check-code').click(function () {
  106. const mobile = $("#mobile").val();
  107. const ssoId = $("#check_ssoId").val();
  108. const code = $("#code").val();
  109. if(!validMobile(mobile)) {
  110. return false;
  111. }
  112. if(ssoId === undefined || ssoId === '') {
  113. showValidError('账号有误!', $('#code'));
  114. return false;
  115. }
  116. if($.trim(code) === '') {
  117. showValidError('验证码不能为空!', $('#code'));
  118. return false;
  119. }
  120. $.ajax({
  121. url: '/sms/mobile',
  122. type: 'post',
  123. data: {ssoId: ssoId, mobile: mobile, code: code},
  124. error: function() {
  125. showValidError('接口出错!',$('#code'));
  126. },
  127. beforeSend: function() {
  128. },
  129. success: function(response) {
  130. if (response.err === 0) {
  131. $("#login").click();
  132. $('#phone').modal('hide');
  133. } else {
  134. showValidError(response.msg,$('#code'));
  135. }
  136. }
  137. })
  138. });
  139. // };
  140. $("input").blur(function () {
  141. cleanError();
  142. cleanValidError($(this));
  143. });
  144. $('#inputEmail').blur(function () {
  145. let account = $("#inputEmail").val();
  146. if(!/^1[3456789]\d{9}$/.test(account) && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(account)) {
  147. $('#emailHelp').text('您输入的 邮箱/手机 格式不对');
  148. }
  149. });
  150. $(".form-control").on('input', function () {
  151. $('#hint').html(' ');
  152. });
  153. $("#get-code").click(function() {
  154. const mobile = $("#mobile").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: 1},
  164. error: function() {
  165. showValidError('短信接口出错!',$('#mobile'));
  166. },
  167. beforeSend: function() {
  168. },
  169. success: function(response) {
  170. if (response.err === 0) {
  171. codeSuccess(btn);
  172. } else {
  173. showValidError(response.msg,$('#mobile'));
  174. }
  175. }
  176. });
  177. }
  178. });
  179. // 切换登录方式
  180. $('#changeLogin').click(function () {
  181. if ($(this).attr('data-status') === 'user') {
  182. $(this).attr('data-status', 'sms');
  183. $('.change-login-p').text('短信登录');
  184. $(this).text('账号登录');
  185. $('.sms-login-modal').show();
  186. $('.user-login-modal').hide();
  187. $('.sms-login-modal input').attr('disabled', false);
  188. $('.user-login-modal input').attr('disabled', true);
  189. $('#login').text('登录');
  190. } else {
  191. $(this).attr('data-status', 'user');
  192. $('.change-login-p').text('账号登录');
  193. $(this).text('短信登录');
  194. $('.sms-login-modal').hide();
  195. $('.user-login-modal').show();
  196. $('.sms-login-modal input').attr('disabled', true);
  197. $('.user-login-modal input').attr('disabled', false);
  198. $('#login').text('登录');
  199. }
  200. });
  201. $('#changeReg').click(function () {
  202. $('#changeLogin').attr('data-status', 'sms');
  203. $('.change-login-p').text('短信注册');
  204. $('#changeLogin').text('账号登录');
  205. $('.sms-login-modal').show();
  206. $('.user-login-modal').hide();
  207. $('.sms-login-modal input').attr('disabled', false);
  208. $('.user-login-modal input').attr('disabled', true);
  209. $('#login').text('注册');
  210. });
  211. // 切换到短信登录
  212. $('#changeSmsLogin').click(function () {
  213. $('#changeLogin').attr('data-status', 'sms');
  214. $('.change-login-p').text('短信登录');
  215. $('#changeLogin').text('账号登录');
  216. $('.sms-login-modal').show();
  217. $('.user-login-modal').hide();
  218. $('.sms-login-modal input').attr('disabled', false);
  219. $('.user-login-modal input').attr('disabled', true);
  220. $('#phonepass').modal('hide');
  221. });
  222. $("#get-code2").click(function() {
  223. const mobile = $("#mobileLogin").val();
  224. if(!validMobile(mobile, 0)){
  225. return false;
  226. }
  227. const btn = $(this);
  228. if(!btn.hasClass('disabled')){
  229. // 判断该手机号是否已注册通行账号
  230. // $.ajax({
  231. // url: '/sms/check/mobile',
  232. // type: 'post',
  233. // data: {mobile: mobile},
  234. // error: function() {
  235. // $('#phoneHelp').text('号码查询接口出错!');
  236. // },
  237. // beforeSend: function() {
  238. // },
  239. // success: function(response) {
  240. // if (response.err === 0) {
  241. $.ajax({
  242. url: '/sms/code',
  243. type: 'post',
  244. data: { mobile: mobile, type: 3},
  245. error: function() {
  246. $('#phoneHelp').text('短信接口出错!');
  247. },
  248. beforeSend: function() {
  249. },
  250. success: function(response) {
  251. if (response.err === 0) {
  252. codeSuccess(btn);
  253. } else {
  254. $('#phoneHelp').text(response.msg);
  255. }
  256. }
  257. });
  258. // } else {
  259. // $('#phoneHelp').text(response.msg);
  260. // }
  261. // }
  262. // })
  263. }
  264. });
  265. });
  266. function login() {
  267. $.ajax({
  268. url: '/captcha?t='+ (new Date()).getTime(),
  269. type: 'get',
  270. dataType: 'json',
  271. timeout: 5000,
  272. error: function() {
  273. // $("#captcha-box").html('验证码加载失败');
  274. },
  275. beforeSend: function() {
  276. // $("#captcha-box").html('正在加载验证码');
  277. },
  278. success: function(response) {
  279. $("#captcha-box").html('');
  280. if (response.success === 0) {
  281. alert('验证码初始化失败!');
  282. return false;
  283. }
  284. initGeetest({
  285. // 以下配置参数来自服务端 SDK
  286. gt: response.gt,
  287. challenge: response.challenge,
  288. offline: !response.success,
  289. new_captcha: response.new_captcha,
  290. // width: '100%',
  291. product: "bind"
  292. }, function (catpchaObj) {
  293. catpchaObj.onReady(function () {
  294. catpchaObj.verify();
  295. }).onClose(function () {
  296. }).onSuccess(function () {
  297. /* 延迟到动画结束后再alert */
  298. var lastUTC = new Date(), duration = 1100;
  299. function _alert(msg) {
  300. var elapsed = new Date() - lastUTC;
  301. if (elapsed >= duration) { return alert(msg) }
  302. setTimeout(function () { alert(msg) }, duration - elapsed);
  303. }
  304. var result = catpchaObj.getValidate();
  305. if (!result) {return alert('请完成验证');}
  306. $('#login').attr('disabled', true);
  307. let geetest_challenge = $('input[name="geetest_challenge"]').val();
  308. let geetest_validate = $('input[name="geetest_validate"]').val();
  309. let geetest_seccode = $('input[name="geetest_seccode"]').val();
  310. const postData = {
  311. geetest_challenge: geetest_challenge,
  312. geetest_validate: geetest_validate,
  313. geetest_seccode: geetest_seccode,
  314. };
  315. if ($('#changeLogin').attr('data-status') === 'user') {
  316. let account = $("#inputEmail").val();
  317. let pw = $("#inputPassword").val();
  318. postData.account = account;
  319. postData.pw = pw;
  320. } else {
  321. let mobile = $('#mobileLogin').val();
  322. let code = $("#codeLogin").val();
  323. postData.mobile = mobile;
  324. postData.code = code;
  325. }
  326. $.ajax({
  327. url: '/login',
  328. type: 'post',
  329. data: postData,
  330. success: function (response) {
  331. if (response.error === 0) {
  332. // $('#phonepass').modal('hide');
  333. const url = response.last_page !== null && response.last_page !== undefined && response.last_page !== '' ?
  334. response.last_page : '/pm';
  335. if (response.login_ask === 0) {
  336. location.href = url;
  337. } else {
  338. response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
  339. null : JSON.parse(response.compilation_list);
  340. if (response.compilation_list === null || response.compilation_list.length <= 0) {
  341. location.href = url;
  342. return false;
  343. }
  344. console.log(response.compilation_list);
  345. setVersion(response.compilation_list);
  346. $('#ver').modal('show');
  347. }
  348. } else if(response.error === 2) {
  349. // $('#phonepass').modal('hide');
  350. // captchaObj.reset();
  351. $('#check_ssoId').val(response.ssoId);
  352. $('#phone').modal('show');
  353. $('#login').removeAttr('disabled');
  354. } else if(response.error === 3) {
  355. // captchaObj.reset();
  356. $('#phonepass').modal('show');
  357. $('#mobileLogin').val(response.data);
  358. $('#login').removeAttr('disabled');
  359. } else {
  360. // $('#phonepass').modal('hide');
  361. let msg = response.msg !== undefined ? response.msg : '未知错误';
  362. showError(msg, $("input"));
  363. $('#login').removeAttr('disabled');
  364. // captchaObj.reset();
  365. }
  366. },
  367. error: function (result) {
  368. showError('内部程序错误', null);
  369. $('#login').removeAttr('disabled');
  370. }
  371. });
  372. })
  373. });
  374. }
  375. });
  376. }
  377. /**
  378. * 获取成功后的操作
  379. *
  380. * @param {Object} btn - 点击的按钮
  381. * @return {void}
  382. */
  383. function codeSuccess(btn) {
  384. let counter = 60;
  385. btn.removeClass('btn-primary').addClass('btn-outline-secondary disabled').text(counter + '秒 重新获取');
  386. btn.parents().siblings('div').children('input').removeAttr('readonly');
  387. const countDown = setInterval(function() {
  388. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + '秒 ';
  389. // 倒数结束后
  390. if (countString === '') {
  391. clearInterval(countDown);
  392. btn.removeClass('btn-outline-secondary disabled').addClass('btn-primary').text('获取验证码');
  393. }
  394. const text = countString + '重新获取';
  395. btn.text(text);
  396. counter -= 1;
  397. }, 1000);
  398. }
  399. /**
  400. * 验证手机号是否正确
  401. *
  402. * @return {boolean}
  403. */
  404. function validMobile(mobile, status = 1) {
  405. let result = true;
  406. if($.trim(mobile) === ''){
  407. if (status === 1) {
  408. showValidError('手机号不能为空!',$('#mobile'));
  409. } else {
  410. $('#phoneHelp').text('手机号不能为空!');
  411. }
  412. return false;
  413. }
  414. let mobileValid = /^1[3456789]\d{9}$/;
  415. if(!mobileValid.test(mobile)){
  416. if (status === 1) {
  417. showValidError('手机号码格式有误!',$('#mobile'));
  418. } else {
  419. $('#phoneHelp').text('手机号码格式有误!');
  420. }
  421. return false;
  422. }
  423. return result;
  424. }
  425. /**
  426. * 提示验证信息错误
  427. *
  428. * @param {string} msg
  429. * @param {object} element
  430. * @return {void}
  431. */
  432. function showValidError(msg, element) {
  433. if (element !== null) {
  434. element.addClass('is-invalid');
  435. element.siblings().text(msg);
  436. }
  437. }
  438. /**
  439. * 清除验证信息错误提示
  440. *
  441. * @return {void}
  442. */
  443. function cleanValidError(element) {
  444. element.removeClass('is-invalid');
  445. element.siblings().text('');
  446. }
  447. /**
  448. * 验证数据
  449. *
  450. * @return {boolean}
  451. */
  452. function valid() {
  453. let result = true;
  454. if ($('#changeLogin').attr('data-status') === 'user') {
  455. let account = $("#inputEmail").val();
  456. if (account === undefined || account === '') {
  457. showError('用户名不能为空!', $("#inputEmail"));
  458. return false;
  459. }
  460. let password = $("#inputPassword").val();
  461. if (password === undefined || password === '') {
  462. showError('密码不能为空!', $("#inputPassword"));
  463. return false;
  464. }
  465. } else {
  466. let mobile = $('#mobileLogin').val();
  467. if (mobile === undefined || mobile === '') {
  468. showError('手机号码不能为空!', $("#mobileLogin"));
  469. return false;
  470. }
  471. let code = $('#codeLogin').val();
  472. if (code === undefined || code === '') {
  473. showError('验证码不能为空!', $("#codeLogin"));
  474. return false;
  475. }
  476. }
  477. return result;
  478. }
  479. /**
  480. * 提示错误
  481. *
  482. * @param {string} msg
  483. * @param {object} element
  484. * @return {void}
  485. */
  486. function showError(msg, element) {
  487. if (element !== null) {
  488. element.parent().addClass('has-danger');
  489. }
  490. $("#message").html(msg);
  491. $("#error-tips").show("fast");
  492. }
  493. /**
  494. * 清除错误提示
  495. *
  496. * @return {void}
  497. */
  498. function cleanError() {
  499. $("input").parent().removeClass('has-danger');
  500. $("#message").text('');
  501. $("#error-tips").hide("fast");
  502. }
  503. /**
  504. * 设置版本信息
  505. *
  506. * @param {Object} versionData
  507. * @return {void}
  508. */
  509. function setVersion(versionData) {
  510. let html = '';
  511. for (let version of versionData) {
  512. let description = version.description ? version.description : '介绍内容';
  513. let tmpHtml = '<div class="col-sm-4 mb-4">' +
  514. '<div class="card card-block">' +
  515. '<div class="card-body">' +
  516. '<h4 class="card-title d-flex justify-content-between">'+ version.name +
  517. '<a class="btn btn-sm btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a></h4>' +
  518. '<p class="small mb-0 text-muted">' + description + '</p>' +
  519. '</div>' +
  520. '</div>' +
  521. '</div>';
  522. html += tmpHtml;
  523. }
  524. $("#version-area").html(html);
  525. }