Files
crawler-plugin/frontend-vue/src/pages/jd/jd-publish-shop.ts
T
huangzd1997 9092849ae9 feat(京东上架): 选分类=整分类上架+多店勾选不再整单拒(2026-11-26 晚客户口径)
客户口径「商品信息不要显示,选分类上架就行」「去掉一次只能发一家店的约束」:

- 后端 JdPublishTaskService:resolveSingleShop→resolveSourceShops(多店不整单判死);
  applyTargetShop 四档——没带目标店且多店仍拒(老页面/老客户端 fail-closed),
  带目标店时异店或多店一律 NEW;多店任务级来源店不钉死(明细行各带来源店快照)
- 用户端:商品勾选列表/分页器下线,选中分类自动分页拉全整分类商品纳入上架集
  (终止按累计行数对撞 total,不按本页满不满判);multi 档带 target=与 cross 同档
  (协议≥4+琥珀人工确认),未检测=先点检测;建单目标店透传
- 台账:catch-silence blocks 338→339(loadProducts 拉取失败有红色出口,非静默)
- 判据:JdPublishTaskServiceTest 多店两支(新增不钉来源/老口径仍拒)+
  jd-publish-shop 纯函数+jd-publish-tab/mount 对齐
2026-11-26 17:59:31 +08:00

