Files
DPM/src/utils/updateCheck.js
T

41 lines
1.3 KiB
JavaScript
Raw Normal View History

/* =========================================================
客户端更新检测(仅 Windows)
后端 POST /api/update/check 返回最新版本 + 安装包下载地址;
前端比对后提示,并提供下载(opener 打开安装包下载链接)。
========================================================= */
import { getApiBase } from '../config';
import { openUrl } from '@tauri-apps/plugin-opener';
// 当前版本:发布新版本时同步 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 || '',
};
}
} catch { /* 后端不可达时忽略 */ }
return null;
}
/** 打开安装包下载地址(优先系统浏览器下载)。 */
export function openUpdate(url) {
try {
openUrl(url);
} catch {
window.open(url, '_blank');
}
}