login.js 7.5 KB

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