mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 23:09:19 +08:00
refactor: 重构推广费导入链
This commit is contained in:
@@ -14,19 +14,26 @@ export interface BatchImportPddGoodsAdFeeRequest {
|
||||
items: BatchImportPddGoodsAdFeeItem[];
|
||||
}
|
||||
|
||||
export interface BatchImportPddGoodsAdFeeResult {
|
||||
/** 导入任务摘要(创建立即返回 pending,需轮询至终态) */
|
||||
export interface PddGoodsAdFeeImportTask {
|
||||
id: number;
|
||||
companyId?: number;
|
||||
channelShopId: number;
|
||||
status: 'pending' | 'processing' | 'success' | 'partial' | 'failed' | string;
|
||||
totalCount: number;
|
||||
successCount: number;
|
||||
failureCount: number;
|
||||
successIds?: number[];
|
||||
failureRecords?: Array<{
|
||||
rowNumber?: number;
|
||||
channelShopId?: number;
|
||||
goodsId?: string;
|
||||
failureReason?: string;
|
||||
}>;
|
||||
failCount: number;
|
||||
authExpired?: number;
|
||||
message?: string | null;
|
||||
startedAt?: string | null;
|
||||
finishedAt?: string | null;
|
||||
createTime?: string;
|
||||
}
|
||||
|
||||
const TERMINAL_STATUS = new Set(['success', 'partial', 'failed']);
|
||||
const POLL_INTERVAL_MS = 2500;
|
||||
const POLL_TIMEOUT_MS = 30 * 60 * 1000;
|
||||
|
||||
/** 插件统一模型 → 后端 batch-import 入参(日期取自每条 item;金额须大于 0) */
|
||||
export function toBatchImportRequest(
|
||||
batch: AdvertisingCostBatch,
|
||||
@@ -43,18 +50,52 @@ export function toBatchImportRequest(
|
||||
};
|
||||
}
|
||||
|
||||
export async function getPddGoodsAdFeeImportTask(
|
||||
taskId: number,
|
||||
): Promise<PddGoodsAdFeeImportTask> {
|
||||
return openApiRequest<PddGoodsAdFeeImportTask>(
|
||||
`${OPEN_API_PATHS.pddGoodsAdFeeImportTask}/${taskId}`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function waitForPddGoodsAdFeeImportTask(
|
||||
taskId: number,
|
||||
onProgress?: (task: PddGoodsAdFeeImportTask) => void,
|
||||
): Promise<PddGoodsAdFeeImportTask> {
|
||||
const deadline = Date.now() + POLL_TIMEOUT_MS;
|
||||
while (Date.now() < deadline) {
|
||||
const task = await getPddGoodsAdFeeImportTask(taskId);
|
||||
onProgress?.(task);
|
||||
if (TERMINAL_STATUS.has(task.status)) {
|
||||
return task;
|
||||
}
|
||||
await sleep(POLL_INTERVAL_MS);
|
||||
}
|
||||
throw new Error('导入超时,请稍后在后台查看任务进度');
|
||||
}
|
||||
|
||||
/** 提交导入任务并等到处理结束 */
|
||||
export async function batchImportPddGoodsAdFee(
|
||||
batch: AdvertisingCostBatch,
|
||||
): Promise<BatchImportPddGoodsAdFeeResult> {
|
||||
onProgress?: (task: PddGoodsAdFeeImportTask) => void,
|
||||
): Promise<PddGoodsAdFeeImportTask> {
|
||||
const body = toBatchImportRequest(batch);
|
||||
if (!body.items.length) {
|
||||
throw new Error('没有可传入的记录(推广费须大于 0)');
|
||||
}
|
||||
return openApiRequest<BatchImportPddGoodsAdFeeResult>(
|
||||
const created = await openApiRequest<PddGoodsAdFeeImportTask>(
|
||||
OPEN_API_PATHS.pddGoodsAdFeeBatchImport,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
);
|
||||
onProgress?.(created);
|
||||
return waitForPddGoodsAdFeeImportTask(created.id, onProgress);
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export type {
|
||||
BatchImportPddGoodsAdFeeItem,
|
||||
BatchImportPddGoodsAdFeeRequest,
|
||||
BatchImportPddGoodsAdFeeResult,
|
||||
PddGoodsAdFeeImportTask,
|
||||
} from './open/pddGoodsAdFee';
|
||||
export type { PddChannelShop, PageQueryDTO, PageResultVO } from './open/pddShop';
|
||||
|
||||
@@ -393,11 +393,20 @@ async function handleSync() {
|
||||
...batch.value,
|
||||
channelShopId: channelShopId.value!,
|
||||
};
|
||||
const result = await batchImportPddGoodsAdFee(payload);
|
||||
statusText.value = '同步完成';
|
||||
const result = await batchImportPddGoodsAdFee(payload, (task) => {
|
||||
const done = (task.successCount ?? 0) + (task.failCount ?? 0);
|
||||
statusText.value = `正在导入… ${done}/${task.totalCount ?? 0}`;
|
||||
});
|
||||
const failed = result.status === 'failed';
|
||||
const partial = result.status === 'partial';
|
||||
statusText.value = failed ? '同步失败' : partial ? '部分成功' : '同步完成';
|
||||
const authHint = result.authExpired
|
||||
? ',授权已失效请重新授权后再导失败商品'
|
||||
: '';
|
||||
syncResultText.value =
|
||||
`成功 ${result.successCount}/${result.totalCount}` +
|
||||
(result.failureCount ? `,失败 ${result.failureCount}` : '');
|
||||
(result.failCount ? `,失败 ${result.failCount}` : '') +
|
||||
authHint;
|
||||
} catch (error) {
|
||||
statusText.value = '同步失败';
|
||||
errorText.value = toUserMessage(error);
|
||||
|
||||
@@ -3,6 +3,7 @@ export const EXTENSION_NAME = 'ECBI 数据同步';
|
||||
export const OPEN_API_PATHS = {
|
||||
pddShopList: '/open/v1/channel/pdd/shop/list',
|
||||
pddGoodsAdFeeBatchImport: '/open/v1/channel/pdd/goods/adfee/batch-import',
|
||||
pddGoodsAdFeeImportTask: '/open/v1/channel/pdd/goods/adfee/import-task',
|
||||
extensionFeatureList: '/open/v1/extension/features',
|
||||
extensionFeatureScript: '/open/v1/extension/features',
|
||||
} as const;
|
||||
|
||||
Reference in New Issue
Block a user