订单最后一页问题处理

This commit is contained in:
ray
2026-05-08 10:27:06 +08:00
parent 3a7f91419e
commit f4854a2630

View File

@@ -1352,6 +1352,7 @@ async function syncOrderDetails(page, cachedOrderIds, options = {}) {
console.log(`[订单详情续爬] 从 checkpoint 恢复: index=${startIndex}, records=${allDetails.length}`); console.log(`[订单详情续爬] 从 checkpoint 恢复: index=${startIndex}, records=${allDetails.length}`);
} }
let currentListPage = 0; let currentListPage = 0;
let currentWindowKey = '';
await page.goto(datasets.orders.url, { waitUntil: 'domcontentloaded' }); await page.goto(datasets.orders.url, { waitUntil: 'domcontentloaded' });
await waitUntilReady(page, datasets.orders.heading); 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) { for (let index = startIndex; index < orderTargets.length; index += 1) {
await runtimeCheckpoint(`订单详情 ${index + 1}/${orderTargets.length}`); await runtimeCheckpoint(`订单详情 ${index + 1}/${orderTargets.length}`);
const target = orderTargets[index]; 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) { if (target.pageNum > 0 && currentListPage !== target.pageNum) {
const reached = await jumpToOrderPage(page, 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 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; currentListPage = target.pageNum;
} }
@@ -1985,6 +1993,8 @@ function collectOrderDetailTargets(records, cachedOrderIds = []) {
for (const record of records) { for (const record of records) {
const orderId = String(record.orderId || '').trim(); const orderId = String(record.orderId || '').trim();
const pageNum = Number.parseInt(String(record.listPageNum || 0), 10) || 0; 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) { if (!orderId || !isValidOrderId(orderId) || pageNum <= 0) {
continue; continue;
} }
@@ -1995,9 +2005,15 @@ function collectOrderDetailTargets(records, cachedOrderIds = []) {
continue; continue;
} }
seen.add(orderId); 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) { async function clickCustomerDetailFromList(page, target) {
@@ -2124,6 +2140,14 @@ async function waitForStableOrderList(page) {
await waitForTableRows(page).catch(() => null); 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) { async function recoverCustomerListState(page, pageNum) {
await waitUntilReady(page, datasets.customers.heading).catch(() => null); await waitUntilReady(page, datasets.customers.heading).catch(() => null);
await trySetPageSize(page, datasets.customers.pageSize).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 waitUntilReady(page, datasets.orders.heading).catch(() => null);
if (windowStart && windowEnd) {
await restoreOrderWindow(page, windowStart, windowEnd).catch(() => null);
} else {
await trySetPageSize(page, datasets.orders.pageSize).catch(() => null); await trySetPageSize(page, datasets.orders.pageSize).catch(() => null);
}
if (pageNum > 0) { if (pageNum > 0) {
await jumpToOrderPage(page, pageNum).catch(() => null); await jumpToOrderPage(page, pageNum).catch(() => null);
await waitForStableOrderList(page).catch(() => null); await waitForStableOrderList(page).catch(() => null);