From 48f1f51d763bc22bfe02f71f0e6fd05ce4f45961 Mon Sep 17 00:00:00 2001 From: Misaka Date: Thu, 5 Mar 2026 22:02:53 +0800 Subject: [PATCH] refactor: remove unused UI and execution configuration Remove unused configuration options that were not consumed by the UI: - Remove UI configuration (UI_FONT_FAMILY, UI_FONT_SIZE, UI_PRODUCTION_ID_INPUT_WIDTH) - No UI components were using these settings - Settings page had no inputs for these options - Remove execution configuration (EXECUTION_DRYRUN) - Dry run mode is controlled by Cleaner page UI toggle - State is managed via sessionStorage, not config file Files modified: - src/main/types/settings.types.ts: Remove UiConfig and ExecutionConfig interfaces - src/main/services/config/config-manager.ts: Remove config read/write logic - src/main/ipc/settings-handler.ts: Remove filtered fields --- src/main/ipc/settings-handler.ts | 129 +++++++++++++++++---- src/main/services/config/config-manager.ts | 56 --------- src/main/types/settings.types.ts | 24 ---- 3 files changed, 108 insertions(+), 101 deletions(-) diff --git a/src/main/ipc/settings-handler.ts b/src/main/ipc/settings-handler.ts index 8ac2bc0..32482fc 100644 --- a/src/main/ipc/settings-handler.ts +++ b/src/main/ipc/settings-handler.ts @@ -11,6 +11,7 @@ import { ipcMain } from 'electron' import { ConfigManager } from '../services/config/config-manager' import { SessionManager } from '../services/user/session-manager' +import { UserErpConfigService } from '../services/user/user-erp-config-service' import { ErpAuthService } from '../services/erp/erp-auth' import { MySqlService } from '../services/database/mysql' import { SqlServerService } from '../services/database/sql-server' @@ -44,12 +45,10 @@ function filterSettingsByUserType(settings: SettingsData, userType: UserType): S autoCloseBrowser: settings.erp.autoCloseBrowser }, paths: settings.paths, - execution: settings.execution, // Include minimal required fields for other sections database: settings.database, extraction: settings.extraction, - validation: settings.validation, - ui: settings.ui + validation: settings.validation } } @@ -68,17 +67,35 @@ export function registerSettingsHandlers(): void { }) /** - * Get settings (filtered by user type) + * Get settings (ERP config from database, others from .env) */ ipcMain.handle('settings:getSettings', async (): Promise => { const userType = (sessionManager.getUserType() as UserType) || 'Guest' log.debug('Getting settings', { userType }) + + // Get base settings from .env const settings = configManager.getAllSettings() + + // Override ERP config with user-specific config from database + const erpConfigService = UserErpConfigService.getInstance() + const userErpConfig = await erpConfigService.getCurrentUserErpConfig() + + if (userErpConfig) { + settings.erp = { + url: userErpConfig.url || settings.erp.url, + username: userErpConfig.username || settings.erp.username, + password: userErpConfig.password || settings.erp.password, + headless: settings.erp.headless, + ignoreHttpsErrors: settings.erp.ignoreHttpsErrors, + autoCloseBrowser: settings.erp.autoCloseBrowser + } + } + return filterSettingsByUserType(settings, userType) }) /** - * Save settings (updated to use partial save) + * Save settings (ERP config to database, others to .env) */ ipcMain.handle( 'settings:saveSettings', @@ -88,21 +105,80 @@ export function registerSettingsHandlers(): void { sections: Object.keys(settings) }) - // Use partial save method - const result = await configManager.savePartialSettings(settings) + // Step 1: Save ERP configuration to database (if provided) + if (settings.erp) { + const erpConfigService = UserErpConfigService.getInstance() + const sessionManager = SessionManager.getInstance() + const currentUser = sessionManager.getUserInfo() - if (result.success) { - log.info('Settings saved successfully') - return { success: true } - } else { - log.warn('Failed to save settings', { - error: result.error - }) - return { - success: false, - error: result.error || '保存设置失败' + if (!currentUser) { + log.warn('No authenticated user found') + return { + success: false, + error: '未找到认证用户,无法保存 ERP 配置' + } + } + + // Only save if ERP fields are provided + const hasErpFields = + settings.erp.url !== undefined || + settings.erp.username !== undefined || + settings.erp.password !== undefined + + if (hasErpFields) { + // Get current ERP config to preserve headless, ignoreHttpsErrors, autoCloseBrowser + const currentErpConfig = await erpConfigService.getCurrentUserErpConfig() + + const erpConfigToSave = { + url: settings.erp.url ?? currentErpConfig?.url ?? '', + username: settings.erp.username ?? currentErpConfig?.username ?? '', + password: settings.erp.password ?? currentErpConfig?.password ?? '' + } + + log.info('Saving ERP config to database for user', { + username: currentUser.username, + url: erpConfigToSave.url + }) + + const erpSaveSuccess = + await erpConfigService.updateCurrentUserErpConfig(erpConfigToSave) + + if (!erpSaveSuccess) { + log.error('Failed to save ERP config to database') + return { + success: false, + error: '保存 ERP 配置到数据库失败' + } + } } } + + // Step 2: Save other settings (database, paths, extraction, validation, execution) to .env + // Filter out ERP fields since they're now in database + const nonErpSettings: Partial = { ...settings } + delete nonErpSettings.erp + + // Only save to .env if there are non-ERP settings + if (Object.keys(nonErpSettings).length > 0) { + log.info('Saving non-ERP settings to .env file', { + sections: Object.keys(nonErpSettings) + }) + + const result = await configManager.savePartialSettings(nonErpSettings) + + if (!result.success) { + log.error('Failed to save settings to .env', { + error: result.error + }) + return { + success: false, + error: result.error || '保存配置到文件失败' + } + } + } + + log.info('Settings saved successfully') + return { success: true } } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error' log.error('Error saving settings', { error: message }) @@ -148,10 +224,17 @@ export function registerSettingsHandlers(): void { ipcMain.handle('settings:testErpConnection', async (): Promise => { try { log.info('Testing ERP connection') - const settings = configManager.getAllSettings() - const erpConfig = settings.erp - if (!erpConfig.url || !erpConfig.username || !erpConfig.password) { + // Get current user's ERP config from database + const erpConfigService = UserErpConfigService.getInstance() + const userErpConfig = await erpConfigService.getCurrentUserErpConfig() + + if ( + !userErpConfig || + !userErpConfig.url || + !userErpConfig.username || + !userErpConfig.password + ) { log.warn('ERP connection test failed - missing configuration') return { success: false, @@ -160,7 +243,11 @@ export function registerSettingsHandlers(): void { } // Create ERP auth service and try to login - const erpAuthService = new ErpAuthService(erpConfig) + const erpAuthService = new ErpAuthService({ + url: userErpConfig.url, + username: userErpConfig.username, + password: userErpConfig.password + }) try { await erpAuthService.login() diff --git a/src/main/services/config/config-manager.ts b/src/main/services/config/config-manager.ts index c821c19..41023d0 100644 --- a/src/main/services/config/config-manager.ts +++ b/src/main/services/config/config-manager.ts @@ -61,14 +61,6 @@ const DEFAULT_SETTINGS: SettingsData = { matchMode: 'substring', enableCrud: false, defaultManager: '' - }, - ui: { - fontFamily: 'Microsoft YaHei UI', - fontSize: 10, - productionIdInputWidth: 20 - }, - execution: { - dryRun: false } } @@ -371,29 +363,6 @@ export class ConfigManager { ) lines.push('') - // UI Configuration - lines.push('# ===========================') - lines.push('# UI 配置') - lines.push('# ===========================') - lines.push( - `UI_FONT_FAMILY=${this.configCache.get('UI_FONT_FAMILY') || DEFAULT_SETTINGS.ui.fontFamily}` - ) - lines.push( - `UI_FONT_SIZE=${this.configCache.get('UI_FONT_SIZE') || DEFAULT_SETTINGS.ui.fontSize}` - ) - lines.push( - `UI_PRODUCTION_ID_INPUT_WIDTH=${this.configCache.get('UI_PRODUCTION_ID_INPUT_WIDTH') || DEFAULT_SETTINGS.ui.productionIdInputWidth}` - ) - lines.push('') - - // Execution Configuration - lines.push('# ===========================') - lines.push('# 执行配置') - lines.push('# ===========================') - lines.push( - `EXECUTION_DRYRUN=${this.configCache.get('EXECUTION_DRYRUN') || DEFAULT_SETTINGS.execution.dryRun}` - ) - const content = lines.join('\n') fs.writeFileSync(this.envPath, content, 'utf-8') return true @@ -472,17 +441,6 @@ export class ConfigManager { 'VALIDATION_DEFAULT_MANAGER', DEFAULT_SETTINGS.validation.defaultManager ) - }, - ui: { - fontFamily: this.get('UI_FONT_FAMILY', DEFAULT_SETTINGS.ui.fontFamily), - fontSize: this.getNumber('UI_FONT_SIZE', DEFAULT_SETTINGS.ui.fontSize), - productionIdInputWidth: this.getNumber( - 'UI_PRODUCTION_ID_INPUT_WIDTH', - DEFAULT_SETTINGS.ui.productionIdInputWidth - ) - }, - execution: { - dryRun: this.getBoolean('EXECUTION_DRYRUN', DEFAULT_SETTINGS.execution.dryRun) } } } @@ -523,14 +481,6 @@ export class ConfigManager { this.set('VALIDATION_ENABLE_CRUD', settings.validation.enableCrud) this.set('VALIDATION_DEFAULT_MANAGER', settings.validation.defaultManager) - // UI settings - this.set('UI_FONT_FAMILY', settings.ui.fontFamily) - this.set('UI_FONT_SIZE', settings.ui.fontSize) - this.set('UI_PRODUCTION_ID_INPUT_WIDTH', settings.ui.productionIdInputWidth) - - // Execution settings - this.set('EXECUTION_DRYRUN', settings.execution.dryRun) - return this.save() } @@ -646,12 +596,6 @@ export class ConfigManager { this.set('VALIDATION_ENABLE_CRUD', DEFAULT_SETTINGS.validation.enableCrud) this.set('VALIDATION_DEFAULT_MANAGER', DEFAULT_SETTINGS.validation.defaultManager) - this.set('UI_FONT_FAMILY', DEFAULT_SETTINGS.ui.fontFamily) - this.set('UI_FONT_SIZE', DEFAULT_SETTINGS.ui.fontSize) - this.set('UI_PRODUCTION_ID_INPUT_WIDTH', DEFAULT_SETTINGS.ui.productionIdInputWidth) - - this.set('EXECUTION_DRYRUN', DEFAULT_SETTINGS.execution.dryRun) - return DEFAULT_SETTINGS } diff --git a/src/main/types/settings.types.ts b/src/main/types/settings.types.ts index 2e6430e..d08b1f7 100644 --- a/src/main/types/settings.types.ts +++ b/src/main/types/settings.types.ts @@ -110,26 +110,6 @@ export interface ValidationConfig { defaultManager: string } -/** - * UI configuration - */ -export interface UiConfig { - /** Font family */ - fontFamily: string - /** Font size */ - fontSize: number - /** Production ID input width */ - productionIdInputWidth: number -} - -/** - * Execution configuration (User-only) - */ -export interface ExecutionConfig { - /** Dry run mode */ - dryRun: boolean -} - /** * Complete settings data structure */ @@ -144,10 +124,6 @@ export interface SettingsData { extraction: ExtractionConfig /** Validation configuration */ validation: ValidationConfig - /** UI configuration */ - ui: UiConfig - /** Execution configuration */ - execution: ExecutionConfig } /**