navigation.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // 导航栏交互逻辑
  2. (function () {
  3. // 检查是否已登录
  4. function isLoggedIn() {
  5. const userAvatarContainer = document.getElementById('userAvatarContainer');
  6. // 如果用户头像容器显示,说明已登录
  7. if (userAvatarContainer) {
  8. const computedStyle = window.getComputedStyle(userAvatarContainer);
  9. return computedStyle.display !== 'none';
  10. }
  11. return false;
  12. }
  13. // 导航切换功能
  14. function initNavigation() {
  15. const navButtons = document.querySelectorAll('.nav-btn');
  16. navButtons.forEach(button => {
  17. button.addEventListener('click', function() {
  18. const page = this.getAttribute('data-page');
  19. // 如果点击"我的资产"且未登录,显示登录界面
  20. if (page === 'assets' && !isLoggedIn()) {
  21. // 恢复按钮状态
  22. navButtons.forEach(btn => btn.classList.remove('active'));
  23. const storeBtn = document.querySelector('.nav-btn[data-page="store"]');
  24. if (storeBtn) {
  25. storeBtn.classList.add('active');
  26. }
  27. // 显示登录界面
  28. if (window.parent !== window) {
  29. window.parent.postMessage({
  30. type: 'navigation',
  31. page: 'login'
  32. }, '*');
  33. }
  34. return;
  35. }
  36. // 更新按钮状态
  37. navButtons.forEach(btn => btn.classList.remove('active'));
  38. this.classList.add('active');
  39. // 通知父窗口切换页面
  40. if (window.parent !== window) {
  41. window.parent.postMessage({
  42. type: 'navigation',
  43. page: page
  44. }, '*');
  45. }
  46. });
  47. });
  48. // 登录按钮功能
  49. const loginBtn = document.querySelector('.login-btn');
  50. if (loginBtn) {
  51. loginBtn.addEventListener('click', function() {
  52. // console.log('[1-Navigation] 登录按钮点击');
  53. if (window.parent !== window) {
  54. window.parent.postMessage({
  55. type: 'navigation',
  56. page: 'login'
  57. }, '*');
  58. }
  59. });
  60. }
  61. // 注册按钮功能
  62. const registerBtn = document.querySelector('.register-btn');
  63. if (registerBtn) {
  64. registerBtn.addEventListener('click', function() {
  65. // console.log('[1-Navigation] 注册按钮点击');
  66. if (window.parent !== window) {
  67. window.parent.postMessage({
  68. type: 'navigation',
  69. page: 'register'
  70. }, '*');
  71. }
  72. });
  73. }
  74. // 用户头像点击功能
  75. const userAvatarContainer = document.getElementById('userAvatarContainer');
  76. const userAvatar = document.getElementById('userAvatar');
  77. function handleAvatarClick() {
  78. if (window.parent !== window) {
  79. window.parent.postMessage({
  80. type: 'navigation',
  81. page: 'profile'
  82. }, '*');
  83. }
  84. }
  85. if (userAvatarContainer) {
  86. userAvatarContainer.addEventListener('click', handleAvatarClick);
  87. userAvatarContainer.style.cursor = 'pointer';
  88. }
  89. if (userAvatar) {
  90. userAvatar.addEventListener('click', handleAvatarClick);
  91. userAvatar.style.cursor = 'pointer';
  92. }
  93. }
  94. // 更新用户头像显示
  95. function updateUserAvatar(userInfo) {
  96. const authButtons = document.getElementById('authButtons');
  97. const userAvatarContainer = document.getElementById('userAvatarContainer');
  98. const userAvatar = document.getElementById('userAvatar');
  99. if (userInfo) {
  100. // 有用户信息,说明已登录 - 显示用户头像,隐藏登录/注册按钮
  101. if (authButtons) {
  102. authButtons.style.display = 'none';
  103. }
  104. if (userAvatarContainer) {
  105. userAvatarContainer.style.display = 'flex';
  106. }
  107. if (userAvatar) {
  108. // 如果头像URL是相对路径,需要转换为完整URL
  109. let avatarUrl = userInfo.avatar;
  110. if (avatarUrl && !avatarUrl.startsWith('http') && !avatarUrl.startsWith('data:')) {
  111. // 如果是相对路径,添加服务器地址前缀
  112. avatarUrl = 'http://localhost:3000' + (avatarUrl.startsWith('/') ? avatarUrl : '/' + avatarUrl);
  113. }
  114. userAvatar.src = avatarUrl || '../../static/default-avatar.png';
  115. userAvatar.alt = userInfo.username || '用户头像';
  116. }
  117. } else {
  118. // 没有用户信息,说明未登录 - 隐藏用户头像,显示登录/注册按钮
  119. if (authButtons) {
  120. authButtons.style.display = 'flex';
  121. }
  122. if (userAvatarContainer) {
  123. userAvatarContainer.style.display = 'none';
  124. }
  125. }
  126. }
  127. // 接收来自父窗口的消息,同步导航状态
  128. window.addEventListener('message', function(event) {
  129. // 安全检查:确保消息来自同源
  130. if (event.origin !== window.location.origin) {
  131. return;
  132. }
  133. const data = event.data;
  134. // 处理页面切换消息(只处理store和assets,不处理login/register)
  135. if (data.type === 'navigation' && data.page && (data.page === 'store' || data.page === 'assets')) {
  136. const navButtons = document.querySelectorAll('.nav-btn');
  137. navButtons.forEach(btn => {
  138. if (btn.getAttribute('data-page') === data.page) {
  139. btn.classList.add('active');
  140. } else {
  141. btn.classList.remove('active');
  142. }
  143. });
  144. }
  145. // 处理登录成功消息
  146. if (data.type === 'login-success' && data.user) {
  147. updateUserAvatar(data.user);
  148. }
  149. // 处理登出消息
  150. if (data.type === 'logout') {
  151. updateUserAvatar(null);
  152. }
  153. });
  154. // 检查并恢复登录状态
  155. function checkAndRestoreLogin() {
  156. try {
  157. const loginDataStr = localStorage.getItem('loginData');
  158. if (!loginDataStr) {
  159. return;
  160. }
  161. const loginData = JSON.parse(loginDataStr);
  162. const now = Date.now();
  163. // 检查是否过期
  164. if (now >= loginData.expireTime) {
  165. // 已过期,清除登录信息
  166. localStorage.removeItem('loginData');
  167. console.log('[Navigation] 登录信息已过期,已清除');
  168. return;
  169. }
  170. // 未过期,恢复登录状态
  171. if (loginData.user) {
  172. console.log('[Navigation] 恢复登录状态,用户:', loginData.user.username);
  173. updateUserAvatar(loginData.user);
  174. }
  175. } catch (error) {
  176. console.error('[Navigation] 恢复登录状态失败:', error);
  177. localStorage.removeItem('loginData');
  178. }
  179. }
  180. // 页面加载完成后初始化
  181. window.addEventListener('DOMContentLoaded', function() {
  182. // 先检查并恢复登录状态
  183. checkAndRestoreLogin();
  184. initNavigation();
  185. });
  186. })();