mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-19 11:10:49 +08:00
refactor(settings): delegate config UI updates to module
This commit is contained in:
1
app.js
1
app.js
@@ -148,6 +148,7 @@ class CLIProxyManager {
|
|||||||
init() {
|
init() {
|
||||||
this.initUiVersion();
|
this.initUiVersion();
|
||||||
this.initializeTheme();
|
this.initializeTheme();
|
||||||
|
this.registerSettingsListeners();
|
||||||
this.checkLoginStatus();
|
this.checkLoginStatus();
|
||||||
this.bindEvents();
|
this.bindEvents();
|
||||||
this.setupNavigation();
|
this.setupNavigation();
|
||||||
|
|||||||
@@ -349,78 +349,11 @@ export const connectionModule = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// 从配置对象更新所有设置
|
// 从配置对象更新所有设置 —— 委派给 settings 模块,保持兼容旧调用
|
||||||
async updateSettingsFromConfig(config, keyStats = null) {
|
async updateSettingsFromConfig(config, keyStats = null) {
|
||||||
// 调试设置
|
if (typeof this.applySettingsFromConfig === 'function') {
|
||||||
if (config.debug !== undefined) {
|
return this.applySettingsFromConfig(config, keyStats);
|
||||||
document.getElementById('debug-toggle').checked = config.debug;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 代理设置
|
|
||||||
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() {
|
detectApiBaseFromLocation() {
|
||||||
|
|||||||
@@ -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 = {
|
export const settingsModule = {
|
||||||
updateDebug,
|
updateDebug,
|
||||||
updateProxyUrl,
|
updateProxyUrl,
|
||||||
@@ -292,5 +405,7 @@ export const settingsModule = {
|
|||||||
updateWsAuth,
|
updateWsAuth,
|
||||||
updateLoggingToFile,
|
updateLoggingToFile,
|
||||||
updateSwitchProject,
|
updateSwitchProject,
|
||||||
updateSwitchPreviewModel
|
updateSwitchPreviewModel,
|
||||||
|
applySettingsFromConfig,
|
||||||
|
registerSettingsListeners
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user