feat(jd-collect): 任务面板改版——后端 dashboard/history/progress-batch/delete 四端点+前端左按钮右 TaskCenterPanel(历史/断点续传/中断/重试走通用原位复位)

This commit is contained in:
2026-11-15 01:56:37 +08:00
parent 55f4f17480
commit 45d0989ada
21 changed files with 1494 additions and 378 deletions
+8
View File
@@ -213,6 +213,14 @@ export const API_ENDPOINTS = {
progress: '/api/jd-collect/tasks/{taskId}/progress',
heartbeat: '/api/jd-collect/tasks/{taskId}/heartbeat',
chunk: '/api/jd-collect/tasks/{taskId}/chunk',
/** 任务面板总览(运行中/成功/失败计数统计卡) */
dashboard: '/api/jd-collect/dashboard',
/** 任务历史(活跃任务排前,含行数与断点游标) */
history: '/api/jd-collect/history',
/** 批量任务进度(面板轮询专用,归属先经公共出口过滤) */
progressBatch: '/api/jd-collect/tasks/progress/batch',
/** 删除任务(连带清理京东数据库该任务的商品/图片/SKU 行与断点游标) */
deleteTask: '/api/jd-collect/tasks/{taskId}',
},
userSecret: {
bundle: '/api/user-secrets',
@@ -1,8 +1,9 @@
import { get, post, type JavaApiResponse, unwrapJavaResponse } from '../../http.ts'
import { buildJavaUrl } from '../../url.ts'
import { get, post, del, type JavaApiResponse, unwrapJavaResponse } from '../../http.ts'
import { buildJavaUrl, JAVA_API_PREFIX } from '../../url.ts'
import { getCurrentUserId } from '../../user.ts'
import { API_ENDPOINTS } from '../../endpoints.ts'
import { getTaskProgressTimeoutMs } from '../../../task-progress-config.ts'
import { getTaskProgressCacheTtlMs, getTaskProgressTimeoutMs } from '../../../task-progress-config.ts'
import { createTaskProgressRequestCache } from '../../../task-progress-request-cache.ts'
/** 京东店铺采集建单回执(蓝图 §3.2):任务落 PENDING、执行通道固定 LOCAL,由本机客户端领取执行。 */
export interface JdCollectTaskVo {
@@ -28,13 +29,13 @@ export type JdListTab = 'ONSALE' | 'OFFSALE'
export interface JdCollectProgressVo {
taskId: number;
status: string;
itemsReceived: number | null;
total: number | null;
page: number | null;
listTab: string | null;
batches: number | null;
message: string | null;
updatedAt: string | null;
itemsReceived?: number;
total?: number;
page?: number;
listTab?: string;
batches?: number;
message?: string;
updatedAt?: string;
}
/** 轮询采集进度:uid 与建单同口径(会话身份),任务 owner 不一致服务端直接拒。 */
@@ -76,3 +77,184 @@ export function createJdCollectTask(shopName: string, listTabs: JdListTab[], max
},
))
}
// ---------------------------------------------------------------------------
// 任务面板(v1.5):总览统计卡 / 历史层 / 活跃任务批量进度 / 删除任务。
// 形状照外观专利 appearance-patent.ts 同名函数;京东采集没有结果文件,
// 所以没有 downloadUrl / fileStatus 那一层,进度直接用游标 itemsReceived/total。
// ---------------------------------------------------------------------------
/** 任务面板总览(运行中/已结束/成功/失败任务数)。 */
export interface JdCollectDashboardVo {
pendingTaskCount: number;
processedTaskCount: number;
successTaskCount: number;
failedTaskCount: number;
}
/**
* 历史行(任务卡片数据源)。`total` 为 null 时页面用不定态进度条,
* 与单任务进度条同口径(客户端抓过列表首页才写进游标,不编造分母)。
*/
export interface JdCollectHistoryItemVo {
resultId?: number;
taskId?: number;
taskNo?: string;
/** biz_file_task.status 原值:PENDING/RUNNING/SUCCESS/FAILED */
taskStatus?: string;
success?: boolean;
errorMessage?: string;
shopName?: string;
listTabs?: string[];
/** 已落京东数据库的商品行数 */
rowCount?: number;
itemsReceived?: number;
total?: number;
page?: number;
listTab?: string;
batches?: number;
updatedAt?: string;
createdAt?: string;
finishedAt?: string;
}
export interface JdCollectHistoryVo {
items: JdCollectHistoryItemVo[];
}
/** 批量进度条目:任务头 + 单元素进度行(与外观专利 TaskDetailVo 同形)。 */
export interface JdCollectTaskDetailVo {
task?: {
id?: number;
taskNo?: string;
status?: string;
errorMessage?: string;
updatedAt?: string;
};
items?: JdCollectHistoryItemVo[];
}
export interface JdCollectTaskBatchVo {
items: JdCollectTaskDetailVo[];
missingTaskIds: number[];
}
export function fetchJdCollectDashboard() {
return unwrapJavaResponse(
get<JavaApiResponse<JdCollectDashboardVo>>(
buildJavaUrl(API_ENDPOINTS.jdCollect.dashboard),
{ params: { user_id: getCurrentUserId() } },
),
);
}
export function fetchJdCollectHistory() {
return unwrapJavaResponse(
get<JavaApiResponse<JdCollectHistoryVo>>(
buildJavaUrl(API_ENDPOINTS.jdCollect.history),
{ params: { user_id: getCurrentUserId(), limit: 50 } },
),
);
}
interface JdProgressBatchOptions {
force?: boolean;
}
const taskProgressResponseCache = createTaskProgressRequestCache<unknown>({
ttlMs: () => getTaskProgressCacheTtlMs(),
maxEntries: 100,
maxInflight: 16,
});
function normalizeTaskIds(taskIds: number[]) {
return Array.from(
new Set(
(taskIds || [])
.map((taskId) => Number(taskId))
.filter((taskId) => Number.isFinite(taskId) && taskId > 0),
),
).sort((left, right) => left - right);
}
function buildTaskProgressRequestKey(path: string, taskIds: number[]) {
return `${path}::${taskIds.join(',')}`;
}
/** 批量进度请求缓存(与外观专利/delete-brand 同一形状:TTL 缓存 + 在飞合并 + force 旁路)。 */
async function postTaskProgressBatch<T>(
path: string,
taskIds: number[],
options: JdProgressBatchOptions = {},
) {
const normalizedTaskIds = normalizeTaskIds(taskIds);
if (!normalizedTaskIds.length) {
return { items: [], missingTaskIds: [] } as T;
}
const cacheKey = buildTaskProgressRequestKey(path, normalizedTaskIds);
const cached = taskProgressResponseCache.get(cacheKey);
if (!options.force && cached !== undefined) {
return cached as T;
}
const inflight = taskProgressResponseCache.getInflight(cacheKey);
if (!options.force && inflight) {
return (await inflight) as T;
}
const requestPromise = unwrapJavaResponse(
post<JavaApiResponse<T>, { taskIds: number[]; userId?: number }>(path, {
taskIds: normalizedTaskIds,
userId: Number(window.localStorage.getItem('uid')) || undefined,
}, {
timeout: getTaskProgressTimeoutMs(),
}),
)
.then((data) => {
taskProgressResponseCache.set(cacheKey, data);
return data;
})
.finally(() => {
taskProgressResponseCache.endInflight(cacheKey);
});
taskProgressResponseCache.startInflight(cacheKey, requestPromise);
return await requestPromise;
}
export function fetchJdCollectTaskProgressBatch(
taskIds: number[],
options: JdProgressBatchOptions = {},
) {
return postTaskProgressBatch<JdCollectTaskBatchVo>(
buildJavaUrl(API_ENDPOINTS.jdCollect.progressBatch),
taskIds,
options,
);
}
/** 删除任务:服务端连带清理京东数据库该任务的商品/图片/SKU 行与断点游标。 */
export function deleteJdCollectTask(taskId: number) {
return unwrapJavaResponse(
del<JavaApiResponse<null>>(
buildJavaUrl(API_ENDPOINTS.jdCollect.deleteTask.replace('{taskId}', String(taskId))),
{ params: { user_id: getCurrentUserId() } },
),
);
}
/**
* 用户中断任务:在跑/待跑的任务落 CANCELLED——客户端心跳拿到 notAlive 后协作停止,
* 已采集的行保留在京东数据库(断点游标也在),之后可「继续」接着跑。
* 终态任务重按是幂等 no-op;普通账号只能中断自己名下的任务。
*/
export function interruptJdCollectTask(taskId: number) {
return unwrapJavaResponse(
post<JavaApiResponse<null>>(
`${JAVA_API_PREFIX}/tasks/${taskId}/interrupt`,
undefined,
{ params: { user_id: getCurrentUserId() } },
),
);
}