feat(admin-jd): 商品目录左栏改读服务端按店铺授权树(无上报时回落内置快照)

承接后端 85425771:左栏树不再只有硬编码快照这一档。

- jd-shop-category-api.ts:GET /api/jd-shop-category-tree 取并集森林(复用 jd-category-tree.ts
  的 parseJdCategoryNodeTree 做脏值/超深/超量护栏,与客户端上报侧同口径)
- JdDatabasePage:onMounted 增一路 loadShopCategoryTree(失败静默回落快照,不占首屏
  失败提示);activeCategoryTree=服务端真树优先、否则内置快照,并作为父级点选展开
  集合的唯一口径源(filter.categoryNames 在选中那一刻按同一棵树算好)
- JdCategorySidebar:prop categoryTree 可注入,计数树按它算;tier1 的 treeForeign
  消歧档照旧(整树零命中且树外有行时才收树点名)
- jd-product-filter:state.categoryNames 优先于按快照展开的 jdCategoryNamesUnder

判据:jd-category-tree.test.ts +2(parse 形状/脏值/超深;自定义树计数对照内置快照)、
mount +1(服务端树渲染「收纳用品 260」且不出现服饰内衣/无 treeForeign、不重复出树外组);
source-pin 基线 jd-category-tree.test.ts 15→17。admin 三连绿:node --test 1979、
vitest 251、vue-tsc rc=0。
This commit is contained in:
2026-11-23 21:27:55 +08:00
parent 854257712a
commit 02cf5e95e5
8 changed files with 228 additions and 11 deletions
@@ -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<string, unknown>
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<string, number>()
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) {