login.js 12 KB

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