login.js 16 KB

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