Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/import-task/apis.ts
T

172 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ImportTaskDTO, ImportTaskVO } from './types';
import type { PageQueryDTO, PageResultVO } from '#/types/common';
import { useAppConfig } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';
import { requestClient } from '#/api/request';
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
/**
* 导入任务管理API
*/
/**
* 创建导入任务
*/
export async function createImportTaskApi(
data: ImportTaskDTO.CreateImportTaskDTO,
) {
return requestClient.post<ImportTaskVO.ImportTaskInfoVO>(
'/ec/import-task/create',
data,
);
}
/**
* 分页获取导入任务列表
*/
export async function getImportTaskPageApi(
params: PageQueryDTO<ImportTaskDTO.ImportTaskFilterDTO>,
) {
return requestClient.post<PageResultVO<ImportTaskVO.ImportTaskInfoVO>>(
'/ec/import-task/list',
params,
);
}
/**
* SSE连接管理器(使用原生EventSource,支持多设备和 token 刷新重连)
*/
class SseConnection {
private eventSource: EventSource | null = null;
private listeners: Map<string, Set<(event: MessageEvent) => void>> =
new Map();
// EventSource 原生状态常量
private readyState: number = EventSource.CLOSED;
/** 设备标识,同一标签页内共用,重开标签页是新ID */
private deviceId: string;
/** 设备标识,每个标签页独立,不跨标签页共享 */
private deviceId: string;
constructor() {
// 每个 SseConnection 实例独立生成 deviceId
// 不存 sessionStorage:同 origin 不同标签页不应共享 ID
this.deviceId = crypto.randomUUID
? crypto.randomUUID()
: `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}
/**
* 添加事件监听器(需在 connect 之前调用)
*/
addEventListener(eventType: string, handler: (event: MessageEvent) => void) {
if (!this.listeners.has(eventType)) {
this.listeners.set(eventType, new Set());
}
this.listeners.get(eventType)!.add(handler);
// 如果 EventSource 已建立,额外绑定一次(防止在 connect 之后调用 listener 漏掉)
if (this.eventSource) {
this.eventSource.addEventListener(eventType, handler as EventListener);
}
}
/**
* 移除事件监听器
*/
removeEventListener(eventType: string, handler: (event: MessageEvent) => void) {
const handlers = this.listeners.get(eventType);
if (handlers) {
handlers.delete(handler);
}
if (this.eventSource) {
this.eventSource.removeEventListener(eventType, handler as EventListener);
}
}
/**
* 建立SSE连接
* EventSource 原生支持自动重连,无需手动实现
*/
connect(): void {
if (this.eventSource) {
this.disconnect();
}
// token 必须由调用方传入(connectSse 在重连前已刷新 token)
const accessStore = useAccessStore();
const token = accessStore.accessToken;
if (!token) {
console.error('无法建立SSE连接:token不存在');
return;
}
// 加时间戳参数避免浏览器复用/合并同 URL 的 EventSource 连接
const ts = Date.now();
const url = `${apiURL}/ec/import-task/sse/connect?token=${encodeURIComponent(token)}&deviceId=${encodeURIComponent(this.deviceId)}&t=${ts}`;
this.readyState = EventSource.CONNECTING;
const es = new EventSource(url);
this.eventSource = es;
es.onopen = () => {
this.readyState = EventSource.OPEN;
};
es.onerror = (event) => {
this.readyState = EventSource.CLOSED;
this.dispatchEvent('error', new MessageEvent('error', { data: event }));
// EventSource 会自动重连,无需手动处理
};
// 将所有自定义事件类型的 listener 注册到 EventSource 上
this.listeners.forEach((handlers, eventType) => {
handlers.forEach((handler) => {
es.addEventListener(eventType, handler as EventListener);
});
});
}
/**
* 断开连接(手动断开后不会自动重连)
*/
disconnect() {
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
this.readyState = EventSource.CLOSED;
}
}
/**
* 获取连接状态
*/
getReadyState(): number {
return this.readyState;
}
private dispatchEvent(eventType: string, event: MessageEvent) {
const handlers = this.listeners.get(eventType);
if (handlers) {
handlers.forEach((handler) => {
try {
handler(event);
} catch (error) {
console.error('事件处理器执行失败:', error);
}
});
}
}
}
/**
* 建立SSE连接
* @returns SseConnection实例
*/
export function createSseConnection(): SseConnection {
return new SseConnection();
}