login.js 12 KB

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