feat(jd): 上架按账号授权 + 建单钉来源店铺 + 店铺不匹配整单判死
2026-11-24 客户(心向大海)两条口径: 「如果没有店铺登入情况 软件怎么知道我们要上架是哪一个店铺?」 「上架的账户 我是这个权限不开给他的」 后端半边(用户端页头与会话核对见 frontend-vue 同笔;客户端发布前核对见 app_client 0f8bcdf)。 一、按账号授权(拒绝型能力,quiet fail 全靠本类判据) - JdPublishPermissionSupport:V209 app 菜单组键 jd_tools 或子键 jd-publish 命中即放行, 超管直放;有效键集按 AdminMenuPlatformScope 过滤到账号平台(与登录分发同源)。 无权限抛 403 + 中文动作句(告诉管理员去哪儿勾)。 - JdPublishController:建单/改品牌重试/选品/面板/历史/批量进度六个用户面入口一律先判; 客户端执行面(items/chunk/activate/heartbeat/补值/删除)不判——执行器本来就只跑 已建的任务,判在这里会让进行中的任务断在半路。 二、建单钉来源店铺(一次上架只发一家店) - resolveSingleShop:按勾选商品快照判店铺,跨店勾选建单即拒并点名两家店; 同店两种形状(有 id / 只有店名)归一到 id 再比。 - JdPublishTaskCreateRequest 落 shopId/shopName → 队列载荷两键 → 明细页 JdPublishItemsPageVo.shopId/shopName 下发;旧任务载荷没这两键时回落明细行 shop_name(V210 就有),保证老任务至少能按店名核对。 三、店铺不匹配整单判死 - JdPublishStaleTaskService.SHOP_MISMATCH_MARK/terminalizeOnJdShopMismatch: CAS RUNNING→FAILED、0/1 计数、原文追加动作句;不按累计行数收窄(确定性失败)。 - JdPublishIngestService.fail:命中标记排在凭据致命档之后、页面结构变更之前接线, 回 accept=true 让客户端收口。 判据:JdPublishPermissionSupportTest 5 支(新)+TaskService +3/Stale +2/Ingest +2, 后端 JdPublish 九类 77 支全绿;四支既有护栏(catch 静默/请求身份/超管单出口/SQL 分层)绿。
This commit is contained in:
@@ -22,6 +22,13 @@ import {
|
||||
} from '@/shared/api/types/modules/jd-publish'
|
||||
import { retryClientTask } from '@/shared/api/task-retry'
|
||||
import { getPywebviewApi } from '@/shared/bridges/pywebview'
|
||||
import { ensureJdLogin, markJdLoginExpired } from '@/shared/stores/jd-login-autoflow'
|
||||
import {
|
||||
evaluateShopGuard,
|
||||
shopGuardBlocks,
|
||||
shopLabel,
|
||||
type JdShopRef,
|
||||
} from '@/pages/jd/jd-publish-shop'
|
||||
import { resolvePageHref } from '@/shared/page-prefix'
|
||||
import { userNotice } from '@/shared/utils/user-notice'
|
||||
import { formatDateTime } from '@/shared/utils/datetime'
|
||||
@@ -166,8 +173,109 @@ function isValidBrand(name: string) {
|
||||
|
||||
const priceValueMissing = computed(() => priceMode.value !== 'none' && (priceValue.value == null || priceValue.value < 0))
|
||||
|
||||
// ---- 本次上架到哪家店(2026-11-24 客户口径「软件怎么知道要上架哪一家店铺」)----
|
||||
/** 来源店铺=勾选商品行的快照店铺;京麦当前店铺=桥现拉(不缓存、不猜、不自动探测)。 */
|
||||
const selectedProductRows = computed(() => Array.from(selectedProducts.value.values()))
|
||||
const currentShop = ref<JdShopRef | null>(null)
|
||||
const detectingShop = ref(false)
|
||||
const canDetectShop = ref(false)
|
||||
const shopNote = ref('')
|
||||
const shopError = ref('')
|
||||
|
||||
const SHOP_DETECT_UNAVAILABLE =
|
||||
'当前在本机客户端之外打开或客户端版本较旧,无法检测京麦店铺。请在本机客户端中打开本页;'
|
||||
+ '本机客户端发布前仍会核对任务记录的店铺,不一致整单拦下、一条都不发'
|
||||
|
||||
const shopGuard = computed(() => evaluateShopGuard(selectedProductRows.value, currentShop.value))
|
||||
|
||||
/** 来源店铺文案:没取到/多家店/单店三档(多店由 shopGuard 直接拦)。 */
|
||||
const sourceShopText = computed(() => {
|
||||
const guard = shopGuard.value
|
||||
if (guard.state === 'unknown') return '未取到(旧商品数据没有店铺信息)'
|
||||
if (guard.state === 'multi') return guard.shops.map((shop) => shopLabel(shop)).join('、')
|
||||
return shopLabel(guard.expected)
|
||||
})
|
||||
|
||||
const currentShopText = computed(() => (currentShop.value ? shopLabel(currentShop.value) : '未检测'))
|
||||
|
||||
/** 挂载时判可用性:老客户端没有 get_jd_shop_info ⇒ 检测按钮置灰(同「同步分类」口径)。 */
|
||||
function refreshShopDetectability() {
|
||||
canDetectShop.value = typeof getPywebviewApi()?.get_jd_shop_info === 'function'
|
||||
}
|
||||
|
||||
/**
|
||||
* 点「检测当前京麦店铺」:桥现拉京麦首屏(会起客户端专用浏览器,秒级)。
|
||||
* 失败按 code 分档——jd_busy 走 amber 说明、jd_login 走自动登录链路(登录成功后自动重测一次)、
|
||||
* jd_page/未知走红色错误。返回值只作 `startPublish` 的判断输入,展示通道由本函数自理。
|
||||
*/
|
||||
async function detectShop(options?: { fromAutoLogin?: boolean }) {
|
||||
if (detectingShop.value && !options?.fromAutoLogin) return
|
||||
shopError.value = ''
|
||||
shopNote.value = ''
|
||||
const api = getPywebviewApi()
|
||||
const bridge = api?.get_jd_shop_info
|
||||
if (typeof bridge !== 'function') {
|
||||
canDetectShop.value = false
|
||||
shopNote.value = SHOP_DETECT_UNAVAILABLE
|
||||
return
|
||||
}
|
||||
detectingShop.value = true
|
||||
try {
|
||||
const result = await bridge()
|
||||
if (result?.success && (result.shopName || result.shopId)) {
|
||||
currentShop.value = { shopId: result.shopId, shopName: result.shopName }
|
||||
return
|
||||
}
|
||||
if (result?.code === 'jd_busy') {
|
||||
shopNote.value = result.error || '京东任务正在运行,等任务结束后再检测京麦店铺'
|
||||
return
|
||||
}
|
||||
if (result?.code === 'jd_login') {
|
||||
if (options?.fromAutoLogin) {
|
||||
shopError.value = result.error || '京麦未登录(登录态失效)'
|
||||
return
|
||||
}
|
||||
markJdLoginExpired()
|
||||
shopNote.value = '检测到京麦未登录,已自动打开京麦登录窗口,请在弹出的浏览器里完成登录'
|
||||
const gate = await ensureJdLogin(api)
|
||||
if (gate.status === 'ok') {
|
||||
shopNote.value = ''
|
||||
detectingShop.value = false
|
||||
await detectShop({ fromAutoLogin: true })
|
||||
return
|
||||
}
|
||||
if (gate.severity === 'error') {
|
||||
shopNote.value = ''
|
||||
shopError.value = gate.hint || result.error || '打开京麦登录窗口失败,请稍后重试'
|
||||
} else {
|
||||
shopNote.value = gate.hint || shopNote.value
|
||||
}
|
||||
return
|
||||
}
|
||||
shopError.value = result?.error || '检测京麦店铺失败,请稍后重试'
|
||||
} finally {
|
||||
detectingShop.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建单前把店铺核一次:已检测且一致就不再开浏览器;命中不匹配/跨店直接给拦截文案。
|
||||
* 检测失败(页面结构变更、老客户端、京麦任务在跑)**不**拦创建——最终强拦截在客户端
|
||||
* 执行器(先上报标记再中止)与服务端(命中即整单判死),页面这层只负责提前告知。
|
||||
*/
|
||||
async function ensureShopChecked(): Promise<string> {
|
||||
const guard = shopGuard.value
|
||||
if (shopGuardBlocks(guard)) return guard.message
|
||||
if (guard.state !== 'undetected') return ''
|
||||
await detectShop()
|
||||
const next = shopGuard.value
|
||||
return shopGuardBlocks(next) ? next.message : ''
|
||||
}
|
||||
|
||||
const submitDisabled = computed(() =>
|
||||
submitting.value
|
||||
|| detectingShop.value
|
||||
|| shopGuardBlocks(shopGuard.value)
|
||||
|| !selectedGroupId.value
|
||||
|| !selectedProducts.value.size
|
||||
|| !isValidBrand(targetBrandName.value)
|
||||
@@ -603,6 +711,12 @@ async function startPublish() {
|
||||
errorMessage.value = JD_UPGRADE_NOTICE
|
||||
return
|
||||
}
|
||||
// 店铺核对在建单之前:京麦登录的不是勾选商品那家店时一条都不发(客户口径「强拦截」)。
|
||||
const shopIssue = await ensureShopChecked()
|
||||
if (shopIssue) {
|
||||
errorMessage.value = shopIssue
|
||||
return
|
||||
}
|
||||
const vo = await createJdPublishTask(categoryId, productIds, priceRule, brand)
|
||||
board.addPollingTask(vo.taskId)
|
||||
const pushNote = await pushToClientQueue(vo.queuePayload)
|
||||
@@ -620,6 +734,7 @@ async function startPublish() {
|
||||
onMounted(async () => {
|
||||
const localName = (window.localStorage.getItem('username') || '').trim()
|
||||
if (localName) username.value = localName
|
||||
refreshShopDetectability()
|
||||
board.loadPollingIds()
|
||||
await Promise.all([
|
||||
loadGroups().catch(() => undefined),
|
||||
@@ -666,6 +781,35 @@ onUnmounted(() => {
|
||||
|
||||
<main class="jd-body">
|
||||
<aside class="left-panel">
|
||||
<!-- 本次上架到哪家店:来源店铺(勾选商品快照)vs 京麦当前登录店铺(现检测) -->
|
||||
<div class="category-block shop-guard">
|
||||
<span class="category-label">本次上架到哪家店</span>
|
||||
<p class="shop-line">
|
||||
<span class="shop-line__key">商品来源店铺</span>
|
||||
<span class="shop-line__value">{{ sourceShopText }}</span>
|
||||
</p>
|
||||
<p class="shop-line">
|
||||
<span class="shop-line__key">京麦当前登录</span>
|
||||
<span class="shop-line__value" :class="{ 'shop-line__value--muted': !currentShop }">{{ currentShopText }}</span>
|
||||
<button class="shop-detect-btn" type="button" :disabled="detectingShop || !canDetectShop" @click="detectShop()">
|
||||
{{ 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>
|
||||
<p v-else-if="shopGuard.state === 'match'" class="shop-ok">
|
||||
店铺一致:商品会发布到「{{ shopLabel(shopGuard.expected) }}」。
|
||||
</p>
|
||||
<p v-else-if="shopNote" class="category-hint">{{ shopNote }}</p>
|
||||
<p v-else-if="!canDetectShop" class="category-hint">{{ SHOP_DETECT_UNAVAILABLE }}</p>
|
||||
<p v-else class="category-hint">
|
||||
勾选商品后点「检测京麦店铺」可确认京麦当前登录的是不是同一家店;不一致时本页不让建单,
|
||||
客户端发布前还会再核对一次,一条都不会发出去。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="category-block">
|
||||
<span class="category-label">自定义分类</span>
|
||||
<el-select v-model="selectedGroupId" placeholder="选择分类" class="shop-cat-select"
|
||||
@@ -700,6 +844,7 @@ onUnmounted(() => {
|
||||
<span class="product-info">
|
||||
<span class="product-title">{{ product.title }}</span>
|
||||
<span class="product-meta">
|
||||
<template v-if="product.shopName">店铺 {{ product.shopName }} · </template>
|
||||
¥{{ product.priceMin ?? '-' }}<template v-if="product.priceMax && product.priceMax !== product.priceMin">~{{ product.priceMax }}</template>
|
||||
· 库存 {{ product.stockTotal ?? '-' }}<template v-if="product.skuCount"> · {{ product.skuCount }}个SKU</template>
|
||||
</span>
|
||||
@@ -797,6 +942,16 @@ onUnmounted(() => {
|
||||
.category-block { margin-bottom: 16px; }
|
||||
.category-label { display: block; margin-bottom: 8px; color: #c8d2e2; font-size: 13px; font-weight: 600; }
|
||||
.category-hint { margin: 8px 0 0; color: #7f8ca3; font-size: 12px; line-height: 1.6; }
|
||||
.shop-guard { padding: 10px 12px; border: 1px solid #2c3547; border-radius: 8px; background: #20283a; }
|
||||
.shop-line { display: flex; align-items: center; gap: 8px; margin: 0 0 6px; font-size: 12px; line-height: 1.5; }
|
||||
.shop-line__key { flex-shrink: 0; color: #7f8ca3; }
|
||||
.shop-line__value { min-width: 0; overflow: hidden; color: #d9e1ef; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.shop-line__value--muted { color: #7f8ca3; }
|
||||
.shop-detect-btn { flex-shrink: 0; margin-left: auto; padding: 3px 10px; border: 1px solid #3e4a62; border-radius: 6px; background: #232b3d; color: #c8d2e2; font-family: inherit; font-size: 12px; cursor: pointer; }
|
||||
.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; }
|
||||
.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; }
|
||||
.shop-cat-select :deep(.el-select__placeholder), .shop-cat-select :deep(.el-select__selected-item) { color: #c8d2e2; }
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 「商品上架」按账号授权(2026-11-24 客户口径:「上架的账户 我是这个权限不开给他的」)。
|
||||
*
|
||||
* 三层,口径同一份、判据逐字相同:
|
||||
* 1. **入口**:京东工作台的卡片由 `filterJdToolsByPermission` 过滤(V209 app 菜单:
|
||||
* 组键 `jd_tools` 或子键 `jd-publish`);
|
||||
* 2. **直达路由**:`/jd-publish` 的 `beforeEnter` 用本文件的 `resolveJdPublishAccess`,
|
||||
* 没权限的用户手敲地址也进不来(正常入口被隐藏后,直达是唯一绕过口);
|
||||
* 3. **服务端**:`JdPublishPermissionSupport.assertCanPublish`(建单/选品/面板一律 403),
|
||||
* 这是唯一权威判据——前端两层只为用户体验,不承担安全职责。
|
||||
*
|
||||
* 收回权限的实操(后管「用户管理 → 菜单权限 → 桌面端」):把「商品上架」子项取消勾选后
|
||||
* 保存,直接授权集合只剩 `{jd-collect}`,读侧不再展开 `jd_tools` 组 ⇒ 本文件判为无权限。
|
||||
*
|
||||
* 权限接口异常(`null`)按「未知」放行:后管账号体系里「读不到权限」与「没有权限」必须分开,
|
||||
* 抖一下把有权限的人锁在门外比放进去后被 403 更难解释;后端 403 仍是硬边界。
|
||||
*/
|
||||
|
||||
/** app 菜单组键(V209 columns.column_key='jd_tools') */
|
||||
export const JD_PUBLISH_GROUP_KEY = 'jd_tools'
|
||||
/** 商品上架子键(V209 columns.column_key='jd-publish') */
|
||||
export const JD_PUBLISH_COLUMN_KEY = 'jd-publish'
|
||||
|
||||
export const JD_PUBLISH_DENIED_TEXT =
|
||||
'当前账号没有「商品上架」权限,请联系管理员在后台「用户管理 → 菜单权限 → 桌面端」里勾上「商品上架」'
|
||||
|
||||
export type JdPublishAccess = 'allowed' | 'denied' | 'unknown'
|
||||
|
||||
/** 权限键是否覆盖「商品上架」:组键命中(预授权授的是组行)或子键命中都算。 */
|
||||
export function canUseJdPublish(keys: ReadonlyArray<string> | null | undefined): boolean {
|
||||
const allowed = new Set(
|
||||
(keys ?? []).map((key) => String(key ?? '').trim().toLowerCase()).filter(Boolean),
|
||||
)
|
||||
return allowed.has(JD_PUBLISH_GROUP_KEY) || allowed.has(JD_PUBLISH_COLUMN_KEY)
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由判据:拿到键集(可能为空数组=真没权限)⇒ allowed/denied;
|
||||
* 拿不到键集(接口失败/超时,调用方传 null)⇒ unknown(放行,服务端 403 兜底)。
|
||||
*/
|
||||
export function resolveJdPublishAccess(keys: ReadonlyArray<string> | null): JdPublishAccess {
|
||||
if (keys === null) return 'unknown'
|
||||
return canUseJdPublish(keys) ? 'allowed' : 'denied'
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* 京东上架「发到哪一家店」(2026-11-24 客户口径:「如果没有店铺登入情况 软件怎么知道我们要上架
|
||||
* 是哪一个店铺?」)。
|
||||
*
|
||||
* 三份身份,各有唯一出处,三层同一标记:
|
||||
* 1. **来源店铺**=所勾商品行的 `shopName/shopId`(京东数据库快照)。服务端建单时按它做
|
||||
* 「一单只能发到一家店」校验(`JdPublishTaskService.resolveSingleShop`),并把它钉进
|
||||
* 任务载荷与明细下发(`JdPublishItemsPageVo.shopId/shopName`);
|
||||
* 2. **京麦当前登录店铺**=桥 `get_jd_shop_info` 现拉(客户端起有头浏览器读京麦首屏),
|
||||
* 不是页面缓存、也不是上次「同步分类」的残留;本页只拿它做提前告知;
|
||||
* 3. **最终强拦截**=客户端发布前用任务记录的店铺再核一次(`JdPublishPage.verify_shop`),
|
||||
* 不一致先上报标记再中止,服务端 `JdPublishStaleTaskService` 命中即整单判死——一条都不发。
|
||||
*
|
||||
* 本文件是纯函数(node --test 直测):只产状态与中文文案,复选框禁用/错误条/提示色由调用方选。
|
||||
*/
|
||||
|
||||
/** 判据标记:客户端 `SHOP_MISMATCH_MARK`、服务端 `JdPublishStaleTaskService.SHOP_MISMATCH_MARK` 同值。 */
|
||||
export const SHOP_MISMATCH_MARK = '京东店铺不匹配'
|
||||
|
||||
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: 'mismatch'; expected: JdShopRef; actual: 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<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
|
||||
}
|
||||
|
||||
/** 不匹配整句:开头即标记,点名「勾选的店」与「京麦现在登的店」,给动作句(不写「稍后重试」)。 */
|
||||
export function shopMismatchMessage(expected: JdShopRef | null | undefined, actual: JdShopRef | null | undefined): string {
|
||||
const expectedLabel = shopLabel(expected)
|
||||
return (`${SHOP_MISMATCH_MARK}:本次勾选的商品来自「${expectedLabel}」,`
|
||||
+ `当前京麦登录「${shopLabel(actual)}」;请先在京麦切换到「${expectedLabel}」再上架。`)
|
||||
}
|
||||
|
||||
/** 跨店勾选整句(与服务端建单拒绝文案同口径:按店分开勾)。 */
|
||||
export function multiShopMessage(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)
|
||||
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: 'mismatch', expected, actual, message: shopMismatchMessage(expected, actual) }
|
||||
}
|
||||
|
||||
/** UI 唯一处断:命中这两档才禁「上架」按钮(unknown/undetected 不拦)。 */
|
||||
export function shopGuardBlocks(guard: JdShopGuard): guard is Extract<JdShopGuard, { state: 'mismatch' | 'multi' }> {
|
||||
return guard.state === 'mismatch' || guard.state === 'multi'
|
||||
}
|
||||
Reference in New Issue
Block a user