feat(types): 新增共享类型中心,包含用户、产品、订单等核心领域类型 fix(types): 修复类型定义错误,统一各模块类型引用 style(types): 优化类型文件格式和注释 docs(types): 更新类型文档和变更日志 test(types): 添加类型测试用例 build(types): 配置类型共享路径 chore(types): 清理重复类型定义文件
91 lines
4.1 KiB
TypeScript
91 lines
4.1 KiB
TypeScript
/**
|
|
* [MOCK-TASK] TaskCenter模块DataSource
|
|
*/
|
|
|
|
export interface Task {
|
|
id: string;
|
|
taskId?: string;
|
|
name: string;
|
|
type: 'PRODUCT_SYNC' | 'ORDER_PROCESS' | 'DATA_ANALYSIS' | 'AD_OPTIMIZATION' | 'INVENTORY_CHECK' | 'CUSTOM' | 'sync' | 'report' | 'import' | 'export' | 'automation';
|
|
status: 'PENDING' | 'RUNNING' | 'PAUSED' | 'COMPLETED' | 'FAILED' | 'CANCELLED' | 'pending' | 'running' | 'completed' | 'failed';
|
|
priority?: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
|
|
progress: number;
|
|
total?: number;
|
|
result?: string | any;
|
|
error?: string;
|
|
errorMessage?: string;
|
|
startedAt?: string;
|
|
completedAt?: string;
|
|
createdAt: string;
|
|
estimatedDuration?: number;
|
|
actualDuration?: number;
|
|
retryCount?: number;
|
|
maxRetries?: number;
|
|
shopId?: string;
|
|
shopName?: string;
|
|
createdBy?: string;
|
|
scheduleType?: 'MANUAL' | 'SCHEDULED' | 'TRIGGERED';
|
|
scheduleTime?: string;
|
|
}
|
|
|
|
export interface ITaskCenterDataSource {
|
|
fetchTasks(params?: { status?: string; type?: string }): Promise<Task[]>;
|
|
createTask(data: Partial<Task>): Promise<Task>;
|
|
cancelTask(id: string): Promise<void>;
|
|
retryTask(id: string): Promise<Task>;
|
|
deleteTask(id: string): Promise<void>;
|
|
}
|
|
|
|
class MockTaskCenterDataSource implements ITaskCenterDataSource {
|
|
async fetchTasks(params?: { status?: string; type?: string }): Promise<Task[]> {
|
|
return [
|
|
{ id: 'task_001', type: 'sync', name: 'Inventory Sync - Amazon US', status: 'completed', progress: 100, total: 100, result: 'Synced 500 products', startedAt: '2026-03-18 10:00:00', completedAt: '2026-03-18 10:15:00', createdAt: '2026-03-18 09:55:00' },
|
|
{ id: 'task_002', type: 'report', name: 'Monthly Sales Report', status: 'running', progress: 65, total: 100, startedAt: '2026-03-18 14:00:00', createdAt: '2026-03-18 13:55:00' },
|
|
{ id: 'task_003', type: 'import', name: 'Product Import from CSV', status: 'failed', progress: 45, total: 100, error: 'Invalid SKU format at row 156', startedAt: '2026-03-18 11:00:00', completedAt: '2026-03-18 11:10:00', createdAt: '2026-03-18 10:55:00' },
|
|
{ id: 'task_004', type: 'export', name: 'Order Export - Q1 2026', status: 'pending', progress: 0, total: 100, createdAt: '2026-03-18 14:30:00' },
|
|
{ id: 'task_005', type: 'automation', name: 'Auto Price Adjustment', status: 'completed', progress: 100, total: 100, result: 'Updated 23 products', startedAt: '2026-03-18 08:00:00', completedAt: '2026-03-18 08:30:00', createdAt: '2026-03-18 07:55:00' },
|
|
];
|
|
}
|
|
|
|
async createTask(data: Partial<Task>): Promise<Task> {
|
|
return { ...data, id: `task_${Date.now()}`, status: 'pending', progress: 0, total: 100, createdAt: new Date().toISOString() } as Task;
|
|
}
|
|
|
|
async cancelTask(id: string): Promise<void> {}
|
|
async retryTask(id: string): Promise<Task> {
|
|
return { id, type: 'sync', name: 'Retry Task', status: 'running', progress: 0, total: 100, startedAt: new Date().toISOString(), createdAt: new Date().toISOString() };
|
|
}
|
|
async deleteTask(id: string): Promise<void> {}
|
|
}
|
|
|
|
class ApiTaskCenterDataSource implements ITaskCenterDataSource {
|
|
private baseUrl = '/api/tasks';
|
|
|
|
async fetchTasks(params?: { status?: string; type?: string }): Promise<Task[]> {
|
|
const query = new URLSearchParams(params as any).toString();
|
|
const res = await fetch(`${this.baseUrl}?${query}`);
|
|
return res.json();
|
|
}
|
|
|
|
async createTask(data: Partial<Task>): Promise<Task> {
|
|
const res = await fetch(this.baseUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) });
|
|
return res.json();
|
|
}
|
|
|
|
async cancelTask(id: string): Promise<void> {
|
|
await fetch(`${this.baseUrl}/${id}/cancel`, { method: 'POST' });
|
|
}
|
|
|
|
async retryTask(id: string): Promise<Task> {
|
|
const res = await fetch(`${this.baseUrl}/${id}/retry`, { method: 'POST' });
|
|
return res.json();
|
|
}
|
|
|
|
async deleteTask(id: string): Promise<void> {
|
|
await fetch(`${this.baseUrl}/${id}`, { method: 'DELETE' });
|
|
}
|
|
}
|
|
|
|
const useMock = process.env.REACT_APP_USE_MOCK === 'true';
|
|
export const taskCenterDataSource: ITaskCenterDataSource = useMock ? new MockTaskCenterDataSource() : new ApiTaskCenterDataSource();
|