login.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. let geetest_challenge = $('input[name="geetest_challenge"]').val();
  255. let geetest_validate = $('input[name="geetest_validate"]').val();
  256. let geetest_seccode = $('input[name="geetest_seccode"]').val();
  257. const postData = {
  258. geetest_challenge: geetest_challenge,
  259. geetest_validate: geetest_validate,
  260. geetest_seccode: geetest_seccode,
  261. };
  262. if ($('#changeLogin').attr('data-status') === 'user') {
  263. let account = $("#inputEmail").val();
  264. let pw = $("#inputPassword").val();
  265. postData.account = account;
  266. postData.pw = pw;
  267. } else {
  268. let mobile = $('#mobileLogin').val();
  269. let code = $("#codeLogin").val();
  270. postData.mobile = mobile;
  271. postData.code = code;
  272. }
  273. $.ajax({
  274. url: '/login',
  275. type: 'post',
  276. data: postData,
  277. success: function (response) {
  278. if (response.error === 0) {
  279. // $('#phonepass').modal('hide');
  280. const url = response.last_page !== null && response.last_page !== undefined && response.last_page !== '' ?
  281. response.last_page : '/pm';
  282. if (response.login_ask === 0) {
  283. location.href = url;
  284. } else {
  285. response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
  286. null : JSON.parse(response.compilation_list);
  287. if (response.compilation_list === null || response.compilation_list.length <= 0) {
  288. location.href = url;
  289. return false;
  290. }
  291. console.log(response.compilation_list);
  292. setVersion(response.compilation_list);
  293. $('#ver').modal('show');
  294. }
  295. } else if(response.error === 2) {
  296. // $('#phonepass').modal('hide');
  297. // captchaObj.reset();
  298. $('#check_ssoId').val(response.ssoId);
  299. $('#phone').modal('show');
  300. } else if(response.error === 3) {
  301. // captchaObj.reset();
  302. $('#phonepass').modal('show');
  303. $('#mobileLogin').val(response.data);
  304. } else {
  305. // $('#phonepass').modal('hide');
  306. let msg = response.msg !== undefined ? response.msg : '未知错误';
  307. showError(msg, $("input"));
  308. // captchaObj.reset();
  309. }
  310. },
  311. error: function (result) {
  312. showError('内部程序错误', null);
  313. }
  314. });
  315. }
  316. /**
  317. * 获取成功后的操作
  318. *
  319. * @param {Object} btn - 点击的按钮
  320. * @return {void}
  321. */
  322. function codeSuccess(btn) {
  323. let counter = 60;
  324. btn.removeClass('btn-primary').addClass('btn-outline-secondary disabled').text(counter + '秒 重新获取');
  325. btn.parents().siblings('div').children('input').removeAttr('readonly');
  326. const countDown = setInterval(function() {
  327. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + '秒 ';
  328. // 倒数结束后
  329. if (countString === '') {
  330. clearInterval(countDown);
  331. btn.removeClass('btn-outline-secondary disabled').addClass('btn-primary').text('获取验证码');
  332. }
  333. const text = countString + '重新获取';
  334. btn.text(text);
  335. counter -= 1;
  336. }, 1000);
  337. }
  338. /**
  339. * 验证手机号是否正确
  340. *
  341. * @return {boolean}
  342. */
  343. function validMobile(mobile, status = 1) {
  344. let result = true;
  345. if($.trim(mobile) === ''){
  346. if (status === 1) {
  347. showValidError('手机号不能为空!',$('#mobile'));
  348. } else {
  349. $('#phoneHelp').text('手机号不能为空!');
  350. }
  351. return false;
  352. }
  353. let mobileValid = /^1[3456789]\d{9}$/;
  354. if(!mobileValid.test(mobile)){
  355. if (status === 1) {
  356. showValidError('手机号码格式有误!',$('#mobile'));
  357. } else {
  358. $('#phoneHelp').text('手机号码格式有误!');
  359. }
  360. return false;
  361. }
  362. return result;
  363. }
  364. /**
  365. * 提示验证信息错误
  366. *
  367. * @param {string} msg
  368. * @param {object} element
  369. * @return {void}
  370. */
  371. function showValidError(msg, element) {
  372. if (element !== null) {
  373. element.addClass('is-invalid');
  374. element.siblings().text(msg);
  375. }
  376. }
  377. /**
  378. * 清除验证信息错误提示
  379. *
  380. * @return {void}
  381. */
  382. function cleanValidError(element) {
  383. element.removeClass('is-invalid');
  384. element.siblings().text('');
  385. }
  386. /**
  387. * 验证数据
  388. *
  389. * @return {boolean}
  390. */
  391. function valid() {
  392. let result = true;
  393. if ($('#changeLogin').attr('data-status') === 'user') {
  394. let account = $("#inputEmail").val();
  395. if (account === undefined || account === '') {
  396. showError('用户名不能为空!', $("#inputEmail"));
  397. return false;
  398. }
  399. let password = $("#inputPassword").val();
  400. if (password === undefined || password === '') {
  401. showError('密码不能为空!', $("#inputPassword"));
  402. return false;
  403. }
  404. } else {
  405. let mobile = $('#mobileLogin').val();
  406. if (mobile === undefined || mobile === '') {
  407. showError('手机号码不能为空!', $("#mobileLogin"));
  408. return false;
  409. }
  410. let code = $('#codeLogin').val();
  411. if (code === undefined || code === '') {
  412. showError('验证码不能为空!', $("#codeLogin"));
  413. return false;
  414. }
  415. }
  416. return result;
  417. }
  418. /**
  419. * 提示错误
  420. *
  421. * @param {string} msg
  422. * @param {object} element
  423. * @return {void}
  424. */
  425. function showError(msg, element) {
  426. if (element !== null) {
  427. element.parent().addClass('has-danger');
  428. }
  429. $("#message").html(msg);
  430. $("#error-tips").show("fast");
  431. }
  432. /**
  433. * 清除错误提示
  434. *
  435. * @return {void}
  436. */
  437. function cleanError() {
  438. $("input").parent().removeClass('has-danger');
  439. $("#message").text('');
  440. $("#error-tips").hide("fast");
  441. }
  442. /**
  443. * 设置版本信息
  444. *
  445. * @param {Object} versionData
  446. * @return {void}
  447. */
  448. function setVersion(versionData) {
  449. let html = '';
  450. for (let version of versionData) {
  451. let description = version.description ? version.description : '介绍内容';
  452. let tmpHtml = '<div class="col-sm-6 mb-3">' +
  453. '<div class="card card-block">' +
  454. '<div class="card-body">' +
  455. '<h3 class="card-title">'+ version.name +'</h3>' +
  456. '<p class="card-text">' + description + '</p>' +
  457. '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
  458. '</div>' +
  459. '</div>' +
  460. '</div>';
  461. html += tmpHtml;
  462. }
  463. $("#version-area").html(html);
  464. }