2026-08-19 13:14:00 +08:00
|
|
|
|
/* =========================================================
|
2026-08-19 13:40:00 +08:00
|
|
|
|
客户端更新检测与安装(仅 Windows)
|
2026-08-19 13:14:00 +08:00
|
|
|
|
后端 POST /api/update/check 返回最新版本 + 安装包下载地址;
|
2026-08-19 13:40:00 +08:00
|
|
|
|
前端 fetch 下载安装包字节 → invoke install_update(写盘 → 启动安装程序 → 退出应用)。
|
2026-08-19 13:14:00 +08:00
|
|
|
|
========================================================= */
|
|
|
|
|
|
import { getApiBase } from '../config';
|
2026-08-19 13:40:00 +08:00
|
|
|
|
import { invoke } from '@tauri-apps/api/core';
|
2026-08-19 13:14:00 +08:00
|
|
|
|
|
|
|
|
|
|
// 当前版本:发布新版本时同步 package.json 的 version
|
|
|
|
|
|
export const APP_VERSION = '0.1.0';
|
|
|
|
|
|
|
|
|
|
|
|
export async function checkForUpdate() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch(`${getApiBase()}/api/update/check`, {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
body: JSON.stringify({ current: APP_VERSION }),
|
|
|
|
|
|
});
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
if (data && data.ok) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
current: APP_VERSION,
|
|
|
|
|
|
latest: data.latest_version,
|
|
|
|
|
|
available: !!data.update_available,
|
|
|
|
|
|
url: `${getApiBase()}${data.download_url || '/download'}`,
|
2026-08-19 13:40:00 +08:00
|
|
|
|
name: data.installer_name || 'dpm_update_setup.exe',
|
2026-08-19 13:14:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch { /* 后端不可达时忽略 */ }
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-19 13:40:00 +08:00
|
|
|
|
/** 下载安装包并交给后端安装(写盘 → 启动安装程序 → 退出应用)。 */
|
|
|
|
|
|
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',
|
|
|
|
|
|
});
|
2026-08-19 13:14:00 +08:00
|
|
|
|
}
|