feat(pdd): 原生内嵌拼多多多店铺订单同步子服务与anti-content纯算引擎
1. 纯算引擎 (pdd-anti-content.ts): - 100% 纯 TypeScript 原生还原 0as anti-content 算法,零 npm 依赖(仅基于 Node.js 原生 zlib 与 crypto)。 - 包含 22 个环境特征 Tag TLV 组包、zlib 标准压缩、4 字节尾部扰动盐与 64 字符动态位移 Base64 编码,单次耗时 < 0.1ms。 2. MMS 接口客户端与数据映射 (pdd-client.ts, pdd-order-mapper.ts): - 基于原生 fetch 实现高可用 MMS 请求,支持拼多多网关毫秒级服务器时间校准 (/api/server/_stm)。 - 实现拼多多订单状态与字段规范映射为平台 SourceOrderEvent,精准保留买家留言与充值账号信息。 3. 多店铺配置与防风控错峰调度 (pdd-source-config-service.ts, pdd-sync-service.ts): - 支持多店铺账号配置持久化(PostgreSQL app_config_entries 与本地 JSON 双模兜底)。 - 实现店铺间轻量错峰拉取机制,避免并发峰值触发平台 40002 频控。 4. 内存级自动进单与派单流转: - 拉取新订单后在 Node.js 内存中直接调用 upsertOrderFromSource(),自动完成 orders/order_items 入库、商品规则匹配与打手大厅工单 (work_orders) 实时广播。 5. 调度器与管理端 API (scheduler-service.ts, platform-config/pdd.ts): - 注册 pdd_order_sync 定时任务(默认 20s 轮询),提供管理端店铺配置查看、保存、删除及手动立即同步接口。 - 所有新增代码均遵循中英双语注释规范,并通过类型检查与单元测试。
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Pinduoduo Scheduled Order Sync Job
|
||||
* 拼多多定时订单同步任务 (错峰智能调度与异常保护)
|
||||
*/
|
||||
|
||||
import type { JsonObject } from '../../types/json.js'
|
||||
import { logInfo } from '../../utils/logger.js'
|
||||
import { syncAllPddStores } from '../platforms/pdd/pdd-sync-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 syncResult = await syncAllPddStores({ pageSize })
|
||||
|
||||
if (!syncResult.enabled) {
|
||||
return {
|
||||
ok: true,
|
||||
status: 'disabled',
|
||||
message: '拼多多同步子服务已全局停用',
|
||||
accountCount: syncResult.storeCount,
|
||||
checkedCount: 0,
|
||||
okCount: 0,
|
||||
failedCount: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const okCount = syncResult.results.filter((r) => !r.error).length
|
||||
const failedCount = syncResult.results.filter((r) => Boolean(r.error)).length
|
||||
const totalUpserted = syncResult.results.reduce((acc, r) => acc + r.upsertedCount, 0)
|
||||
const totalFetched = syncResult.results.reduce((acc, r) => acc + r.fetchedCount, 0)
|
||||
|
||||
logInfo('[pdd-order-sync-job]', '拼多多定时拉单执行完成', {
|
||||
storeCount: syncResult.storeCount,
|
||||
syncedCount: syncResult.syncedCount,
|
||||
okCount,
|
||||
failedCount,
|
||||
totalFetched,
|
||||
totalUpserted,
|
||||
})
|
||||
|
||||
return {
|
||||
ok: failedCount === 0,
|
||||
status: failedCount > 0 ? (okCount > 0 ? 'partial_error' : 'failed') : 'ok',
|
||||
message: `已同步 ${syncResult.syncedCount} 个店铺,拉取 ${totalFetched} 单,入库/更新 ${totalUpserted} 单${failedCount > 0 ? `,失败 ${failedCount} 个店铺` : ''}`,
|
||||
accountCount: syncResult.storeCount,
|
||||
checkedCount: syncResult.syncedCount,
|
||||
okCount,
|
||||
failedCount,
|
||||
asset: totalUpserted,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user