diff --git a/src-tauri/src/commands/config_cmd.rs b/src-tauri/src/commands/config_cmd.rs index 3d3f3d925..5c5d26cf9 100644 --- a/src-tauri/src/commands/config_cmd.rs +++ b/src-tauri/src/commands/config_cmd.rs @@ -38,7 +38,7 @@ pub fn get_config_status(app_type: String) -> Result { AppType::Claude => config_dir.join("settings.json"), AppType::Codex => config_dir.join("auth.json"), AppType::Gemini => config_dir.join(".env"), - AppType::ProxyCast => config_dir.join("config.json"), + AppType::ProxyCast => config_dir.join("config.yaml"), }; let has_env = match app { @@ -50,11 +50,20 @@ pub fn get_config_status(app_type: String) -> Result { } AppType::Codex => config_dir.join("auth.json").exists(), AppType::Gemini => config_dir.join(".env").exists(), - AppType::ProxyCast => config_dir.join("config.json").exists(), + AppType::ProxyCast => { + config_dir.join("config.yaml").exists() || config_dir.join("config.json").exists() + } + }; + + let exists = match app { + AppType::ProxyCast => { + config_dir.join("config.yaml").exists() || config_dir.join("config.json").exists() + } + _ => main_config.exists(), }; Ok(ConfigStatus { - exists: main_config.exists(), + exists, path: config_dir.to_string_lossy().to_string(), has_env, }) diff --git a/src-tauri/src/config/yaml.rs b/src-tauri/src/config/yaml.rs index 719d909c5..b2bbda689 100644 --- a/src-tauri/src/config/yaml.rs +++ b/src-tauri/src/config/yaml.rs @@ -678,8 +678,12 @@ pub fn load_config() -> Result> { Ok(Config::default()) } -/// 保存配置(向后兼容,使用 JSON 格式) +/// 保存配置(同时写入 YAML 与 JSON,兼容旧版) pub fn save_config(config: &Config) -> Result<(), Box> { + // 主配置优先写入 YAML + save_config_yaml(config)?; + + // 兼容旧版 JSON 配置 let path = json_config_path(); if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; diff --git a/src-tauri/src/database/migration.rs b/src-tauri/src/database/migration.rs index abde27d49..d0b748bdf 100644 --- a/src-tauri/src/database/migration.rs +++ b/src-tauri/src/database/migration.rs @@ -17,16 +17,11 @@ pub fn migrate_from_json(conn: &Connection) -> Result<(), String> { return Ok(()); } - // 读取旧配置文件(兼容历史路径) - let mut legacy_paths = Vec::new(); - if let Some(config_dir) = dirs::config_dir() { - legacy_paths.push(config_dir.join("proxycast").join("config.json")); - } - if let Some(home) = dirs::home_dir() { - legacy_paths.push(home.join(".proxycast").join("config.json")); - } + // 读取旧配置文件(历史路径) + let home = dirs::home_dir().ok_or_else(|| "无法获取主目录".to_string())?; + let config_path = home.join(".proxycast").join("config.json"); - if let Some(config_path) = legacy_paths.into_iter().find(|path| path.exists()) { + if config_path.exists() { // 备份旧配置,避免误覆盖 let backup_path = config_path.with_file_name("config.json.backup"); if !backup_path.exists() { @@ -34,10 +29,10 @@ pub fn migrate_from_json(conn: &Connection) -> Result<(), String> { .map_err(|e| format!("备份旧配置失败: {}", e))?; } - return Err(format!( - "检测到旧版 config.json({}),当前版本尚未支持自动迁移。请手动导出/重建配置后再启动。", - config_path.display() - )); + return Err( + "检测到旧版 config.json(~/.proxycast/config.json),当前版本尚未支持自动迁移。请手动导出/重建配置后再启动。" + .to_string(), + ); } // 标记迁移完成 diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 907ef2daa..07c3224f3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1365,7 +1365,7 @@ pub fn run() { if config.server.api_key == config::DEFAULT_API_KEY { let new_key = generate_api_key(); config.server.api_key = new_key.clone(); - if let Err(err) = config::save_config_yaml(&config) { + if let Err(err) = config::save_config(&config) { tracing::error!("自动生成 API key 失败,无法保存配置,已中止启动: {}", err); eprintln!("自动生成 API key 失败,无法保存配置,已中止启动: {}", err); return;