login.js 6.8 KB

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