script_profile.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const btnLoginPage = document.querySelector('.btn-login');
  2. const btnUserProfile = document.querySelector('.btn-userProfile');
  3. const btnLogout = document.querySelector('.btn-logout');
  4. function getCookie(name) {
  5. const value = `; ${document.cookie}`;
  6. const parts = value.split(`; ${name}=`);
  7. if (parts.length === 2) return parts.pop().split(';').shift();
  8. }
  9. function renderView() {
  10. let token = getCookie('jwt_token');
  11. document.cookie = "access_token_cookie" + "=" + token;
  12. // axios.defaults.withCredentials = false;
  13. axios({
  14. method: 'post',
  15. url: 'http://www.choozmo.com:8887/user_profile',
  16. headers: {
  17. 'accept': 'text/html',
  18. 'Authorization': `Bearer ${token}`
  19. }
  20. }).then(res => {
  21. console.log(res.data);
  22. const userInfo = res.data;
  23. const str = `<img src="static/img/undraw_male_avatar_323b.svg" alt="">
  24. <p class="card-profile-txt">User Profile</p>
  25. <p class="card-profile-cnt">${userInfo.user_info.userName}</p>
  26. <p class="card-profile-cnt">${userInfo.user_info.email}</p>
  27. <div class="d-flex justify-content-around">
  28. <div>
  29. <p>已使用</p>
  30. <p><strong>${userInfo.user_info.total_sec}</strong>秒</p>
  31. </div>
  32. <div>
  33. <p>未使用</p>
  34. <p><strong>${userInfo.user_info.left_sec}</strong>秒</p>
  35. </div>
  36. </div>`;
  37. $('.card-profile').html(str);
  38. }).catch(err => {
  39. console.log(err);
  40. })
  41. }
  42. renderView();
  43. function checkLogin() {
  44. let token = getCookie('jwt_token');
  45. if(token) {
  46. btnLoginPage.style.display = 'none';
  47. btnLogout.style.display = 'block';
  48. btnUserProfile.style.display = 'block';
  49. } else {
  50. window.location.href = 'login.html';
  51. btnLoginPage.style.display = 'block';
  52. btnLogout.style.display = 'none';
  53. btnUserProfile.style.display = 'none';
  54. }
  55. }
  56. checkLogin();