login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const btnLogin = document.querySelector('#btn_login');
  2. const inputPassword = document.querySelector('#login #password');
  3. const btnLoginPage = document.querySelector('.btn-login');
  4. const btnUserProfile = document.querySelector('.btn-userProfile');
  5. const btnLogout = document.querySelector('.btn-logout');
  6. const btnLoginPage_d = document.querySelector('.login-top .btn-login');
  7. const btnUserProfile_d = document.querySelector('.login-top .btn-userProfile');
  8. const btnLogout_d= document.querySelector('.login-top .btn-logout');
  9. inputPassword.addEventListener('keyup', loginByEnter);
  10. btnLogin.addEventListener('click', login);
  11. function loginByEnter(e) {
  12. if (e.keyCode === 13) {
  13. e.preventDefault();
  14. console.log('login!');
  15. login();
  16. }
  17. };
  18. function login(){
  19. console.log('login!');
  20. var url = "https://www.choozmo.com:8887/login";
  21. var username = $('#username').val();
  22. var password = $('#password').val();
  23. if (username == '' || password == '') {
  24. let title = "登入失敗";
  25. let text = "請先輸入您的帳號/密碼";
  26. if (lang == 'en') {
  27. title = "Login Failed!";
  28. text = "Please enter your username and password";
  29. }
  30. Swal.fire({
  31. title: title,
  32. icon: 'error',
  33. text: text,
  34. confirmButtonColor: '#3085d6',
  35. });
  36. return;
  37. }
  38. var data = "grant_type=&username=" + username + "&password="+password+"&scope=&client_id=&client_secret=";
  39. const headers = {
  40. "accept": "application/json",
  41. "Content-Type": "application/x-www-form-urlencoded"
  42. }
  43. axios({
  44. method: 'post',
  45. url: url,
  46. headers: headers,
  47. data: data
  48. }).then(res => {
  49. console.log(res.data);
  50. if(res.data.access_token!=null) {
  51. var title = "登入成功";
  52. if (lang == 'en') { // 英文版訊息
  53. title = "Login Successfully!"
  54. }
  55. Swal.fire({
  56. title: title,
  57. icon: 'success',
  58. confirmButtonColor: '#3085d6',
  59. });
  60. window.setTimeout(() => {
  61. window.location.href = 'user_profile2.html';
  62. }, 2000);
  63. btnLoginPage.style.display = 'none';
  64. } else {
  65. var title = "登入失敗";
  66. if (lang == 'en') { // 英文版訊息
  67. title = "Login Failed!"
  68. }
  69. Swal.fire({
  70. title: title,
  71. icon: 'error',
  72. text: responseOBJ.detail,
  73. confirmButtonColor: '#3085d6',
  74. });
  75. }
  76. }).catch(err => {
  77. console.log(err);
  78. })
  79. }
  80. /* var xhr = new XMLHttpRequest();
  81. xhr.open("POST", url);
  82. xhr.setRequestHeader("accept", "application/json");
  83. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  84. xhr.onreadystatechange = function () {
  85. if (xhr.readyState === 4) {
  86. console.log(xhr.responseText); // test
  87. responseOBJ = JSON.parse(xhr.responseText);
  88. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  89. document.cookie = 'jwt_token='+responseOBJ.access_token;
  90. console.log(responseOBJ);
  91. if (responseOBJ.access_token!=null)
  92. {
  93. var title = "登入成功";
  94. if (lang == 'en') { // 英文版訊息
  95. title = "Login Successfully!"
  96. }
  97. Swal.fire({
  98. title: title,
  99. icon: 'success',
  100. confirmButtonColor: '#3085d6',
  101. });
  102. window.setTimeout(() => {
  103. window.location.href = 'user_profile2.html';
  104. }, 2000);
  105. btnLoginPage.style.display = 'none';
  106. }
  107. else{
  108. var title = "登入失敗";
  109. if (lang == 'en') { // 英文版訊息
  110. title = "Login Failed!"
  111. }
  112. Swal.fire({
  113. title: title,
  114. icon: 'error',
  115. text: responseOBJ.detail,
  116. confirmButtonColor: '#3085d6',
  117. });
  118. }
  119. }
  120. };
  121. // 登入資料檢查
  122. if (username == '' || password == '') {
  123. let title = "登入失敗";
  124. let text = "請先輸入您的帳號/密碼";
  125. if (lang == 'en') {
  126. title = "Login Failed!";
  127. text = "Please enter your username and password";
  128. }
  129. Swal.fire({
  130. title: title,
  131. icon: 'error',
  132. text: text,
  133. confirmButtonColor: '#3085d6',
  134. });
  135. return;
  136. }
  137. result = xhr.send(data);
  138. console.log(result);
  139. } */
  140. function loginControl() {
  141. btnLoginPage.style.display = 'block';
  142. btnLogout.style.display = 'none';
  143. btnUserProfile.style.display = 'none';
  144. btnLoginPage_d.style.display = 'block';
  145. btnLogout_d.style.display = 'none';
  146. btnUserProfile_d.style.display = 'none';
  147. }
  148. loginControl();