From d2fc7841167292e47684393d985b38e0a299a526 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Fri, 21 Nov 2025 09:48:50 +0800 Subject: [PATCH] refactor(settings): delegate config UI updates to module --- app.js | 1 + src/core/connection.js | 73 ++----------------------- src/modules/settings.js | 117 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 71 deletions(-) diff --git a/app.js b/app.js index 6d19123..61dcf3d 100644 --- a/app.js +++ b/app.js @@ -148,6 +148,7 @@ class CLIProxyManager { init() { this.initUiVersion(); this.initializeTheme(); + this.registerSettingsListeners(); this.checkLoginStatus(); this.bindEvents(); this.setupNavigation(); diff --git a/src/core/connection.js b/src/core/connection.js index b5e22d4..5fbd205 100644 --- a/src/core/connection.js +++ b/src/core/connection.js @@ -349,78 +349,11 @@ export const connectionModule = { } }, - // 从配置对象更新所有设置 + // 从配置对象更新所有设置 —— 委派给 settings 模块,保持兼容旧调用 async updateSettingsFromConfig(config, keyStats = null) { - // 调试设置 - if (config.debug !== undefined) { - document.getElementById('debug-toggle').checked = config.debug; + if (typeof this.applySettingsFromConfig === 'function') { + return this.applySettingsFromConfig(config, keyStats); } - - // 代理设置 - if (config['proxy-url'] !== undefined) { - document.getElementById('proxy-url').value = config['proxy-url'] || ''; - } - - // 请求重试设置 - if (config['request-retry'] !== undefined) { - document.getElementById('request-retry').value = config['request-retry']; - } - - // 配额超出行为 - if (config['quota-exceeded']) { - if (config['quota-exceeded']['switch-project'] !== undefined) { - document.getElementById('switch-project-toggle').checked = config['quota-exceeded']['switch-project']; - } - if (config['quota-exceeded']['switch-preview-model'] !== undefined) { - document.getElementById('switch-preview-model-toggle').checked = config['quota-exceeded']['switch-preview-model']; - } - } - - if (config['usage-statistics-enabled'] !== undefined) { - const usageToggle = document.getElementById('usage-statistics-enabled-toggle'); - if (usageToggle) { - usageToggle.checked = config['usage-statistics-enabled']; - } - } - - // 日志记录设置 - if (config['logging-to-file'] !== undefined) { - const loggingToggle = document.getElementById('logging-to-file-toggle'); - if (loggingToggle) { - loggingToggle.checked = config['logging-to-file']; - } - // 显示或隐藏日志查看栏目 - this.toggleLogsNavItem(config['logging-to-file']); - } - if (config['request-log'] !== undefined) { - const requestLogToggle = document.getElementById('request-log-toggle'); - if (requestLogToggle) { - requestLogToggle.checked = config['request-log']; - } - } - if (config['ws-auth'] !== undefined) { - const wsAuthToggle = document.getElementById('ws-auth-toggle'); - if (wsAuthToggle) { - wsAuthToggle.checked = config['ws-auth']; - } - } - - // API 密钥 - if (config['api-keys']) { - this.renderApiKeys(config['api-keys']); - } - - // Gemini keys - await this.renderGeminiKeys(this.getGeminiKeysFromConfig(config), keyStats); - - // Codex 密钥 - await this.renderCodexKeys(Array.isArray(config['codex-api-key']) ? config['codex-api-key'] : [], keyStats); - - // Claude 密钥 - await this.renderClaudeKeys(Array.isArray(config['claude-api-key']) ? config['claude-api-key'] : [], keyStats); - - // OpenAI 兼容提供商 - await this.renderOpenAIProviders(Array.isArray(config['openai-compatibility']) ? config['openai-compatibility'] : [], keyStats); }, detectApiBaseFromLocation() { diff --git a/src/modules/settings.js b/src/modules/settings.js index 2197cc2..6e83e1d 100644 --- a/src/modules/settings.js +++ b/src/modules/settings.js @@ -275,6 +275,119 @@ export async function updateSwitchPreviewModel(enabled) { } } +// 统一应用配置到界面,供 connection 模块或事件总线调用 +export async function applySettingsFromConfig(config = {}, keyStats = null) { + if (!config || typeof config !== 'object') { + return; + } + + // 调试设置 + if (config.debug !== undefined) { + const toggle = document.getElementById('debug-toggle'); + if (toggle) { + toggle.checked = config.debug; + } + } + + // 代理设置 + if (config['proxy-url'] !== undefined) { + const proxyInput = document.getElementById('proxy-url'); + if (proxyInput) { + proxyInput.value = config['proxy-url'] || ''; + } + } + + // 请求重试设置 + if (config['request-retry'] !== undefined) { + const retryInput = document.getElementById('request-retry'); + if (retryInput) { + retryInput.value = config['request-retry']; + } + } + + // 配额超出行为 + if (config['quota-exceeded']) { + if (config['quota-exceeded']['switch-project'] !== undefined) { + const toggle = document.getElementById('switch-project-toggle'); + if (toggle) { + toggle.checked = config['quota-exceeded']['switch-project']; + } + } + if (config['quota-exceeded']['switch-preview-model'] !== undefined) { + const toggle = document.getElementById('switch-preview-model-toggle'); + if (toggle) { + toggle.checked = config['quota-exceeded']['switch-preview-model']; + } + } + } + + if (config['usage-statistics-enabled'] !== undefined) { + const usageToggle = document.getElementById('usage-statistics-enabled-toggle'); + if (usageToggle) { + usageToggle.checked = config['usage-statistics-enabled']; + } + } + + // 日志记录设置 + if (config['logging-to-file'] !== undefined) { + const loggingToggle = document.getElementById('logging-to-file-toggle'); + if (loggingToggle) { + loggingToggle.checked = config['logging-to-file']; + } + if (typeof this.toggleLogsNavItem === 'function') { + this.toggleLogsNavItem(config['logging-to-file']); + } + } + if (config['request-log'] !== undefined) { + const requestLogToggle = document.getElementById('request-log-toggle'); + if (requestLogToggle) { + requestLogToggle.checked = config['request-log']; + } + } + if (config['ws-auth'] !== undefined) { + const wsAuthToggle = document.getElementById('ws-auth-toggle'); + if (wsAuthToggle) { + wsAuthToggle.checked = config['ws-auth']; + } + } + + // API 密钥 + if (config['api-keys'] && typeof this.renderApiKeys === 'function') { + this.renderApiKeys(config['api-keys']); + } + + // Gemini keys + if (typeof this.renderGeminiKeys === 'function') { + await this.renderGeminiKeys(this.getGeminiKeysFromConfig(config), keyStats); + } + + // Codex 密钥 + if (typeof this.renderCodexKeys === 'function') { + await this.renderCodexKeys(Array.isArray(config['codex-api-key']) ? config['codex-api-key'] : [], keyStats); + } + + // Claude 密钥 + if (typeof this.renderClaudeKeys === 'function') { + await this.renderClaudeKeys(Array.isArray(config['claude-api-key']) ? config['claude-api-key'] : [], keyStats); + } + + // OpenAI 兼容提供商 + if (typeof this.renderOpenAIProviders === 'function') { + await this.renderOpenAIProviders(Array.isArray(config['openai-compatibility']) ? config['openai-compatibility'] : [], keyStats); + } +} + +// 设置模块订阅全局事件,减少与连接层耦合 +export function registerSettingsListeners() { + if (!this.events || typeof this.events.on !== 'function') { + return; + } + this.events.on('data:config-loaded', (event) => { + const detail = event?.detail || {}; + this.applySettingsFromConfig(detail.config || {}, detail.keyStats || null); + }); +} + export const settingsModule = { updateDebug, updateProxyUrl, @@ -292,5 +405,7 @@ export const settingsModule = { updateWsAuth, updateLoggingToFile, updateSwitchProject, - updateSwitchPreviewModel + updateSwitchPreviewModel, + applySettingsFromConfig, + registerSettingsListeners };