diff --git a/web/admin/app.js b/web/admin/app.js index 9ccc7039..36166565 100644 --- a/web/admin/app.js +++ b/web/admin/app.js @@ -312,6 +312,9 @@ const app = createApp({ mounted() { console.log('Vue app mounted, authToken:', !!this.authToken, 'activeTab:', this.activeTab); + // 从URL参数中读取tab信息 + this.initializeTabFromUrl(); + // 初始化防抖函数 this.setTrendPeriod = this.debounce(this._setTrendPeriod, 300); @@ -325,6 +328,11 @@ const app = createApp({ } }); + // 监听浏览器前进后退按钮事件 + window.addEventListener('popstate', () => { + this.initializeTabFromUrl(); + }); + if (this.authToken) { this.isLoggedIn = true; @@ -390,6 +398,40 @@ const app = createApp({ }, methods: { + // 从URL读取tab参数并设置activeTab + initializeTabFromUrl() { + const urlParams = new URLSearchParams(window.location.search); + const tabParam = urlParams.get('tab'); + + // 检查tab参数是否有效 + const validTabs = this.tabs.map(tab => tab.key); + if (tabParam && validTabs.includes(tabParam)) { + this.activeTab = tabParam; + } + }, + + // 切换tab并更新URL + switchTab(tabKey) { + if (this.activeTab !== tabKey) { + this.activeTab = tabKey; + this.updateUrlTab(tabKey); + } + }, + + // 更新URL中的tab参数 + updateUrlTab(tabKey) { + const url = new URL(window.location.href); + if (tabKey === 'dashboard') { + // 如果是默认的dashboard标签,移除tab参数 + url.searchParams.delete('tab'); + } else { + url.searchParams.set('tab', tabKey); + } + + // 使用pushState更新URL但不刷新页面 + window.history.pushState({}, '', url.toString()); + }, + // 统一的API请求方法,处理token过期等错误 async apiRequest(url, options = {}) { try { diff --git a/web/admin/index.html b/web/admin/index.html index faf49691..dd79b742 100644 --- a/web/admin/index.html +++ b/web/admin/index.html @@ -208,7 +208,7 @@