feat: 新增导入功能

This commit is contained in:
ah--desktop
2025-12-04 23:22:02 +08:00
parent e2caac2965
commit d3d9fd47d8
33 changed files with 1585 additions and 176 deletions
@@ -7,12 +7,16 @@ export namespace ImportMappingDTO {
export interface CreateImportMappingDTO {
/** 导入类型(product-商品 order-订单) */
type: string;
/** 映射名称 */
name: string;
/** 字段映射(JSON格式) */
mapping: string;
}
/** 更新导入映射参数 */
export interface UpdateImportMappingDTO {
/** 映射名称 */
name?: string;
/** 字段映射(JSON格式) */
mapping: string;
}
@@ -21,5 +25,7 @@ export namespace ImportMappingDTO {
export interface ImportMappingFilterDTO {
/** 导入类型(product-商品 order-订单) */
type?: string;
/** 映射名称(模糊查询) */
name?: string;
}
}
@@ -11,6 +11,8 @@ export namespace ImportMappingVO {
companyId?: number;
/** 导入类型(product-商品 order-订单) */
type: string;
/** 映射名称 */
name: string;
/** 字段映射(JSON格式) */
mapping: string;
/** 创建时间 */
@@ -2,8 +2,13 @@ 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
*/
@@ -31,3 +36,191 @@ export async function getImportTaskPageApi(
params,
);
}
/**
* SSE连接管理器(使用fetch API支持自定义请求头)
*/
class SseConnection {
private abortController: AbortController | null = null;
private accessStore: ReturnType<typeof useAccessStore>;
private listeners: Map<string, Set<(event: MessageEvent) => void>> =
new Map();
private readyState: number = EventSource.CLOSED;
constructor() {
this.accessStore = useAccessStore();
}
/**
* 添加事件监听器
*/
addEventListener(eventType: string, handler: (event: MessageEvent) => void) {
if (!this.listeners.has(eventType)) {
this.listeners.set(eventType, new Set());
}
this.listeners.get(eventType)!.add(handler);
}
/**
* 建立SSE连接(使用fetch API)
*/
connect(): void {
if (this.abortController) {
this.disconnect();
}
const token = this.accessStore.accessToken;
if (!token) {
console.error('无法建立SSE连接:token不存在');
return;
}
const url = `${apiURL}/ec/import-task/sse/connect`;
this.abortController = new AbortController();
this.readyState = EventSource.CONNECTING;
fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
Accept: 'text/event-stream',
},
signal: this.abortController.signal,
})
.then((response) => {
if (!response.ok) {
throw new Error(`SSE连接失败: ${response.status}`);
}
this.readyState = EventSource.OPEN;
const reader = response.body?.getReader();
const decoder = new TextDecoder();
if (!reader) {
throw new Error('无法获取响应流');
}
// 读取流数据
const readStream = () => {
reader
.read()
.then(({ done, value }) => {
if (done) {
this.readyState = EventSource.CLOSED;
return;
}
const chunk = decoder.decode(value, { stream: true });
this.parseSSE(chunk);
readStream();
})
.catch((error) => {
if (error.name !== 'AbortError') {
console.error('读取SSE流失败:', error);
this.readyState = EventSource.CLOSED;
this.dispatchEvent(
'error',
new MessageEvent('error', { data: error }),
);
}
});
};
readStream();
})
.catch((error) => {
if (error.name !== 'AbortError') {
console.error('建立SSE连接失败:', error);
this.readyState = EventSource.CLOSED;
this.dispatchEvent(
'error',
new MessageEvent('error', { data: error }),
);
}
});
}
/**
* 断开连接
*/
disconnect() {
if (this.abortController) {
this.abortController.abort();
this.abortController = null;
this.readyState = EventSource.CLOSED;
}
}
/**
* 获取连接状态
*/
getReadyState(): number {
return this.readyState;
}
/**
* 移除事件监听器
*/
removeEventListener(
eventType: string,
handler: (event: MessageEvent) => void,
) {
const handlers = this.listeners.get(eventType);
if (handlers) {
handlers.delete(handler);
}
}
/**
* 分发事件
*/
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数据
*/
private parseSSE(chunk: string) {
const lines = chunk.split('\n');
let eventType = 'message';
let data = '';
for (const line of lines) {
if (line.startsWith('event:')) {
eventType = line.slice(6).trim();
} else if (line.startsWith('data:')) {
data += `${line.slice(5).trim()}\n`;
} else if (
line === '' && // 空行表示一个完整的消息
data
) {
const event = new MessageEvent(eventType, {
data: data.trim(),
});
this.dispatchEvent(eventType, event);
this.dispatchEvent('message', event);
data = '';
eventType = 'message';
}
}
}
}
/**
* 建立SSE连接
* @returns SseConnection实例
*/
export function createSseConnection(): SseConnection {
return new SseConnection();
}
@@ -7,6 +7,8 @@ export namespace ProductDTO {
export interface Create {
/** 商品名称 */
name: string;
/** 商品编码 */
productCode?: string;
/** 主图图片ID */
mainImageId?: number;
/** 商品规格列表(可选) */
@@ -18,6 +20,8 @@ export namespace ProductDTO {
/** 商品名称 */
name: string;
/** 商品编码 */
productCode?: string;
/** 商品编码 */
specCode?: string;
/** 商品SKU */
skuCode?: string;
@@ -64,6 +68,8 @@ export namespace ProductDTO {
/** 商品名称(模糊查询) */
name?: string;
/** 商品编码(模糊查询) */
productCode?: string;
/** 商品编码(模糊查询) */
specCode?: string;
/** 商品SKU(模糊查询) */
skuCode?: string;
@@ -11,6 +11,8 @@ export namespace ProductVO {
companyId: number;
/** 商品名称 */
name: string;
/** 商品编码 */
productCode?: string;
/** 主图URL(冗余字段) */
mainImage?: string;
/** 主图图片ID */