diff --git a/app.js b/app.js index cbe5c86..df7694d 100644 --- a/app.js +++ b/app.js @@ -67,6 +67,7 @@ class CLIProxyManager { // 状态更新定时器 this.statusUpdateTimer = null; + this.lastConnectionStatusEmitted = null; // 日志自动刷新定时器 this.logsRefreshTimer = null; @@ -150,6 +151,9 @@ class CLIProxyManager { this.initializeTheme(); this.registerSettingsListeners(); this.registerUsageListeners(); + if (typeof this.registerLogsListeners === 'function') { + this.registerLogsListeners(); + } if (typeof this.registerConfigEditorListeners === 'function') { this.registerConfigEditorListeners(); } @@ -164,7 +168,6 @@ class CLIProxyManager { this.updateLoginConnectionInfo(); // 检查主机名,如果不是 localhost 或 127.0.0.1,则隐藏 OAuth 登录框 this.checkHostAndHideOAuth(); - if (typeof this.registerAuthFilesListeners === 'function') { this.registerAuthFilesListeners(); } diff --git a/src/core/connection.js b/src/core/connection.js index f1d0ff7..8c7ead6 100644 --- a/src/core/connection.js +++ b/src/core/connection.js @@ -215,10 +215,14 @@ export const connectionModule = { this.updateConnectionInfo(); if (this.events && typeof this.events.emit === 'function') { - this.events.emit('connection:status-changed', { - isConnected: this.isConnected, - apiBase: this.apiBase - }); + const shouldEmit = this.lastConnectionStatusEmitted !== this.isConnected; + if (shouldEmit) { + this.events.emit('connection:status-changed', { + isConnected: this.isConnected, + apiBase: this.apiBase + }); + this.lastConnectionStatusEmitted = this.isConnected; + } } }, @@ -325,9 +329,6 @@ export const connectionModule = { // 从配置中提取并设置各个设置项(现在传递keyStats) await this.updateSettingsFromConfig(config, keyStats); - // 认证文件需要单独加载,因为不在配置中 - await this.loadAuthFiles(keyStats); - if (this.events && typeof this.events.emit === 'function') { this.events.emit('data:config-loaded', { config, diff --git a/src/modules/logs.js b/src/modules/logs.js index 833e914..a83fdc7 100644 --- a/src/modules/logs.js +++ b/src/modules/logs.js @@ -406,5 +406,29 @@ export const logsModule = { } this.showNotification(i18n.t('logs.auto_refresh_disabled'), 'info'); } + }, + + registerLogsListeners() { + if (!this.events || typeof this.events.on !== 'function') { + return; + } + this.events.on('connection:status-changed', (event) => { + const detail = event?.detail || {}; + if (detail.isConnected) { + // 仅在日志页激活时刷新,避免非日志页面触发请求 + const logsSection = document.getElementById('logs'); + if (logsSection && logsSection.classList.contains('active')) { + this.refreshLogs(false); + } + } else { + this.latestLogTimestamp = null; + } + }); + this.events.on('navigation:section-activated', (event) => { + const detail = event?.detail || {}; + if (detail.sectionId === 'logs' && this.isConnected) { + this.refreshLogs(false); + } + }); } }; diff --git a/src/modules/navigation.js b/src/modules/navigation.js index acd29cd..bf205ac 100644 --- a/src/modules/navigation.js +++ b/src/modules/navigation.js @@ -15,12 +15,13 @@ export const navigationModule = { section.classList.add('active'); } - if (sectionId === 'logs') { - this.refreshLogs(false); - } else if (sectionId === 'config-management') { + if (sectionId === 'config-management') { this.loadConfigFileEditor(); this.refreshConfigEditor(); } + if (this.events && typeof this.events.emit === 'function') { + this.events.emit('navigation:section-activated', { sectionId }); + } }); }); },