invite.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. registerPassword.addEventListener('keyup', registerByEnter);
  10. btnRegister.addEventListener('click', register);
  11. function registerByEnter(e) {
  12. if (e.keyCode === 13) {
  13. e.preventDefault();
  14. console.log('login!');
  15. register();
  16. }
  17. };
  18. function validateEmail(email) {
  19. 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,}))$/;
  20. if(re.test(String(email).toLowerCase()) === false) {
  21. $('#register [name = "email"]').addClass('error');
  22. if( !$('.error-text').length ) {
  23. let msg = '請輸入正確E-mail';
  24. if (lang == 'en') {
  25. msg = 'Please enter valid Email format.';
  26. }
  27. $('input.error').after('<p class="error-text" set-lan="html:errorEmail">' + msg + '</p>');
  28. }
  29. } else {
  30. $('#register [name = "email"]').removeClass('error');
  31. $('.error-text').remove();
  32. }
  33. return re.test(String(email).toLowerCase());
  34. }
  35. function validatePassword(psd) {
  36. if(psd.length >=4) {
  37. $('#register [name = "password"]').removeClass('error');
  38. $('.error-text').remove();
  39. return true;
  40. } else {
  41. $('#register [name = "password"]').addClass('error');
  42. if( !$('.error-text').length ) {
  43. $('input.error').after('<p class="error-text" set-lan="html:errorPsd">密碼至少為4個字元</p>');
  44. }
  45. return false;
  46. }
  47. }
  48. function getpathId() {
  49. const urlParams = new URLSearchParams(window.location.search);
  50. const id = urlParams.get('code');
  51. return id;
  52. }
  53. console.log(getpathId());
  54. function register() {
  55. let url = 'https://www.choozmo.com:8887/register';
  56. let userName = $('#register [name = "username"]').val();
  57. let email = $('#register [name = "email"]').val();
  58. let password = $('#register [name = "password"]').val();
  59. let code = getpathId();
  60. let userObj = {
  61. username: userName,
  62. email,
  63. password
  64. }
  65. if(code) {
  66. url = 'https://www.choozmo.com:8887/register_by_invite';
  67. userObj = {
  68. username: userName,
  69. email,
  70. password,
  71. code: code
  72. }
  73. }
  74. // 註冊資料檢查
  75. if (username == '' || password == '') {
  76. let title = "註冊失敗";
  77. let text = "請先輸入您的帳號/密碼";
  78. if (lang == 'en') {
  79. title = "Login Failed!";
  80. text = "Please enter your username and password";
  81. }
  82. Swal.fire({
  83. title: title,
  84. icon: 'error',
  85. text: text,
  86. confirmButtonColor: '#3085d6',
  87. });
  88. return;
  89. }
  90. if(userName && validateEmail(email) && validatePassword(password)) {
  91. console.log(userObj);
  92. const headers = {
  93. "accept": "application/json",
  94. "Content-Type": "application/json"
  95. }
  96. axios({
  97. method: 'post',
  98. url: 'https://www.choozmo.com:8887/register',
  99. headers: headers,
  100. data: userObj
  101. }).then(res => {
  102. console.log(res.data.msg);
  103. let text;
  104. if (lang == 'en') {
  105. text = res.data.msg.eng;
  106. } else {
  107. text = res.data.msg.zh;
  108. }
  109. Swal.fire({
  110. icon: 'info',
  111. text: text,
  112. confirmButtonColor: '#3085d6',
  113. });
  114. }).catch(err => {
  115. console.log(err);
  116. })
  117. }
  118. }
  119. function loginControl() {
  120. btnLoginPage.style.display = 'block';
  121. btnLogout.style.display = 'none';
  122. btnUserProfile.style.display = 'none';
  123. btnLoginPage_d.style.display = 'block';
  124. btnLogout_d.style.display = 'none';
  125. btnUserProfile_d.style.display = 'none';
  126. }
  127. loginControl();