Files
AlgerMusicPlayer/src/renderer/utils/update.ts
T

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-01-10 22:49:55 +08:00
import { useDateFormat } from '@vueuse/core';
2025-01-01 13:12:46 +08:00
import axios from 'axios';
2025-01-10 22:49:55 +08:00
2025-01-01 13:12:46 +08:00
import config from '../../../package.json';
2025-01-10 22:49:55 +08:00
2025-01-01 13:12:46 +08:00
interface GithubReleaseInfo {
tag_name: string;
body: string;
published_at: string;
html_url: string;
assets: Array<{
browser_download_url: string;
name: string;
size: number;
}>;
}
export interface UpdateResult {
hasUpdate: boolean;
latestVersion: string;
currentVersion: string;
releaseInfo: {
tag_name: string;
body: string;
html_url: string;
assets: Array<{
browser_download_url: string;
name: string;
}>;
} | null;
}
/**
* 获取 GitHub 最新发布版本信息
*/
export const getLatestReleaseInfo = async (): Promise<GithubReleaseInfo | null> => {
try {
2025-01-02 00:45:01 +08:00
const token = import.meta.env.VITE_GITHUB_TOKEN;
const headers = {};
2025-01-04 16:13:37 +08:00
const apiUrls = [
// 原始地址
'https://api.github.com/repos/algerkong/AlgerMusicPlayer/releases/latest',
2025-01-10 22:49:55 +08:00
2025-01-04 16:13:37 +08:00
// 使用 ghproxy.com 代理
2025-01-10 22:49:55 +08:00
'https://www.ghproxy.cn/https://raw.githubusercontent.com/algerkong/AlgerMusicPlayer/dev_electron/package.json'
2025-01-04 16:13:37 +08:00
// 使用 gitee 镜像(如果有的话)
// 'https://gitee.com/api/v5/repos/[用户名]/AlgerMusicPlayer/releases/latest'
];
2025-01-02 00:45:01 +08:00
if (token) {
2025-01-10 22:49:55 +08:00
headers['Authorization'] = `token ${token}`;
2025-01-02 00:45:01 +08:00
}
2025-01-04 16:13:37 +08:00
for (const url of apiUrls) {
try {
const response = await axios.get(url, { headers });
if (url.includes('package.json')) {
// 如果是 package.json,直接读取版本号
return {
tag_name: response.data.version,
2025-01-10 22:49:55 +08:00
body: (
await axios.get(
'https://raw.githubusercontent.com/algerkong/AlgerMusicPlayer/dev_electron/CHANGELOG.md'
)
).data,
2025-01-04 16:13:37 +08:00
html_url: 'https://github.com/algerkong/AlgerMusicPlayer/releases/latest',
assets: []
} as unknown as GithubReleaseInfo;
}
return response.data;
} catch (err) {
console.warn(`尝试访问 ${url} 失败:`, err);
continue;
2025-01-02 00:45:01 +08:00
}
2025-01-01 13:12:46 +08:00
}
2025-01-04 16:13:37 +08:00
throw new Error('所有 API 地址均无法访问');
2025-01-01 13:12:46 +08:00
} catch (error) {
console.error('获取 GitHub Release 信息失败:', error);
return null;
}
};
/**
* 格式化时间
*/
2025-01-06 22:15:25 +08:00
export const formatDate = (dateStr: string): string => {
2025-01-01 13:12:46 +08:00
return useDateFormat(new Date(dateStr), 'YYYY-MM-DD HH:mm').value;
};
/**
* 检查更新
*/
2025-01-10 22:49:55 +08:00
export const checkUpdate = async (
currentVersion: string = config.version
): Promise<UpdateResult | null> => {
2025-01-01 13:12:46 +08:00
try {
const releaseInfo = await getLatestReleaseInfo();
2025-01-10 22:49:55 +08:00
console.log('releaseInfo', releaseInfo);
2025-01-01 13:12:46 +08:00
if (!releaseInfo) {
return null;
}
const latestVersion = releaseInfo.tag_name.replace('v', '');
if (latestVersion === currentVersion) {
return null;
}
2025-01-10 22:49:55 +08:00
console.log('latestVersion', latestVersion);
console.log('currentVersion', currentVersion);
2025-01-01 13:12:46 +08:00
return {
hasUpdate: true,
latestVersion,
currentVersion,
releaseInfo: {
tag_name: latestVersion,
2025-01-04 16:13:37 +08:00
body: `## 更新内容\n\n- 版本: ${latestVersion}\n${releaseInfo.body}`,
2025-01-01 13:12:46 +08:00
html_url: releaseInfo.html_url,
2025-01-10 22:49:55 +08:00
assets: releaseInfo.assets.map((asset) => ({
2025-01-01 13:12:46 +08:00
browser_download_url: asset.browser_download_url,
name: asset.name
}))
}
};
} catch (error) {
console.error('检查更新失败:', error);
return null;
}
2025-01-10 22:49:55 +08:00
};