login.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. JsLoadingOverlay.show({
  39. "overlayBackgroundColor": "#666666",
  40. "overlayOpacity": 0.6,
  41. "spinnerIcon": "ball-circus",
  42. "spinnerColor": "#000",
  43. "spinnerSize": "3x",
  44. "overlayIDName": "overlay",
  45. "spinnerIDName": "spinner",
  46. "offsetX": 0,
  47. "offsetY": 0,
  48. "containerID": null,
  49. "lockScroll": false,
  50. "overlayZIndex": 10,
  51. "spinnerZIndex": 11
  52. });
  53. var data = "grant_type=&username=" + username + "&password="+password+"&scope=&client_id=&client_secret=";
  54. const headers = {
  55. "accept": "application/json",
  56. "Content-Type": "application/x-www-form-urlencoded"
  57. }
  58. axios({
  59. method: 'post',
  60. url: url,
  61. headers: headers,
  62. data: data
  63. }).then(res => {
  64. console.log(res.data);
  65. document.cookie = 'jwt_token='+res.data.access_token;
  66. JsLoadingOverlay.hide();
  67. if(res.data.access_token!=null && res.data.veri == 'ok') {
  68. var title = "登入成功";
  69. if (lang == 'en') { // 英文版訊息
  70. title = "Login Successfully!"
  71. }
  72. Swal.fire({
  73. title: title,
  74. icon: 'success',
  75. confirmButtonColor: '#3085d6',
  76. });
  77. window.setTimeout(() => {
  78. window.location.href = 'user_profile2.html';
  79. }, 2000);
  80. btnLoginPage.style.display = 'none';
  81. } else if (res.data.access_token!=null && res.data.veri != 'ok') {
  82. var title = "請前往信箱完成驗證";
  83. if (lang == 'en') { // 英文版訊息
  84. title = "Please check your email box to get validated!"
  85. }
  86. Swal.fire({
  87. title: title,
  88. icon: 'error',
  89. text: responseOBJ.detail,
  90. confirmButtonColor: '#3085d6',
  91. });
  92. } else {
  93. var title = "登入失敗";
  94. JsLoadingOverlay.hide();
  95. if (lang == 'en') { // 英文版訊息
  96. title = "Login Failed!"
  97. }
  98. Swal.fire({
  99. title: title,
  100. icon: 'error',
  101. text: responseOBJ.detail,
  102. confirmButtonColor: '#3085d6',
  103. });
  104. }
  105. }).catch(err => {
  106. console.log(err);
  107. var title = "錯誤處理中,請稍後再試";
  108. if (lang == 'en') { // 英文版訊息
  109. title = "Oops! Errors occurred. Please try it later!"
  110. }
  111. Swal.fire({
  112. title: title,
  113. icon: 'error',
  114. confirmButtonColor: '#3085d6',
  115. });
  116. })
  117. }
  118. /* var xhr = new XMLHttpRequest();
  119. xhr.open("POST", url);
  120. xhr.setRequestHeader("accept", "application/json");
  121. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  122. xhr.onreadystatechange = function () {
  123. if (xhr.readyState === 4) {
  124. console.log(xhr.responseText); // test
  125. responseOBJ = JSON.parse(xhr.responseText);
  126. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  127. document.cookie = 'jwt_token='+responseOBJ.access_token;
  128. console.log(responseOBJ);
  129. if (responseOBJ.access_token!=null)
  130. {
  131. var title = "登入成功";
  132. if (lang == 'en') { // 英文版訊息
  133. title = "Login Successfully!"
  134. }
  135. Swal.fire({
  136. title: title,
  137. icon: 'success',
  138. confirmButtonColor: '#3085d6',
  139. });
  140. window.setTimeout(() => {
  141. window.location.href = 'user_profile2.html';
  142. }, 2000);
  143. btnLoginPage.style.display = 'none';
  144. }
  145. else{
  146. var title = "登入失敗";
  147. if (lang == 'en') { // 英文版訊息
  148. title = "Login Failed!"
  149. }
  150. Swal.fire({
  151. title: title,
  152. icon: 'error',
  153. text: responseOBJ.detail,
  154. confirmButtonColor: '#3085d6',
  155. });
  156. }
  157. }
  158. };
  159. // 登入資料檢查
  160. if (username == '' || password == '') {
  161. let title = "登入失敗";
  162. let text = "請先輸入您的帳號/密碼";
  163. if (lang == 'en') {
  164. title = "Login Failed!";
  165. text = "Please enter your username and password";
  166. }
  167. Swal.fire({
  168. title: title,
  169. icon: 'error',
  170. text: text,
  171. confirmButtonColor: '#3085d6',
  172. });
  173. return;
  174. }
  175. result = xhr.send(data);
  176. console.log(result);
  177. } */
  178. function loginControl() {
  179. btnLoginPage.style.display = 'block';
  180. btnLogout.style.display = 'none';
  181. btnUserProfile.style.display = 'none';
  182. btnLoginPage_d.style.display = 'block';
  183. btnLogout_d.style.display = 'none';
  184. btnUserProfile_d.style.display = 'none';
  185. }
  186. loginControl();