//获取任务信息
const getTasks = async () => {
try {
const activeResponse = await axios.post('http://localhost:6800/jsonrpc', {
jsonrpc: '2.0',
id: '1',
method: 'aria2.tellActive',
params: [],
});
const activeTasks = activeResponse.data.result.map((task) => ({
gid: task.gid,
name: task.files[0].path.split('/').pop().split('.').slice(0, -1).join('.'),
completedLength: formatBytes(parseFloat(task.completedLength)),
totalLength: formatBytes(parseFloat(task.totalLength)),
downloadSpeed: formatBytes(parseFloat(task.downloadSpeed)) + '/s',
downloadLink: task.files[0].uris.map((uri) => uri.uri),
progress: isNaN(parseInt(Math.round((parseFloat(task.completedLength) / parseFloat(task.totalLength)) * 100))) ? 0 : parseInt(Math.round((parseFloat(task.completedLength) / parseFloat(task.totalLength)) * 100)),
status: task.status,
}));
const allTasks = activeTasks.concat(waitingTasks);
tasks.value = allTasks;
allTasks.forEach(task => {
DownloadCP(task)
});
return tasks;
} catch (error) {
console.error(error);
}
};
setInterval(getTasks, 1000)
//任务下载完成后操做
const DownloadCP = async (task) => {
try {
const status = await axios.post('http://localhost:6800/jsonrpc', {
jsonrpc: '2.0',
id: '1',
method: 'aria2.tellStatus',
params: [task.gid, ['status']],
});
if (status.data.result.status == 'complete') {
ElMessage({
message: "下载完成",
type: "success",
});
} else {
}
} catch (error) {
console.error(error);
}
};
请大家帮我看看这个代码怎么优化,因为我取任务状态使用了1秒的计时器。所以任务在下载完后,并不能百分百的获取任务完成得下载结果。怎么才能在下载完成后,第一时间返回下载结果给DownloadCP。