/** * 京东上架「发到哪一家店」(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 晚客户口径「去掉一次只能发一家店的约束」; * 2026-11-28 客户口径「不要限制,选完分类就建单」后未检测不再拦): * 已检测京麦 ⇒ 整单按跨店新增放行(目标店=京麦登录店,服务端 publishMode=NEW, * 与 cross 档同样要协议 ≥4);未检测 ⇒ 建单带 targetFromLogin(服务端仍 NEW 但不钉 * 目标店),客户端按**京麦当前登录店**发布(verify_shop 对未钉目标店只 WARNING 跳过核对)。 */ | { 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 } : {}) } } /** 人读标签:只出店名(2026-11-28 客户口径「shopId 啥的不要搞复杂」不上屏);缺名时 `店铺 ` 兜底保可识别。 */ export function shopLabel(raw: JdShopRef | null | undefined): string { const shop = normalizeShopRef(raw) if (!shop) return '未记录店铺' if (shop.shopName) return shop.shopName return `店铺 ${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 } /** * 跨店整句(2026-11-28 客户口径「不要搞复杂」压成一句):点名「采集店」与「目标店(京麦当前登录)」, * 保留三件客户要知道的事——在目标店**新建**商品、要过京东审核、目标店须已授权该商品的类目。 * 不写「稍后重试」(类目没授权是确定性失败)。 */ 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, target: JdShopRef): string { const labels = shops.map((shop) => shopLabel(shop)).join('、') return (`勾选的商品来自多个店铺(${labels}),将统一在京麦当前登录的「${shopLabel(target)}」` + '新建上架(需类目已授权,并通过京东审核)。') } /** * 多店还没检测京麦:建议句(2026-11-28 客户口径「不要限制」后不再拦建单)—— * 不检测也照常建单(targetFromLogin),客户端按京麦当前登录店发布;检测可钉定目标店、 * 客户端发布前会强核对,发了不匹配的店会整单判死,所以仍建议先检测。 */ export function multiShopDetectMessage(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) 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`` * 为真时放行(客户端会走新增商品路径)。2026-11-28 客户口径「不要限制」后多店 * 未检测不再拦:建单带 targetFromLogin,客户端按京麦当前登录店发布(页面只建议检测)。 */ export function shopGuardBlocks(guard: JdShopGuard, crossAllowed = false): boolean { if (guard.state === 'cross') return !crossAllowed if (guard.state === 'multi') return !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 '' return crossAllowed ? '' : CROSS_UPGRADE_NOTICE }