Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/store/import-task.ts
T

161 lines
3.6 KiB
TypeScript

import type { ImportTaskVO } from '#/api/ec/import/task';
import type { PageQueryDTO } from '#/types/common';
import { computed, ref } from 'vue';
import { useAccessStore } from '@vben/stores';
import { defineStore } from 'pinia';
import { getImportTaskPageApi } from '#/api/ec/import/task';
import { RealtimeSseEvent } from '#/api/notify/sse';
import { useRealtimeStore } from '#/store/realtime';
/**
* 导入任务管理 Store(进度通过全局 realtime SSE 订阅)
*/
export const useImportTaskStore = defineStore('import-task', () => {
const tasks = ref<ImportTaskVO.ImportTaskInfoVO[]>([]);
const pagination = ref({
current: 1,
pageSize: 10,
total: 0,
});
const loading = ref(false);
const shouldOpenDropdown = ref(false);
let unsubProgress: (() => void) | null = null;
let unsubAllDone: (() => void) | null = null;
const hasActiveTasks = computed(() => {
return tasks.value.some(
(task) => task.status === 'pending' || task.status === 'processing',
);
});
const activeTaskCount = computed(() => {
return tasks.value.filter(
(task) => task.status === 'pending' || task.status === 'processing',
).length;
});
async function fetchTasks(reset = false) {
if (loading.value) return;
try {
loading.value = true;
if (reset) {
pagination.value.current = 1;
}
const params: PageQueryDTO = {
page: pagination.value.current,
pageSize: pagination.value.pageSize,
filters: {},
};
const result = await getImportTaskPageApi(params);
if (reset) {
tasks.value = result.records || [];
} else {
tasks.value.push(...(result.records || []));
}
pagination.value.total = result.total || 0;
} catch (error) {
console.error('获取导入任务列表失败:', error);
throw error;
} finally {
loading.value = false;
}
}
function updateTask(task: ImportTaskVO.ImportTaskInfoVO) {
const index = tasks.value.findIndex((t) => t.id === task.id);
if (index === -1) {
tasks.value.unshift(task);
} else {
tasks.value[index] = task;
}
}
function subscribeRealtime() {
if (unsubProgress) {
return;
}
const realtime = useRealtimeStore();
unsubProgress = realtime.on(
RealtimeSseEvent.IMPORT_TASK_PROGRESS,
(event) => {
try {
const data = JSON.parse(event.data);
if (data.task) {
updateTask(data.task);
}
} catch (error) {
console.error('解析导入任务进度失败:', error);
}
},
);
unsubAllDone = realtime.on(
RealtimeSseEvent.IMPORT_ALL_TASKS_COMPLETED,
() => {
fetchTasks(true).catch(() => {});
},
);
}
function unsubscribeRealtime() {
unsubProgress?.();
unsubAllDone?.();
unsubProgress = null;
unsubAllDone = null;
}
/** 登录后才拉任务列表并订阅 SSE */
async function init() {
if (!useAccessStore().accessToken) {
return;
}
subscribeRealtime();
await fetchTasks(true);
}
function $reset() {
unsubscribeRealtime();
tasks.value = [];
pagination.value = {
current: 1,
pageSize: 10,
total: 0,
};
loading.value = false;
}
function openDropdown() {
shouldOpenDropdown.value = true;
}
function closeDropdown() {
shouldOpenDropdown.value = false;
}
return {
tasks,
pagination,
loading,
shouldOpenDropdown,
hasActiveTasks,
activeTaskCount,
fetchTasks,
updateTask,
init,
openDropdown,
closeDropdown,
$reset,
};
});