script_profile.js 2.0 KB

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