asyncTaskStore.ts
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { create } from 'zustand';
import type { AsyncTask, PaginatedResponse, CancelResult } from '../types';
import { asyncTaskService } from '../services/asyncTaskService';
interface AsyncTaskState {
tasks: AsyncTask[];
total: number;
page: number;
pageSize: number;
totalPages: number;
loading: boolean;
fetchTasks: (params?: {
page?: number;
page_size?: number;
status?: string;
task_id?: string;
start_time?: string;
end_time?: string;
}) => Promise<void>;
cancelTask: (id: string) => Promise<void>;
batchCancelTasks: (taskIds: string[]) => Promise<CancelResult>;
cancelAllTasks: () => Promise<CancelResult>;
}
export const useAsyncTaskStore = create<AsyncTaskState>((set, get) => ({
tasks: [],
total: 0,
page: 1,
pageSize: 20,
totalPages: 0,
loading: false,
fetchTasks: async (params) => {
set({ loading: true });
try {
const p = { ...params };
if (!p.page) p.page = get().page;
if (!p.page_size) p.page_size = get().pageSize;
const res: PaginatedResponse<AsyncTask> = await asyncTaskService.list(p);
set({
tasks: res.items,
total: res.total,
page: res.page,
pageSize: res.page_size,
totalPages: res.total_pages,
loading: false,
});
} catch (e) {
console.error('获取异步任务列表失败:', e);
set({ loading: false });
}
},
cancelTask: async (id) => {
await asyncTaskService.cancel(id);
await get().fetchTasks();
},
batchCancelTasks: async (taskIds) => {
const result = await asyncTaskService.batchCancel(taskIds);
await get().fetchTasks();
return result;
},
cancelAllTasks: async (params?: {
status?: string;
biz_type?: string;
task_id?: string;
start_time?: string;
end_time?: string;
}) => {
const result = await asyncTaskService.cancelAll(params);
await get().fetchTasks();
return result;
},
}));