Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdCategoryCascader.vue
T
huangzd1997 4de78adfc7 feat(jd-admin): 2026-11-18 客户三条反馈闭环——公司超管收窄到名下+京东类目一级/二级可选中(IN 下推)+级联触发框 34px 对齐
【一、用户管理:公司超管「各自看各自名下」(backend-java AdminUserService)】
客户口径(11-18 截图:阿华66 公司超管看到了 admin 直建的 ssss):平台超管看所有,
其余公司超管只看自己名下。读侧 listUsers 公司超管分支由「本公司全部(role≠super_admin)」
改为 company_id=本公司 AND (id=本人 OR created_by_id=本人)——与组长档同一形状;
自己那行永远可见(保证能改自己密码/额度)。写侧 updateUser/deleteUser 公司超管分支
在「同公司」闸之外加同形「目标 created_by_id=本人」谓词,不留「列表看不见、API 照样改」
的洞(停用/改密/额度全走这条闸)。createUser 不动(新建天然落本人名下)。
口径为直接下级:组长再建的普通号归组长看,不在公司超管列表(如需整棵下级树另拍板)。
判据:AdminUserServiceCompanySuperAdminTest 改 1 增 5(名下可见谓词/超管档反向不收窄/
写侧越权 403 两支+本人建的可编辑);其余 admin 测试类夹具全用超管/组长档,核过不受影响。

【二、京东类目一级/二级可选中(前端展开+后端 IN,零表结构变更,用户拍板路线)】
后端:AdminJdProductController.page 的 category 参数改 List<String>(重复 query key 绑定,
参数名不变、单值形状照常);JdProductQueryService.Filter 逐项 trim/去空白/去重、空集合归
null=不过滤;JdProductMapper.xml productConds 类目谓词 eq→IN+foreach(仍 SQL 侧下推,
铁律 20 同形)。落库仍只有末级类目名,无 schema/迁移改动。
前端:jd-category-tree.ts 新增 jdCategoryAllNodes/jdCategorySearchNodes(搜索铺全层级)
+jdCategoryNamesUnder(path)(选中节点→自身+全部后代类目名集合,含自身因实际末级可能
就在这一级;路径失效回 null 兜单名)。JdCategoryCascader.vue:非末级行圆点点击=选中
(emit change 带名称路径)、行体/箭头仍下钻;选中态按路径比对(同名跨支不串);搜索
结果含一级/二级行。JdDatabasePage/jd-product-filter/jd-product-api:categoryPath 入筛选态,
序列化按子树展开集合,axios paramsSerializer indexes:false 出重复 category= 参数。
已知代价(路线自带、用户已选):不同父级下同名末级会随展开集合一起命中(B112-05 同口径)。

【三、样式(JdCategoryCascader.vue)】
触发框 min-height 42→34px+padding 5x10:main.css:245-248 全局密度收口把相邻 input/
el-select/.btn 压到 34,自绘 button 不在收口清单里才显高;非末级圆点不再永远空心。

【台账】source-pin-ledger.json 重录(UPDATE_SOURCE_PIN_BASELINE=1 官方口):本笔
jd-category-tree.test.ts 用例 6→8;同时把 2a8bdd7d 已提交未记账的
user-menu-tree-platform-filter.test.ts(8 例 0 钉 0 读点)一并登记——基线在该窗陈旧,
此处为台账记账、非卷码。

【验证】mvn -o test-compile rc=0;定向 mvn -o test(AdminUserService* 七类+
AdminJdProductControllerTest)rc=0;admin vue-tsc rc=0;node --test jd-category-tree/
jd-database-page/jd-product-group/cascader-position 全绿;vitest mount jd-database-page
44/44(含本笔新增一级/二级选中用例);ops _push_gate.py --fast overall=pass 四面绿
(首轮后管 readSource 格红=本笔用例数,重录基线后复跑绿)。
发版未做:jar+后管随下一窗口走既有流程;full 闸门未跑(模式 D 口径)。
2026-11-18 16:53:36 +08:00

