修复订单爬取

This commit is contained in:
ray
2026-04-29 15:46:44 +08:00
parent 9dac73c9ff
commit 22a5019df0

View File

@@ -207,6 +207,27 @@ async function getContext() {
return _context;
}
async function resolveActivePage(context, targetUrl = '') {
const pages = context.pages();
let page = null;
if (config.browserMode === 'cdp' && targetUrl) {
page = pages.find((item) => item.url().includes(targetUrl));
}
if (!page) {
page = pages[0] || await context.newPage();
}
if (config.browserMode === 'cdp') {
const pageIndex = pages.indexOf(page);
console.log(`[CDP] 使用 tab=${pageIndex >= 0 ? pageIndex : 'new'} url=${page.url() || '(blank)'}`);
await page.bringToFront().catch(() => null);
}
return page;
}
async function restoreStorageState(context) {
if (!fs.existsSync(config.storageStateFile)) {
return;
@@ -427,7 +448,7 @@ export async function login() {
process.once('SIGTERM', onSigterm);
try {
const page = context.pages()[0] || (await context.newPage());
const page = await resolveActivePage(context, '/detail/my_customer/~/customer/list');
await page.goto(datasets.customers.url, { waitUntil: 'domcontentloaded' });
console.log('请在打开的浏览器里完成阿里云伙伴中心登录。检测到进入“我的客户”和“账单查询”页面后,脚本会自动保存登录态并关闭浏览器。');
await waitUntilReady(page, datasets.customers.heading, 10 * 60 * 1000, { allowInteractiveAuth: true });
@@ -453,7 +474,7 @@ export async function syncAll(options = {}) {
try {
const summary = { startedAt: new Date().toISOString(), datasets: {} };
page = context.pages()[0] || (await context.newPage());
page = await resolveActivePage(context, '/detail/my_customer/~/customer/list');
if (config.fullSync) {
summary.datasets.customers = await syncCustomers(page);
@@ -496,7 +517,7 @@ export async function syncBillsOnly(options = {}) {
try {
const summary = { startedAt: new Date().toISOString(), datasets: {} };
page = context.pages()[0] || (await context.newPage());
page = await resolveActivePage(context, '/detail/bill/~/costCenter/bill');
summary.datasets.bills = await syncBills(page, options);
summary.finishedAt = new Date().toISOString();
@@ -526,7 +547,7 @@ export async function syncMessagesOnly(options = {}) {
try {
const summary = { startedAt: new Date().toISOString(), datasets: {} };
page = context.pages()[0] || (await context.newPage());
page = await resolveActivePage(context, '/message');
summary.datasets.messages = await syncMessages(page, options);
summary.finishedAt = new Date().toISOString();
@@ -574,7 +595,7 @@ export async function syncAllIncremental() {
try {
const summary = { startedAt: new Date().toISOString(), mode: 'incremental', datasets: {} };
page = context.pages()[0] || (await context.newPage());
page = await resolveActivePage(context, '/detail/order/~/costCenter/order');
summary.datasets.orders = await syncOrders(page, { incremental: true });
const latestOrders = loadCurrentState('orders', datasets.orders.uniqueKey);
const orderIdsForDetail = collectValidOrderIds(latestOrders.records || []);