阶段七:完成ItemList React feature化与批量发布边界
This commit is contained in:
1 parent
e79d76dcdf
commit
c7c7023ad4
15 files changed
+1356
-1443
No files matched your search
@@ -0,0 +1,21 @@
|
||||
// ItemList feature 通过本适配层访问商品、批量任务和发货地 API。
|
||||
export {
|
||||
getItems,
|
||||
getAccountDetails,
|
||||
syncItemsFromAccount,
|
||||
createItem,
|
||||
publishItem,
|
||||
recommendPublishCategory,
|
||||
previewItemPublishBatch,
|
||||
startItemPublishBatch,
|
||||
getItemPublishBatch,
|
||||
getItemPublishBatches,
|
||||
deleteItemPublishBatch,
|
||||
cancelItemPublishBatch,
|
||||
retryFailedItemPublishBatch,
|
||||
updateItem,
|
||||
deleteItem,
|
||||
getShippingRules,
|
||||
} from '../../../services/api';
|
||||
export type { PublishLocation } from '../../../services/api';
|
||||
export { getPublishLocations } from '../../../services/amapLocation';
|
||||
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import {
|
||||
batchStatusText,
|
||||
canRetryBatch,
|
||||
canStartBatch,
|
||||
isBatchInProgress,
|
||||
isCurrentBatchRequest,
|
||||
selectActivePublishBatch,
|
||||
} from './batchState';
|
||||
|
||||
// ItemList 批量行为测试覆盖预检、取消、重试和过期任务响应。
|
||||
describe('ItemList batch state',
|
||||
// 测试组回调集中验证批量状态机的关键分支。
|
||||
() => {
|
||||
// 预检通过且存在有效行时才能启动批量任务。
|
||||
test('starts only when the preview has publishable rows',
|
||||
// 预检成功场景回调验证有效行门禁。
|
||||
() => {
|
||||
expect(canStartBatch({ preview_id: 'preview-1', valid: 2 })).toBe(true);
|
||||
expect(canStartBatch({ preview_id: 'preview-1', valid: 0 })).toBe(false);
|
||||
expect(canStartBatch(null)).toBe(false);
|
||||
});
|
||||
|
||||
// 运行中和安全取消中的任务都必须继续轮询远端结果。
|
||||
test('keeps polling running and canceling tasks',
|
||||
// 轮询场景回调验证运行和取消中的任务状态。
|
||||
() => {
|
||||
expect(isBatchInProgress('running')).toBe(true);
|
||||
expect(isBatchInProgress('canceling')).toBe(true);
|
||||
expect(isBatchInProgress('completed')).toBe(false);
|
||||
expect(batchStatusText('canceling')).toBe('正在安全取消');
|
||||
});
|
||||
|
||||
// 只有非运行状态下仍有失败行的批次允许重试。
|
||||
test('allows retry only for retryable completed failures',
|
||||
// 重试场景回调验证失败行和状态门禁。
|
||||
() => {
|
||||
expect(canRetryBatch({ id: 'batch-1', retryable: 2, status: 'failed' })).toBe(true);
|
||||
expect(canRetryBatch({ id: 'batch-1', retryable: 2, status: 'running' })).toBe(false);
|
||||
expect(canRetryBatch({ id: 'batch-1', retryable: 0, status: 'failed' })).toBe(false);
|
||||
});
|
||||
|
||||
// 新任务代次产生后,旧轮询响应必须被视为过期并丢弃。
|
||||
test('rejects an expired polling response',
|
||||
// 过期响应场景回调验证轮询代次门禁。
|
||||
() => {
|
||||
expect(isCurrentBatchRequest(1, 2)).toBe(false);
|
||||
expect(isCurrentBatchRequest(2, 2)).toBe(true);
|
||||
});
|
||||
|
||||
// 已完成历史不能覆盖新任务上传流程,运行任务仍可恢复。
|
||||
test('selects only an active recoverable batch',
|
||||
// 恢复场景回调验证完成历史不会覆盖新流程。
|
||||
() => {
|
||||
expect(selectActivePublishBatch([{ id: 'done', status: 'completed' }])).toBeUndefined();
|
||||
expect(selectActivePublishBatch([{ id: 'done', status: 'completed' }, { id: 'active', status: 'running' }])?.id).toBe('active');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
// PublishBatchSummary 是选择最近批量任务时所需的最小状态模型。
|
||||
export interface PublishBatchSummary {
|
||||
// id 是批量任务标识。
|
||||
id?: string;
|
||||
// status 是批量任务状态。
|
||||
status?: string;
|
||||
}
|
||||
|
||||
// selectActivePublishBatch 只选择仍可继续处理的运行中任务。
|
||||
export const selectActivePublishBatch = <T extends PublishBatchSummary>(batches: T[]): T | undefined =>
|
||||
batches.find(
|
||||
// 活跃任务筛选器排除已完成历史,避免覆盖新的上传流程。
|
||||
batch => batch.status === 'running' || batch.status === 'canceling',
|
||||
);
|
||||
|
||||
// isBatchInProgress 判断批量任务是否仍需要继续轮询。
|
||||
export const isBatchInProgress = (status?: string): boolean => status === 'running' || status === 'canceling';
|
||||
|
||||
// BatchPreviewGate 是判断预检是否可以启动任务的最小模型。
|
||||
export interface BatchPreviewGate {
|
||||
// preview_id 是预检任务标识。
|
||||
preview_id?: string;
|
||||
// valid 是可发布行数。
|
||||
valid?: number;
|
||||
}
|
||||
|
||||
// canStartBatch 判断预检结果是否包含可发布商品。
|
||||
export const canStartBatch = (preview?: BatchPreviewGate | null): boolean =>
|
||||
Boolean(preview?.preview_id && Number(preview.valid || 0) > 0);
|
||||
|
||||
// BatchRetryGate 是判断批量任务是否可重试的最小模型。
|
||||
export interface BatchRetryGate {
|
||||
// id 是批量任务标识。
|
||||
id?: string;
|
||||
// retryable 是可重试失败行数。
|
||||
retryable?: number;
|
||||
// status 是批量任务状态。
|
||||
status?: string;
|
||||
}
|
||||
|
||||
// canRetryBatch 判断批量任务是否存在可重试失败行。
|
||||
export const canRetryBatch = (detail?: BatchRetryGate | null): boolean =>
|
||||
Boolean(detail?.id && Number(detail.retryable || 0) > 0 && !isBatchInProgress(detail.status));
|
||||
|
||||
// isCurrentBatchRequest 判断轮询响应是否仍属于当前任务请求代次。
|
||||
export const isCurrentBatchRequest = (requestGeneration: number, currentGeneration: number): boolean =>
|
||||
requestGeneration === currentGeneration;
|
||||
|
||||
// batchStatusText 将后端批量状态转换为中文展示文本。
|
||||
export const batchStatusText = (status?: string): string => {
|
||||
switch (status) {
|
||||
case 'preview': return '待确认';
|
||||
case 'pending': return '等待中';
|
||||
case 'running': return '发布中';
|
||||
case 'canceling': return '正在安全取消';
|
||||
case 'success': return '成功';
|
||||
case 'failed': return '失败';
|
||||
case 'completed': return '已完成';
|
||||
case 'partially_failed': return '部分失败';
|
||||
case 'canceled': return '已取消';
|
||||
default: return status || '-';
|
||||
}
|
||||
};
|
||||
|
||||
// batchStatusClass 返回批量状态对应的标签样式。
|
||||
export const batchStatusClass = (status?: string): string => {
|
||||
switch (status) {
|
||||
case 'success':
|
||||
case 'completed':
|
||||
return 'bg-emerald-50 text-emerald-700 border-emerald-100';
|
||||
case 'partially_failed':
|
||||
case 'failed':
|
||||
return 'bg-red-50 text-red-700 border-red-100';
|
||||
case 'running':
|
||||
return 'bg-blue-50 text-blue-700 border-blue-100';
|
||||
case 'canceled':
|
||||
return 'bg-gray-100 text-gray-600 border-gray-200';
|
||||
default:
|
||||
return 'bg-amber-50 text-amber-700 border-amber-100';
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { BatchPhase } from '../types';
|
||||
|
||||
// BatchPhaseIndicatorProps 描述批量流程指示器的当前步骤。
|
||||
export interface BatchPhaseIndicatorProps {
|
||||
// phase 是当前批量流程步骤。
|
||||
phase: BatchPhase;
|
||||
}
|
||||
|
||||
// BatchPhaseIndicator 展示上传、预检、发布和结果四个批量阶段。
|
||||
export const BatchPhaseIndicator = ({ phase }: BatchPhaseIndicatorProps) => {
|
||||
// phases 是批量流程的固定步骤列表。
|
||||
const phases: Array<[BatchPhase, string]> = [
|
||||
['upload', '1 上传'],
|
||||
['preview', '2 预检'],
|
||||
['running', '3 发布'],
|
||||
['done', '4 结果'],
|
||||
];
|
||||
return (
|
||||
<div className="grid grid-cols-4 gap-2">
|
||||
{phases.map(
|
||||
// 阶段渲染器根据当前步骤切换高亮样式。
|
||||
([phaseID, label]) => (
|
||||
<div key={phaseID} className={`rounded-xl px-3 py-2 text-center text-xs font-extrabold border ${phase === phaseID ? 'bg-blue-600 text-white border-blue-600' : 'bg-gray-50 text-gray-500 border-gray-100'}`}>
|
||||
{label}
|
||||
</div>
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,362 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
cancelItemPublishBatch,
|
||||
deleteItemPublishBatch,
|
||||
getItemPublishBatch,
|
||||
getItemPublishBatches,
|
||||
recommendPublishCategory,
|
||||
previewItemPublishBatch,
|
||||
retryFailedItemPublishBatch,
|
||||
startItemPublishBatch,
|
||||
} from './api';
|
||||
import { canRetryBatch, canStartBatch, isBatchInProgress, isCurrentBatchRequest, selectActivePublishBatch } from './batchState';
|
||||
import type {
|
||||
BatchFallbackCategory,
|
||||
BatchPhase,
|
||||
ItemPublishBatchOptions,
|
||||
ItemPublishBatchState,
|
||||
PublishBatchDetail,
|
||||
PublishBatchPreview,
|
||||
} from './types';
|
||||
|
||||
// useItemPublishBatch 集中管理批量铺货的表单、任务恢复、轮询和重试状态。
|
||||
export const useItemPublishBatch = (options: ItemPublishBatchOptions): ItemPublishBatchState => {
|
||||
// showBatchModal 表示批量铺货弹窗是否打开。
|
||||
const [showBatchModal, setShowBatchModal] = useState(false);
|
||||
// batchPhase 表示批量流程当前所处步骤。
|
||||
const [batchPhase, setBatchPhase] = useState<BatchPhase>('upload');
|
||||
// batchFile 保存待预检的商品表格文件。
|
||||
const [batchFile, setBatchFile] = useState<File | null>(null);
|
||||
// batchImagesZip 保存可选的商品图片压缩包。
|
||||
const [batchImagesZip, setBatchImagesZip] = useState<File | null>(null);
|
||||
// batchCategoryKeyword 保存默认类目搜索词。
|
||||
const [batchCategoryKeyword, setBatchCategoryKeyword] = useState('');
|
||||
// batchCategoryLoading 表示默认类目推荐请求是否正在执行。
|
||||
const [batchCategoryLoading, setBatchCategoryLoading] = useState(false);
|
||||
// batchFallbackCategory 保存默认类目配置。
|
||||
const [batchFallbackCategory, setBatchFallbackCategory] = useState<BatchFallbackCategory>({ catId: '', catName: '', channelCatId: '', tbCatId: '' });
|
||||
// batchPreview 保存最近一次批量预检结果。
|
||||
const [batchPreview, setBatchPreview] = useState<PublishBatchPreview | null>(null);
|
||||
// batchDetail 保存当前批量任务的服务端详情。
|
||||
const [batchDetail, setBatchDetail] = useState<PublishBatchDetail | null>(null);
|
||||
// recentBatch 保存页面入口处展示的最近任务结果。
|
||||
const [recentBatch, setRecentBatch] = useState<PublishBatchDetail | null>(null);
|
||||
// batchLocations 保存批量任务可选的发货地列表。
|
||||
const [batchLocations, setBatchLocations] = useState<NonNullable<ItemPublishBatchState['batchLocations']>>([]);
|
||||
// batchLocation 保存批量任务当前选中的发货地。
|
||||
const [batchLocation, setBatchLocation] = useState<ItemPublishBatchState['batchLocation']>(null);
|
||||
// batchLoading 表示批量任务请求是否正在执行。
|
||||
const [batchLoading, setBatchLoading] = useState(false);
|
||||
// batchPollInFlight 防止同一批次的轮询请求重叠。
|
||||
const batchPollInFlight = useRef(false);
|
||||
// batchRequestGeneration 用于丢弃弹窗关闭后返回的过期轮询响应。
|
||||
const batchRequestGeneration = useRef(0);
|
||||
|
||||
// openBatchModal 初始化上传表单,并恢复仍在执行的批量任务。
|
||||
const openBatchModal = useCallback(
|
||||
// 批量弹窗打开器负责初始化临时表单和恢复任务。
|
||||
async () => {
|
||||
setBatchPhase('upload');
|
||||
setBatchPreview(null);
|
||||
setBatchDetail(null);
|
||||
setBatchFile(null);
|
||||
setBatchImagesZip(null);
|
||||
setBatchCategoryKeyword('');
|
||||
setBatchFallbackCategory({ catId: '', catName: '', channelCatId: '', tbCatId: '' });
|
||||
setBatchLocations([]);
|
||||
setBatchLocation(null);
|
||||
setShowBatchModal(true);
|
||||
setBatchLoading(true);
|
||||
try {
|
||||
// batches 是最近批量任务摘要,用于寻找可恢复任务。
|
||||
const batches = await getItemPublishBatches(20);
|
||||
// recoverable 是仍处于运行或安全取消阶段的任务。
|
||||
const recoverable = selectActivePublishBatch(batches);
|
||||
if (recoverable?.id) {
|
||||
// detail 是可恢复任务的完整详情。
|
||||
const detail = await getItemPublishBatch(recoverable.id);
|
||||
setRecentBatch(detail);
|
||||
setBatchDetail(detail);
|
||||
setBatchPhase(['running', 'canceling'].includes(detail.status) ? 'running' : 'done');
|
||||
}
|
||||
} catch (error /* 恢复任务错误 */) {
|
||||
console.error('恢复最近批量铺货任务失败:', error);
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
// handleRecommendBatchCategory 请求默认发布账号对应的推荐类目。
|
||||
const handleRecommendBatchCategory = useCallback(
|
||||
// 类目推荐动作响应用户点击或回车提交。
|
||||
async () => {
|
||||
// keyword 是去除空白后的类目搜索词。
|
||||
const keyword = batchCategoryKeyword.trim();
|
||||
if (!options.selectedAccount) {
|
||||
alert('请先选择默认发布账号');
|
||||
return;
|
||||
}
|
||||
if (!keyword) {
|
||||
alert('请输入类目关键词');
|
||||
return;
|
||||
}
|
||||
setBatchCategoryLoading(true);
|
||||
try {
|
||||
// result 是类目推荐接口返回的具名响应。
|
||||
const result = await recommendPublishCategory(options.selectedAccount, keyword);
|
||||
// category 是后端返回的推荐类目。
|
||||
const category = result.category;
|
||||
setBatchFallbackCategory({
|
||||
catId: category.cat_id,
|
||||
catName: category.cat_name,
|
||||
channelCatId: category.channel_cat_id,
|
||||
tbCatId: category.tb_cat_id || '',
|
||||
});
|
||||
} catch (error: any /* 推荐类目错误 */) {
|
||||
console.error('获取推荐类目失败:', error);
|
||||
alert(error?.message || '没有匹配到类目,请换一个更具体的关键词');
|
||||
} finally {
|
||||
setBatchCategoryLoading(false);
|
||||
}
|
||||
},
|
||||
[batchCategoryKeyword, options.selectedAccount],
|
||||
);
|
||||
|
||||
// openRecentBatchResult 加载最近批量任务详情并打开结果弹窗。
|
||||
const openRecentBatchResult = useCallback(
|
||||
// 最近结果打开器只读取已存在的批量任务。
|
||||
async () => {
|
||||
if (!recentBatch?.id) return;
|
||||
setBatchLoading(true);
|
||||
setShowBatchModal(true);
|
||||
try {
|
||||
// detail 是最近任务的最新服务端状态。
|
||||
const detail = await getItemPublishBatch(recentBatch.id);
|
||||
setBatchDetail(detail);
|
||||
setBatchPhase(['running', 'canceling'].includes(detail.status) ? 'running' : 'done');
|
||||
} catch (error /* 最近结果读取错误 */) {
|
||||
console.error('加载最近批量铺货结果失败:', error);
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[recentBatch?.id],
|
||||
);
|
||||
|
||||
// handlePreviewBatch 上传表格并执行批量预检。
|
||||
const handlePreviewBatch = useCallback(
|
||||
// 批量预检动作提交上传文件和默认配置。
|
||||
async () => {
|
||||
if (!batchFile) {
|
||||
alert('请先上传商品表格');
|
||||
return;
|
||||
}
|
||||
if (!options.selectedAccount) {
|
||||
alert('请先选择默认发布账号');
|
||||
return;
|
||||
}
|
||||
setBatchLoading(true);
|
||||
try {
|
||||
// result 是批量预检接口返回的行级校验结果。
|
||||
const result = await previewItemPublishBatch({
|
||||
file: batchFile,
|
||||
imagesZip: batchImagesZip,
|
||||
defaultCookieId: options.selectedAccount,
|
||||
fallbackCategory: batchFallbackCategory,
|
||||
location: batchLocation || undefined,
|
||||
});
|
||||
setBatchPreview(result);
|
||||
setBatchDetail(null);
|
||||
setBatchPhase('preview');
|
||||
} catch (error: any /* 预检失败错误 */) {
|
||||
console.error('批量铺货预检失败:', error);
|
||||
alert(error?.message || '预检失败,请检查表格和图片 zip');
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[batchFallbackCategory, batchFile, batchImagesZip, batchLocation, options.selectedAccount],
|
||||
);
|
||||
|
||||
// handleStartBatch 启动预检通过的批量任务并读取首个详情。
|
||||
const handleStartBatch = useCallback(
|
||||
// 批量启动动作只允许预检通过的有效行进入执行阶段。
|
||||
async () => {
|
||||
if (!batchPreview?.preview_id) return;
|
||||
if (!canStartBatch(batchPreview)) {
|
||||
alert('没有可发布的商品行');
|
||||
return;
|
||||
}
|
||||
setBatchLoading(true);
|
||||
try {
|
||||
// started 是批量任务启动接口返回的任务标识。
|
||||
const started = await startItemPublishBatch(batchPreview.preview_id);
|
||||
// detail 是启动后用于驱动轮询的任务详情。
|
||||
const detail = await getItemPublishBatch(started.batch_id || batchPreview.preview_id);
|
||||
setBatchDetail(detail);
|
||||
setRecentBatch(detail);
|
||||
setBatchPhase(detail.status === 'running' ? 'running' : 'done');
|
||||
} catch (error: any /* 启动失败错误 */) {
|
||||
console.error('启动批量铺货失败:', error);
|
||||
alert(error?.message || '启动发布任务失败');
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[batchPreview],
|
||||
);
|
||||
|
||||
// handleCancelBatch 请求安全取消当前批量任务,并保留远端收尾状态。
|
||||
const handleCancelBatch = useCallback(
|
||||
// 批量取消动作遵循后端的安全取消语义。
|
||||
async () => {
|
||||
if (!batchDetail?.id) return;
|
||||
if (!confirm('确认取消当前批量铺货任务吗?正在发布的单个商品可能会继续完成。')) return;
|
||||
setBatchLoading(true);
|
||||
try {
|
||||
// result 是取消请求返回的过渡状态。
|
||||
const result = await cancelItemPublishBatch(batchDetail.id);
|
||||
// detail 是取消请求后的最新任务状态。
|
||||
const detail = await getItemPublishBatch(batchDetail.id);
|
||||
setBatchDetail(detail);
|
||||
setBatchPhase(result?.status === 'canceling' || detail.status === 'canceling' ? 'running' : 'done');
|
||||
} catch (error: any /* 取消失败错误 */) {
|
||||
alert(error?.message || '取消失败');
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[batchDetail?.id],
|
||||
);
|
||||
|
||||
// abandonBatchPreview 删除未启动的预检任务并恢复上传步骤。
|
||||
const abandonBatchPreview = useCallback(
|
||||
// 预检放弃动作删除未启动任务并恢复上传步骤。
|
||||
async () => {
|
||||
// previewId 是当前临时预检任务标识。
|
||||
const previewId = batchPreview?.preview_id;
|
||||
if (previewId && batchPhase === 'preview') {
|
||||
try {
|
||||
await deleteItemPublishBatch(previewId);
|
||||
} catch (error /* 删除预检错误 */) {
|
||||
console.error('清理批量铺货预检失败:', error);
|
||||
}
|
||||
}
|
||||
setBatchPreview(null);
|
||||
setBatchPhase('upload');
|
||||
},
|
||||
[batchPhase, batchPreview?.preview_id],
|
||||
);
|
||||
|
||||
// closeBatchModal 清理临时预检后关闭批量弹窗。
|
||||
const closeBatchModal = useCallback(
|
||||
// 批量弹窗关闭动作先清理临时预检任务。
|
||||
async () => {
|
||||
await abandonBatchPreview();
|
||||
setShowBatchModal(false);
|
||||
},
|
||||
[abandonBatchPreview],
|
||||
);
|
||||
|
||||
// handleRetryBatchFailed 重新执行当前批次中可重试的失败行。
|
||||
const handleRetryBatchFailed = useCallback(
|
||||
// 失败重试动作只提交后端允许重试的行。
|
||||
async () => {
|
||||
if (!batchDetail?.id || !canRetryBatch(batchDetail)) return;
|
||||
setBatchLoading(true);
|
||||
try {
|
||||
await retryFailedItemPublishBatch(batchDetail.id);
|
||||
setBatchDetail(await getItemPublishBatch(batchDetail.id));
|
||||
setBatchPhase('running');
|
||||
} catch (error: any /* 重试失败错误 */) {
|
||||
alert(error?.message || '重试失败');
|
||||
} finally {
|
||||
setBatchLoading(false);
|
||||
}
|
||||
},
|
||||
[batchDetail?.id],
|
||||
);
|
||||
|
||||
useEffect(
|
||||
// 批量轮询副作用只在弹窗展示运行任务时启动。
|
||||
() => {
|
||||
if (!showBatchModal || !batchDetail?.id || !isBatchInProgress(batchDetail.status)) return;
|
||||
// requestGeneration 标记本次轮询生命周期,弹窗关闭时会失效。
|
||||
const requestGeneration = ++batchRequestGeneration.current;
|
||||
// pollBatch 读取任务最新进度并在结束后刷新商品和规则列表。
|
||||
const pollBatch = async () => {
|
||||
if (batchPollInFlight.current) return;
|
||||
batchPollInFlight.current = true;
|
||||
try {
|
||||
// detail 是轮询返回的最新任务详情。
|
||||
const detail = await getItemPublishBatch(batchDetail.id);
|
||||
if (!isCurrentBatchRequest(requestGeneration, batchRequestGeneration.current)) return;
|
||||
setBatchDetail(detail);
|
||||
setRecentBatch(detail);
|
||||
if (!isBatchInProgress(detail.status)) {
|
||||
setBatchPhase('done');
|
||||
await Promise.all([options.loadItems(), options.loadShippingRules()]);
|
||||
}
|
||||
} catch (error /* 轮询读取错误 */) {
|
||||
console.error('刷新批量铺货进度失败:', error);
|
||||
} finally {
|
||||
batchPollInFlight.current = false;
|
||||
}
|
||||
};
|
||||
// timer 是当前批次的轮询计时器。
|
||||
const timer = window.setInterval(
|
||||
// 轮询回调异步读取任务进度。
|
||||
() => void pollBatch(),
|
||||
3000,
|
||||
);
|
||||
return (
|
||||
// 轮询清理器停止计时器并使未完成响应失效。
|
||||
() => {
|
||||
window.clearInterval(timer);
|
||||
batchRequestGeneration.current += 1;
|
||||
}
|
||||
);
|
||||
},
|
||||
[batchDetail?.id, batchDetail?.status, options.loadItems, options.loadShippingRules, showBatchModal],
|
||||
);
|
||||
|
||||
// result 汇总批量状态、更新器和流程动作,保持页面只消费 feature 边界。
|
||||
const result: ItemPublishBatchState = {
|
||||
showBatchModal,
|
||||
setShowBatchModal,
|
||||
batchLoading,
|
||||
batchPhase,
|
||||
setBatchPhase,
|
||||
batchFile,
|
||||
setBatchFile,
|
||||
batchImagesZip,
|
||||
setBatchImagesZip,
|
||||
batchCategoryKeyword,
|
||||
setBatchCategoryKeyword,
|
||||
batchCategoryLoading,
|
||||
batchFallbackCategory,
|
||||
setBatchFallbackCategory,
|
||||
batchPreview,
|
||||
setBatchPreview,
|
||||
batchDetail,
|
||||
setBatchDetail,
|
||||
recentBatch,
|
||||
setRecentBatch,
|
||||
batchLocations,
|
||||
batchLocation,
|
||||
setBatchLocations,
|
||||
setBatchLocation,
|
||||
openBatchModal,
|
||||
handleRecommendBatchCategory,
|
||||
openRecentBatchResult,
|
||||
handlePreviewBatch,
|
||||
handleStartBatch,
|
||||
handleCancelBatch,
|
||||
abandonBatchPreview,
|
||||
closeBatchModal,
|
||||
handleRetryBatchFailed,
|
||||
};
|
||||
return result;
|
||||
};
|
||||
@@ -0,0 +1,216 @@
|
||||
import type { Dispatch, SetStateAction } from 'react';
|
||||
import type { Item, AccountDetail, ShippingRule } from '../../../types';
|
||||
import type { PublishLocation } from './api';
|
||||
|
||||
// ItemListProps 描述商品页面从父级接收的规则配置回调。
|
||||
export interface ItemListProps {
|
||||
// onConfigureDelivery 打开指定商品的自动化发货规则编辑器。
|
||||
onConfigureDelivery: (item: Item) => void;
|
||||
}
|
||||
|
||||
// BatchPhase 表示批量铺货流程当前所在的步骤。
|
||||
export type BatchPhase = 'upload' | 'preview' | 'running' | 'done';
|
||||
|
||||
// PublishCategory 表示批量铺货行使用的类目映射。
|
||||
export interface PublishCategory {
|
||||
// cat_id 是闲鱼类目标识。
|
||||
cat_id: string;
|
||||
// cat_name 是类目展示名称。
|
||||
cat_name: string;
|
||||
// channel_cat_id 是闲鱼频道类目标识。
|
||||
channel_cat_id?: string;
|
||||
// tb_cat_id 是可选的淘宝类目标识。
|
||||
tb_cat_id?: string;
|
||||
}
|
||||
|
||||
// PublishBatchPreviewRow 表示批量预检返回的一行商品。
|
||||
export interface PublishBatchPreviewRow {
|
||||
// row_no 是表格中的原始行号。
|
||||
row_no: number;
|
||||
// valid 表示该行是否可以进入发布阶段。
|
||||
valid: boolean;
|
||||
// errors 是该行的校验错误列表。
|
||||
errors?: string[];
|
||||
// cookie_id 是该行最终使用的账号标识。
|
||||
cookie_id: string;
|
||||
// title 是该行商品标题。
|
||||
title: string;
|
||||
// price 是该行商品价格。
|
||||
price: string;
|
||||
// quantity 是该行商品库存数量。
|
||||
quantity: number;
|
||||
// images 是该行解析出的图片地址。
|
||||
images: string[];
|
||||
// category 是该行最终采用的类目。
|
||||
category: PublishCategory;
|
||||
}
|
||||
|
||||
// PublishBatchDetailRow 表示批量任务中的单行执行结果。
|
||||
export interface PublishBatchDetailRow {
|
||||
// id 是持久化任务行标识。
|
||||
id: number;
|
||||
// row_no 是原始表格行号。
|
||||
row_no: number;
|
||||
// cookie_id 是该行使用的账号标识。
|
||||
cookie_id: string;
|
||||
// title 是该行商品标题。
|
||||
title: string;
|
||||
// price 是该行商品价格。
|
||||
price: string;
|
||||
// quantity 是该行商品库存数量。
|
||||
quantity: number;
|
||||
// status 是该行执行状态。
|
||||
status: string;
|
||||
// item_id 是发布成功后的商品标识。
|
||||
item_id: string;
|
||||
// item_url 是发布成功后的商品链接。
|
||||
item_url: string;
|
||||
// error_message 是该行失败原因。
|
||||
error_message: string;
|
||||
// failure_kind 是后端归类的失败类型。
|
||||
failure_kind: string;
|
||||
// images 是该行实际使用的图片地址。
|
||||
images?: string[];
|
||||
// category 是该行使用的类目。
|
||||
category: PublishCategory;
|
||||
}
|
||||
|
||||
// PublishBatchDetail 表示批量任务的汇总和逐行结果。
|
||||
export interface PublishBatchDetail {
|
||||
// id 是批量任务标识。
|
||||
id: string;
|
||||
// status 是批量任务当前状态。
|
||||
status: string;
|
||||
// filename 是上传的原始文件名。
|
||||
filename: string;
|
||||
// total 是任务总行数。
|
||||
total: number;
|
||||
// success 是成功行数。
|
||||
success: number;
|
||||
// failed 是失败行数。
|
||||
failed: number;
|
||||
// pending 是等待执行行数。
|
||||
pending: number;
|
||||
// running 是正在执行行数。
|
||||
running: number;
|
||||
// retryable 是可重试失败行数。
|
||||
retryable: number;
|
||||
// rows 是逐行执行结果。
|
||||
rows: PublishBatchDetailRow[];
|
||||
}
|
||||
|
||||
// PublishBatchPreview 表示批量预检的汇总和逐行结果。
|
||||
export interface PublishBatchPreview {
|
||||
// preview_id 是预检任务标识。
|
||||
preview_id: string;
|
||||
// total 是预检总行数。
|
||||
total: number;
|
||||
// valid 是可发布行数。
|
||||
valid: number;
|
||||
// invalid 是需要修正的行数。
|
||||
invalid: number;
|
||||
// rows 是逐行预检结果。
|
||||
rows: PublishBatchPreviewRow[];
|
||||
}
|
||||
|
||||
// BatchFallbackCategory 表示批量预检使用的默认类目。
|
||||
export interface BatchFallbackCategory {
|
||||
// catId 是默认类目 ID。
|
||||
catId: string;
|
||||
// catName 是默认类目名称。
|
||||
catName: string;
|
||||
// channelCatId 是默认频道类目 ID。
|
||||
channelCatId: string;
|
||||
// tbCatId 是默认淘宝类目 ID。
|
||||
tbCatId: string;
|
||||
}
|
||||
|
||||
// ItemPublishBatchState 描述批量铺货 Hook 暴露的服务端和表单状态。
|
||||
export interface ItemPublishBatchState {
|
||||
// showBatchModal 表示批量铺货弹窗是否打开。
|
||||
showBatchModal: boolean;
|
||||
// setShowBatchModal 控制批量铺货弹窗的显示状态。
|
||||
setShowBatchModal: Dispatch<SetStateAction<boolean>>;
|
||||
// batchLoading 表示批量任务请求是否正在执行。
|
||||
batchLoading: boolean;
|
||||
// batchPhase 是批量流程当前步骤。
|
||||
batchPhase: BatchPhase;
|
||||
// setBatchPhase 更新批量流程当前步骤。
|
||||
setBatchPhase: Dispatch<SetStateAction<BatchPhase>>;
|
||||
// batchFile 是待预检的商品表格文件。
|
||||
batchFile: File | null;
|
||||
// setBatchFile 更新待预检的商品表格。
|
||||
setBatchFile: Dispatch<SetStateAction<File | null>>;
|
||||
// batchImagesZip 是可选的商品图片压缩包。
|
||||
batchImagesZip: File | null;
|
||||
// setBatchImagesZip 更新可选的图片压缩包。
|
||||
setBatchImagesZip: Dispatch<SetStateAction<File | null>>;
|
||||
// batchCategoryKeyword 是默认类目搜索词。
|
||||
batchCategoryKeyword: string;
|
||||
// setBatchCategoryKeyword 更新默认类目搜索词。
|
||||
setBatchCategoryKeyword: Dispatch<SetStateAction<string>>;
|
||||
// batchCategoryLoading 表示默认类目推荐请求是否正在执行。
|
||||
batchCategoryLoading: boolean;
|
||||
// batchFallbackCategory 是默认类目配置。
|
||||
batchFallbackCategory: BatchFallbackCategory;
|
||||
// setBatchFallbackCategory 更新默认类目配置。
|
||||
setBatchFallbackCategory: Dispatch<SetStateAction<BatchFallbackCategory>>;
|
||||
// batchPreview 是最近一次预检结果。
|
||||
batchPreview: PublishBatchPreview | null;
|
||||
// setBatchPreview 更新最近一次预检结果。
|
||||
setBatchPreview: Dispatch<SetStateAction<ItemPublishBatchState['batchPreview']>>;
|
||||
// batchDetail 是当前批量任务详情。
|
||||
batchDetail: PublishBatchDetail | null;
|
||||
// setBatchDetail 更新当前批量任务详情。
|
||||
setBatchDetail: Dispatch<SetStateAction<PublishBatchDetail | null>>;
|
||||
// recentBatch 是页面入口处展示的最近任务。
|
||||
recentBatch: PublishBatchDetail | null;
|
||||
// setRecentBatch 更新页面入口处展示的最近任务。
|
||||
setRecentBatch: Dispatch<SetStateAction<PublishBatchDetail | null>>;
|
||||
// batchLocations 是批量任务可选的发货地列表。
|
||||
batchLocations: PublishLocation[];
|
||||
// batchLocation 是批量任务当前选择的发货地。
|
||||
batchLocation: PublishLocation | null;
|
||||
// setBatchLocations 更新批量发货地候选列表。
|
||||
setBatchLocations: Dispatch<SetStateAction<PublishLocation[]>>;
|
||||
// setBatchLocation 更新批量当前发货地。
|
||||
setBatchLocation: Dispatch<SetStateAction<PublishLocation | null>>;
|
||||
// openBatchModal 打开批量铺货流程并恢复可继续任务。
|
||||
openBatchModal: () => Promise<void>;
|
||||
// handleRecommendBatchCategory 请求默认类目推荐。
|
||||
handleRecommendBatchCategory: () => Promise<void>;
|
||||
// openRecentBatchResult 打开最近批量任务结果。
|
||||
openRecentBatchResult: () => Promise<void>;
|
||||
// handlePreviewBatch 提交文件并执行批量预检。
|
||||
handlePreviewBatch: () => Promise<void>;
|
||||
// handleStartBatch 启动预检通过的批量任务。
|
||||
handleStartBatch: () => Promise<void>;
|
||||
// handleCancelBatch 请求安全取消当前任务。
|
||||
handleCancelBatch: () => Promise<void>;
|
||||
// abandonBatchPreview 清理预检任务并回到上传步骤。
|
||||
abandonBatchPreview: () => Promise<void>;
|
||||
// closeBatchModal 关闭批量弹窗并清理临时预检任务。
|
||||
closeBatchModal: () => Promise<void>;
|
||||
// handleRetryBatchFailed 重试当前任务的失败行。
|
||||
handleRetryBatchFailed: () => Promise<void>;
|
||||
}
|
||||
|
||||
// ItemPublishBatchOptions 描述批量 Hook 依赖的页面上下文。
|
||||
export interface ItemPublishBatchOptions {
|
||||
// selectedAccount 是批量任务默认使用的账号。
|
||||
selectedAccount: string;
|
||||
// loadItems 刷新商品列表。
|
||||
loadItems: () => Promise<void>;
|
||||
// loadShippingRules 刷新商品关联规则。
|
||||
loadShippingRules: () => Promise<void>;
|
||||
}
|
||||
|
||||
// ItemReferenceState 描述商品页面 Hook 需要的共享列表状态。
|
||||
export interface ItemReferenceState {
|
||||
// items 是商品列表。
|
||||
items: Item[];
|
||||
// shippingRules 是商品关联的自动化规则。
|
||||
shippingRules: ShippingRule[];
|
||||
// accounts 是账号列表。
|
||||
accounts: AccountDetail[];
|
||||
}
|
||||
Reference in new issue
Block a user