更新下载进度,修复不能重新下载的问题

This commit is contained in:
2025-08-23 12:03:40 +08:00
parent 8d4e479ee1
commit b0179139cc
18 changed files with 1986 additions and 633 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ class ApiService {
constructor() {
this.client = axios.create({
baseURL: API_BASE_URL,
timeout: 30000,
timeout: 60000, // 增加到60秒
headers: {
'Content-Type': 'application/json',
},
+92 -45
View File
@@ -9,10 +9,60 @@ class DownloadService {
size?: string;
quality?: string;
format?: string;
skipExisting?: boolean;
} = {}) {
return apiService.post(`/api/download/artwork/${artworkId}`, options);
}
/**
* 获取任务进度
*/
async getTaskProgress(taskId: string) {
return apiService.get(`/api/download/progress/${taskId}`);
}
/**
* 获取所有任务
*/
async getAllTasks() {
return apiService.get('/api/download/tasks');
}
/**
* 暂停任务
*/
async pauseTask(taskId: string) {
return apiService.post(`/api/download/pause/${taskId}`);
}
/**
* 恢复任务
*/
async resumeTask(taskId: string) {
return apiService.post(`/api/download/resume/${taskId}`);
}
/**
* 取消任务
*/
async cancelTask(taskId: string) {
return apiService.delete(`/api/download/cancel/${taskId}`);
}
/**
* 获取下载历史
*/
async getDownloadHistory(offset = 0, limit = 50) {
return apiService.get('/api/download/history', { params: { offset, limit } });
}
/**
* 检查作品是否已下载
*/
async checkArtworkDownloaded(artworkId: number) {
return apiService.get(`/api/download/check/${artworkId}`);
}
/**
* 批量下载作品
*/
@@ -42,56 +92,12 @@ class DownloadService {
}
/**
* 获取任务进度
*/
async getTaskProgress(taskId: string) {
return apiService.get(`/api/download/progress/${taskId}`);
}
/**
* 获取所有任务
*/
async getAllTasks() {
return apiService.get('/api/download/tasks');
}
/**
* 取消下载任务
*/
async cancelTask(taskId: string) {
return apiService.post(`/api/download/cancel/${taskId}`);
}
/**
* 获取下载历史
*/
async getDownloadHistory(limit: number = 50, offset: number = 0) {
return apiService.get('/api/download/history', {
params: { limit, offset }
});
}
/**
* 获取下载的文件列表
* 获取已下载的文件列表
*/
async getDownloadedFiles() {
return apiService.get('/api/download/files');
}
/**
* 检查作品是否已下载
*/
async checkArtworkDownloaded(artworkId: number) {
return apiService.get(`/api/download/check/${artworkId}`);
}
/**
* 获取已下载的作品ID列表
*/
async getDownloadedArtworkIds() {
return apiService.get('/api/download/downloaded-ids');
}
/**
* 删除下载的文件
*/
@@ -100,6 +106,47 @@ class DownloadService {
data: { artist, artwork }
});
}
/**
* 获取已下载的作品ID列表
*/
async getDownloadedArtworkIds() {
return apiService.get('/api/download/downloaded-ids');
}
/**
* 使用SSE监听下载进度
*/
streamTaskProgress(taskId: string, onProgress: (task: DownloadTask) => void, onComplete?: () => void) {
const eventSource = new EventSource(`http://localhost:3000/api/download/stream/${taskId}`);
eventSource.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if (data.type === 'progress') {
onProgress(data.data);
} else if (data.type === 'complete') {
onProgress(data.data);
if (onComplete) {
onComplete();
}
eventSource.close();
}
} catch (error) {
console.error('解析SSE数据失败:', error);
}
};
eventSource.onerror = (error) => {
console.error('SSE连接错误:', error);
eventSource.close();
};
// 返回关闭函数
return () => {
eventSource.close();
};
}
}
export default new DownloadService();