feat(京东跨店上架): 用户端 cross 档 + 目标店二次确认 + 协议 4 闸

客户口径「要跨店」:异店不再硬拦,改成显形目标店 + 风险确认后建单(客户端走京麦新增商品)。

- jd-publish-shop:evaluateShopGuard 加 cross 态(目标店=京麦当前登录店);
  shopGuardBlocks/shopGuardMessage 接受 crossAllowed,协议 <4 时仍拦并给升级文案
- JdPublishTab:跨店显形「目标店(跨店)」行、琥珀风险句(会在目标店新建商品并过京东审核、
  目标店须已授权该类目)、原生勾选二次确认作硬闸;按钮文案改「跨店上架 N 个商品到目标店」;
  协议 >=4 才放行;建单请求带 targetShopId/targetShopName
- api:createJdPublishTask 增加可选 targetShop,body 按需带两个键

判据:jd-publish-shop.test.ts 17 支(cross 五态、跨店文案不写「稍后重试」、协议常量与源码绊线)、
mount 32 支(协议 3 只给升级提示不建单、协议 4 确认后带目标店建单);三次反向验证改坏即红。
node --test 1147/vitest 149/vue-tsc --noEmit rc=0;可读基线 JdPublishTab.vue 1014→1075 逐条点名。
This commit is contained in:
2026-11-26 11:51:34 +08:00
parent 1307caf5e5
commit f88b3d4622
5 changed files with 231 additions and 70 deletions
+87 -26
View File
@@ -25,8 +25,13 @@ import { retryClientTask } from '@/shared/api/task-retry'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { ensureJdLogin, markJdLoginExpired } from '@/shared/stores/jd-login-autoflow'
import {
BRAND_UPGRADE_NOTICE,
CROSS_UPGRADE_NOTICE,
JD_PUBLISH_BRAND_PROTOCOL,
JD_PUBLISH_CROSS_PROTOCOL,
evaluateShopGuard,
shopGuardBlocks,
shopGuardMessage,
shopLabel,
type JdShopRef,
} from '@/pages/jd/jd-publish-shop'
@@ -199,6 +204,27 @@ const SHOP_DETECT_UNAVAILABLE =
const shopGuard = computed(() => evaluateShopGuard(selectedProductRows.value, currentShop.value))
/** 本机发布协议代次(0=读不到/老客户端;品牌要 ≥3、跨店要 ≥4,见 jd-publish-shop 常量)。 */
const localProtocol = ref(0)
const crossSupported = computed(() => localProtocol.value >= JD_PUBLISH_CROSS_PROTOCOL)
/** 异店=跨店上架:目标店就是京麦当前登录店(服务端据此算 publishMode=NEW)。 */
const usesCrossShop = computed(() => shopGuard.value.state === 'cross')
/** 跨店必须人工确认一次(会在目标店新建商品、要过京东审核),勾选或店铺变化即重置。 */
const crossConfirmed = ref(false)
watch(shopGuard, () => { crossConfirmed.value = false })
async function readPublishProtocol(): Promise<number> {
try {
const value = await getPywebviewApi()?.jd_publish_protocol?.()
return typeof value === 'number' && Number.isFinite(value) ? value : 0
} catch (error) {
// 桥拒绝/异常按「本机协议未知」处理:品牌与跨店两个调用方都会弹升级提示(用户可见出口)。
const detail = error instanceof Error ? error.message : String(error)
console.error('[jd-publish] 读取本机发布协议失败:%s', detail)
return 0
}
}
/** 来源店铺文案:没取到/多家店/单店三档(多店由 shopGuard 直接拦)。 */
const sourceShopText = computed(() => {
const guard = shopGuard.value
@@ -207,6 +233,11 @@ const sourceShopText = computed(() => {
return shopLabel(guard.expected)
})
/** 跨店档的目标店文案(同店/未检测档为空,模板按它决定是否显示目标店行)。 */
const crossTargetText = computed(() => (usesCrossShop.value && shopGuard.value.state === 'cross'
? shopLabel(shopGuard.value.target)
: ''))
const currentShopText = computed(() => (currentShop.value ? shopLabel(currentShop.value) : '未检测'))
/** 挂载时判可用性:老客户端没有 get_jd_shop_info ⇒ 检测按钮置灰(同「同步分类」口径)。 */
@@ -214,6 +245,11 @@ function refreshShopDetectability() {
canDetectShop.value = typeof getPywebviewApi()?.get_jd_shop_info === 'function'
}
/** 挂载时读一次协议代次:跨店确认框与按钮禁用都按它算,读不到按 0(未支持)处理。 */
async function refreshPublishProtocol() {
localProtocol.value = await readPublishProtocol()
}
/**
* 点「检测当前京麦店铺」:桥现拉京麦首屏(会起客户端专用浏览器,秒级)。
* 失败按 code 分档——jd_busy 走 amber 说明、jd_login 走自动登录链路(登录成功后自动重测一次)、
@@ -270,41 +306,33 @@ async function detectShop(options?: { fromAutoLogin?: boolean }) {
}
/**
* 建单前把店铺核一次:已检测且一致就不再开浏览器;命中不匹配/跨店直接给拦截文案。
* 建单前把店铺核一次:已检测且同店就不再开浏览器;跨店档要本机协议支持(≥4,否则升级提示)。
* 检测失败(页面结构变更、老客户端、京麦任务在跑)**不**拦创建——最终强拦截在客户端
* 执行器(先上报标记再中止)与服务端(命中即整单判死),页面这层只负责提前告知。
*/
async function ensureShopChecked(): Promise<string> {
const guard = shopGuard.value
if (shopGuardBlocks(guard)) return guard.message
const issue = shopGuardMessage(guard, crossSupported.value)
if (issue) return issue
if (guard.state !== 'undetected') return ''
await detectShop()
const next = shopGuard.value
return shopGuardBlocks(next) ? next.message : ''
return shopGuardMessage(shopGuard.value, crossSupported.value)
}
const submitDisabled = computed(() =>
submitting.value
|| detectingShop.value
|| shopGuardBlocks(shopGuard.value)
|| shopGuardBlocks(shopGuard.value, crossSupported.value)
|| (usesCrossShop.value && !crossConfirmed.value)
|| !selectedGroupId.value
|| !selectedProducts.value.size
|| !isValidBrand(targetBrandName.value)
|| priceValueMissing.value)
// 此闸只约束京东品牌发布,不改变客户端其他功能的全局版本门槛。
const JD_PUBLISH_PROTOCOL = 3
const JD_UPGRADE_NOTICE = '请升级到支持目标品牌发布的数富AI客户端,并在客户端内打开本页。'
// 此闸只约束京东品牌发布与跨店上架,不改变客户端其他功能的全局版本门槛(两档常量为单出口)。
async function supportsBrandPublishing() {
try {
return await getPywebviewApi()?.jd_publish_protocol?.() === JD_PUBLISH_PROTOCOL
} catch (error) {
// 桥拒绝/异常按「本机不支持目标品牌」处理,false 会让两个调用方弹升级提示(用户可见出口)。
const detail = error instanceof Error ? error.message : String(error)
console.error('[jd-publish] 读取本机发布协议失败:%s', detail)
return false
}
localProtocol.value = await readPublishProtocol()
return localProtocol.value >= JD_PUBLISH_BRAND_PROTOCOL
}
// 通用重试回执不带代数:旧代 + 1 只用作读侧下限,不生成任务或队列数据。
@@ -614,7 +642,7 @@ async function interruptTaskRecord(item: JdPublishHistoryItemVo | null | undefin
async function promptRetryBrand(): Promise<string | null> {
if (!await supportsBrandPublishing()) {
ElMessage.error(JD_UPGRADE_NOTICE)
ElMessage.error(BRAND_UPGRADE_NOTICE)
return null
}
const name = await ElMessageBox.prompt('请输入当前京麦店铺可用的目标品牌名称。', '修改品牌并重试', {
@@ -719,16 +747,26 @@ async function startPublish() {
const productIds = selectedProductIds.value
const brand = targetBrandName.value
if (!await supportsBrandPublishing()) {
errorMessage.value = JD_UPGRADE_NOTICE
errorMessage.value = BRAND_UPGRADE_NOTICE
return
}
// 店铺核对在建单之前:京麦登录的不是勾选商品那家店时一条都不发(客户口径「强拦截」)。
if (usesCrossShop.value && !crossSupported.value) {
errorMessage.value = CROSS_UPGRADE_NOTICE
return
}
// 店铺核对在建单之前:多店勾选、或本机不支持跨店时一条都不发;
// 异店(跨店)档由用户在上方勾选确认后才允许建单。
const shopIssue = await ensureShopChecked()
if (shopIssue) {
errorMessage.value = shopIssue
return
}
const vo = await createJdPublishTask(categoryId, productIds, priceRule, brand)
const targetShop = shopGuard.value.state === 'cross'
? { shopId: shopGuard.value.target.shopId, shopName: shopGuard.value.target.shopName }
: undefined
const vo = targetShop
? await createJdPublishTask(categoryId, productIds, priceRule, brand, targetShop)
: await createJdPublishTask(categoryId, productIds, priceRule, brand)
board.addPollingTask(vo.taskId)
const pushNote = await pushToClientQueue(vo.queuePayload)
successMessage.value = pushNote
@@ -746,6 +784,7 @@ onMounted(async () => {
const localName = (window.localStorage.getItem('username') || '').trim()
if (localName) username.value = localName
refreshShopDetectability()
void refreshPublishProtocol()
board.loadPollingIds()
await Promise.all([
loadGroups().catch(() => undefined),
@@ -806,10 +845,21 @@ onUnmounted(() => {
{{ detectingShop ? '检测中…' : (currentShop ? '重新检测' : '检测京麦店铺') }}
</button>
</p>
<p v-if="shopError" class="shop-alert">{{ shopError }}</p>
<p v-else-if="shopGuard.state === 'mismatch' || shopGuard.state === 'multi'" class="shop-alert">
{{ shopGuard.message }}
<!-- 跨店档:目标店=京麦当前登录店,商品会在目标店新建(与来源店那件各自独立)。 -->
<p v-if="crossTargetText" class="shop-line">
<span class="shop-line__key">目标店(跨店)</span>
<span class="shop-line__value">{{ crossTargetText }}</span>
</p>
<p v-if="shopError" class="shop-alert">{{ shopError }}</p>
<p v-else-if="shopGuard.state === 'multi'" class="shop-alert">{{ shopGuard.message }}</p>
<p v-else-if="shopGuard.state === 'cross' && !crossSupported" class="shop-alert">{{ CROSS_UPGRADE_NOTICE }}</p>
<template v-else-if="shopGuard.state === 'cross'">
<p class="shop-alert shop-alert--cross">{{ shopGuard.message }}</p>
<label class="shop-cross-confirm">
<input v-model="crossConfirmed" type="checkbox" />
我已确认:这是在目标店新建商品、需目标店类目授权并通过京东审核
</label>
</template>
<p v-else-if="shopGuard.state === 'match'" class="shop-ok">
店铺一致:商品会发布到「{{ shopLabel(shopGuard.expected) }}」。
</p>
@@ -888,11 +938,19 @@ onUnmounted(() => {
</div>
<button class="collect-btn" type="button" :disabled="submitDisabled" @click="startPublish">
{{ submitting ? '创建中…' : `上架已选 ${selectedProducts.size} 个商品` }}
{{ submitting ? '创建中…' : (usesCrossShop
? `跨店上架 ${selectedProducts.size} 个商品到目标店`
: `上架已选 ${selectedProducts.size} 个商品`) }}
</button>
<p class="collect-hint">
点击后本机客户端会逐个把商品发布到已登录的京麦店铺,请保持客户端在线且京麦已登录;发布进度逐行显示在右侧任务区。
<template v-if="usesCrossShop">
跨店档会在目标店按每个商品的类目新建商品(京麦「新增商品」路径),需目标店已授权该类目并过京东审核;
发布进度逐行显示在右侧任务区。
</template>
<template v-else>
点击后本机客户端会逐个把商品发布到已登录的京麦店铺,请保持客户端在线且京麦已登录;发布进度逐行显示在右侧任务区。
</template>
</p>
<p v-if="successMessage" class="feedback success">{{ successMessage }}</p>
@@ -967,6 +1025,9 @@ onUnmounted(() => {
.shop-detect-btn:hover:not(:disabled) { border-color: #6ea8ff; color: #f5f8fc; }
.shop-detect-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.shop-alert { margin: 6px 0 0; padding: 8px 10px; border-radius: 6px; background: rgba(248, 113, 113, 0.12); color: #fca5a5; font-size: 12px; line-height: 1.6; }
/* 跨店档是「可继续但要知道代价」:琥珀档(同 --bell-warning 口径),与红色硬拦截区分开。 */
.shop-alert--cross { background: rgba(240, 187, 97, 0.12); color: #f0bb61; }
.shop-cross-confirm { display: flex; align-items: flex-start; gap: 6px; margin: 6px 0 0; color: #c8d2e2; font-size: 12px; line-height: 1.6; cursor: pointer; }
.shop-ok { margin: 6px 0 0; color: #6ee7b7; font-size: 12px; }
.shop-cat-select { width: 100%; }
.shop-cat-select :deep(.el-select__wrapper) { background-color: #232b3d; box-shadow: 0 0 0 1px #3e4a62 inset; }
+43 -15
View File
@@ -1,22 +1,33 @@
/**
* 京东上架「发到哪一家店」(2026-11-24 客户口径:「如果没有店铺登入情况 软件怎么知道我们要上架
* 是哪一个店铺?」)。
* 是哪一个店铺?」;2026-11-26 客户口径「要跨店」后判据从「必须同店」改成「同店与跨店都行」)。
*
* 三份身份,各有唯一出处,三层同一标记:
* 1. **来源店铺**=所勾商品行的 `shopName/shopId`(京东数据库快照)。服务端建单时按它做
* 「一单只能发到一家店」校验(`JdPublishTaskService.resolveSingleShop`),并把它钉进
* 任务载荷与明细下发(`JdPublishItemsPageVo.shopId/shopName`);
* 2. **京麦当前登录店铺**=桥 `get_jd_shop_info` 现拉(客户端起有头浏览器读京麦首屏),
* 不是页面缓存、也不是上次「同步分类」的残留;本页只拿它做提前告知;
* 3. **最终强拦截**=客户端发布前用任务记录的店铺再核一次(`JdPublishPage.verify_shop`),
* 不一致先上报标记再中止,服务端 `JdPublishStaleTaskService` 命中即整单判死——一条都不发。
* 不是页面缓存、也不是上次「同步分类」的残留;跨店时它就是**目标店**(服务端据此算
* `publishMode=NEW`,客户端走京麦「新增商品」路径);
* 3. **最终强拦截**=客户端发布前按模式再核一次(`JdPublishPage.verify_shop`:克隆对来源店、
* 新增对目标店),不一致先上报标记再中止,服务端 `JdPublishStaleTaskService` 命中即整单判死。
*
* 本文件是纯函数(node --test 直测):只产状态与中文文案,复选框禁用/错误条/提示色由调用方选。
*/
/** 判据标记:客户端 `SHOP_MISMATCH_MARK`、服务端 `JdPublishStaleTaskService.SHOP_MISMATCH_MARK` 同值。 */
/** 判据标记:客户端 `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
@@ -34,7 +45,8 @@ export type JdShopGuard =
/** 有来源店铺、还没检测京麦 ⇒ 提示可检测(不拦) */
| { state: 'undetected'; expected: JdShopRef }
| { state: 'match'; expected: JdShopRef; actual: JdShopRef }
| { state: 'mismatch'; expected: JdShopRef; actual: JdShopRef; message: string }
/** 京麦登录的不是来源店 ⇒ 跨店上架(目标店=京麦当前登录店,走新增商品路径) */
| { state: 'cross'; expected: JdShopRef; target: JdShopRef; message: string }
/** 勾选的商品来自多家店 ⇒ 服务端也会拒(按店分开勾) */
| { state: 'multi'; shops: JdShopRef[]; message: string }
@@ -83,11 +95,17 @@ export function sourceShopRefs(products: ReadonlyArray<JdShopSource> | null | un
return refs
}
/** 不匹配整句:开头即标记,点名「勾选的店」与「京麦现在登的店」,给动作句(不写「稍后重试」)。 */
export function shopMismatchMessage(expected: JdShopRef | null | undefined, actual: JdShopRef | null | undefined): string {
const expectedLabel = shopLabel(expected)
return (`${SHOP_MISMATCH_MARK}:本次勾选的商品来自「${expectedLabel}」,`
+ `当前京麦登录「${shopLabel(actual)}」;请先在京麦切换到「${expectedLabel}」再上架。`)
/**
* 跨店整句:点名「采集店」与「目标店(京麦当前登录)」,说明三件客户要知道的事——
* 会在目标店**新建**商品(与来源店那件各自独立,wareId 不同)、要过京东审核、
* 目标店必须已授权该商品的类目。不写「稍后重试」(类目没授权是确定性失败)。
*/
export function crossShopMessage(source: JdShopRef | null | undefined, target: JdShopRef | null | undefined): string {
const sourceLabel = shopLabel(source)
const targetLabel = shopLabel(target)
return (`跨店上架:商品采集自「${sourceLabel}」,将在京麦当前登录的「${targetLabel}」新建为全新商品`
+ '(与来源店那件各自独立,需京东审核通过后才上架);目标店须已授权该商品的类目,'
+ '否则该商品会按「目标店没有授权类目」逐行失败。')
}
/** 跨店勾选整句(与服务端建单拒绝文案同口径:按店分开勾)。 */
@@ -118,10 +136,20 @@ export function evaluateShopGuard(
// 比不了(一边只有店名、另一边只有 id)不判不匹配:宁可留给客户端强拦截,也不误拦真实任务。
if (same === null) return { state: 'undetected', expected }
if (same) return { state: 'match', expected, actual }
return { state: 'mismatch', expected, actual, message: shopMismatchMessage(expected, actual) }
return { state: 'cross', expected, target: actual, message: crossShopMessage(expected, actual) }
}
/** UI 唯一处断:命中这两档才禁「上架」按钮(unknown/undetected 不拦)。 */
export function shopGuardBlocks(guard: JdShopGuard): guard is Extract<JdShopGuard, { state: 'mismatch' | 'multi' }> {
return guard.state === 'mismatch' || guard.state === 'multi'
/**
* 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 : ''
}
@@ -41,6 +41,7 @@ export function createJdPublishTask(
productIds: number[],
priceRule: JdPublishPriceRule,
targetBrandName: string,
targetShop?: { shopId?: string; shopName?: string },
) {
return unwrapJavaResponse(post<JavaApiResponse<JdPublishTaskVo>, Record<string, unknown>>(
buildJavaUrl(API_ENDPOINTS.jdPublish.createWithTargetBrand),
@@ -51,6 +52,10 @@ export function createJdPublishTask(
// mode=none 时不带 value(后端 normalizePriceRule 对 none 忽略 value)
priceRule: priceRule.mode === 'none' ? { mode: 'none' } : priceRule,
targetBrandName,
// 跨店(2026-11-26)才带目标店:服务端据此落 publishMode=NEW 与目标店快照;
// 不带=回落来源店走本店克隆,与 5.0.13 行为逐字一致。
...(targetShop?.shopId ? { targetShopId: targetShop.shopId } : {}),
...(targetShop?.shopName ? { targetShopName: targetShop.shopName } : {}),
},
))
}