invite.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. const btnRegister = document.querySelector('.btn-register');
  2. const btnLoginPage = document.querySelector('.btn-login');
  3. const btnUserProfile = document.querySelector('.btn-userProfile');
  4. const btnLogout = document.querySelector('.btn-logout');
  5. const registerPassword = document.querySelector('#register #password');
  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. registerPassword.addEventListener('keyup', registerByEnter);
  21. btnRegister.addEventListener('click', register);
  22. function registerByEnter(e) {
  23. if (e.keyCode === 13) {
  24. e.preventDefault();
  25. console.log('login!');
  26. register();
  27. }
  28. };
  29. function validateEmail(email) {
  30. const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  31. if(re.test(String(email).toLowerCase()) === false) {
  32. $('#register [name = "email"]').addClass('error');
  33. if( !$('.error-text').length ) {
  34. let msg = '請輸入正確E-mail';
  35. if (lang == 'en') {
  36. msg = 'Please enter valid Email format.';
  37. }
  38. $('input.error').after('<p class="error-text" set-lan="html:errorEmail">' + msg + '</p>');
  39. }
  40. } else {
  41. $('#register [name = "email"]').removeClass('error');
  42. $('.error-text').remove();
  43. }
  44. return re.test(String(email).toLowerCase());
  45. }
  46. function validatePassword(psd) {
  47. if(psd.length >=4) {
  48. $('#register [name = "password"]').removeClass('error');
  49. $('.error-text').remove();
  50. return true;
  51. } else {
  52. $('#register [name = "password"]').addClass('error');
  53. if( !$('.error-text').length ) {
  54. $('input.error').after('<p class="error-text" set-lan="html:errorPsd">密碼至少為4個字元</p>');
  55. }
  56. return false;
  57. }
  58. }
  59. function validateConfirmPsd(psd1, psd2) {
  60. if(psd1 == psd2) {
  61. $('#register #confirm_password').removeClass('error');
  62. $('.error-text').remove();
  63. return true;
  64. } else {
  65. $('#register #confirm_password').addClass('error');
  66. if( !$('.error-text').length ) {
  67. $('input.error').after('<p class="error-text" set-lan="html:errorConsistentPsd">密碼輸入需一致</p>');
  68. }
  69. return false;
  70. }
  71. }
  72. function getpathId() {
  73. const urlParams = new URLSearchParams(window.location.search);
  74. const id = urlParams.get('code');
  75. return id;
  76. }
  77. console.log(getpathId());
  78. function register() {
  79. let url = 'https://www.choozmo.com:8887/register';
  80. let userName = $('#register [name = "username"]').val();
  81. let email = $('#register [name = "email"]').val();
  82. let password = $('#register [name = "password"]').val();
  83. let confirm_psd = $('#register #confirm_password').val();
  84. let code = getpathId();
  85. let userObj = {
  86. username: userName,
  87. email,
  88. password
  89. }
  90. if(code) {
  91. url = 'https://www.choozmo.com:8887/register_by_invite';
  92. userObj = {
  93. username: userName,
  94. email,
  95. password,
  96. code: code
  97. }
  98. }
  99. // 註冊資料檢查
  100. if (username == '' || password == '') {
  101. let title = "註冊失敗";
  102. let text = "請先輸入您的帳號/密碼";
  103. if (lang == 'en') {
  104. title = "Login Failed!";
  105. text = "Please enter your username and password";
  106. }
  107. Swal.fire({
  108. title: title,
  109. icon: 'error',
  110. text: text,
  111. confirmButtonColor: '#3085d6',
  112. });
  113. return;
  114. }
  115. if(userName && validateEmail(email) && validatePassword(password) && validateConfirmPsd(password, confirm_psd)) {
  116. JsLoadingOverlay.show({
  117. "overlayBackgroundColor": "#666666",
  118. "overlayOpacity": 0.6,
  119. "spinnerIcon": "ball-circus",
  120. "spinnerColor": "#000",
  121. "spinnerSize": "3x",
  122. "overlayIDName": "overlay",
  123. "spinnerIDName": "spinner",
  124. "offsetX": 0,
  125. "offsetY": 0,
  126. "containerID": null,
  127. "lockScroll": false,
  128. "overlayZIndex": 10,
  129. "spinnerZIndex": 11
  130. });
  131. console.log(userObj);
  132. const headers = {
  133. "accept": "application/json",
  134. "Content-Type": "application/json"
  135. }
  136. axios({
  137. method: 'post',
  138. url: 'https://www.choozmo.com:8887/register',
  139. headers: headers,
  140. data: userObj
  141. }).then(res => {
  142. console.log(res.data.msg);
  143. JsLoadingOverlay.hide();
  144. let text;
  145. if (lang == 'en') {
  146. text = res.data.msg.eng;
  147. } else {
  148. text = res.data.msg.zh;
  149. }
  150. Swal.fire({
  151. icon: 'info',
  152. text: text,
  153. confirmButtonColor: '#3085d6',
  154. });
  155. }).catch(err => {
  156. console.log(err);
  157. var title = "錯誤處理中,請稍後再試";
  158. if (lang == 'en') { // 英文版訊息
  159. title = "Oops! Errors occurred. Please try it later!"
  160. }
  161. Swal.fire({
  162. title: title,
  163. icon: 'error',
  164. confirmButtonColor: '#3085d6',
  165. });
  166. })
  167. }
  168. }
  169. function loginControl() {
  170. btnLoginPage.style.display = 'block';
  171. btnLogout.style.display = 'none';
  172. btnUserProfile.style.display = 'none';
  173. btnLoginPage_d.style.display = 'block';
  174. btnLogout_d.style.display = 'none';
  175. btnUserProfile_d.style.display = 'none';
  176. }
  177. loginControl();