feat(task): add batch delete and cancel API support

支持批量删除和取消任务的接口:
- deleteTask 接口支持 POST 方式传入 ids 数组
- cancelTask 接口支持 POST 方式传入 ids 数组
- 前端批量操作改为调用批量接口,减少请求数量

Co-Authored-By: AI
This commit is contained in:
2026-04-07 17:38:52 +08:00
parent aa59a4559f
commit e8f71b04ac
3 changed files with 148 additions and 41 deletions
+20
View File
@@ -73,7 +73,27 @@ export const deleteTask = async (id: number) => {
if (!res.success) throw new Error(res.message)
}
/** 批量删除任务 */
export const deleteTasks = async (ids: number[]) => {
const res = await fetch('/api/deleteTask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids })
}).then(res => res.json()) as ResJSON
if (!res.success) throw new Error(res.message)
}
export const cancelTask = async (id: number) => {
const res = await fetch(`/api/cancelTask?id=${id}`).then(res => res.json()) as ResJSON
if (!res.success) throw new Error(res.message)
}
/** 批量取消任务 */
export const cancelTasks = async (ids: number[]) => {
const res = await fetch('/api/cancelTask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids })
}).then(res => res.json()) as ResJSON
if (!res.success) throw new Error(res.message)
}
+11 -13
View File
@@ -1,7 +1,7 @@
import van, { State } from 'vanjs-core'
import { Route, goto, now } from 'vanjs-router'
import { checkLogin, GLOBAL_HAS_LOGIN, GLOBAL_HIDE_PAGE, ResJSON, VanComponent } from '../mixin'
import { deleteTask, getActiveTask, getTaskList, showFile, cancelTask } from './data'
import { deleteTask, getActiveTask, getTaskList, showFile, cancelTask, deleteTasks, cancelTasks } from './data'
import { TaskInDB, TaskStatus } from '../work/type'
import { LoadingBox } from '../view'
import { PlayerModalComp } from './playerModal'
@@ -86,13 +86,14 @@ export class TaskRoute implements VanComponent {
hidden: () => _that.selectedCancellable.val.length == 0,
onclick() {
if (!confirm(`确定要取消选中的 ${_that.selectedCancellable.val.length} 个任务吗?`)) return
_that.selectedCancellable.val.forEach(t => {
cancelTask(t.id).then(() => {
const ids = _that.selectedCancellable.val.map(t => t.id)
cancelTasks(ids).then(() => {
_that.selectedCancellable.val.forEach(t => {
t.statusState.val = 'error'
t.selected.val = false
}).catch(error => {
alert(error.message)
})
}).catch(error => {
alert(error.message)
})
}
}, () => `批量取消 (${_that.selectedCancellable.val.length})`),
@@ -101,14 +102,11 @@ export class TaskRoute implements VanComponent {
hidden: () => _that.selectedDeletable.val.length == 0,
onclick() {
if (!confirm(`确定要删除选中的 ${_that.selectedDeletable.val.length} 个任务吗?`)) return
_that.selectedDeletable.val.forEach(t => {
t.deleting.val = true
deleteTask(t.id).then(() => {
_that.taskList.val = _that.taskList.val.filter(taskInDB => taskInDB.id != t.id)
}).catch(error => {
t.deleting.val = false
alert(error.message)
})
const ids = _that.selectedDeletable.val.map(t => t.id)
deleteTasks(ids).then(() => {
_that.taskList.val = _that.taskList.val.filter(taskInDB => !ids.includes(taskInDB.id))
}).catch(error => {
alert(error.message)
})
}
}, () => `批量删除 (${_that.selectedDeletable.val.length})`),