diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index bbcd7fc..817356a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -16,6 +16,12 @@ pub struct AppSettings { pub repo_url: String, } +#[derive(Clone, Serialize, Deserialize)] +pub struct EssentialsRepo { + pub version: String, + pub essentials: Vec, +} + impl Default for AppSettings { fn default() -> Self { Self { @@ -99,15 +105,15 @@ async fn sync_essentials(app: AppHandle) -> Result { Ok(response) => { if response.status().is_success() { let content = response.text().await.map_err(|e| e.to_string())?; - // 验证 JSON 格式 - let validation: Result, _> = serde_json::from_str(&content); + // 验证 JSON 格式(新格式:{ version: string, essentials: Vec }) + let validation: Result = serde_json::from_str(&content); if validation.is_ok() { let path = get_essentials_path(&app); fs::write(path, content).map_err(|e| e.to_string())?; emit_log(&app, "sync-essentials", "Result", "Essentials list updated successfully.", "success"); Ok(true) } else { - emit_log(&app, "sync-essentials", "Error", "Invalid JSON format from repository.", "error"); + emit_log(&app, "sync-essentials", "Error", "Invalid JSON format from repository. Expected { version, essentials }.", "error"); Err("Invalid JSON format".to_string()) } } else { @@ -118,7 +124,7 @@ async fn sync_essentials(app: AppHandle) -> Result { } Err(e) => { emit_log(&app, "sync-essentials", "Skipped", &format!("Network issue: {}. Using local cache.", e), "info"); - Ok(false) // 静默失败,返回 false 表示未更新但可继续 + Ok(false) } } } @@ -132,14 +138,14 @@ async fn initialize_app(app: AppHandle) -> Result { } #[tauri::command] -fn get_essentials(app: AppHandle) -> Vec { +fn get_essentials(app: AppHandle) -> Option { let file_path = get_essentials_path(&app); if !file_path.exists() { - return vec![]; + return None; } - let content = fs::read_to_string(file_path).unwrap_or_else(|_| "[]".to_string()); - serde_json::from_str(&content).unwrap_or_default() + let content = fs::read_to_string(file_path).unwrap_or_default(); + serde_json::from_str(&content).ok() } #[tauri::command] diff --git a/src/store/software.ts b/src/store/software.ts index d0b34a9..29eb43d 100644 --- a/src/store/software.ts +++ b/src/store/software.ts @@ -15,6 +15,7 @@ export interface LogEntry { export const useSoftwareStore = defineStore('software', { state: () => ({ essentials: [] as any[], + essentialsVersion: '', updates: [] as any[], allSoftware: [] as any[], selectedEssentialIds: [] as string[], @@ -117,7 +118,14 @@ export const useSoftwareStore = defineStore('software', { }, async fetchEssentials() { - this.essentials = await invoke('get_essentials') + const res = await invoke('get_essentials') as any; + if (res) { + this.essentials = res.essentials; + this.essentialsVersion = res.version; + } else { + this.essentials = []; + this.essentialsVersion = ''; + } }, async fetchUpdates() { if (this.isBusy) return; @@ -156,12 +164,20 @@ export const useSoftwareStore = defineStore('software', { // 在获取全量数据之前,先同步云端清单 await invoke('sync_essentials').catch(() => {}); - const [essentials, all, updates] = await Promise.all([ - invoke('get_essentials'), + const [repo, all, updates] = await Promise.all([ + invoke('get_essentials') as Promise, invoke('get_all_software'), invoke('get_updates') ]); - this.essentials = essentials as any[]; + + if (repo) { + this.essentials = repo.essentials; + this.essentialsVersion = repo.version; + } else { + this.essentials = []; + this.essentialsVersion = ''; + } + this.allSoftware = all as any[]; this.updates = updates as any[]; this.lastFetched = Date.now(); diff --git a/src/views/Essentials.vue b/src/views/Essentials.vue index 34fb577..8a00abc 100644 --- a/src/views/Essentials.vue +++ b/src/views/Essentials.vue @@ -3,6 +3,7 @@

装机必备

+ 版本: {{ store.essentialsVersion }}