script_profiles.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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: 'https://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 set-lan="html:used">已使用</p>
  32. // <p><strong>${userInfo.user_info.total_sec}</strong><span set-lan="html:sec">秒</span></p>
  33. // </div>
  34. // <div>
  35. // <p set-lan="html:left">未使用</p>
  36. // <p><strong>${userInfo.user_info.left_sec}</strong><span set-lan="html:sec">秒</span></p>
  37. // </div>
  38. // </div>`;
  39. const str = `<img src="static/img/undraw_male_avatar_323b.svg" alt="">
  40. <p class="card-profile-txt">User Profile</p>
  41. <p class="card-profile-cnt">${userInfo.user_info.userName}</p>
  42. <p class="card-profile-cnt">${userInfo.user_info.email}</p>
  43. <div class="d-flex justify-content-around">
  44. <div>
  45. <p set-lan="html:used">已使用</p>
  46. <p><strong>${userInfo.user_info.total_sec}</strong><span set-lan="html:sec">秒</span></p>
  47. </div>
  48. <div>
  49. <p set-lan="html:left">未使用</p>
  50. <p><strong>${userInfo.user_info.left_sec}</strong><span set-lan="html:sec">秒</span></p>
  51. </div>
  52. </div>`;
  53. const userName = `<h2 class="user-name text-white mt-4 fw-bold">Hello,${userInfo.user_info.userName}</h2>`;
  54. const usedtime=`<h1 class="text-center">${userInfo.user_info.total_sec}&ensp;<span style="font-size:15px;">秒</span></h1>`;
  55. const NotUsedTime=`<h1 class="text-center">${userInfo.user_info.left_sec}&ensp;<span style="font-size:15px;">秒</span></h1>`;
  56. const infContent=`
  57. <div class="inf-content">
  58. <p>${userInfo.user_info.userName}</p>
  59. <p>abc***************0&nbsp;<a href="">更改密碼</a></p>
  60. <p>${userInfo.user_info.email}</p>
  61. </div>`;
  62. $('.userinf').html(infContent);
  63. $('.userName').html(userName);
  64. $('.used-time').html(usedtime);
  65. $('.not-used-time').html(NotUsedTime);
  66. $('.card-profile').html(str);
  67. $('.card-profile').html(str);
  68. }).catch(err => {
  69. console.log(err);
  70. })
  71. }
  72. renderView();
  73. function checkLogin() {
  74. let token = getCookie('jwt_token');
  75. if(token) {
  76. btnLoginPage.style.display = 'none';
  77. btnLogout.style.display = 'block';
  78. btnUserProfile.style.display = 'block';
  79. } else {
  80. window.location.href = 'login.html';
  81. btnLoginPage.style.display = 'block';
  82. btnLogout.style.display = 'none';
  83. btnUserProfile.style.display = 'none';
  84. }
  85. }
  86. checkLogin();