feat: 添加安装更新功能,支持下载并安装更新包;优化双屏控制逻辑,增强错误处理

This commit is contained in:
Pine
2026-08-19 13:40:00 +08:00
parent c9460c8d6e
commit fd6493f582
4 changed files with 132 additions and 68 deletions
+14 -11
View File
@@ -1,10 +1,10 @@
/* =========================================================
客户端更新检测(仅 Windows)
客户端更新检测与安装(仅 Windows
后端 POST /api/update/check 返回最新版本 + 安装包下载地址;
前端比对后提示,并提供下载(opener 打开安装包下载链接)。
前端 fetch 下载安装包字节 → invoke install_update(写盘 → 启动安装程序 → 退出应用)。
========================================================= */
import { getApiBase } from '../config';
import { openUrl } from '@tauri-apps/plugin-opener';
import { invoke } from '@tauri-apps/api/core';
// 当前版本:发布新版本时同步 package.json 的 version
export const APP_VERSION = '0.1.0';
@@ -23,18 +23,21 @@ export async function checkForUpdate() {
latest: data.latest_version,
available: !!data.update_available,
url: `${getApiBase()}${data.download_url || '/download'}`,
name: data.installer_name || '',
name: data.installer_name || 'dpm_update_setup.exe',
};
}
} catch { /* 后端不可达时忽略 */ }
return null;
}
/** 打开安装包下载地址(优先系统浏览器下载)。 */
export function openUpdate(url) {
try {
openUrl(url);
} catch {
window.open(url, '_blank');
}
/** 下载安装包并交给后端安装(写盘 → 启动安装程序 → 退出应用)。 */
export async function downloadAndInstall(url, filename) {
const res = await fetch(url);
if (!res.ok) throw new Error(`下载失败(HTTP ${res.status}`);
const buf = new Uint8Array(await res.arrayBuffer());
if (buf.length < 1000) throw new Error('安装包内容无效');
return invoke('install_update', {
bytes: Array.from(buf),
filename: filename || 'dpm_update_setup.exe',
});
}