diff --git a/aliyun-sync/aliyun-aps-sync/src/sync.js b/aliyun-sync/aliyun-aps-sync/src/sync.js index c52c78a..259b279 100644 --- a/aliyun-sync/aliyun-aps-sync/src/sync.js +++ b/aliyun-sync/aliyun-aps-sync/src/sync.js @@ -1352,6 +1352,7 @@ async function syncOrderDetails(page, cachedOrderIds, options = {}) { console.log(`[订单详情续爬] 从 checkpoint 恢复: index=${startIndex}, records=${allDetails.length}`); } let currentListPage = 0; + let currentWindowKey = ''; await page.goto(datasets.orders.url, { waitUntil: 'domcontentloaded' }); await waitUntilReady(page, datasets.orders.heading); @@ -1360,7 +1361,14 @@ async function syncOrderDetails(page, cachedOrderIds, options = {}) { for (let index = startIndex; index < orderTargets.length; index += 1) { await runtimeCheckpoint(`订单详情 ${index + 1}/${orderTargets.length}`); const target = orderTargets[index]; - console.log(`[订单详情] ${index + 1}/${orderTargets.length} orderId=${target.orderId} page=${target.pageNum}`); + console.log(`[订单详情] ${index + 1}/${orderTargets.length} orderId=${target.orderId} page=${target.pageNum} window=${target.windowStart}~${target.windowEnd}`); + + const nextWindowKey = `${target.windowStart}|${target.windowEnd}`; + if (target.windowStart && target.windowEnd && currentWindowKey !== nextWindowKey) { + await restoreOrderWindow(page, target.windowStart, target.windowEnd); + currentWindowKey = nextWindowKey; + currentListPage = 1; + } if (target.pageNum > 0 && currentListPage !== target.pageNum) { const reached = await jumpToOrderPage(page, target.pageNum); @@ -1403,7 +1411,7 @@ async function syncOrderDetails(page, cachedOrderIds, options = {}) { } await page.goBack({ waitUntil: 'domcontentloaded' }).catch(() => null); - await recoverOrderListState(page, currentListPage).catch(() => null); + await recoverOrderListState(page, currentListPage, target.windowStart, target.windowEnd).catch(() => null); currentListPage = target.pageNum; } @@ -1985,6 +1993,8 @@ function collectOrderDetailTargets(records, cachedOrderIds = []) { for (const record of records) { const orderId = String(record.orderId || '').trim(); const pageNum = Number.parseInt(String(record.listPageNum || 0), 10) || 0; + const windowStart = String(record.windowStart || '').trim(); + const windowEnd = String(record.windowEnd || '').trim(); if (!orderId || !isValidOrderId(orderId) || pageNum <= 0) { continue; } @@ -1995,9 +2005,15 @@ function collectOrderDetailTargets(records, cachedOrderIds = []) { continue; } seen.add(orderId); - targets.push({ orderId, pageNum }); + targets.push({ orderId, pageNum, windowStart, windowEnd }); } - return targets.sort((a, b) => a.pageNum - b.pageNum); + return targets.sort((a, b) => { + const windowCompare = `${a.windowStart}|${a.windowEnd}`.localeCompare(`${b.windowStart}|${b.windowEnd}`); + if (windowCompare !== 0) { + return windowCompare; + } + return a.pageNum - b.pageNum; + }); } async function clickCustomerDetailFromList(page, target) { @@ -2124,6 +2140,14 @@ async function waitForStableOrderList(page) { await waitForTableRows(page).catch(() => null); } +async function restoreOrderWindow(page, windowStart, windowEnd) { + await waitUntilReady(page, datasets.orders.heading).catch(() => null); + await setDateRange(page, windowStart, windowEnd); + await clickQuery(page); + await trySetPageSize(page, datasets.orders.pageSize).catch(() => null); + await waitForStableOrderList(page).catch(() => null); +} + async function recoverCustomerListState(page, pageNum) { await waitUntilReady(page, datasets.customers.heading).catch(() => null); await trySetPageSize(page, datasets.customers.pageSize).catch(() => null); @@ -2133,9 +2157,13 @@ async function recoverCustomerListState(page, pageNum) { } } -async function recoverOrderListState(page, pageNum) { +async function recoverOrderListState(page, pageNum, windowStart = '', windowEnd = '') { await waitUntilReady(page, datasets.orders.heading).catch(() => null); - await trySetPageSize(page, datasets.orders.pageSize).catch(() => null); + if (windowStart && windowEnd) { + await restoreOrderWindow(page, windowStart, windowEnd).catch(() => null); + } else { + await trySetPageSize(page, datasets.orders.pageSize).catch(() => null); + } if (pageNum > 0) { await jumpToOrderPage(page, pageNum).catch(() => null); await waitForStableOrderList(page).catch(() => null);