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