mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 03:09:19 +08:00
feat: 新增导入功能
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
import type { ImportTaskVO } from '#/api/ecommerce/import-task';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
import {
|
||||
createSseConnection,
|
||||
getImportTaskPageApi,
|
||||
} from '#/api/ecommerce/import-task';
|
||||
|
||||
/**
|
||||
* 导入任务管理 Store
|
||||
*/
|
||||
export const useImportTaskStore = defineStore('import-task', () => {
|
||||
// 任务列表
|
||||
const tasks = ref<ImportTaskVO.ImportTaskInfoVO[]>([]);
|
||||
|
||||
// 分页信息
|
||||
const pagination = ref({
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
});
|
||||
|
||||
// SSE连接
|
||||
const sseConnection = ref<null | ReturnType<typeof createSseConnection>>(
|
||||
null,
|
||||
);
|
||||
const sseConnected = ref(false);
|
||||
|
||||
// 加载状态
|
||||
const loading = ref(false);
|
||||
|
||||
// 控制下拉菜单打开状态
|
||||
const shouldOpenDropdown = ref(false);
|
||||
|
||||
// 计算属性:是否有进行中的任务
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新任务状态(通过SSE接收到的更新)
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接
|
||||
* @returns Promise,连接建立成功后resolve
|
||||
*/
|
||||
function connectSse(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 如果已经连接且已连接成功,直接resolve
|
||||
if (sseConnection.value && sseConnected.value) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经连接但未成功,先关闭
|
||||
if (sseConnection.value) {
|
||||
disconnectSse();
|
||||
}
|
||||
|
||||
try {
|
||||
const connection = createSseConnection();
|
||||
sseConnection.value = connection;
|
||||
sseConnected.value = false;
|
||||
|
||||
let timeoutId: null | ReturnType<typeof setTimeout> = null;
|
||||
|
||||
// 监听连接成功事件
|
||||
const connectedHandler = (event: MessageEvent) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'connected') {
|
||||
sseConnected.value = true;
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
} catch {
|
||||
// 忽略解析错误,继续等待
|
||||
}
|
||||
};
|
||||
|
||||
// 监听连接错误
|
||||
const errorHandler = (_event: MessageEvent) => {
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
connection.removeEventListener('error', errorHandler);
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
reject(new Error('SSE连接失败'));
|
||||
};
|
||||
|
||||
connection.addEventListener('message', connectedHandler);
|
||||
connection.addEventListener('error', errorHandler);
|
||||
|
||||
// 设置超时,避免无限等待
|
||||
timeoutId = setTimeout(() => {
|
||||
if (!sseConnected.value) {
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
connection.removeEventListener('error', errorHandler);
|
||||
reject(new Error('SSE连接超时'));
|
||||
}
|
||||
}, 10_000); // 10秒超时
|
||||
|
||||
connection.connect();
|
||||
|
||||
// 连接成功(如果不在等待连接,则正常处理)
|
||||
connection.addEventListener('message', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'connected') {
|
||||
sseConnected.value = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析SSE消息失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 接收任务进度更新
|
||||
connection.addEventListener('task_progress', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'task_progress' && data.task) {
|
||||
updateTask(data.task);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析任务进度消息失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 所有任务完成
|
||||
connection.addEventListener('all_tasks_completed', () => {
|
||||
try {
|
||||
// 刷新任务列表
|
||||
fetchTasks(true);
|
||||
} catch (error) {
|
||||
console.error('刷新任务列表失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 连接错误
|
||||
connection.addEventListener('error', (event) => {
|
||||
console.error('SSE连接错误:', event);
|
||||
sseConnected.value = false;
|
||||
// 如果连接关闭,尝试重连
|
||||
if (connection.getReadyState() === EventSource.CLOSED) {
|
||||
// 延迟重连,避免频繁重连
|
||||
setTimeout(() => {
|
||||
if (hasActiveTasks.value) {
|
||||
connectSse();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('建立SSE连接失败:', error);
|
||||
sseConnected.value = false;
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开SSE连接
|
||||
*/
|
||||
function disconnectSse() {
|
||||
if (sseConnection.value) {
|
||||
sseConnection.value.disconnect();
|
||||
sseConnection.value = null;
|
||||
sseConnected.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化:获取第一页任务
|
||||
*/
|
||||
async function init() {
|
||||
await fetchTasks(true);
|
||||
|
||||
// 如果有进行中的任务,建立SSE连接
|
||||
if (hasActiveTasks.value && !sseConnection.value) {
|
||||
connectSse();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置store
|
||||
*/
|
||||
function $reset() {
|
||||
tasks.value = [];
|
||||
pagination.value = {
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
};
|
||||
disconnectSse();
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开下拉菜单
|
||||
*/
|
||||
function openDropdown() {
|
||||
shouldOpenDropdown.value = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭下拉菜单
|
||||
*/
|
||||
function closeDropdown() {
|
||||
shouldOpenDropdown.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
tasks,
|
||||
pagination,
|
||||
sseConnection,
|
||||
sseConnected,
|
||||
loading,
|
||||
shouldOpenDropdown,
|
||||
hasActiveTasks,
|
||||
activeTaskCount,
|
||||
fetchTasks,
|
||||
updateTask,
|
||||
connectSse,
|
||||
disconnectSse,
|
||||
init,
|
||||
openDropdown,
|
||||
closeDropdown,
|
||||
$reset,
|
||||
};
|
||||
});
|
||||
@@ -1 +1,2 @@
|
||||
export * from './auth';
|
||||
export * from './import-task';
|
||||
|
||||
Reference in New Issue
Block a user