login.js 12 KB

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