181 lines
9.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 京东上架「发到哪一家店」(2026-11-24 客户口径:「如果没有店铺登入情况 软件怎么知道我们要上架
* 是哪一个店铺?」;2026-11-26 客户口径「要跨店」后判据从「必须同店」改成「同店与跨店都行」)。
*
* 三份身份,各有唯一出处,三层同一标记:
* 1. **来源店铺**=上架集商品行(2026-11-26 晚起=所选分类全部商品行)的 `shopName/shopId`
* (京东数据库快照)。服务端建单时按它归一
* 发布模式(`JdPublishTaskService.resolveSourceShops`:单店可克隆、异店/多店整单走新增;
* 2026-11-26 晚客户口径「去掉一次只能发一家店的约束」后多店不再整单拒绝),
* 单店时钉进任务载荷、多店时只有明细行各带来源店(`JdPublishItemsPageVo.shopId/shopName`);
* 2. **京麦当前登录店铺**=桥 `get_jd_shop_info` 现拉(客户端起有头浏览器读京麦首屏),
* 不是页面缓存、也不是上次「同步分类」的残留;跨店时它就是**目标店**(服务端据此算
* `publishMode=NEW`,客户端走京麦「新增商品」路径);
* 3. **最终强拦截**=客户端发布前按模式再核一次(`JdPublishPage.verify_shop`:克隆对来源店、
* 新增对目标店),不一致先上报标记再中止,服务端 `JdPublishStaleTaskService` 命中即整单判死。
*
* 本文件是纯函数(node --test 直测):只产状态与中文文案,复选框禁用/错误条/提示色由调用方选。
*/
/** 判据标记:客户端 `SHOP_MISMATCH_MARK`、服务端 `JdPublishStaleTaskService.SHOP_MISMATCH_MARK` 同值。
* 跨店后本页不再产生这句(异店=可跨店),但客户端/服务端的任务错误里仍会出现,界面按标记识别。 */
export const SHOP_MISMATCH_MARK = '京东店铺不匹配'
/** 发布协议代次门槛(与客户端 `JD_PUBLISH_PROTOCOL`、服务端 `JdPublishProtocol` 同档)。 */
export const JD_PUBLISH_BRAND_PROTOCOL = 3
export const JD_PUBLISH_CROSS_PROTOCOL = 4
/** 本机协议不够时的升级提示(品牌档/跨店档各一句,调用方原样弹给用户)。 */
export const BRAND_UPGRADE_NOTICE = '请升级到支持目标品牌发布的数富AI客户端,并在客户端内打开本页。'
export const CROSS_UPGRADE_NOTICE =
'请升级到支持跨店上架的数富AI客户端(5.1.0 及以上),并在客户端内打开本页。'
export interface JdShopRef {
shopId?: string
shopName?: string
}
/** 来源店铺的最小子集(只要这两个字段,页面传 `JdPublishProductVo[]` 进来即可)。 */
export interface JdShopSource {
shopId?: string
shopName?: string
}
export type JdShopGuard =
/** 勾选的商品没带店铺信息(旧数据/未勾选)⇒ 页面不拦,交给客户端与服务端兜底 */
| { state: 'unknown' }
/** 有来源店铺、还没检测京麦 ⇒ 提示可检测(不拦) */
| { state: 'undetected'; expected: JdShopRef }
| { state: 'match'; expected: JdShopRef; actual: JdShopRef }
/** 京麦登录的不是来源店 ⇒ 跨店上架(目标店=京麦当前登录店,走新增商品路径) */
| { state: 'cross'; expected: JdShopRef; target: JdShopRef; message: string }
/**
* 勾选的商品来自多家店(2026-11-26 晚客户口径「去掉一次只能发一家店的约束」):
* 已检测京麦 ⇒ 整单按跨店新增放行(目标店=京麦登录店,服务端 publishMode=NEW,
* 与 cross 档同样要协议 ≥4 与人工确认);未检测 ⇒ 拦(建单没有目标店,服务端也会拒)。
*/
| { state: 'multi'; shops: JdShopRef[]; target: JdShopRef | null; message: string }
function text(value: unknown): string {
return String(value ?? '').trim()
}
/** 归一化店铺身份:两名皆空返回 null(把「没记录」和「记录了空串」当同一件事)。 */
export function normalizeShopRef(raw: JdShopRef | null | undefined): JdShopRef | null {
const shopId = text(raw?.shopId)
const shopName = text(raw?.shopName)
if (!shopId && !shopName) return null
return { ...(shopId ? { shopId } : {}), ...(shopName ? { shopName } : {}) }
}
/** 人读标签:`家居日用数据库店(shopId=228346061)`,缺一半只报有的那半。 */
export function shopLabel(raw: JdShopRef | null | undefined): string {
const shop = normalizeShopRef(raw)
if (!shop) return '未记录店铺'
if (shop.shopName && shop.shopId) return `${shop.shopName}(shopId=${shop.shopId})`
if (shop.shopName) return shop.shopName
return `shopId=${shop.shopId}`
}
/**
* 勾选商品的来源店铺去重(id 优先、回退店名):同一家店的不同行只算一家,
* 顺序按勾选先后稳定输出(页面文案不会因 Map 重排跳字)。
*
* 跨形状归一:库存里有行带 id、有行只带店名(快照字段缺失),both 指同一家店时
* 必须并成一条并补齐缺的字段——否则页面对同店勾选误报「来自多个店铺」。
*/
export function sourceShopRefs(products: ReadonlyArray<JdShopSource> | null | undefined): JdShopRef[] {
const refs: JdShopRef[] = []
for (const product of products ?? []) {
const shop = normalizeShopRef(product)
if (!shop) continue
const existing = refs.find((ref) => (shop.shopId && ref.shopId === shop.shopId)
|| (shop.shopName && ref.shopName === shop.shopName))
if (existing) {
if (shop.shopId && !existing.shopId) existing.shopId = shop.shopId
if (shop.shopName && !existing.shopName) existing.shopName = shop.shopName
continue
}
refs.push({ ...shop })
}
return refs
}
/**
* 跨店整句:点名「采集店」与「目标店(京麦当前登录)」,说明三件客户要知道的事——
* 会在目标店**新建**商品(与来源店那件各自独立,wareId 不同)、要过京东审核、
* 目标店必须已授权该商品的类目。不写「稍后重试」(类目没授权是确定性失败)。
*/
export function crossShopMessage(source: JdShopRef | null | undefined, target: JdShopRef | null | undefined): string {
const sourceLabel = shopLabel(source)
const targetLabel = shopLabel(target)
return (`跨店上架:商品采集自「${sourceLabel}」,将在京麦当前登录的「${targetLabel}」新建为全新商品`
+ '(与来源店那件各自独立,需京东审核通过后才上架);目标店须已授权该商品的类目,'
+ '否则该商品会按「目标店没有授权类目」逐行失败。')
}
/** 多店已检测京麦:整单跨店新增的说明句(与服务端 NEW 档口径一致)。 */
export function multiShopCrossMessage(shops: ReadonlyArray<JdShopRef>, target: JdShopRef): string {
const labels = shops.map((shop) => shopLabel(shop)).join('、')
return (`本次勾选的商品来自多个店铺(${labels}),将全部在京麦当前登录的「${shopLabel(target)}」`
+ '以「新增商品」方式新建上架(与各来源店的商品各自独立,需京东审核通过后才上架);'
+ '目标店须已授权各商品的类目,否则按「目标店没有授权类目」逐行失败。')
}
/** 多店还没检测京麦:先检测的指引句(不带目标店建单服务端也会拒)。 */
export function multiShopDetectMessage(shops: ReadonlyArray<JdShopRef>): string {
const labels = shops.map((shop) => shopLabel(shop)).join('、')
return (`本次勾选的商品来自多个店铺(${labels}):多店商品只能整单在京麦当前登录店铺`
+ '以新增商品方式上架,请先点「检测京麦店铺」确认登录店后再建单。')
}
/** true/false=能比;null=两边都缺可比字段(只认同一维度:id 对 id、名对名)。 */
function sameShop(expected: JdShopRef, actual: JdShopRef): boolean | null {
if (expected.shopId && actual.shopId) return expected.shopId === actual.shopId
if (expected.shopName && actual.shopName) return expected.shopName === actual.shopName
return null
}
/** 页面判据:来源店铺(勾选集) vs 京麦当前登录店铺(检测结果,null=没检测/没读到)。 */
export function evaluateShopGuard(
products: ReadonlyArray<JdShopSource> | null | undefined,
detected: JdShopRef | null | undefined,
): JdShopGuard {
const refs = sourceShopRefs(products)
const actual = normalizeShopRef(detected)
if (refs.length > 1) {
return {
state: 'multi',
shops: refs,
target: actual,
message: actual ? multiShopCrossMessage(refs, actual) : multiShopDetectMessage(refs),
}
}
const expected = refs[0]
if (!expected) return { state: 'unknown' }
if (!actual) return { state: 'undetected', expected }
const same = sameShop(expected, actual)
// 比不了(一边只有店名、另一边只有 id)不判不匹配:宁可留给客户端强拦截,也不误拦真实任务。
if (same === null) return { state: 'undetected', expected }
if (same) return { state: 'match', expected, actual }
return { state: 'cross', expected, target: actual, message: crossShopMessage(expected, actual) }
}
/**
* UI 唯一处断:跨店与「多店已检测」档默认拦(本机不支持跨店=协议 < 4),
* ``crossAllowed`` 为真时放行(客户端会走新增商品路径);
* 多店还没检测京麦必拦(建单没有目标店,服务端也会拒)。
*/
export function shopGuardBlocks(guard: JdShopGuard, crossAllowed = false): boolean {
if (guard.state === 'cross') return !crossAllowed
if (guard.state === 'multi') return !guard.target || !crossAllowed
return false
}
/** 拦截文案(空串=可建单):协议不够时给升级提示,见 ``CROSS_UPGRADE_NOTICE``。 */
export function shopGuardMessage(guard: JdShopGuard, crossAllowed = false): string {
if (guard.state === 'cross') return crossAllowed ? '' : CROSS_UPGRADE_NOTICE
if (guard.state !== 'multi') return ''
if (!guard.target) return guard.message
return crossAllowed ? '' : CROSS_UPGRADE_NOTICE
}