feat(search): 新增中图法分类树接口及分面优化

This commit is contained in:
SUNJIAJINGS
2026-10-23 13:53:46 +08:00
parent b735c1465a
commit a68f27f7f2
12 changed files with 908 additions and 8 deletions
+18
View File
@@ -2,12 +2,23 @@ import request from './request'
import type { PageResult } from './types'
import type { ResourceQuery, VersionResource } from './resource'
/** 中图法分类树节点(部类 / 大类 / 类目) */
export interface ClcNode {
code: string
name: string
level: number
count: number
children: ClcNode[]
}
/** 检索条件(对应后端 SearchQuery) */
export interface SearchQuery extends ResourceQuery {
keyword?: string
publisher?: string
isbn?: string
cdoi?: string
/** 多个中图分类号,前方一致,任一命中即可(中图法树导航点选部类时传其下所有大类) */
clcCodes?: string[]
sourceLibraries?: string[]
publishYearStart?: string
publishYearEnd?: string
@@ -20,6 +31,8 @@ export interface SearchQuery extends ResourceQuery {
export interface FacetItem {
name: string
value: number
/** 筛选值(中图法分面为分类号前缀,如 E;其余分面为空) */
code?: string
}
export interface YearStat {
@@ -46,6 +59,11 @@ export function advancedSearch(params: SearchQuery) {
return request.get<never, SearchResultVO>('/search/advanced', { params })
}
/** 中图法分类树(带当前检索条件下的命中数量) */
export function clcTree(params?: SearchQuery) {
return request.get<never, ClcNode[]>('/search/clc/tree', { params })
}
/** 分页检索(POST,支持数组条件) */
export function searchPage(data: SearchQuery) {
return request.post<never, SearchResultVO>('/search/page', data)
+112
View File
@@ -0,0 +1,112 @@
<script setup lang="ts">
import { ref } from 'vue'
import type { ClcNode } from '@/api/search'
const props = defineProps<{
nodes: ClcNode[]
/** 当前选中的分类号 */
selected: string
/** 默认展开的层级(1 表示只展开部类) */
depth?: number
}>()
const emit = defineEmits<{
(e: 'select', node: ClcNode, codes: string[]): void
}>()
/** 已展开节点的分类号(点名称检索,点箭头展开/收起) */
const expanded = ref<Set<string>>(new Set())
/** 初始化时按 depth 自动展开前几层 */
function autoExpand(nodes: ClcNode[], level: number) {
const max = props.depth ?? 1
if (level >= max) return
for (const n of nodes) {
if (n.children?.length && n.count > 0) {
expanded.value.add(n.code)
autoExpand(n.children, level + 1)
}
}
}
autoExpand(props.nodes, 0)
function toggle(node: ClcNode) {
expanded.value.has(node.code) ? expanded.value.delete(node.code) : expanded.value.add(node.code)
}
/** 点节点名称:按该分类检索,并顺带展开下一级(部类传其下所有大类号,其余用自身号前方一致) */
function pick(node: ClcNode) {
if (hasChild(node)) {
expanded.value.add(node.code)
}
const codes = node.level === 1 ? (node.children || []).map((c) => c.code) : [node.code]
emit('select', node, codes.filter(Boolean))
}
/** 展开/收起图标:有下级时用箭头,无下级时占位保持缩进对齐 */
function hasChild(node: ClcNode) {
return !!node.children?.length
}
</script>
<template>
<div class="clc-tree">
<div v-for="n in nodes" :key="n.code" class="clc-level">
<div class="clc-row" :class="{ on: selected === n.code }">
<span class="arrow" @click.stop="hasChild(n) && toggle(n)">
{{ hasChild(n) ? (expanded.has(n.code) ? '▾' : '▸') : '' }}
</span>
<span class="label" :title="`${n.code} ${n.name}`" @click="pick(n)">
<span class="code">{{ n.code }}</span>
<span class="name">{{ n.name }}</span>
</span>
<em>{{ n.count }}</em>
</div>
<div v-if="expanded.has(n.code) && hasChild(n)" class="clc-children">
<ClcTree
:nodes="n.children"
:selected="selected"
:depth="0"
@select="(node, codes) => emit('select', node, codes)"
/>
</div>
</div>
</div>
</template>
<style scoped lang="scss">
.clc-row {
display: flex;
align-items: center;
gap: 4px;
font-size: 13px;
color: #5b6472;
padding: 3px 6px;
border-radius: 4px;
cursor: pointer;
&:hover { background: var(--brand-light); }
&.on { background: var(--brand-light); color: var(--brand-primary); }
.arrow {
width: 12px;
flex: none;
color: #9aa3b2;
font-size: 11px;
}
.label {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.code { color: #9aa3b2; margin-right: 4px; }
em { color: #9aa3b2; font-style: normal; flex: none; }
}
/* 逐级缩进 */
.clc-children { margin-left: 12px; border-left: 1px dashed #eceef2; padding-left: 4px; }
</style>
+72 -4
View File
@@ -4,8 +4,9 @@ import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import * as XLSX from 'xlsx'
import { listFields, type ListFieldConfig } from '@/api/admin'
import { getSearchDetail, getSimilar, unifiedSearch, searchList, type FacetItem, type SearchQuery } from '@/api/search'
import { getSearchDetail, getSimilar, unifiedSearch, searchList, clcTree, type ClcNode, type FacetItem, type SearchQuery } from '@/api/search'
import type { VersionResource } from '@/api/resource'
import ClcTree from './ClcTree.vue'
const route = useRoute()
const loading = ref(false)
@@ -15,6 +16,12 @@ const total = ref(0)
const costMs = ref(0)
const mediaFacets = ref<FacetItem[]>([])
const libraryFacets = ref<FacetItem[]>([])
const clcFacets = ref<FacetItem[]>([])
/** 中图法分类树(部类 -> 大类 -> 类目),带当前检索条件下的命中数量 */
const clcNodes = ref<ClcNode[]>([])
/** 当前选中的分类号(高亮用) */
const selectedClc = ref('')
const query = reactive<SearchQuery>({
current: 1,
@@ -133,13 +140,55 @@ async function doSearch() {
costMs.value = res.costMs || 0
mediaFacets.value = res.mediaTypeFacets || []
libraryFacets.value = res.sourceLibraryFacets || []
clcFacets.value = res.clcFacets || []
} finally {
loading.value = false
}
// 检索后刷新分类树的命中数量(不阻塞列表渲染)
loadClcTree()
}
function pick(type: 'mediaType' | 'sourceLibrary', v: string) {
query[type] = v
/** 拉取中图法分类树:带上当前检索条件,数量与结果一致 */
async function loadClcTree() {
try {
clcNodes.value = await clcTree({
keyword: query.keyword,
title: query.title,
author: query.author,
publisher: query.publisher,
isbn: query.isbn,
mediaType: query.mediaType,
sourceLibrary: query.sourceLibrary,
publishYearStart: query.publishYearStart,
publishYearEnd: query.publishYearEnd,
matchType: query.matchType
})
} catch {
clcNodes.value = []
}
}
/** 清除中图法筛选 */
function clearClc() {
selectedClc.value = ''
query.clcCodes = undefined
query.current = 1
doSearch()
}
/** 点选中图法节点:部类传其下所有大类号,大类/类目传自身号(前方一致) */
function pickClc(node: ClcNode, codes: string[]) {
const same = selectedClc.value === node.code
selectedClc.value = same ? '' : node.code
query.clcCodes = same ? undefined : codes
query.clcCode = undefined
query.current = 1
doSearch()
}
/** 点选左侧分面:再点一次已选中的项即取消筛选 */
function pick(type: 'mediaType' | 'sourceLibrary' | 'clcCode', v: string) {
query[type] = query[type] === v ? '' : v
query.current = 1
doSearch()
}
@@ -344,6 +393,14 @@ watch(
:class="{ on: query.sourceLibrary === f.name }" @click="pick('sourceLibrary', f.name)">
<span>{{ f.name }}</span><em>{{ f.value }}</em>
</div>
<div class="side-title">
中图法
<el-button v-if="selectedClc" link type="primary" class="clc-clear" @click="clearClc">清除</el-button>
</div>
<div class="clc-box">
<ClcTree :nodes="clcNodes" :selected="selectedClc" :depth="1" @select="pickClc" />
<div v-if="!clcNodes.length" class="clc-empty">暂无分类数据</div>
</div>
</aside>
<section class="list">
@@ -478,8 +535,19 @@ watch(
margin: 0 auto;
}
/* 中图法树导航:层级深、名称长,限高滚动避免侧栏过长 */
.clc-box {
max-height: 460px;
overflow-y: auto;
margin-right: -4px;
padding-right: 4px;
}
.clc-clear { float: right; font-weight: 400; }
.clc-empty { font-size: 12px; color: #b6bcc7; padding: 4px 6px; }
.side {
width: 210px;
width: 268px;
flex: none;
background: #fff;
border: 1px solid var(--card-border);