503 lines
17 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/** 京东数据库「类目」筛选的京麦同款级联面板(2026-11-16 客户口径「京麦类目是这些 我们
* 后台的现在的不对」+「你直接爬」→ 树数据换成京麦授权类目真树,见 ./jd-category-tree.ts
* 头注释的取证来源与边界)。
*
* 交互(京麦真机同款,2026-11-16 面板逐级点开取证):面板按点亮路径动态出栏——第一栏
* 一级(3 个)、后续栏=点亮节点的 children(最深 4 栏);**非末级行点击=下钻**(推进
* 路径)、**末级行(children 空)点击=选中**;2026-11-18 客户口径「一级/二级类目也要能
* 选来搜索」:**非末级行的圆点点击=选中该节点**(emit change 带名称路径,页面按子树
* 展开类目名集合下发,后端 IN 过滤;行体/箭头仍下钻)。搜索框搜**树内全部层级**
* (一级/二级/末级都可选,名称或路径命中,行 title 带全路径);树外命中(已落库但京麦
* 类目体系变更后的历史名)从 options 兜底段选,语义同旧版第三栏。「全部类目」行=清选;
* 触发框已选时出 × 清除;面板外点透明遮罩关闭。
* 零依赖自绘(radio 圆点/箭头都是 CSS),无 element 依赖。 */
import { computed, nextTick, ref, watch } from 'vue'
import {
jdCategoryChildren,
jdCategoryRoots,
jdCategorySearchNodes,
type JdCategoryNode,
} from './jd-category-tree.ts'
import { clampPanelLeftX } from './jd-category-cascader-position.ts'
const props = defineProps<{
/** 已落库类目全集(/categories 现算,页面传入)——只作**树外兜底**(搜索无树内
* 命中时列出「已落库类目(树外历史)」段),不再默认铺第三栏。 */
options?: { name: string; productCount: number }[]
/** 当前选中类目名(末级或父级);'' = 全部类目。 */
modelValue: string
/** 当前选中节点的名称路径(从一级起);空/缺省=末级按名等值或树外行。
* 2026-11-18 一级/二级可选中:同名跨支(吊带/背心等)靠路径区分选中态回显。 */
selectedPath?: string[]
groups?: { id: number; name: string; productCount: number }[]
groupId?: string
createVersion?: number
busy?: boolean
treeOnly?: boolean
}>()
const emit = defineEmits<{
/** (类目名, 节点名称路径):末级/父级选中都带路径;树外兜底行与清选带 []。 */
(event: 'change', value: string, path: string[]): void
(event: 'group', value: string): void
(event: 'create-group', value: string): void
(event: 'delete-group', id: number): void
}>()
const newGroupName = ref('')
watch(() => props.createVersion, () => { newGroupName.value = '' })
function createGroup(): void {
const name = newGroupName.value.trim()
if (name) emit('create-group', name)
}
function pickGroup(id: number): void {
emit('group', String(id))
close()
}
const open = ref(false)
/** 已下钻的节点名路径(非末级行点击推进)。 */
const activePath = ref<string[]>([])
const searchText = ref('')
/** 面板水平定位钳制(2026-11-16「超出视窗了」):触发框在筛选行中部偏右时,锚 left:0
* 的多栏面板会伸出视窗右缘。打开与栏数/搜索态变化后用真实 rect 重算 left 偏移——
* 右缘贴齐视口后继续下钻即向左生长,不再截掉末栏。钳制数学在
* ./jd-category-cascader-position.ts(纯函数,tests/jd-category-cascader-position.test.ts)。 */
const hostRef = ref<HTMLElement | null>(null)
const panelRef = ref<HTMLElement | null>(null)
const panelOffsetX = ref(0)
function reposition(): void {
const host = hostRef.value
const panel = panelRef.value
if (!host || !panel) return
panelOffsetX.value = clampPanelLeftX({
hostLeft: host.getBoundingClientRect().left,
panelWidth: panel.offsetWidth,
viewportWidth: window.innerWidth,
})
}
const roots = jdCategoryRoots()
/** 树内全部节点名集合(含非末级;判定 options 哪些是「树外历史」——11-18 起父级名
* 也可选中过滤,历史行名只要树内有任何同名节点就走树内行,不再重复铺兜底段)。 */
const treeNodeNames = new Set(
jdCategorySearchNodes('').map((node) => node.name),
)
/** 面板各栏内容:第 0 栏一级,第 i+1 栏=点亮路径前 i+1 个的 children(路径推进到
* 末级时末栏是叶子所在栏,children 空不出栏——栏数随路径动态 1~4)。 */
const columns = computed<JdCategoryNode[][]>(() => {
const cols: JdCategoryNode[][] = [roots]
for (let i = 0; i < activePath.value.length; i++) {
const children = jdCategoryChildren(activePath.value.slice(0, i + 1))
if (!children.length) break
cols.push(children)
}
return cols
})
/** 搜索激活=搜索词非空:面板切换成扁平结果列表(2026-11-18 起含一级/二级)。 */
const searching = computed(() => searchText.value.trim().length > 0)
/** 树内命中节点(全层级;名称或路径段命中)。 */
const searchNodes = computed(() =>
searching.value ? jdCategorySearchNodes(searchText.value) : [],
)
/** 树外兜底:options 里不在树名集的行按搜索词过滤(历史行仍可选中过滤)。 */
const fallbackOptions = computed(() => {
if (!searching.value) return []
const keyword = searchText.value.trim().toLowerCase()
return (props.options ?? [])
.filter((option) => !treeNodeNames.has(option.name))
.filter((option) => option.name.toLowerCase().includes(keyword))
})
/** 栏数随下钻动态 1~4、搜索态切单栏 ⇒ 面板宽度变,开着就要重算偏移(下钻向左生长)。 */
watch(
() => [columns.value.length, searching.value] as const,
() => {
if (open.value) nextTick(reposition)
},
)
function toggle(): void {
if (open.value) {
close()
return
}
activePath.value = []
searchText.value = ''
open.value = true
nextTick(reposition)
}
function close(): void {
open.value = false
activePath.value = []
}
/** 栏内节点的完整名称路径(下钻态:已点亮前缀+本节点名)。 */
function nodePath(node: JdCategoryNode, depth: number): string[] {
return [...activePath.value.slice(0, depth), node.name]
}
/** 选中态回显按**路径**比对(同名跨支不串);树外兜底行没有路径,按名比对。 */
function isSelected(path: string[]): boolean {
const current = props.selectedPath ?? []
return current.length === path.length && current.every((segment, i) => segment === path[i])
}
/** 非末级行体/箭头点击=下钻;末级行点击=选中(带路径)。 */
function pick(node: JdCategoryNode, depth: number): void {
if (node.children && node.children.length) {
activePath.value = nodePath(node, depth)
return
}
selectNode(node, depth)
}
/** 圆点点击(任意层级)=选中该节点并收面板——2026-11-18 一级/二级类目可选中,
* 页面按 jdCategoryNamesUnder(path) 展开子树类目名集合下发。 */
function selectNode(node: JdCategoryNode, depth: number): void {
emit('change', node.name, nodePath(node, depth))
close()
}
function pickAll(): void {
emit('change', '', [])
close()
}
/** 搜索结果行点击:树内行(含一级/二级)按路径选中;树外兜底行按名选中(path 空)。 */
function pickName(name: string, path: string[]): void {
emit('change', name, path)
close()
}
function clear(): void {
emit('change', '', [])
}
</script>
<template>
<div ref="hostRef" class="cat-cascader">
<button
class="cat-trigger"
type="button"
:class="{ open, filled: !!modelValue || !!groupId }"
@click="toggle"
>
<span class="cat-trigger-text" :title="modelValue || ''">{{ groups?.find(group => String(group.id) === groupId)?.name || modelValue || '全部类目' }}</span>
<span
v-if="modelValue || groupId"
class="cat-clear"
role="button"
aria-label="清除类目"
@click.stop="clear"
>×</span>
<span class="cat-chevron" aria-hidden="true"></span>
</button>
<!-- 面板外点透明遮罩关闭(零 document 监听器,无泄漏面)。 -->
<div v-if="open" class="cat-mask" @click="close"></div>
<div v-if="open" ref="panelRef" class="cat-panel" :style="{ left: `${panelOffsetX}px` }">
<div v-if="!treeOnly" class="cat-custom cat-col-custom">
<div class="cat-seg">自定义分类</div>
<form class="cat-create" @submit.prevent="createGroup">
<input v-model="newGroupName" class="cat-search" placeholder="新建分类" aria-label="新建分类名称" />
<button type="submit" :disabled="busy || !newGroupName.trim()">添加</button>
</form>
<div v-for="group in groups || []" :key="group.id" class="cat-row" :class="{ active: groupId === String(group.id) }">
<button type="button" class="cat-group-pick" :title="group.name" @click="pickGroup(group.id)">{{ group.name }}({{ group.productCount }})</button>
<button type="button" :aria-label="`删除分类 ${group.name}`" @click.stop="emit('delete-group', group.id)">删除</button>
</div>
</div>
<!-- 搜索态:树内全层级命中(一级/二级/末级都可点选)+树外兜底段(扁平列表,行 title 带全路径)。 -->
<template v-if="searching">
<div class="cat-col cat-col-search">
<div class="cat-search-row"><input v-model="searchText" type="text" class="cat-search" placeholder="搜索类目" /></div>
<template v-if="searchNodes.length || fallbackOptions.length">
<div
v-for="node in searchNodes"
:key="`tree-${node.path.join('/')}`"
class="cat-row"
:class="{ active: isSelected(node.path) }"
:title="node.leaf ? node.path.join(' > ') : `${node.path.join(' > ')}(含下级,选中按整棵子树筛选)`"
@click="pickName(node.name, node.path)"
>
<span class="cat-dot" :class="{ on: isSelected(node.path) }" aria-hidden="true"></span>
<span class="cat-name">{{ node.name }}</span>
<span v-if="!node.leaf" class="cat-arrow" aria-hidden="true">›</span>
<span class="cat-path">{{ node.path.slice(0, -1).join(' › ') }}</span>
</div>
<div v-if="fallbackOptions.length" class="cat-seg">已落库类目(树外历史)</div>
<div
v-for="option in fallbackOptions"
:key="`db-${option.name}`"
class="cat-row"
:class="{ active: modelValue === option.name && !(selectedPath && selectedPath.length) }"
:title="option.name"
@click="pickName(option.name, [])"
>
<span class="cat-dot" :class="{ on: modelValue === option.name && !(selectedPath && selectedPath.length) }" aria-hidden="true"></span>
<span class="cat-name">{{ option.name }}</span>
<span class="cat-count">{{ option.productCount }}</span>
</div>
</template>
<div v-else class="cat-empty">未找到类目</div>
</div>
</template>
<!-- 下钻态:逐级动态栏;首栏顶部「全部类目」行=清选。 -->
<template v-else>
<div
v-for="(col, depth) in columns"
:key="depth"
class="cat-col"
>
<div v-if="depth === 0" class="cat-search-row">
<input
v-model="searchText"
type="text"
class="cat-search"
placeholder="搜索类目"
@click.stop
/>
</div>
<div v-if="depth === 0" class="cat-row cat-row-all" :class="{ active: !modelValue }" @click="pickAll">
<span class="cat-dot" :class="{ on: !modelValue }" aria-hidden="true"></span>
<span class="cat-name">全部类目</span>
</div>
<div
v-for="node in col"
:key="`${depth}-${node.name}`"
class="cat-row"
:class="{ active: activePath[depth] === node.name || isSelected(nodePath(node, depth)) }"
:title="node.children && node.children.length ? `${node.name}(点行下钻,点圆点选中整棵子树)` : node.name"
@click="pick(node, depth)"
>
<span
class="cat-dot"
:class="{ on: isSelected(nodePath(node, depth)) }"
role="button"
:aria-label="`选中 ${node.name}`"
@click.stop="selectNode(node, depth)"
></span>
<span class="cat-name">{{ node.name }}</span>
<span v-if="node.children && node.children.length" class="cat-arrow" aria-hidden="true">›</span>
</div>
</div>
</template>
</div>
</div>
</template>
<style scoped>
.cat-custom { flex: none; width: 200px; max-height: 340px; overflow-y: auto; border-right: 1px solid #e7eef5; }
.cat-create { display: flex; gap: 4px; padding: 6px 8px; }
.cat-create input { min-width: 0; }
.cat-group-pick { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; color: inherit; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-cascader {
position: relative;
min-width: 0;
}
/* 触发框像素对齐筛选行实际渲染高度(2026-11-18 客户「输入框高了一些」):
main.css 全局密度收口把 .form-group input/el-select/.btn 压到 34 高,自绘 button
不在收口选择器清单里,这里同值对齐(34 高/5x10 padding/9 圆角/#cbd9e6 边框/#f8fbfd 底)。 */
.cat-trigger {
display: flex;
align-items: center;
gap: 6px;
width: 100%;
min-height: 34px;
padding: 5px 10px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
cursor: pointer;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.cat-trigger:hover {
border-color: #9fb7cd;
}
.cat-trigger.open,
.cat-trigger:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.cat-trigger-text {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-align: left;
}
.cat-trigger:not(.filled) .cat-trigger-text {
color: #8293a5;
}
.cat-clear {
flex: none;
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #e7eef5;
color: #5b6f83;
font-size: 13px;
line-height: 1;
}
.cat-clear:hover {
background: #dbe7f1;
color: #24384d;
}
/* 下拉箭头:CSS 三角(京麦同款向下 chevron,展开翻向上)。 */
.cat-chevron {
flex: none;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #7b8a99;
transition: transform 160ms ease;
}
.cat-trigger.open .cat-chevron {
transform: rotate(180deg);
}
.cat-mask {
position: fixed;
inset: 0;
z-index: 2550;
}
.cat-panel {
position: absolute;
top: calc(100% + 4px);
left: 0;
z-index: 2560;
display: flex;
align-items: stretch;
max-width: min(820px, 90vw);
border: 1px solid #d8e3ee;
border-radius: 10px;
background: #ffffff;
box-shadow: 0 16px 44px rgba(39, 67, 94, 0.18);
overflow-x: auto;
overflow-y: hidden;
}
.cat-col {
flex: none;
width: 200px;
max-height: 340px;
overflow-y: auto;
padding: 6px 0;
border-right: 1px solid #e7eef5;
}
.cat-col:last-child {
border-right: 0;
}
.cat-col-search {
width: 300px;
}
/* 行=京麦同款:圆点 radio + 名称 + 右箭头;hover 浅蓝底。 */
.cat-row {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
font-size: 13.5px;
color: #24384d;
cursor: pointer;
}
.cat-row:hover,
.cat-row.active {
background: #f1f7fb;
}
.cat-row.active {
color: #2f5d8b;
font-weight: 550;
}
.cat-dot {
flex: none;
width: 13px;
height: 13px;
border: 1px solid #c0ccda;
border-radius: 50%;
background: #ffffff;
}
/* radio 选中=京麦同款蓝芯。 */
.cat-dot.on {
border-color: #3b82c4;
background: radial-gradient(circle, #3b82c4 0 4px, #ffffff 4.6px);
}
.cat-name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 搜索态行尾的路径灰字(同名跨支时定位用)。 */
.cat-path {
flex: none;
max-width: 110px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: #9fb0c0;
font-size: 12px;
}
.cat-arrow {
flex: none;
color: #9fb0c0;
font-size: 15px;
line-height: 1;
}
.cat-count {
flex: none;
color: #8293a5;
font-size: 12px;
}
.cat-seg {
padding: 8px 12px 4px;
color: #8293a5;
font-size: 11.5px;
border-top: 1px solid #e7eef5;
}
.cat-search-row {
padding: 4px 8px 6px;
border-bottom: 1px solid #e7eef5;
}
.cat-search {
display: block;
width: 100%;
min-height: 30px;
padding: 4px 10px;
border: 1px solid #cbd9e6;
border-radius: 8px;
background: #f8fbfd;
color: #24384d;
font-size: 12.5px;
font-family: inherit;
outline: none;
}
.cat-search:focus {
background: #ffffff;
border-color: #5f85ad;
}
.cat-empty {
padding: 16px 12px;
color: #8293a5;
font-size: 12.5px;
text-align: center;
}
</style>