diff --git a/admin-frontend-vue/src/pages/jd/JdCategorySidebar.vue b/admin-frontend-vue/src/pages/jd/JdCategorySidebar.vue index 730d30880..f0dc7178d 100644 --- a/admin-frontend-vue/src/pages/jd/JdCategorySidebar.vue +++ b/admin-frontend-vue/src/pages/jd/JdCategorySidebar.vue @@ -10,8 +10,10 @@ * (点兜底行=单名等值旧形状,path 空数组交给页面回落)。 */ import { computed, nextTick, ref, watch } from 'vue' import { + JD_CATEGORY_TREE, jdCategoryCountTree, type JdCategoryCountedNode, + type JdCategoryNode, } from './jd-category-tree.ts' import type { JdProductCategoryOption, JdProductGroup } from './jd-product-api.ts' @@ -31,6 +33,10 @@ const props = defineProps<{ selectedGroupId?: number | null /** 当前店铺筛选(v1.7 店铺名单点选;空/缺省=未按店铺过滤)。 */ selectedShopName?: string | null + /** 授权类目树(2026-11-23):页面从服务端取「已上报店铺并集」的真树传入; + * 缺省/空数组=回落内置快照(`JD_CATEGORY_TREE`,一家店的历史取证, + * 另由 treeForeign 判据收树点名,不冒充本店类目)。 */ + categoryTree?: JdCategoryNode[] | null }>() const emit = defineEmits<{ @@ -64,7 +70,11 @@ const activeTab = ref<'catalog' | 'custom'>('catalog') // ---------------- 计数聚合(Phase 1 纯函数) ---------------- -const counted = computed(() => jdCategoryCountTree(props.categoryOptions ?? [])) +/** 生效的授权类目树:服务端真树优先,没有则回落到内置快照(一家店的历史取证)。 */ +const activeTree = computed(() => + (props.categoryTree && props.categoryTree.length ? props.categoryTree : JD_CATEGORY_TREE)) + +const counted = computed(() => jdCategoryCountTree(props.categoryOptions ?? [], activeTree.value)) /** 内置授权树与本店类目体系不同源:整棵树零命中、且兜底组确有行。内置 `JD_CATEGORY_TREE` * 是 2026-11-16 从**一家店铺**(花屿巷旗舰店)京麦授权接口抓的固定快照,换一家店(如家居 diff --git a/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue b/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue index eb22c1ff9..c50d36f09 100644 --- a/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue +++ b/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue @@ -46,6 +46,8 @@ import { JD_PRODUCT_PAGE_SIZE_OPTIONS, } from './jd-product-filter.ts' import { JD_PRODUCT_DEFAULT_SIZE, type JdProductItem } from './jd-product-model.ts' +import { JD_CATEGORY_TREE, jdCategoryNamesUnder, type JdCategoryNode } from './jd-category-tree.ts' +import { fetchJdShopCategoryTree } from './jd-shop-category-api.ts' import JdCategorySidebar from './JdCategorySidebar.vue' import JdProductCard from './JdProductCard.vue' import JdProductDetailModal from './JdProductDetailModal.vue' @@ -163,6 +165,12 @@ const activeFilterChips = computed(() => { function onSelectCategory(value: { name: string; path: string[] } | null): void { filter.category = value?.name || '' filter.categoryPath = value?.path ?? [] + // 父级点选的子树展开集合按**当前生效的树**算好存进过滤态(2026-11-23):树换成服务端 + // 真树后,点「收纳用品」展开的就是真树的子类目名;filter 纯函数层不持有树, + // 避免「侧栏用真树、查询用快照树」两处各读一份。 + filter.categoryNames = value && value.path.length + ? (jdCategoryNamesUnder(value.path, activeCategoryTree.value) ?? undefined) + : undefined apply() } @@ -381,6 +389,28 @@ async function loadCategoryOptions(): Promise { } } +/** 服务端授权类目树(2026-11-23:客户端采集时按店铺上报、服务端并集)。 + * null=还没取到或取不到 ⇒ 左栏回落内置快照(再由侧栏 treeForeign 判据收树点名)。 */ +const serverCategoryTree = ref(null) + +/** 左栏实际生效的授权树:服务端真树优先,否则内置快照。 + * 它同时是「父级点选展开集合」的口径源——树换了两处必须同源,不能各读各的。 */ +const activeCategoryTree = computed(() => + (serverCategoryTree.value && serverCategoryTree.value.length + ? serverCategoryTree.value + : JD_CATEGORY_TREE)) + +/** 取授权类目树:失败静默回落快照(左栏不是首屏主功能,不占 loadFailure 提示, + * 与 loadCategoryOptions 同档兜底)。 */ +async function loadShopCategoryTree(): Promise { + try { + const result = await fetchJdShopCategoryTree() + serverCategoryTree.value = result.nodes.length ? result.nodes : null + } catch { + serverCategoryTree.value = null + } +} + /** 切换「分组」下拉:分类筛选(属于上一组的分类)清空,分类选项换成本组,回第一页重取。 * 勾选集是按商品 id 记的(跨页累计),切组不清——归类的落点组随 ownerGroupId 走, * 误跨组批量归类由归类对话框里的组名标注防住。 */ @@ -635,6 +665,7 @@ onMounted(async () => { // 不要先发一条不带 owner_group_id 的列表请求再补,两次请求会落在不同默认组上)。 await loadCategoryScope() void loadCategoryOptions() + void loadShopCategoryTree() await load() }) @@ -832,6 +863,7 @@ onMounted(async () => { :selected-category="selectedCategoryInfo" :selected-group-id="selectedGroupIdNum" :selected-shop-name="filter.shopName" + :category-tree="activeCategoryTree" @select-category="onSelectCategory" @select-group="onSelectGroup" @select-shop="onSelectShop" diff --git a/admin-frontend-vue/src/pages/jd/jd-category-tree.ts b/admin-frontend-vue/src/pages/jd/jd-category-tree.ts index c5ff88d96..d5d26adc7 100644 --- a/admin-frontend-vue/src/pages/jd/jd-category-tree.ts +++ b/admin-frontend-vue/src/pages/jd/jd-category-tree.ts @@ -38,6 +38,31 @@ export function jdCategoryRoots(): JdCategoryNode[] { return JD_CATEGORY_TREE } +/** 解析服务端下发的授权树(2026-11-23):形状与内置快照同款 `{name, value, children}`。 + * + * 脏值归一:空名/非对象节点自身丢弃但子级仍展开(不丢整支);value 缺失按空串; + * 超过 8 层截断(京麦现状最深 4 级,护栏只防脏数据递归)。 + * 纯函数、零依赖——放在本文件而不是 api 文件,`node --test` 才能直测。 */ +export function parseJdCategoryNodeTree(raw: unknown, depth = 0): JdCategoryNode[] { + if (!Array.isArray(raw) || depth > 8) return [] + const out: JdCategoryNode[] = [] + for (const item of raw) { + if (!item || typeof item !== 'object') continue + const record = item as Record + const name = typeof record.name === 'string' ? record.name.trim() : '' + const value = record.value == null ? '' : String(record.value).trim() + if (!name) { + out.push(...parseJdCategoryNodeTree(record.children, depth + 1)) + continue + } + const node: JdCategoryNode = { name, value } + const children = parseJdCategoryNodeTree(record.children, depth + 1) + if (children.length) node.children = children + out.push(node) + } + return out +} + /** 按名称路径下钻取子级;路径无效返回空数组(面板该栏缺省)。 */ export function jdCategoryChildren(path: string[]): JdCategoryNode[] { let level = JD_CATEGORY_TREE @@ -129,8 +154,8 @@ export function jdCategorySearchNodes(keyword: string): JdCategoryNodeRef[] { * 某批行的末级名(真机样本「艺考形体服」即三级节点名)。路径无效返回 null(调用方 * 按单名等值兜底,语义同树外历史行)。 */ -export function jdCategoryNamesUnder(path: string[]): string[] | null { - let level = JD_CATEGORY_TREE +export function jdCategoryNamesUnder(path: string[], tree: JdCategoryNode[] = JD_CATEGORY_TREE): string[] | null { + let level = tree let target: JdCategoryNode | null = null for (const name of path) { const hit = level.find((node) => node.name === name) @@ -192,8 +217,14 @@ export interface JdCategoryCountTree { * 吊带/背心/赠品)按过滤语义如实计入两个分支——点哪个分支都按同一名字等值命中 * 同一批行,不是双计;父层子树内按名字去重求和(与展开集合口径一致,父层徽标 * 不因同名跨支翻倍)。名字对不上树上任何节点的行归树外兜底组。脏值归一:空名 - * 跳过、计数非有限数归 0、负数钳 0、同名多行求和。 */ -export function jdCategoryCountTree(entries: JdCategoryCountEntry[]): JdCategoryCountTree { + * 跳过、计数非有限数归 0、负数钳 0、同名多行求和。 + * + * {@code tree} 缺省=内置快照(一家店的历史取证);后管拿到服务端按店铺上报的授权树后 + * 传它进来,同一套计数/兜底口径直接复用(2026-11-23)。 */ +export function jdCategoryCountTree( + entries: JdCategoryCountEntry[], + tree: JdCategoryNode[] = JD_CATEGORY_TREE, +): JdCategoryCountTree { const counts = new Map() for (const entry of Array.isArray(entries) ? entries : []) { const name = entry && typeof entry.name === 'string' ? entry.name.trim() : '' @@ -223,7 +254,7 @@ export function jdCategoryCountTree(entries: JdCategoryCountEntry[]): JdCategory } return { out, names } } - const roots = build(JD_CATEGORY_TREE).out + const roots = build(tree).out const extra: JdCategoryCountEntry[] = [] let extraTotal = 0 for (const [name, count] of counts) { diff --git a/admin-frontend-vue/src/pages/jd/jd-product-filter.ts b/admin-frontend-vue/src/pages/jd/jd-product-filter.ts index 4e9dd77d2..abb0cc9aa 100644 --- a/admin-frontend-vue/src/pages/jd/jd-product-filter.ts +++ b/admin-frontend-vue/src/pages/jd/jd-product-filter.ts @@ -53,6 +53,11 @@ export interface JdProductFilterState { * jdCategoryNamesUnder 展开为子树类目名集合下发(后端 IN);空=末级/树外行, * 按单名等值下发(旧形状)。 */ categoryPath?: string[] + /** 父级点选时按**当前生效授权树**展开好的类目名集合(2026-11-23):树可以是服务端 + * 按店铺上报的真树,纯函数层不持有树,所以由页面在选中那一刻算好放在这里。 + * 有值时优先于按内置快照展开的 `jdCategoryNamesUnder(categoryPath)`; + * 空/缺省=照旧按快照树展开(向后兼容旧调用与测试)。 */ + categoryNames?: string[] /** 店铺名(v1.7 店铺名单维度:侧栏点店铺行下发,精确 eq;空串不限)。 */ shopName: string /** 分类 id 文本(侧栏店铺分类 Tab 里那一行),空串=全部分类。 */ @@ -82,6 +87,7 @@ export function createJdProductFilterState(): JdProductFilterState { keyword: '', category: '', categoryPath: [], + categoryNames: [], shopName: '', groupId: '', classified: JD_PRODUCT_DEFAULT_CLASSIFIED, @@ -164,9 +170,13 @@ function toJdProductFilterQuery(state: JdProductFilterState): Omit { + const { data } = await http.get(JD_SHOP_CATEGORY_TREE_ENDPOINT) + const core = unwrap(data) + if (!core || typeof core !== 'object') return { nodes: [], shops: [] } + const record = core as Record + const shops: JdShopCategoryTreeShop[] = [] + for (const raw of Array.isArray(record.shops) ? record.shops : []) { + if (!raw || typeof raw !== 'object') continue + const row = raw as Record + const shopId = row.shopId == null ? '' : String(row.shopId).trim() + if (!shopId) continue + shops.push({ + shopId, + shopName: typeof row.shopName === 'string' ? row.shopName : null, + capturedAt: typeof row.capturedAt === 'string' ? row.capturedAt : null, + nodeCount: typeof row.nodeCount === 'number' && Number.isFinite(row.nodeCount) + ? Math.max(0, Math.floor(row.nodeCount)) + : 0, + }) + } + return { nodes: parseJdCategoryNodeTree(record.nodes), shops } +} \ No newline at end of file diff --git a/admin-frontend-vue/tests/baselines/source-pin-ledger.json b/admin-frontend-vue/tests/baselines/source-pin-ledger.json index c7915c39c..d7511bbe4 100644 --- a/admin-frontend-vue/tests/baselines/source-pin-ledger.json +++ b/admin-frontend-vue/tests/baselines/source-pin-ledger.json @@ -2,7 +2,7 @@ "note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。", "totals": { "files": 265, - "cases": 1860, + "cases": 1862, "pins": 463, "reads": 664 }, @@ -318,7 +318,7 @@ "reads": 5 }, "jd-category-tree.test.ts": { - "cases": 15, + "cases": 17, "pins": 0, "reads": 0 }, diff --git a/admin-frontend-vue/tests/jd-category-tree.test.ts b/admin-frontend-vue/tests/jd-category-tree.test.ts index 3d5b074a1..0ba9b1c63 100644 --- a/admin-frontend-vue/tests/jd-category-tree.test.ts +++ b/admin-frontend-vue/tests/jd-category-tree.test.ts @@ -10,7 +10,9 @@ import { jdCategoryRoots, jdCategorySearchLeaves, jdCategorySearchNodes, + parseJdCategoryNodeTree, type JdCategoryCountedNode, + type JdCategoryNode, } from '../src/pages/jd/jd-category-tree.ts' /** 京麦授权类目真树(2026-11-16 真机取证 getVenderLicenseCategory 快照;与用户端 @@ -290,3 +292,57 @@ test('计数聚合——空输入:全树 0、兜底组空(结构仍完整可 assert.equal(junk.roots.length, 3) assert.equal(junk.extraTotal, 0) }) + +test('服务端授权树解析:与内置快照同形、空名节点丢自身保子级、非数组/超深归空', () => { + const parsed = parseJdCategoryNodeTree([ + { name: ' 收纳用品 ', value: '7001', children: [ + { name: '化妆品收纳盒', value: '7002' }, + { name: '', value: 'x', children: [{ name: '孤儿但保留', value: '7003' }] }, + { notAnObject: true }, + ] }, + { name: '厨房用品', value: 7004 }, + ]) + assert.deepEqual(parsed, [ + { name: '收纳用品', value: '7001', children: [ + { name: '化妆品收纳盒', value: '7002' }, + { name: '孤儿但保留', value: '7003' }, + ] }, + { name: '厨房用品', value: '7004' }, + ], 'name trim、value 数字转串、空名节点自身丢弃但子级上提') + + assert.deepEqual(parseJdCategoryNodeTree(null), [], '非数组按空处理(接口抖动不炸渲染)') + + // 10 层脏数据:只留前 9 层(depth 0..8),第 10 层被护栏截断。 + let deep: unknown = [{ name: 'L10', value: '10' }] + for (let i = 9; i >= 1; i--) { + deep = [{ name: 'L' + i, value: String(i), children: deep }] + } + assert.equal(JSON.stringify(parseJdCategoryNodeTree(deep)).includes('L10'), false, + '超过 8 层的脏数据不递归下去(护栏在解析层,不留给渲染层)') +}) + +test('计数聚合——传入服务端真树:家居类目挂得上树、不再落兜底(2026-11-23 家居店活样本)', () => { + const homeTree: JdCategoryNode[] = [ + { name: '收纳用品', value: '7001', children: [ + { name: '化妆品收纳盒', value: '7002' }, + { name: '脏衣篓', value: '7003' }, + ] }, + { name: '厨房用品', value: '7101', children: [{ name: '其他收纳架', value: '7102' }] }, + ] + const counts = [ + { name: '化妆品收纳盒', productCount: 260 }, + { name: '其他收纳架', productCount: 4 }, + ] + const real = jdCategoryCountTree(counts, homeTree) + assert.equal(real.treeMatched, true, '真树有命中 ⇒ 侧栏不收树') + assert.equal(real.extra.length, 0, '家居类目全部挂得上真树,不再落树外兜底') + assert.equal(real.roots.length, 2) + assert.equal(nodeAt(real.roots, ['收纳用品', '化妆品收纳盒'])!.count, 260) + assert.equal(nodeAt(real.roots, ['收纳用品'])!.count, 260, '父级徽标=子树和') + assert.equal(nodeAt(real.roots, ['厨房用品'])!.count, 4) + + // 同一批计数行套内置快照树 ⇒ 整树零命中(数据没错,是树不对——这就是本次修的那个形状)。 + const snapshot = jdCategoryCountTree(counts) + assert.equal(snapshot.treeMatched, false) + assert.equal(snapshot.extraTotal, 264) +}) diff --git a/admin-frontend-vue/tests/mount/jd-category-sidebar.mount.test.ts b/admin-frontend-vue/tests/mount/jd-category-sidebar.mount.test.ts index 01588fc2a..82386e867 100644 --- a/admin-frontend-vue/tests/mount/jd-category-sidebar.mount.test.ts +++ b/admin-frontend-vue/tests/mount/jd-category-sidebar.mount.test.ts @@ -280,6 +280,31 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开 expect(treeDisplay(catalog)).not.toBe('none') wrapper.unmount() }) + + it('传服务端真树(家居店):树上就是收纳类目、兜底清空、不同源判据不触发', () => { + const wrapper = mount(JdCategorySidebar, { + props: { + categoryOptions: FOREIGN_OPTIONS, + categories: CATEGORIES, + editable: true, + selectedCategory: null, + selectedGroupId: null, + // 2026-11-23:客户端按店铺上报的授权树(形状与内置快照同款)。 + categoryTree: [ + { name: '收纳用品', value: '7001', children: [{ name: '化妆品收纳盒', value: '7002' }] }, + { name: '厨房用品', value: '7101', children: [{ name: '其他收纳架', value: '7102' }] }, + ], + }, + }) + const catalog = wrapper.get('.jd-side-catalog') + expect(catalog.find('.jd-side-foreign-head').exists()).toBe(false, + '真树有命中 ⇒ 不再点名「本店无匹配」') + expect(treeDisplay(catalog)).not.toBe('none', '真树直接渲染,不收起') + expect(rowByText(catalog, '收纳用品').text()).toContain('260', '计数徽标按真树聚合') + expect(catalog.find('.jd-side-extra').exists()).toBe(false, '所有类目都挂得上真树,兜底组不占位') + expect(catalog.text()).not.toContain('服饰内衣', '内置快照树被服务端真树替换') + wrapper.unmount() + }) }) describe('店铺分类 Tab(当前分组分类平铺+入口分档)', () => {