/** * 京东上架「发到哪一家店」(2026-11-24 客户口径:「如果没有店铺登入情况 软件怎么知道我们要上架 * 是哪一个店铺?」;2026-11-26 客户口径「要跨店」后判据从「必须同店」改成「同店与跨店都行」)。 * * 三份身份,各有唯一出处,三层同一标记: * 1. **来源店铺**=所勾商品行的 `shopName/shopId`(京东数据库快照)。服务端建单时按它做 * 「一单只能发到一家店」校验(`JdPublishTaskService.resolveSingleShop`),并把它钉进 * 任务载荷与明细下发(`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 } /** 勾选的商品来自多家店 ⇒ 服务端也会拒(按店分开勾) */ | { state: 'multi'; shops: JdShopRef[]; 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 | 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}」新建为全新商品` + '(与来源店那件各自独立,需京东审核通过后才上架);目标店须已授权该商品的类目,' + '否则该商品会按「目标店没有授权类目」逐行失败。') } /** 跨店勾选整句(与服务端建单拒绝文案同口径:按店分开勾)。 */ export function multiShopMessage(shops: ReadonlyArray): 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 | null | undefined, detected: JdShopRef | null | undefined, ): JdShopGuard { const refs = sourceShopRefs(products) if (refs.length > 1) return { state: 'multi', shops: refs, message: multiShopMessage(refs) } const expected = refs[0] if (!expected) return { state: 'unknown' } const actual = normalizeShopRef(detected) 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 return guard.state === 'multi' } /** 拦截文案(空串=可建单):跨店档在协议不够时给升级提示,见 ``CROSS_UPGRADE_NOTICE``。 */ export function shopGuardMessage(guard: JdShopGuard, crossAllowed = false): string { if (guard.state === 'cross') return crossAllowed ? '' : CROSS_UPGRADE_NOTICE return guard.state === 'multi' ? guard.message : '' }