feat(douyin-mashida): 皮肤自动发货(按内部 SKU 分流 + 领取链接 + 买家领取接口)
抖音码速达的皮肤类商品改为「自动发货 + 领取链接」:买家不用输订单号,点链接直接领。
**分流(按内部 SKU)**
- 平台闸门放行 `provider='douyin_mashida' && platform='doudian'`,走**独立分支**
(原有 91/快手路径一行未动,避免外溢)
- 判据:内部 SKU(`sku_order_list[].code`,即 order_items.sku_code)命中权威表
`AFFILIATE_DASH_SKU_MAPPING` → 新执行器 `douyin_mashida_skin`;未命中 → `manual_dispatch`
- 皮肤判定与商品图同源(都用那张权威表),保证「能路由 ⇒ 有图」
- 履约路由新增 `internal_sku` 匹配维度(`rules[].internalSku`,NFKC+去空白+小写,exact/contains);
旧规则缺 `matchField` 时默认 `product_name`,**归一化/优先级/回退顺序完全不变**
**执行器与领取 token**
- 新增 `douyin-mashida-skin-executor`:建 claim token、置 `link_generated/pending_claim`,
`resolveDeliveryLink` 返回本站路径式领取链接;注册进 registry、planner 分支、
bootstrap 种子 profile(`抖音皮肤自动发货`);进 `ROUTABLE_EXECUTOR_KEYS`
- token 粒度 = 一个履约单元一个(`claim_tokens.task_id` 唯一);一单多项时
cert_link/买家消息取 task id 最小的那个并告警
**cert_link 与发消息**
- 皮肤订单且该订单项已有 token 且配了 `claimLinkBase` → `cert_link = claimLinkBase/token`;
其它情况一律用 `certLink`(老行为不变)
- 领取链接**只认** `claimLinkBase`,绝不回退 `buildClaimUrl`/`orders.claimBaseUrl`
(那是 hash 形式的打手领取页,语义不同)
- 付款建单+发券成功后按 `messageTemplate`(`{link}/{sku}/{orderNo}/{productTitle}`)
经 `order.message` 发给买家;best-effort,失败只记 warn
**买家领取接口**
- `GET/POST /api/v1/claim-skin/:token`(限流、公开);详情含内部SKU/引流标题/商品图/
数量/金额/支付时间/动态表单字段;token 失效给明确 errorCode(404/410/409),不打 500
- 提交账号:先核销平台券再落记录(避免「记账了但券没核销」的资损),已提交幂等
**隐私**
- 买家游戏账号落 `fulfillment_tasks.claim_submission_json`(迁移 096),
**不进** `work_orders.material_json`(那是发给打手的素材);详情接口不返回券码明文/手机号/地址/open_id
This commit is contained in:
@@ -9,6 +9,15 @@ import { FULFILLMENT_EXECUTOR_KEYS } from './executors/types.js'
|
||||
|
||||
export type FulfillmentRoutingRuleMatchType = 'exact' | 'contains'
|
||||
|
||||
/**
|
||||
* 规则匹配维度:
|
||||
* - `product_name`:按**商品名**匹配(历史规则,语义完全不变);
|
||||
* - `internal_sku`:按**内部 SKU**匹配(如抖店 `sku_order_list[].code` = 「幸运币2个」)。
|
||||
* 内部 SKU 用 `normalizeInternalSkuMatchKey` 归一(NFKC + 去空白 + 小写),
|
||||
* 这样全角数字/多余空格也能命中权威映射表里的写法。
|
||||
*/
|
||||
export type FulfillmentRoutingRuleMatchField = 'product_name' | 'internal_sku'
|
||||
|
||||
export type FulfillmentRoutingExecutorConfig = {
|
||||
enabled: boolean
|
||||
}
|
||||
@@ -16,8 +25,11 @@ export type FulfillmentRoutingExecutorConfig = {
|
||||
export type FulfillmentRoutingRule = {
|
||||
id: string
|
||||
enabled: boolean
|
||||
matchField: FulfillmentRoutingRuleMatchField
|
||||
productName: string
|
||||
normalizedProductName: string
|
||||
internalSku: string
|
||||
normalizedInternalSku: string
|
||||
matchType: FulfillmentRoutingRuleMatchType
|
||||
executorKey: string
|
||||
priority: number
|
||||
@@ -63,6 +75,7 @@ const FULFILLMENT_ROUTING_CONFIG_FILE_PATH = path.join(
|
||||
|
||||
const ROUTABLE_EXECUTOR_KEYS: string[] = [
|
||||
FULFILLMENT_EXECUTOR_KEYS.AFFILIATE_DASH,
|
||||
FULFILLMENT_EXECUTOR_KEYS.DOUYIN_MASHIDA_SKIN,
|
||||
FULFILLMENT_EXECUTOR_KEYS.MANUAL_DISPATCH,
|
||||
]
|
||||
|
||||
@@ -131,11 +144,13 @@ export function normalizeFulfillmentRoutingConfig(rawValue: unknown): Fulfillmen
|
||||
|
||||
export function resolveFulfillmentRoute({
|
||||
productName,
|
||||
internalSku = '',
|
||||
candidates,
|
||||
config = getFulfillmentRoutingConfig(),
|
||||
preferredExecutorKey = '',
|
||||
}: {
|
||||
productName: unknown
|
||||
internalSku?: unknown
|
||||
candidates: FulfillmentRoutingCandidate[]
|
||||
config?: FulfillmentRoutingConfig
|
||||
preferredExecutorKey?: string
|
||||
@@ -167,7 +182,10 @@ export function resolveFulfillmentRoute({
|
||||
}
|
||||
}
|
||||
|
||||
const matchedRule = findMatchedRoutingRule(productName, normalizedConfig.rules)
|
||||
// 匹配顺序刻意保持:先按商品名(历史规则,行为完全不变),再按内部 SKU(新增维度)。
|
||||
const matchedRule =
|
||||
findMatchedRoutingRule(productName, normalizedConfig.rules) ||
|
||||
findMatchedInternalSkuRoutingRule(internalSku, normalizedConfig.rules)
|
||||
if (matchedRule) {
|
||||
const selected = resolveExecutorSelection({
|
||||
executorKey: matchedRule.executorKey,
|
||||
@@ -381,7 +399,10 @@ function findMatchedRoutingRule(productName: unknown, rules: FulfillmentRoutingR
|
||||
|
||||
return (
|
||||
rules.find((rule) => {
|
||||
if (rule.enabled === false || !rule.normalizedProductName) {
|
||||
if (rule.enabled === false || rule.matchField !== 'product_name') {
|
||||
return false
|
||||
}
|
||||
if (!rule.normalizedProductName) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -394,13 +415,87 @@ function findMatchedRoutingRule(productName: unknown, rules: FulfillmentRoutingR
|
||||
)
|
||||
}
|
||||
|
||||
/** 内部 SKU 维度的规则匹配:与商品名规则同样的 exact/contains 语义。 */
|
||||
function findMatchedInternalSkuRoutingRule(internalSku: unknown, rules: FulfillmentRoutingRule[]) {
|
||||
const normalizedInternalSku = normalizeInternalSkuMatchKey(internalSku)
|
||||
if (!normalizedInternalSku) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
rules.find((rule) => {
|
||||
if (rule.enabled === false || rule.matchField !== 'internal_sku') {
|
||||
return false
|
||||
}
|
||||
if (!rule.normalizedInternalSku) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (rule.matchType === 'contains') {
|
||||
return normalizedInternalSku.includes(rule.normalizedInternalSku)
|
||||
}
|
||||
|
||||
return normalizedInternalSku === rule.normalizedInternalSku
|
||||
}) || null
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部 SKU 归一化:NFKC(全角→半角)→ 去所有空白 → 小写。
|
||||
* 与权威映射表 `AFFILIATE_DASH_SKU_MAPPING` 的查询归一化保持同一套语义。
|
||||
*/
|
||||
export function normalizeInternalSkuMatchKey(value: unknown): string {
|
||||
return String(value || '')
|
||||
.normalize('NFKC')
|
||||
.replace(/\s+/g, '')
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则归一化。
|
||||
*
|
||||
* 兼容旧配置:没有 `matchField` / `internalSku` 的规则一律视为**商品名**规则,行为与之前完全一致
|
||||
* (id、归一化键、matchType、priority 的算法都没动)。
|
||||
* 新维度通过 `internalSku`(或显式 `matchField: 'internal_sku'`)声明。
|
||||
*/
|
||||
function normalizeFulfillmentRoutingRule(rawValue: unknown): FulfillmentRoutingRule | null {
|
||||
const source = isPlainObject(rawValue) ? rawValue : {}
|
||||
const matchField = resolveRuleMatchField(source)
|
||||
const executorKey = normalizeExecutorKey(source.executorKey)
|
||||
|
||||
if (matchField === 'internal_sku') {
|
||||
const internalSku = String(source.internalSku ?? source.sku ?? '').trim()
|
||||
const normalizedInternalSku =
|
||||
normalizeInternalSkuMatchKey(source.normalizedInternalSku) ||
|
||||
normalizeInternalSkuMatchKey(internalSku)
|
||||
|
||||
if (!internalSku || !normalizedInternalSku || !executorKey) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
id:
|
||||
String(source.id || `${normalizedInternalSku}:${executorKey}`).trim() ||
|
||||
`${normalizedInternalSku}:${executorKey}`,
|
||||
enabled: source.enabled !== false,
|
||||
matchField: 'internal_sku',
|
||||
// 展示与诊断仍走 productName 字段(值为内部 SKU);normalizedProductName 留空,
|
||||
// 保证商品名匹配器永远不会把它当成商品名规则命中。
|
||||
productName: internalSku,
|
||||
normalizedProductName: '',
|
||||
internalSku,
|
||||
normalizedInternalSku,
|
||||
matchType: source.matchType === 'contains' ? 'contains' : 'exact',
|
||||
executorKey,
|
||||
priority: normalizeInteger(source.priority, 100),
|
||||
notes: String(source.notes || '').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
const productName = String(source.productName || source.name || '').trim()
|
||||
const normalizedProductName =
|
||||
normalizeProductMatchName(source.normalizedProductName) ||
|
||||
normalizeProductMatchName(productName)
|
||||
const executorKey = normalizeExecutorKey(source.executorKey)
|
||||
|
||||
if (!productName || !normalizedProductName || !executorKey) {
|
||||
return null
|
||||
@@ -411,8 +506,11 @@ function normalizeFulfillmentRoutingRule(rawValue: unknown): FulfillmentRoutingR
|
||||
String(source.id || `${normalizedProductName}:${executorKey}`).trim() ||
|
||||
`${normalizedProductName}:${executorKey}`,
|
||||
enabled: source.enabled !== false,
|
||||
matchField: 'product_name',
|
||||
productName,
|
||||
normalizedProductName,
|
||||
internalSku: '',
|
||||
normalizedInternalSku: '',
|
||||
matchType: source.matchType === 'contains' ? 'contains' : 'exact',
|
||||
executorKey,
|
||||
priority: normalizeInteger(source.priority, 100),
|
||||
@@ -420,6 +518,16 @@ function normalizeFulfillmentRoutingRule(rawValue: unknown): FulfillmentRoutingR
|
||||
}
|
||||
}
|
||||
|
||||
function resolveRuleMatchField(source: JsonObject): FulfillmentRoutingRuleMatchField {
|
||||
if (String(source.matchField || '').trim() === 'internal_sku') {
|
||||
return 'internal_sku'
|
||||
}
|
||||
|
||||
const hasProductName = Boolean(String(source.productName || source.name || '').trim())
|
||||
const hasInternalSku = Boolean(String(source.internalSku ?? source.sku ?? '').trim())
|
||||
return !hasProductName && hasInternalSku ? 'internal_sku' : 'product_name'
|
||||
}
|
||||
|
||||
function normalizeRoutingCandidates(candidates: FulfillmentRoutingCandidate[]) {
|
||||
const result = new Map<string, FulfillmentRoutingCandidate>()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user