Files
DPM/src/utils/updateCheck.js
T

44 lines
1.7 KiB
JavaScript
Raw Normal View History

/* =========================================================
客户端更新检测与安装(仅 Windows)
后端 POST /api/update/check 返回最新版本 + 安装包下载地址;
前端 fetch 下载安装包字节 → invoke install_update(写盘 → 启动安装程序 → 退出应用)。
========================================================= */
import { getApiBase } from '../config';
import { invoke } from '@tauri-apps/api/core';
// 当前版本:发布新版本时同步 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'}`,
name: data.installer_name || 'dpm_update_setup.exe',
};
}
} catch { /* 后端不可达时忽略 */ }
return null;
}
/** 下载安装包并交给后端安装(写盘 → 启动安装程序 → 退出应用)。 */
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',
});
}