fix(admin-jd): 类目计数未就绪时不画树,消掉首帧闪出别店快照树

首帧 /categories 还没回(categoryOptions 空)时,侧栏按回落快照把整棵服饰树
画了出来(徽标全 0、「全部商品 0」),计数到达后再收成「本店无匹配」一行——
用户看到的是「原本这样过一会变那样」。页面投影 categoryOptionsReady(失败回空
也算就绪),侧栏 catalogReady=false 时只留一行加载提示、总数显示「—」。

- props.categoriesReady 用 withDefaults 显式默认 true(Vue 把 Boolean 缺省强转
  false,不写默认值会让所有老调用点永远停在加载行)
- 判据:jd-category-sidebar.mount.test.ts +2(未就绪不画树/就绪后切出树与计数)
- 后管 node --test 1979 绿、vitest 253 绿、vue-tsc rc=0,快检四面绿
This commit is contained in:
2026-11-24 13:18:29 +08:00
parent 9538c1c646
commit 10fb52d0e9
3 changed files with 81 additions and 7 deletions
@@ -17,7 +17,7 @@ import {
} from './jd-category-tree.ts' } from './jd-category-tree.ts'
import type { JdProductCategoryOption, JdProductGroup } from './jd-product-api.ts' import type { JdProductCategoryOption, JdProductGroup } from './jd-product-api.ts'
const props = defineProps<{ const props = withDefaults(defineProps<{
/** /categories 计数行(京麦末级类目+商品数;页面持有,失败静默回空数组)。 */ /** /categories 计数行(京麦末级类目+商品数;页面持有,失败静默回空数组)。 */
categoryOptions: JdProductCategoryOption[] categoryOptions: JdProductCategoryOption[]
/** 当前数据权限分组的分类(V203 归属口径,带 productCount 徽标;v1.7 每行可带 /** 当前数据权限分组的分类(V203 归属口径,带 productCount 徽标;v1.7 每行可带
@@ -37,7 +37,15 @@ const props = defineProps<{
* 缺省/空数组=回落内置快照(`JD_CATEGORY_TREE`,一家店的历史取证, * 缺省/空数组=回落内置快照(`JD_CATEGORY_TREE`,一家店的历史取证,
* 另由 treeForeign 判据收树点名,不冒充本店类目)。 */ * 另由 treeForeign 判据收树点名,不冒充本店类目)。 */
categoryTree?: JdCategoryNode[] | null categoryTree?: JdCategoryNode[] | null
}>() /** 类目计数行是否已取过(页面 loading 态投影;缺省/true=已就绪)。首帧未就绪时侧栏
* 不画树:否则会先把回落快照整棵树(别店类目、徽标全 0、「全部商品 0」)画出来,计数
* 到达后再收成「本店无匹配」一行——用户肉眼看到一次「过一会变了」的闪变。 */
categoriesReady?: boolean
}>(), {
// Vue 的 Boolean prop 缺省会被强转成 false(不看 TS 可选性):不显式给默认值,
// 所有老调用点都会变成「永远加载中」——这里钉住出厂就绪。
categoriesReady: true,
})
const emit = defineEmits<{ const emit = defineEmits<{
/** 选类目:树内节点带完整路径(页面按子树展开类目名集合 IN 下发);树外兜底名 /** 选类目:树内节点带完整路径(页面按子树展开类目名集合 IN 下发);树外兜底名
@@ -76,6 +84,11 @@ 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)
/** 内置授权树与本店类目体系不同源:整棵树零命中、且兜底组确有行。内置 `JD_CATEGORY_TREE` /** 内置授权树与本店类目体系不同源:整棵树零命中、且兜底组确有行。内置 `JD_CATEGORY_TREE`
* 是 2026-11-16 从**一家店铺**(花屿巷旗舰店)京麦授权接口抓的固定快照,换一家店(如家居 * 是 2026-11-16 从**一家店铺**(花屿巷旗舰店)京麦授权接口抓的固定快照,换一家店(如家居
* 日用店)打开时它仍是整棵服饰内衣/母婴/运动户外树、节点全 0,而库内商品(收纳盒/脏衣篓…) * 日用店)打开时它仍是整棵服饰内衣/母婴/运动户外树、节点全 0,而库内商品(收纳盒/脏衣篓…)
@@ -357,17 +370,20 @@ function cancelShopRename(): void {
@click="emit('select-category', null)" @click="emit('select-category', null)"
> >
<span class="jd-side-name">全部商品</span> <span class="jd-side-name">全部商品</span>
<span class="jd-side-count">{{ totalCount }}</span> <span class="jd-side-count">{{ catalogReady ? totalCount : '—' }}</span>
</button> </button>
<!-- 类目计数未就绪(页面首帧):只留一行加载提示,不画整棵树——
否则会先显示回落快照的别店类目(徽标全 0),数据到达后再收树,肉眼一次闪变。 -->
<p v-if="!catalogReady" class="jd-side-loading">类目加载中…</p>
<!-- 全部展开/收起(v1.7 树增强,对齐原型树控件):深树一次到底/一键收干净。 <!-- 全部展开/收起(v1.7 树增强,对齐原型树控件):深树一次到底/一键收干净。
不同源(treeForeign)时随树一起藏,只在手动点开那棵树时出现。 --> 不同源(treeForeign)时随树一起藏,只在手动点开那棵树时出现。 -->
<div v-if="flatTree.length && (!treeForeign || foreignOpen)" class="jd-side-tree-ops"> <div v-if="catalogReady && flatTree.length && (!treeForeign || foreignOpen)" 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-23):内置授权树是别店的固定快照、本店类目一个都不在树上。
整棵树默认收起,只留这一行点名「本店无匹配」,避免把别店的服饰树当成自己店的类目。 --> 整棵树默认收起,只留这一行点名「本店无匹配」,避免把别店的服饰树当成自己店的类目。 -->
<template v-if="treeForeign"> <template v-if="catalogReady && treeForeign">
<button <button
type="button" type="button"
class="jd-side-row jd-side-foreign-head" class="jd-side-row jd-side-foreign-head"
@@ -383,7 +399,7 @@ function cancelShopRename(): void {
已按原名归入下方「树外兜底」。 已按原名归入下方「树外兜底」。
</p> </p>
</template> </template>
<div class="jd-side-tree" v-show="!treeForeign || foreignOpen"> <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('/')"
@@ -406,7 +422,7 @@ 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="counted.extra.length" class="jd-side-extra"> <div v-if="catalogReady && counted.extra.length" class="jd-side-extra">
<button <button
type="button" type="button"
class="jd-side-row jd-side-extra-head" class="jd-side-row jd-side-extra-head"
@@ -759,6 +775,14 @@ function cancelShopRename(): void {
text-align: center; text-align: center;
} }
/* 类目加载行(首帧未就绪占位):与空态同款弱化,不让整棵别店快照树先闪出来。 */
.jd-side-loading {
margin: 8px 4px;
font-size: 13px;
color: #c0c4cc;
text-align: center;
}
.jd-side-create { .jd-side-create {
display: flex; display: flex;
gap: 6px; gap: 6px;
@@ -98,6 +98,10 @@ const ownerGroupId = ref(0)
const groupOptions = ref<JdProductGroup[]>([]) const groupOptions = ref<JdProductGroup[]>([])
/** 类目计数行(京麦末级类目+商品数):侧栏商品目录树的徽标数据源;失败静默回空。 */ /** 类目计数行(京麦末级类目+商品数):侧栏商品目录树的徽标数据源;失败静默回空。 */
const categoryOptions = ref<JdProductCategoryOption[]>([]) const categoryOptions = ref<JdProductCategoryOption[]>([])
/** 类目计数是否已取过(成功或失败回空都算就绪):侧栏据此区分「还没到」与「确实没有」,
* 首帧未就绪时不画树——否则先把回落快照的别店类目整棵树(徽标全 0、「全部商品 0」)
* 画出来,计数到达后再收成「本店无匹配」一行,用户会看到一次莫名的闪变。 */
const categoryOptionsReady = ref(false)
const detailItem = ref<JdProductItem | null>(null) const detailItem = ref<JdProductItem | null>(null)
const detailVisible = ref(false) const detailVisible = ref(false)
/** 归类对话框(2026-17 替代下线的店铺分类弹窗,工具栏「归入分类」打开)。 */ /** 归类对话框(2026-17 替代下线的店铺分类弹窗,工具栏「归入分类」打开)。 */
@@ -386,6 +390,9 @@ async function loadCategoryOptions(): Promise<void> {
categoryOptions.value = await fetchJdProductCategories() categoryOptions.value = await fetchJdProductCategories()
} catch { } catch {
categoryOptions.value = [] categoryOptions.value = []
} finally {
// 失败也算就绪:回空=「确实没有类目」,不能永远停在加载行(否则整棵树下线)。
categoryOptionsReady.value = true
} }
} }
@@ -864,6 +871,7 @@ onMounted(async () => {
:selected-group-id="selectedGroupIdNum" :selected-group-id="selectedGroupIdNum"
:selected-shop-name="filter.shopName" :selected-shop-name="filter.shopName"
:category-tree="activeCategoryTree" :category-tree="activeCategoryTree"
:categories-ready="categoryOptionsReady"
@select-category="onSelectCategory" @select-category="onSelectCategory"
@select-group="onSelectGroup" @select-group="onSelectGroup"
@select-shop="onSelectShop" @select-shop="onSelectShop"
@@ -307,6 +307,48 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开
}) })
}) })
describe('类目计数未就绪(首帧加载态,2026-11-24)', () => {
/** 首帧真实形状:/categories 还没回(categoryOptions 空)、页面 loading 投影为 false。
* 旧行为会把回落快照整棵树先画出来(徽标全 0、「全部商品 0」),计数到达后再收成
* 「本店无匹配」一行——用户看到「原本这样过一会变那样」的闪变。 */
function mountLoading(): VueWrapper<Element> {
return mount(JdCategorySidebar, {
props: {
categoryOptions: [],
categories: CATEGORIES,
editable: true,
selectedCategory: null,
selectedGroupId: null,
categoriesReady: false,
},
})
}
it('未就绪 ⇒ 只留加载行,不先把内置快照整棵树画出来', () => {
const wrapper = mountLoading()
const catalog = wrapper.get('.jd-side-catalog')
expect(catalog.get('.jd-side-loading').text()).toContain('加载中')
// 树容器 v-show 收起(jsdom 的 text() 仍含隐藏子树,判据只认 inline display)。
expect((catalog.get('.jd-side-tree').element as HTMLElement).style.display).toBe('none',
'首帧不得闪出别店快照树')
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '加载态不摆展开/收起')
expect(catalog.find('.jd-side-extra').exists()).toBe(false, '加载态不摆兜底组')
expect(catalog.get('.jd-side-all').text()).toContain('—', '总数未知时不冒充 0')
wrapper.unmount()
})
it('数据到达(就绪)后同一实例切出树与计数,加载行消失', async () => {
const wrapper = mountLoading()
await wrapper.setProps({ categoryOptions: CATEGORY_OPTIONS, categoriesReady: true })
const catalog = wrapper.get('.jd-side-catalog')
expect(catalog.find('.jd-side-loading').exists()).toBe(false)
expect(catalog.get('.jd-side-all').text()).toContain('37')
expect((catalog.get('.jd-side-tree').element as HTMLElement).style.display).not.toBe('none')
expect(catalog.text()).toContain('服饰内衣')
wrapper.unmount()
})
})
describe('店铺分类 Tab(当前分组分类平铺+入口分档)', () => { describe('店铺分类 Tab(当前分组分类平铺+入口分档)', () => {
it('点店铺分类切 Tab;分类行带计数;点行 emit id、再点已选中的 emit null(toggle 清选)', async () => { it('点店铺分类切 Tab;分类行带计数;点行 emit id、再点已选中的 emit null(toggle 清选)', async () => {
const wrapper = mountSidebar() const wrapper = mountSidebar()