diff --git a/apps/backend/src/routes/pdd-extension.ts b/apps/backend/src/routes/pdd-extension.ts index 660ef1a4..81e1e1ac 100644 --- a/apps/backend/src/routes/pdd-extension.ts +++ b/apps/backend/src/routes/pdd-extension.ts @@ -124,6 +124,41 @@ router.post('/sync-cookie', async (req, res, next) => { }) if (!testResult.valid) { + // 容错机制:若 Cookie 包含 PASS_ID 且拼多多官方接口触发频控 (40002),说明会话身份有效已通达官方网关 + // (若未登录或已失效,PDD 会优先返回 43001 会话过期,而非 40002 频控)。 + // 此时采纳并保存 Cookie,避免客服陷入重复点击导致的频控死循环。 + if (testResult.rateLimited && hasPassId) { + logWarn( + '[pdd-extension]', + `插件上报店铺 [${store.shopName}(${store.shopId})] Cookie 探测触发频控 (40002),但包含有效 PASS_ID,视为有效并采纳更新`, + { + shopId: store.shopId, + hasPassId, + hasRckk, + }, + ) + + const updatedStore = await updatePddStoreCookieFromExtension(store.shopId, { + cookie: decrypted.cookie, + userAgent: decrypted.userAgent, + etag: decrypted.etag, + }) + + res.json({ + code: 0, + message: + 'Cookie 同步成功!(网关提示操作过于频繁 40002,凭证已保存生效,后台将在静默后自动拉单)', + data: { + shopId: updatedStore.shopId, + shopName: updatedStore.shopName, + valid: true, + rateLimited: true, + lastExtensionSyncAt: updatedStore.lastExtensionSyncAt, + }, + }) + return + } + logWarn('[pdd-extension]', `插件上报的 Cookie 实测未通过: ${testResult.message}`, { shopId: store.shopId, hasPassId, diff --git a/apps/backend/src/services/platforms/pdd/pdd-client.ts b/apps/backend/src/services/platforms/pdd/pdd-client.ts index 4504191a..6f7fbfb7 100644 --- a/apps/backend/src/services/platforms/pdd/pdd-client.ts +++ b/apps/backend/src/services/platforms/pdd/pdd-client.ts @@ -46,6 +46,7 @@ export interface PddOrderQueryResult { export interface PddTestCookieResult { valid: boolean + rateLimited?: boolean | undefined message: string totalOrders?: number | undefined firstOrderSn?: string | undefined @@ -253,16 +254,19 @@ export class PddClient { }) try { + const nowSec = Math.floor(Date.now() / 1000) const result = await client.queryRecentOrders({ pageNumber: 1, pageSize: 1, - orderType: 0, + orderType: 2, // 2: 已发货 (比 orderType: 0 扫描全量库更轻量,不容易触发拼多多 MMS 网关频控) + startTime: nowSec - 86400 * 7, // 探测最近 7 天 + endTime: nowSec, }) const firstItem = result.items[0] return { valid: true, - message: `Cookie 有效,网关校验通过(店铺在售/历史订单数: ${result.total})`, + message: `Cookie 有效,网关校验通过(近期订单数: ${result.total})`, totalOrders: result.total, firstOrderSn: firstItem?.order_sn, } @@ -279,9 +283,10 @@ export class PddClient { message: `Cookie 已失效或登录已过期: ${errMsg}`, } } - if (errMsg.includes('40002') || errMsg.includes('频控')) { + if (errMsg.includes('40002') || errMsg.includes('频控') || errMsg.includes('频繁')) { return { valid: false, + rateLimited: true, message: `触发网关频控 (40002): 操作过于频繁,请稍后重试`, } }