login.js 7.3 KB

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