feat(pdd-sync): 默认拉取已发货订单并支持配置单次拉单上限与抓取状态
This commit is contained in:
@@ -159,8 +159,15 @@ router.post(
|
||||
'/pdd/sync',
|
||||
createJsonHandler(
|
||||
async (req) => {
|
||||
const body = (req.body || {}) as { shopId?: string; pageSize?: number }
|
||||
const body = (req.body || {}) as {
|
||||
shopId?: string
|
||||
pageSize?: number
|
||||
orderType?: number
|
||||
}
|
||||
const shopId = String(body.shopId || '').trim()
|
||||
const config = getPddSourcesConfig()
|
||||
const pageSize = Math.min(100, Math.max(1, Number(body.pageSize || config.pageSize || 50)))
|
||||
const orderType = Number(body.orderType ?? config.orderType ?? 2)
|
||||
|
||||
if (shopId) {
|
||||
const store = getPddStoreById(shopId)
|
||||
@@ -170,10 +177,10 @@ router.post(
|
||||
errorCode: 'pdd_store_not_found',
|
||||
})
|
||||
}
|
||||
return syncStoreOrders(store, { pageSize: body.pageSize || 10 })
|
||||
return syncStoreOrders(store, { pageSize, orderType })
|
||||
}
|
||||
|
||||
return syncAllPddStores({ pageSize: body.pageSize || 10 })
|
||||
return syncAllPddStores({ pageSize, orderType })
|
||||
},
|
||||
{
|
||||
successMessage: '拼多多同步已触发',
|
||||
|
||||
@@ -124,13 +124,13 @@ export class PddClient {
|
||||
|
||||
/**
|
||||
* Query recent orders page from PDD MMS.
|
||||
* 单页查询拼多多商户近期订单(默认查询待发货订单)。
|
||||
* 单页查询拼多多商户近期订单(默认查询已发货订单)。
|
||||
*/
|
||||
public async queryRecentOrders(options: QueryOrdersOptions = {}): Promise<PddOrderQueryResult> {
|
||||
const {
|
||||
pageNumber = 1,
|
||||
pageSize = 20,
|
||||
orderType = 1, // 1: 待发货 (Pending shipment)
|
||||
pageSize = 50,
|
||||
orderType = 2, // 2: 已发货 (Shipped, default)
|
||||
afterSaleStatus = 0,
|
||||
groupType = 0,
|
||||
remarkStatus = -1,
|
||||
|
||||
@@ -27,6 +27,8 @@ export interface PddStoreConfigItem {
|
||||
export interface PddSourcesConfig {
|
||||
enabled: boolean
|
||||
pollIntervalSeconds: number
|
||||
orderType: number // 0: 全部, 1: 待发货, 2: 已发货 (默认 2)
|
||||
pageSize: number // 单次拉取数量 (默认 50)
|
||||
stores: PddStoreConfigItem[]
|
||||
}
|
||||
|
||||
@@ -167,9 +169,21 @@ export function normalizePddSourcesConfig(rawValue: unknown): PddSourcesConfig {
|
||||
? Math.min(3600, Math.max(10, rawInterval))
|
||||
: 20
|
||||
|
||||
const rawOrderType = Number(source.orderType)
|
||||
const orderType = Number.isInteger(rawOrderType) && [0, 1, 2].includes(rawOrderType)
|
||||
? rawOrderType
|
||||
: 2
|
||||
|
||||
const rawPageSize = Number(source.pageSize)
|
||||
const pageSize = Number.isInteger(rawPageSize)
|
||||
? Math.min(100, Math.max(10, rawPageSize))
|
||||
: 50
|
||||
|
||||
return {
|
||||
enabled: source.enabled !== false,
|
||||
pollIntervalSeconds,
|
||||
orderType,
|
||||
pageSize,
|
||||
stores,
|
||||
}
|
||||
}
|
||||
@@ -178,6 +192,8 @@ function createDefaultPddSourcesConfig(): PddSourcesConfig {
|
||||
return {
|
||||
enabled: true,
|
||||
pollIntervalSeconds: 20,
|
||||
orderType: 2,
|
||||
pageSize: 50,
|
||||
stores: [],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import { logError, logInfo, logWarn } from '../../../utils/logger.js'
|
||||
export interface SyncStoreOrdersOptions {
|
||||
pageNumber?: number | undefined
|
||||
pageSize?: number | undefined
|
||||
orderType?: number | undefined // 1: 待发货 (默认)
|
||||
orderType?: number | undefined // 2: 已发货 (默认), 1: 待发货, 0: 全部
|
||||
startTime?: number | undefined
|
||||
endTime?: number | undefined
|
||||
}
|
||||
@@ -71,9 +71,10 @@ export async function syncStoreOrders(
|
||||
}
|
||||
|
||||
const client = new PddClient(store)
|
||||
const pageSize = Math.min(50, Math.max(1, Number(options.pageSize || 10)))
|
||||
const sourcesConfig = getPddSourcesConfig()
|
||||
const pageSize = Math.min(100, Math.max(1, Number(options.pageSize || sourcesConfig.pageSize || 50)))
|
||||
const pageNumber = Math.max(1, Number(options.pageNumber || 1))
|
||||
const orderType = options.orderType ?? 1 // 1: 待发货 (Pending shipment)
|
||||
const orderType = options.orderType ?? sourcesConfig.orderType ?? 2 // 2: 已发货 (Shipped, default)
|
||||
|
||||
try {
|
||||
const queryOptions: QueryOrdersOptions = {
|
||||
|
||||
@@ -6,15 +6,18 @@
|
||||
import type { JsonObject } from '../../types/json.js'
|
||||
import { logInfo } from '../../utils/logger.js'
|
||||
import { syncAllPddStores } from '../platforms/pdd/pdd-sync-service.js'
|
||||
import { getPddSourcesConfig } from '../platforms/pdd/pdd-source-config-service.js'
|
||||
|
||||
export async function runPddOrderSyncJob(job: JsonObject) {
|
||||
const config =
|
||||
job.config && typeof job.config === 'object' && !Array.isArray(job.config)
|
||||
? (job.config as JsonObject)
|
||||
: {}
|
||||
const pageSize = Math.min(50, Math.max(1, Number(config.pageSize || 10)))
|
||||
const sourcesConfig = getPddSourcesConfig()
|
||||
const pageSize = Math.min(100, Math.max(1, Number(config.pageSize || sourcesConfig.pageSize || 50)))
|
||||
const orderType = Number(config.orderType ?? sourcesConfig.orderType ?? 2)
|
||||
|
||||
const syncResult = await syncAllPddStores({ pageSize })
|
||||
const syncResult = await syncAllPddStores({ pageSize, orderType })
|
||||
|
||||
if (!syncResult.enabled) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user