feat(admin-jd): 商品目录只渲染本店有商品的类目分支(去掉「树外兜底」退化解)
客户口径:「正确是一开始显示的那棵树,而不是什么树兜底」。授权树里零命中的分 支整支剪掉: - 本店商品名挂得上树(花屿巷旗舰店)⇒ 只显示命中分支,别店的空类目不再占屏; - 树与本店不同源(家居日用店看别店快照)⇒ 整树剪空,本店类目名平铺成一级列表, 不再出现「授权类目树(本店无匹配)」行 + 说明框,也不再出现「树外兜底」分组; - 树有命中时,挂不上树的本店历史名仍收在一组(改名「授权树外的类目」)。 - 纯函数 jdCategoryPruneZero(零命中整支剪掉、重算 leaf) - 一级默认展开改挂剪枝后的根(空根不置位,等真根到达再补) - 判据:jd-category-tree.test.ts +3(剪枝三态)、侧栏 mount 改写(平铺/剪支/真树)、 page mount 组名同步;source-pin 台账按文件点名 17→20 - 后管 node --test 1982 绿、vitest 252 绿、vue-tsc rc=0,快检四面绿
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
/** 京东数据库页左侧栏(v1.6 蓝图 Phase 2,additive——页面接线随 Phase 5):
|
/** 京东数据库页左侧栏(v1.6 蓝图 Phase 2,additive——页面接线随 Phase 5):
|
||||||
* 双 Tab=商品目录(京麦授权类目真树+计数徽标+展开折叠+树外兜底组)/
|
* 双 Tab=商品目录(京麦授权类目真树+计数徽标+展开折叠+树外类目组)/
|
||||||
* 店铺分类(当前数据权限分组的分类平铺+计数+新建分类+分类管理入口)。
|
* 店铺分类(当前数据权限分组的分类平铺+计数+新建分类+分类管理入口)。
|
||||||
*
|
*
|
||||||
* 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本组件不发请求、
|
* 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本组件不发请求、
|
||||||
* 不 import api——mount 测试免 mock;内部状态只有双 Tab 切换、树展开/折叠与新建输入框。
|
* 不 import api——mount 测试免 mock;内部状态只有双 Tab 切换、树展开/折叠与新建输入框。
|
||||||
* 计数聚合走 jdCategoryCountTree(v1.6 Phase 1):节点徽标=自身名+子树叶和(与
|
* 计数聚合走 jdCategoryCountTree(v1.6 Phase 1):节点徽标=自身名+子树叶和(与
|
||||||
* 「点节点 → 子树展开 IN 过滤」的命中集合同源),树上对不上的历史行归「树外兜底」组
|
* 「点节点 → 子树展开 IN 过滤」的命中集合同源)。树只渲染本店真有商品的类目分支
|
||||||
* (点兜底行=单名等值旧形状,path 空数组交给页面回落)。 */
|
* (零命中整支剪掉,2026-11-24),树上挂不上的本店类目平铺/归组显示,点行=单名等值
|
||||||
|
* 旧形状(path 空数组交给页面回落)。 */
|
||||||
import { computed, nextTick, ref, watch } from 'vue'
|
import { computed, nextTick, ref, watch } from 'vue'
|
||||||
import {
|
import {
|
||||||
JD_CATEGORY_TREE,
|
JD_CATEGORY_TREE,
|
||||||
jdCategoryCountTree,
|
jdCategoryCountTree,
|
||||||
|
jdCategoryPruneZero,
|
||||||
type JdCategoryCountedNode,
|
type JdCategoryCountedNode,
|
||||||
type JdCategoryNode,
|
type JdCategoryNode,
|
||||||
} from './jd-category-tree.ts'
|
} from './jd-category-tree.ts'
|
||||||
@@ -35,7 +37,7 @@ const props = withDefaults(defineProps<{
|
|||||||
selectedShopName?: string | null
|
selectedShopName?: string | null
|
||||||
/** 授权类目树(2026-11-23):页面从服务端取「已上报店铺并集」的真树传入;
|
/** 授权类目树(2026-11-23):页面从服务端取「已上报店铺并集」的真树传入;
|
||||||
* 缺省/空数组=回落内置快照(`JD_CATEGORY_TREE`,一家店的历史取证,
|
* 缺省/空数组=回落内置快照(`JD_CATEGORY_TREE`,一家店的历史取证,
|
||||||
* 另由 treeForeign 判据收树点名,不冒充本店类目)。 */
|
* 另由零命中剪枝兜住,不冒充本店类目)。 */
|
||||||
categoryTree?: JdCategoryNode[] | null
|
categoryTree?: JdCategoryNode[] | null
|
||||||
/** 类目计数行是否已取过(页面 loading 态投影;缺省/true=已就绪)。首帧未就绪时侧栏
|
/** 类目计数行是否已取过(页面 loading 态投影;缺省/true=已就绪)。首帧未就绪时侧栏
|
||||||
* 不画树:否则会先把回落快照整棵树(别店类目、徽标全 0、「全部商品 0」)画出来,计数
|
* 不画树:否则会先把回落快照整棵树(别店类目、徽标全 0、「全部商品 0」)画出来,计数
|
||||||
@@ -48,7 +50,7 @@ const props = withDefaults(defineProps<{
|
|||||||
})
|
})
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
/** 选类目:树内节点带完整路径(页面按子树展开类目名集合 IN 下发);树外兜底名
|
/** 选类目:树内节点带完整路径(页面按子树展开类目名集合 IN 下发);树外名
|
||||||
* path=[](序列化回落单名等值);null=「全部商品」清选重查。 */
|
* path=[](序列化回落单名等值);null=「全部商品」清选重查。 */
|
||||||
(event: 'select-category', value: { name: string; path: string[] } | null): void
|
(event: 'select-category', value: { name: string; path: string[] } | null): void
|
||||||
/** 选分类:分类 id;再点已选中的=清选(emit null,回全部分类)。 */
|
/** 选分类:分类 id;再点已选中的=清选(emit null,回全部分类)。 */
|
||||||
@@ -84,26 +86,25 @@ const activeTree = computed<JdCategoryNode[]>(() =>
|
|||||||
|
|
||||||
const counted = computed(() => jdCategoryCountTree(props.categoryOptions ?? [], activeTree.value))
|
const counted = computed(() => jdCategoryCountTree(props.categoryOptions ?? [], activeTree.value))
|
||||||
|
|
||||||
/** 类目计数就绪(页面首帧未回时不画树/不画兜底,只留一行加载提示)。
|
/** 类目计数就绪(页面首帧未回时只留加载行,总数显示「—」)。 */
|
||||||
* 与 treeForeign 的关系:treeForeign 只在「数据到了且全不匹配」时成立,未就绪时它本
|
|
||||||
* 不触发——但树本体照样会按空计数画出来,所以加载态必须自己挡。 */
|
|
||||||
const catalogReady = computed(() => props.categoriesReady !== false)
|
const catalogReady = computed(() => props.categoriesReady !== false)
|
||||||
|
|
||||||
/** 内置授权树与本店类目体系不同源:整棵树零命中、且兜底组确有行。内置 `JD_CATEGORY_TREE`
|
/** 只渲染「本店真有商品」的类目分支(2026-11-24 客户口径「正确是一开始显示的那样,而不是
|
||||||
* 是 2026-11-16 从**一家店铺**(花屿巷旗舰店)京麦授权接口抓的固定快照,换一家店(如家居
|
* 什么树兜底」):授权树里 count=0 的整支剪掉——落地后效果是
|
||||||
* 日用店)打开时它仍是整棵服饰内衣/母婴/运动户外树、节点全 0,而库内商品(收纳盒/脏衣篓…)
|
* - 本店商品名都在树上(如花屿巷旗舰店)⇒ 只显示命中分支,别店的空分支不出现;
|
||||||
* 全落树外兜底——不标注就会让人以为「这家店怎么冒出服饰类目」。数据没到(extra 也空)
|
* - 树与本店不同源(如家居日用店看花屿巷快照)⇒ 整树剪空 ⇒ 本店类目名平铺成一级列表,
|
||||||
* 不触发,避免把加载中误判成不同源。 */
|
* 既不再显示别店的服饰树,也不再出现「树外兜底」这种退化分组。
|
||||||
const treeForeign = computed(() => !counted.value.treeMatched && counted.value.extra.length > 0)
|
* 树内节点徽标本来就是子树去重和(jdCategoryCountTree 同源),剪枝只裁掉 count=0 且无子的节点。 */
|
||||||
|
const visibleRoots = computed(() => jdCategoryPruneZero(counted.value.roots))
|
||||||
|
|
||||||
/** 不同源时那棵树的展开态:默认收起(只留一行说明),点开可查原树形状。 */
|
/** 树之外的本店类目(授权树里对不上的名字);树被剪空时它们就是整列内容,故不摆分组头。 */
|
||||||
const foreignOpen = ref(false)
|
const extraIsSolo = computed(() => visibleRoots.value.length === 0)
|
||||||
/** 「全部商品」徽标=全部计数行求和(树内+树外=有类目名的商品总数;无类目名的行
|
/** 「全部商品」徽标=全部计数行求和(树内+树外=有类目名的商品总数;无类目名的行
|
||||||
* 不在任何类目筛选的命中集里,不计进任何徽标)。 */
|
* 不在任何类目筛选的命中集里,不计进任何徽标)。 */
|
||||||
const totalCount = computed(() =>
|
const totalCount = computed(() =>
|
||||||
(props.categoryOptions ?? []).reduce((sum, row) => sum + (row.productCount || 0), 0))
|
(props.categoryOptions ?? []).reduce((sum, row) => sum + (row.productCount || 0), 0))
|
||||||
|
|
||||||
/** 展开态:按路径串('/' 连接)记,含树外兜底组头。出厂(无记忆)一级默认展开(二级可见);
|
/** 展开态:按路径串('/' 连接)记。出厂(无记忆)一级默认展开(二级可见);
|
||||||
* v1.7 树增强:展开态持久化 localStorage `jd_db_cat_expand`(切页/重进不丢),损坏/不存在
|
* v1.7 树增强:展开态持久化 localStorage `jd_db_cat_expand`(切页/重进不丢),损坏/不存在
|
||||||
* =按出厂态(宁可重展一级也不冒充用户记忆)。 */
|
* =按出厂态(宁可重展一级也不冒充用户记忆)。 */
|
||||||
const EXPAND_STORAGE_KEY = 'jd_db_cat_expand'
|
const EXPAND_STORAGE_KEY = 'jd_db_cat_expand'
|
||||||
@@ -141,9 +142,11 @@ function persistExpanded(): void {
|
|||||||
let hasStoredExpand = initialExpand.stored
|
let hasStoredExpand = initialExpand.stored
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => counted.value.roots,
|
() => visibleRoots.value,
|
||||||
(roots) => {
|
(roots) => {
|
||||||
if (hasStoredExpand) return
|
if (hasStoredExpand) return
|
||||||
|
// 树还没到(首帧计数空 / 整树被剪空)不置位:等真根到达时再补一级展开。
|
||||||
|
if (!roots.length) return
|
||||||
const next = new Set(expanded.value)
|
const next = new Set(expanded.value)
|
||||||
for (const root of roots) next.add(expandKey([root.name]))
|
for (const root of roots) next.add(expandKey([root.name]))
|
||||||
// 补过一次就置位:之后类目重取不再补默认(用户中途收起的一级行不被重取展开回去)。
|
// 补过一次就置位:之后类目重取不再补默认(用户中途收起的一级行不被重取展开回去)。
|
||||||
@@ -175,7 +178,7 @@ const flatTree = computed<FlatNode[]>(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
walk(counted.value.roots, 0, [])
|
walk(visibleRoots.value, 0, [])
|
||||||
return out
|
return out
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -200,7 +203,7 @@ function expandAll(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
walk(counted.value.roots, [])
|
walk(visibleRoots.value, [])
|
||||||
expanded.value = next
|
expanded.value = next
|
||||||
hasStoredExpand = true
|
hasStoredExpand = true
|
||||||
persistExpanded()
|
persistExpanded()
|
||||||
@@ -252,7 +255,7 @@ const isSelectedNode = (node: FlatNode): boolean => {
|
|||||||
return selected.path.every((name, index) => name === node.path[index])
|
return selected.path.every((name, index) => name === node.path[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- 树外兜底组(树上对不上的历史行,单名等值出口) ----------------
|
// ---------------- 树外类目组(树上挂不上的本店行,单名等值出口) ----------------
|
||||||
|
|
||||||
const extraOpen = ref(true)
|
const extraOpen = ref(true)
|
||||||
|
|
||||||
@@ -361,7 +364,7 @@ function cancelShopRename(): void {
|
|||||||
>店铺分类</button>
|
>店铺分类</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 商品目录:全部商品 + 京麦授权类目树(计数徽标)+ 树外兜底组 -->
|
<!-- 商品目录:全部商品 + 京麦授权类目树(计数徽标)+ 树外类目组 -->
|
||||||
<div v-show="activeTab === 'catalog'" class="jd-sidebar-body jd-side-catalog">
|
<div v-show="activeTab === 'catalog'" class="jd-sidebar-body jd-side-catalog">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -376,30 +379,15 @@ function cancelShopRename(): void {
|
|||||||
否则会先显示回落快照的别店类目(徽标全 0),数据到达后再收树,肉眼一次闪变。 -->
|
否则会先显示回落快照的别店类目(徽标全 0),数据到达后再收树,肉眼一次闪变。 -->
|
||||||
<p v-if="!catalogReady" class="jd-side-loading">类目加载中…</p>
|
<p v-if="!catalogReady" class="jd-side-loading">类目加载中…</p>
|
||||||
<!-- 全部展开/收起(v1.7 树增强,对齐原型树控件):深树一次到底/一键收干净。
|
<!-- 全部展开/收起(v1.7 树增强,对齐原型树控件):深树一次到底/一键收干净。
|
||||||
不同源(treeForeign)时随树一起藏,只在手动点开那棵树时出现。 -->
|
只对「真有命中的树」摆(整树被剪空时下面直接是本店类目平铺列表)。 -->
|
||||||
<div v-if="catalogReady && flatTree.length && (!treeForeign || foreignOpen)" class="jd-side-tree-ops">
|
<div v-if="catalogReady && flatTree.length" class="jd-side-tree-ops">
|
||||||
<button type="button" aria-label="展开全部类目" @click="expandAll">全部展开</button>
|
<button type="button" aria-label="展开全部类目" @click="expandAll">全部展开</button>
|
||||||
<button type="button" aria-label="收起全部类目" @click="collapseAll">全部收起</button>
|
<button type="button" aria-label="收起全部类目" @click="collapseAll">全部收起</button>
|
||||||
</div>
|
</div>
|
||||||
<!-- 不同源说明行(2026-11-23):内置授权树是别店的固定快照、本店类目一个都不在树上。
|
<!-- 授权树里「本店真有商品」的分支(2026-11-24 客户口径:正确是一开始显示的那棵树,
|
||||||
整棵树默认收起,只留这一行点名「本店无匹配」,避免把别店的服饰树当成自己店的类目。 -->
|
而不是树外兜底这种退化分组)。零命中的别店分支已由 jdCategoryPruneZero 剪掉,
|
||||||
<template v-if="catalogReady && treeForeign">
|
整树剪空时这里不渲染,本店类目由下方平铺列表承担。 -->
|
||||||
<button
|
<div class="jd-side-tree" v-show="catalogReady && flatTree.length">
|
||||||
type="button"
|
|
||||||
class="jd-side-row jd-side-foreign-head"
|
|
||||||
:aria-expanded="foreignOpen"
|
|
||||||
@click="foreignOpen = !foreignOpen"
|
|
||||||
>
|
|
||||||
<span class="jd-side-arrow" :class="{ open: foreignOpen }">▸</span>
|
|
||||||
<span class="jd-side-name">授权类目树(本店无匹配)</span>
|
|
||||||
<span class="jd-side-count">0</span>
|
|
||||||
</button>
|
|
||||||
<p v-if="foreignOpen" class="jd-side-foreign-tip">
|
|
||||||
内置授权类目树是固定快照(取自一家店铺的京麦授权类目),本店商品的类目名都不在其内,
|
|
||||||
已按原名归入下方「树外兜底」。
|
|
||||||
</p>
|
|
||||||
</template>
|
|
||||||
<div class="jd-side-tree" v-show="catalogReady && (!treeForeign || foreignOpen)">
|
|
||||||
<button
|
<button
|
||||||
v-for="flat in flatTree"
|
v-for="flat in flatTree"
|
||||||
:key="flat.path.join('/')"
|
:key="flat.path.join('/')"
|
||||||
@@ -422,23 +410,26 @@ function cancelShopRename(): void {
|
|||||||
<span class="jd-side-count">{{ flat.node.count }}</span>
|
<span class="jd-side-count">{{ flat.node.count }}</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="catalogReady && counted.extra.length" class="jd-side-extra">
|
<!-- 树外名(授权树里挂不上的本店类目):树被剪空时它们就是整列内容,直接铺成一级、
|
||||||
|
不摆分组头;树有命中时仍收在「授权树外的类目」组里(可折叠,单名等值仍可选)。 -->
|
||||||
|
<div v-if="catalogReady && counted.extra.length" class="jd-side-extra" :class="{ 'jd-side-extra-solo': extraIsSolo }">
|
||||||
<button
|
<button
|
||||||
|
v-if="!extraIsSolo"
|
||||||
type="button"
|
type="button"
|
||||||
class="jd-side-row jd-side-extra-head"
|
class="jd-side-row jd-side-extra-head"
|
||||||
:aria-expanded="extraOpen"
|
:aria-expanded="extraOpen"
|
||||||
@click="extraOpen = !extraOpen"
|
@click="extraOpen = !extraOpen"
|
||||||
>
|
>
|
||||||
<span class="jd-side-name">树外兜底</span>
|
<span class="jd-side-name">授权树外的类目</span>
|
||||||
<span class="jd-side-count">{{ counted.extraTotal }}</span>
|
<span class="jd-side-count">{{ counted.extraTotal }}</span>
|
||||||
</button>
|
</button>
|
||||||
<template v-if="extraOpen">
|
<template v-if="extraIsSolo || extraOpen">
|
||||||
<button
|
<button
|
||||||
v-for="row in counted.extra"
|
v-for="row in counted.extra"
|
||||||
:key="row.name"
|
:key="row.name"
|
||||||
type="button"
|
type="button"
|
||||||
class="jd-side-row depth-1"
|
class="jd-side-row"
|
||||||
:class="{ selected: isSelectedExtra(row.name) }"
|
:class="[{ 'depth-1': !extraIsSolo, selected: isSelectedExtra(row.name) }]"
|
||||||
:title="row.name"
|
:title="row.name"
|
||||||
@click="pickExtra(row.name)"
|
@click="pickExtra(row.name)"
|
||||||
>
|
>
|
||||||
@@ -749,19 +740,10 @@ function cancelShopRename(): void {
|
|||||||
padding-top: 4px;
|
padding-top: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 不同源说明(2026-11-23):整树零命中时树收成一行,这里给点名句;视觉弱化但可读。 */
|
/* 树外名分组(授权树挂不上的本店类目):树被剪空时它就是主列表,不画上缘虚线。 */
|
||||||
.jd-side-foreign-head {
|
.jd-side-extra-solo {
|
||||||
color: #909399;
|
border-top: none;
|
||||||
}
|
padding-top: 0;
|
||||||
|
|
||||||
.jd-side-foreign-tip {
|
|
||||||
margin: 2px 8px 6px;
|
|
||||||
padding: 6px 8px;
|
|
||||||
border-radius: 6px;
|
|
||||||
background: #fdf6ec;
|
|
||||||
color: #b88230;
|
|
||||||
font-size: 12px;
|
|
||||||
line-height: 1.5;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.jd-side-extra-head {
|
.jd-side-extra-head {
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ const activeFilterChips = computed(() => {
|
|||||||
return chips
|
return chips
|
||||||
})
|
})
|
||||||
|
|
||||||
/** 侧栏选类目:树内节点带完整路径(序列化按子树展开 IN 下发)、树外兜底名 path 空=
|
/** 侧栏选类目:树内节点带完整路径(序列化按子树展开 IN 下发)、树外名 path 空=
|
||||||
* 单名等值旧形状;null=「全部商品」清选重查。选中即查(回第一页)。 */
|
* 单名等值旧形状;null=「全部商品」清选重查。选中即查(回第一页)。 */
|
||||||
function onSelectCategory(value: { name: string; path: string[] } | null): void {
|
function onSelectCategory(value: { name: string; path: string[] } | null): void {
|
||||||
filter.category = value?.name || ''
|
filter.category = value?.name || ''
|
||||||
@@ -397,7 +397,7 @@ async function loadCategoryOptions(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 服务端授权类目树(2026-11-23:客户端采集时按店铺上报、服务端并集)。
|
/** 服务端授权类目树(2026-11-23:客户端采集时按店铺上报、服务端并集)。
|
||||||
* null=还没取到或取不到 ⇒ 左栏回落内置快照(再由侧栏 treeForeign 判据收树点名)。 */
|
* null=还没取到或取不到 ⇒ 左栏回落内置快照(侧栏只渲染命中分支,零命中整支剪掉)。 */
|
||||||
const serverCategoryTree = ref<JdCategoryNode[] | null>(null)
|
const serverCategoryTree = ref<JdCategoryNode[] | null>(null)
|
||||||
|
|
||||||
/** 左栏实际生效的授权树:服务端真树优先,否则内置快照。
|
/** 左栏实际生效的授权树:服务端真树优先,否则内置快照。
|
||||||
@@ -860,7 +860,7 @@ onMounted(async () => {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<div class="jd-body">
|
<div class="jd-body">
|
||||||
<!-- 左侧栏(v1.6 → v1.7 名单维度):商品目录(京麦授权类目树+计数徽标+树外兜底)/
|
<!-- 左侧栏(v1.6 → v1.7 名单维度):商品目录(京麦授权类目树+计数徽标+树外类目)/
|
||||||
店铺分类(分类平铺+店铺名单展开/点选过滤/行内改名移除+添加店铺入口+新建+
|
店铺分类(分类平铺+店铺名单展开/点选过滤/行内改名移除+添加店铺入口+新建+
|
||||||
分类管理入口)。取数在页面,侧栏纯展示。 -->
|
分类管理入口)。取数在页面,侧栏纯展示。 -->
|
||||||
<JdCategorySidebar
|
<JdCategorySidebar
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ export interface JdCategoryCountedNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 计数聚合回执:roots=树形计数树(全部授权类目都在,无词条的节点 count=0);
|
/** 计数聚合回执:roots=树形计数树(全部授权类目都在,无词条的节点 count=0);
|
||||||
* extra=树上任何节点名(含非叶)都对不上的历史行(树外兜底组——京麦改树后的
|
* extra=树上任何节点名(含非叶)都对不上的历史行(树外类目组——京麦改树后的
|
||||||
* 存量 category_path 从这里选,语义同旧版级联第三栏兜底)。 */
|
* 存量 category_path 从这里选,语义同旧版级联第三栏兜底)。 */
|
||||||
export interface JdCategoryCountTree {
|
export interface JdCategoryCountTree {
|
||||||
roots: JdCategoryCountedNode[]
|
roots: JdCategoryCountedNode[]
|
||||||
@@ -216,7 +216,7 @@ export interface JdCategoryCountTree {
|
|||||||
/** /categories 计数行 → 树形计数聚合(v1.6 蓝图 Phase 1)。同名跨支叶(短裤/唐装/
|
/** /categories 计数行 → 树形计数聚合(v1.6 蓝图 Phase 1)。同名跨支叶(短裤/唐装/
|
||||||
* 吊带/背心/赠品)按过滤语义如实计入两个分支——点哪个分支都按同一名字等值命中
|
* 吊带/背心/赠品)按过滤语义如实计入两个分支——点哪个分支都按同一名字等值命中
|
||||||
* 同一批行,不是双计;父层子树内按名字去重求和(与展开集合口径一致,父层徽标
|
* 同一批行,不是双计;父层子树内按名字去重求和(与展开集合口径一致,父层徽标
|
||||||
* 不因同名跨支翻倍)。名字对不上树上任何节点的行归树外兜底组。脏值归一:空名
|
* 不因同名跨支翻倍)。名字对不上树上任何节点的行归树外类目组。脏值归一:空名
|
||||||
* 跳过、计数非有限数归 0、负数钳 0、同名多行求和。
|
* 跳过、计数非有限数归 0、负数钳 0、同名多行求和。
|
||||||
*
|
*
|
||||||
* {@code tree} 缺省=内置快照(一家店的历史取证);后管拿到服务端按店铺上报的授权树后
|
* {@code tree} 缺省=内置快照(一家店的历史取证);后管拿到服务端按店铺上报的授权树后
|
||||||
@@ -271,3 +271,23 @@ export function jdCategoryCountTree(
|
|||||||
}
|
}
|
||||||
return { roots, extra, extraTotal, treeMatched }
|
return { roots, extra, extraTotal, treeMatched }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 剪掉「本店一件商品都没有」的类目分支(2026-11-24 客户口径「正确是一开始显示的那棵树,
|
||||||
|
* 而不是什么树兜底」):
|
||||||
|
* - 授权树里 count=0 且子树也全 0 的支整支去掉(别店快照的服饰/母婴分支不再出现在
|
||||||
|
* 家居店左栏,界面只剩本店真有的类目);
|
||||||
|
* - 于是「整树零命中」时返回空数组,调用方直接把 extra(本店类目名)平铺成一级列表,
|
||||||
|
* 不再需要「树外兜底」这种退化分组;
|
||||||
|
* - count 是子树去重和(见 jdCategoryCountTree),父级 count>0 必然来自自身或后代命中,
|
||||||
|
* 所以判据只需看 count 与剪枝后的子级,不会把有命中的祖先一起剪掉。
|
||||||
|
* 纯函数、无副作用,node --test 直测。 */
|
||||||
|
export function jdCategoryPruneZero(nodes: JdCategoryCountedNode[]): JdCategoryCountedNode[] {
|
||||||
|
const out: JdCategoryCountedNode[] = []
|
||||||
|
for (const node of Array.isArray(nodes) ? nodes : []) {
|
||||||
|
const children = jdCategoryPruneZero(node.children)
|
||||||
|
if (node.count > 0 || children.length) {
|
||||||
|
out.push({ ...node, children, leaf: children.length === 0 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
* 与内置快照 `JD_CATEGORY_TREE`(2026-11-16 一家店的历史取证)**同形**——
|
* 与内置快照 `JD_CATEGORY_TREE`(2026-11-16 一家店的历史取证)**同形**——
|
||||||
* `{name, value, children}`,页面拿到后直接替换树数据源,计数/兜底/展开逻辑零改动。
|
* `{name, value, children}`,页面拿到后直接替换树数据源,计数/兜底/展开逻辑零改动。
|
||||||
*
|
*
|
||||||
* 失败/空一律返回空节点数组:页面据此回落内置快照(再由侧栏 `treeForeign` 判据
|
* 失败/空一律返回空节点数组:页面据此回落内置快照(侧栏只渲染本店命中的分支,
|
||||||
* 收树点名「本店无匹配」),绝不让接口抖动把左栏变成一个错误块。 */
|
* 零命中整支剪掉、树空就把本店类目平铺成一级),绝不让接口抖动把左栏变成错误块。 */
|
||||||
import { http } from '@/api/http'
|
import { http } from '@/api/http'
|
||||||
import { unwrap } from '../../api/envelope.ts'
|
import { unwrap } from '../../api/envelope.ts'
|
||||||
import { parseJdCategoryNodeTree, type JdCategoryNode } from './jd-category-tree.ts'
|
import { parseJdCategoryNodeTree, type JdCategoryNode } from './jd-category-tree.ts'
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。",
|
"note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。",
|
||||||
"totals": {
|
"totals": {
|
||||||
"files": 265,
|
"files": 265,
|
||||||
"cases": 1862,
|
"cases": 1865,
|
||||||
"pins": 463,
|
"pins": 463,
|
||||||
"reads": 664
|
"reads": 664
|
||||||
},
|
},
|
||||||
@@ -318,7 +318,7 @@
|
|||||||
"reads": 5
|
"reads": 5
|
||||||
},
|
},
|
||||||
"jd-category-tree.test.ts": {
|
"jd-category-tree.test.ts": {
|
||||||
"cases": 17,
|
"cases": 20,
|
||||||
"pins": 0,
|
"pins": 0,
|
||||||
"reads": 0
|
"reads": 0
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
jdCategoryCountTree,
|
jdCategoryCountTree,
|
||||||
jdCategoryLeaves,
|
jdCategoryLeaves,
|
||||||
jdCategoryNamesUnder,
|
jdCategoryNamesUnder,
|
||||||
|
jdCategoryPruneZero,
|
||||||
jdCategoryRoots,
|
jdCategoryRoots,
|
||||||
jdCategorySearchLeaves,
|
jdCategorySearchLeaves,
|
||||||
jdCategorySearchNodes,
|
jdCategorySearchNodes,
|
||||||
@@ -262,8 +263,8 @@ test('计数聚合——treeMatched 三态:树内有命中/整树零命中
|
|||||||
assert.equal(foreign.extraTotal, 264)
|
assert.equal(foreign.extraTotal, 264)
|
||||||
assert.equal(foreign.roots.every((root) => root.count === 0), true)
|
assert.equal(foreign.roots.every((root) => root.count === 0), true)
|
||||||
|
|
||||||
// 数据没到(空输入)同样是 false,但兜底为空——侧栏必须靠 extra 非空才判「不同源」,
|
// 数据没到(空输入)同样是 false,但兜底为空——侧栏靠剪枝后树为空 + extra 非空决定
|
||||||
// 否则加载中会闪一下「本店无匹配」。
|
// 怎么渲染(树空则本店类目平铺成一级列表),不靠 treeMatched 判「加载中」。
|
||||||
const empty = jdCategoryCountTree([])
|
const empty = jdCategoryCountTree([])
|
||||||
assert.equal(empty.treeMatched, false)
|
assert.equal(empty.treeMatched, false)
|
||||||
assert.equal(empty.extra.length, 0)
|
assert.equal(empty.extra.length, 0)
|
||||||
@@ -346,3 +347,28 @@ test('计数聚合——传入服务端真树:家居类目挂得上树、不
|
|||||||
assert.equal(snapshot.treeMatched, false)
|
assert.equal(snapshot.treeMatched, false)
|
||||||
assert.equal(snapshot.extraTotal, 264)
|
assert.equal(snapshot.extraTotal, 264)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('剪枝——零命中的别店分支整支剪掉,整树剪空(2026-11-24 客户口径「要的是类目树,不是树外兜底」)', () => {
|
||||||
|
const foreign = jdCategoryCountTree([
|
||||||
|
{ name: '化妆品收纳盒', productCount: 260 },
|
||||||
|
{ name: '其他收纳架', productCount: 4 },
|
||||||
|
])
|
||||||
|
assert.equal(jdCategoryPruneZero(foreign.roots).length, 0,
|
||||||
|
'家居类目套服饰快照树 ⇒ 整树剪空,调用方把 extra 平铺成一级列表(不再有「本店无匹配」档)')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('剪枝——命中分支保留祖先链、重算 leaf,零命中兄弟支不出现', () => {
|
||||||
|
const counted = jdCategoryCountTree([{ name: '连衣裙', productCount: 7 }])
|
||||||
|
const pruned = jdCategoryPruneZero(counted.roots)
|
||||||
|
assert.deepEqual(pruned.map((node) => node.name), ['服饰内衣'], '只有服饰内衣这一支真有商品')
|
||||||
|
assert.equal(nodeAt(pruned, ['服饰内衣', '女装'])!.count, 7, '祖先链上徽标不变(子树去重和)')
|
||||||
|
const dress = nodeAt(pruned, ['服饰内衣', '女装', '连衣裙'])!
|
||||||
|
assert.equal(dress.leaf, true, '子级全被剪掉后重算 leaf(不再显示展开箭头)')
|
||||||
|
assert.equal(nodeAt(pruned, ['服饰内衣', '男装']), null, '零命中兄弟支剪掉')
|
||||||
|
assert.equal(nodeAt(pruned, ['母婴']), null, '整支零命中的一级剪掉')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('剪枝——空输入/异常入参回空数组(渲染层不炸)', () => {
|
||||||
|
assert.deepEqual(jdCategoryPruneZero([]), [])
|
||||||
|
assert.deepEqual(jdCategoryPruneZero(null as unknown as JdCategoryCountedNode[]), [])
|
||||||
|
})
|
||||||
|
|||||||
@@ -77,7 +77,8 @@ describe('商品目录 Tab(京麦授权类目树+计数徽标)', () => {
|
|||||||
expect(all.text()).toContain('37', '全部商品徽标=5 条计数行求和(7+4+5+9+12)')
|
expect(all.text()).toContain('37', '全部商品徽标=5 条计数行求和(7+4+5+9+12)')
|
||||||
expect(rowByText(catalog, '服饰内衣').text()).toContain('20',
|
expect(rowByText(catalog, '服饰内衣').text()).toContain('20',
|
||||||
'服饰内衣根徽标=子树去重和(连衣裙7+短袖4+短裤9,短裤跨支同名只记一次)')
|
'服饰内衣根徽标=子树去重和(连衣裙7+短袖4+短裤9,短裤跨支同名只记一次)')
|
||||||
expect(rowByText(catalog, '母婴').text()).toContain('0', '无词条一级归 0 不缺节点')
|
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('母婴'))).toBe(false,
|
||||||
|
'零命中一级整支剪掉(2026-11-24:不显示本店没有的类目分支)')
|
||||||
expect(rowByText(catalog, '运动户外').text()).toContain('5')
|
expect(rowByText(catalog, '运动户外').text()).toContain('5')
|
||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
@@ -193,7 +194,7 @@ describe('树外兜底组(树上对不上的历史行,单名等值出口)'
|
|||||||
const wrapper = mountSidebar()
|
const wrapper = mountSidebar()
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
const catalog = wrapper.get('.jd-side-catalog')
|
||||||
const head = catalog.get('.jd-side-extra-head')
|
const head = catalog.get('.jd-side-extra-head')
|
||||||
expect(head.text()).toContain('树外兜底')
|
expect(head.text()).toContain('授权树外的类目', '树有命中时这一组的名字如实点名来处')
|
||||||
expect(head.text()).toContain('12', '兜底组头徽标=extraTotal(历史行求和)')
|
expect(head.text()).toContain('12', '兜底组头徽标=extraTotal(历史行求和)')
|
||||||
const extra = rowByText(catalog, '已下架历史类目')
|
const extra = rowByText(catalog, '已下架历史类目')
|
||||||
await extra.trigger('click')
|
await extra.trigger('click')
|
||||||
@@ -218,9 +219,12 @@ describe('树外兜底组(树上对不上的历史行,单名等值出口)'
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('内置授权树与本店不同源(2026-11-23:家居日用店打开服饰快照树)', () => {
|
describe('内置授权树与本店不同源(2026-11-24 客户口径:要的是类目树,不是「树外兜底」)', () => {
|
||||||
/** 活样本:库里只有家居收纳类目(家居日用数据库店的 264 件),内置树(花屿巷旗舰店的
|
/** 活样本:库里只有家居收纳类目(家居日用数据库店的 474 件),内置树(花屿巷旗舰店的
|
||||||
* 服饰授权快照)节点全 0——旧行为照样渲染整棵服饰树,界面看着像「这家店卖女装」。 */
|
* 服饰授权快照)节点全 0——旧行为照样渲染整棵服饰树(看着像「这家店卖女装」),
|
||||||
|
* 2026-11-23 一轮收成「本店无匹配」一行+全部落「树外兜底」(客户:「正确是一开始
|
||||||
|
* 显示的那棵树,而不是什么树兜底」)。现行口径:零命中分支整支剪掉,树空就把本店
|
||||||
|
* 类目平铺成一级列表——始终是一棵树,不出现别店类目、也不出现退化分组。 */
|
||||||
const FOREIGN_OPTIONS: JdProductCategoryOption[] = [
|
const FOREIGN_OPTIONS: JdProductCategoryOption[] = [
|
||||||
{ name: '化妆品收纳盒', productCount: 260 },
|
{ name: '化妆品收纳盒', productCount: 260 },
|
||||||
{ name: '其他收纳架', productCount: 4 },
|
{ name: '其他收纳架', productCount: 4 },
|
||||||
@@ -244,44 +248,36 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开
|
|||||||
return (catalog.get('.jd-side-tree').element as HTMLElement).style.display
|
return (catalog.get('.jd-side-tree').element as HTMLElement).style.display
|
||||||
}
|
}
|
||||||
|
|
||||||
it('整树零命中 ⇒ 树默认收起、只留一行「本店无匹配」,兜底行仍可选', async () => {
|
it('整树零命中 ⇒ 不渲染别店树,本店类目平铺成一级(无分组头、无「本店无匹配」行)', async () => {
|
||||||
const wrapper = mountForeign()
|
const wrapper = mountForeign()
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
const catalog = wrapper.get('.jd-side-catalog')
|
||||||
const head = catalog.get('.jd-side-foreign-head')
|
expect(treeDisplay(catalog)).toBe('none', '别店快照的服饰分支一件商品都没有 ⇒ 整树剪空')
|
||||||
expect(head.text()).toContain('本店无匹配', '点名「这棵树不适用本店」,不让人以为本店有服饰类目')
|
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '没有树就不摆展开/收起')
|
||||||
expect(treeDisplay(catalog)).toBe('none', '整棵树收起(别店快照不冒充本店类目)')
|
expect(catalog.find('.jd-side-extra-head').exists()).toBe(false,
|
||||||
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '收起态不摆展开/收起按钮')
|
'本店类目就是整列内容,不再套「树外兜底」分组头')
|
||||||
|
expect(catalog.text()).not.toContain('服饰内衣', '别店类目一个都不露脸')
|
||||||
|
|
||||||
const extra = rowByText(catalog, '化妆品收纳盒')
|
const extra = rowByText(catalog, '化妆品收纳盒')
|
||||||
|
expect(extra.classes()).not.toContain('depth-1', '平铺成本店一级类目(与树行同一层级)')
|
||||||
|
expect(extra.text()).toContain('260')
|
||||||
await extra.trigger('click')
|
await extra.trigger('click')
|
||||||
await flushPromises()
|
await flushPromises()
|
||||||
expect(wrapper.emitted('select-category')).toEqual([[{ name: '化妆品收纳盒', path: [] }]],
|
expect(wrapper.emitted('select-category')).toEqual([[{ name: '化妆品收纳盒', path: [] }]],
|
||||||
'树外历史行照旧按单名等值出口,筛选不因收树失效')
|
'平铺行仍按单名等值出口(path 空数组=无路径序列化)')
|
||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('点说明行 ⇒ 展开原树与提示文案;再点收起', async () => {
|
it('树内有命中 ⇒ 只渲染命中分支(零命中的一级整支不出现)', () => {
|
||||||
const wrapper = mountForeign()
|
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
|
||||||
await catalog.get('.jd-side-foreign-head').trigger('click')
|
|
||||||
await flushPromises()
|
|
||||||
expect(treeDisplay(catalog)).not.toBe('none', '点开仍可查原树形状(只读参考)')
|
|
||||||
expect(catalog.get('.jd-side-foreign-tip').text()).toContain('固定快照')
|
|
||||||
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(true, '点开后恢复展开/收起操作')
|
|
||||||
await catalog.get('.jd-side-foreign-head').trigger('click')
|
|
||||||
await flushPromises()
|
|
||||||
expect(treeDisplay(catalog)).toBe('none')
|
|
||||||
wrapper.unmount()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('树内有命中时照旧全树渲染(不同源判据不误伤正常店)', () => {
|
|
||||||
const wrapper = mountSidebar()
|
const wrapper = mountSidebar()
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
const catalog = wrapper.get('.jd-side-catalog')
|
||||||
expect(catalog.find('.jd-side-foreign-head').exists()).toBe(false)
|
|
||||||
expect(treeDisplay(catalog)).not.toBe('none')
|
expect(treeDisplay(catalog)).not.toBe('none')
|
||||||
|
expect(rowByText(catalog, '服饰内衣').text()).toContain('20')
|
||||||
|
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('母婴'))).toBe(false,
|
||||||
|
'母婴支零命中 ⇒ 剪掉(不拿别店的空类目占屏)')
|
||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('传服务端真树(家居店):树上就是收纳类目、兜底清空、不同源判据不触发', () => {
|
it('传服务端真树(家居店):树上就是收纳类目、不同源判据不触发', () => {
|
||||||
const wrapper = mount(JdCategorySidebar, {
|
const wrapper = mount(JdCategorySidebar, {
|
||||||
props: {
|
props: {
|
||||||
categoryOptions: FOREIGN_OPTIONS,
|
categoryOptions: FOREIGN_OPTIONS,
|
||||||
@@ -297,11 +293,9 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
const catalog = wrapper.get('.jd-side-catalog')
|
||||||
expect(catalog.find('.jd-side-foreign-head').exists()).toBe(false,
|
expect(treeDisplay(catalog)).not.toBe('none', '真树直接渲染')
|
||||||
'真树有命中 ⇒ 不再点名「本店无匹配」')
|
|
||||||
expect(treeDisplay(catalog)).not.toBe('none', '真树直接渲染,不收起')
|
|
||||||
expect(rowByText(catalog, '收纳用品').text()).toContain('260', '计数徽标按真树聚合')
|
expect(rowByText(catalog, '收纳用品').text()).toContain('260', '计数徽标按真树聚合')
|
||||||
expect(catalog.find('.jd-side-extra').exists()).toBe(false, '所有类目都挂得上真树,兜底组不占位')
|
expect(catalog.find('.jd-side-extra').exists()).toBe(false, '所有类目都挂得上真树,无树外组')
|
||||||
expect(catalog.text()).not.toContain('服饰内衣', '内置快照树被服务端真树替换')
|
expect(catalog.text()).not.toContain('服饰内衣', '内置快照树被服务端真树替换')
|
||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -352,14 +352,14 @@ describe('侧栏商品目录(类目筛选迁移自级联面板)', () => {
|
|||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('树外兜底组(已落库但京麦树里没有的历史名):点行单名等值下发;「全部商品」行清选重查', async () => {
|
it('树外类目组(已落库但京麦树里没有的历史名):点行单名等值下发;「全部商品」行清选重查', async () => {
|
||||||
jdApi.fetchJdProductCategories.mockResolvedValue(CATEGORY_OPTIONS)
|
jdApi.fetchJdProductCategories.mockResolvedValue(CATEGORY_OPTIONS)
|
||||||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 50 })
|
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 50 })
|
||||||
|
|
||||||
const catalog = wrapper.get('.jd-side-catalog')
|
const catalog = wrapper.get('.jd-side-catalog')
|
||||||
// 树外兜底段:树外历史行从兜底组搜出可选(path 空数组=页面按无路径序列化单名等值)。
|
// 授权树外的本店历史行收在单独一组(path 空数组=页面按无路径序列化单名等值)。
|
||||||
const extra = catalog.get('.jd-side-extra')
|
const extra = catalog.get('.jd-side-extra')
|
||||||
expect(extra.text()).toContain('树外兜底')
|
expect(extra.text()).toContain('授权树外的类目')
|
||||||
await extra.findAll('.jd-side-row').find((row) => row.text().includes('已下架历史类目'))!.trigger('click')
|
await extra.findAll('.jd-side-row').find((row) => row.text().includes('已下架历史类目'))!.trigger('click')
|
||||||
await flushPromises()
|
await flushPromises()
|
||||||
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ category: '已下架历史类目', categoryPath: [] })
|
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ category: '已下架历史类目', categoryPath: [] })
|
||||||
|
|||||||
Reference in New Issue
Block a user