feat(admin): 添加列表显示字段配置功能

- 新增 list_field_config 表及后端接口,支持查询、保存、恢复默认字段配置
- 管理后台新增“列表字段配置”页签,可增删字段、调整顺序、修改显示名与可见性
- version_resource 表新增作者单位、来源资源库名字段及升级脚本
- 引入 xlsx 依赖以支持列表导出
This commit is contained in:
SUNJIAJINGS
2026-10-22 17:33:34 +08:00
parent 46be7346a2
commit 3626939ccb
14 changed files with 891 additions and 63 deletions
+31
View File
@@ -115,3 +115,34 @@ export function resourceStats(dim: 'sourceLibrary' | 'mediaType' | 'title', limi
params: { dim, limit },
})
}
// ==================== 列表显示字段配置 ====================
export interface ListFieldConfig {
id?: number
/** 配置场景:SEARCH 检索结果列表 */
scene?: string
/** 字段键,对应 VersionResource 的驼峰属性名 */
fieldKey: string
/** 列表/导出显示名 */
fieldLabel: string
/** 展示顺序,小的靠前 */
sortNo?: number
/** 1显示 0隐藏 */
visible?: number
}
/** 查询场景展示字段(为空时返回默认配置) */
export function listFields(scene: string) {
return request.get<never, ListFieldConfig[]>(`/admin/list-fields/${scene}`)
}
/** 全量覆盖保存展示字段 */
export function saveListFields(scene: string, items: ListFieldConfig[]) {
return request.put<never, void>(`/admin/list-fields/${scene}`, items)
}
/** 恢复场景默认展示字段 */
export function resetListFields(scene: string) {
return request.post<never, ListFieldConfig[]>(`/admin/list-fields/${scene}/reset`)
}
+151 -1
View File
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const activeTab = ref('words')
@@ -9,7 +9,10 @@ import {
createResourceNode,
createSensitiveWord,
handleHitAction,
listFields,
listResourceNodes,
resetListFields,
saveListFields,
pageHits,
pageSensitiveWords,
removeResourceNode,
@@ -18,6 +21,7 @@ import {
updateResourceNode,
updateSensitiveWord,
type HitAction,
type ListFieldConfig,
type ResourceNode,
type SensitiveHit,
type SensitiveWord,
@@ -267,11 +271,111 @@ async function batchDeleteNodes() {
fetchNodes()
}
// ==================== 列表显示字段配置 ====================
/** 可选字段池:新增字段行时从此处挑选 */
const fieldPool: { key: string; label: string }[] = [
{ key: 'title', label: '文献题名' },
{ key: 'publishYear', label: '发行时间' },
{ key: 'publishDate', label: '出版日期' },
{ key: 'author', label: '作者' },
{ key: 'authorOrg', label: '作者单位' },
{ key: 'publisher', label: '来源刊名/出版社' },
{ key: 'mediaType', label: '资源类型' },
{ key: 'sourceLibrary', label: '来源院校' },
{ key: 'sourceRepo', label: '来源资源库名' },
{ key: 'clcCode', label: '中图分类' },
{ key: 'keywords', label: '关键词' },
{ key: 'isbn', label: 'ISBN' },
{ key: 'format', label: '格式' },
{ key: 'language', label: '语种' },
{ key: 'pageCount', label: '页码' },
{ key: 'summary', label: '摘要' },
{ key: 'cdoi', label: 'CDOI' },
{ key: 'governStatus', label: '治理状态' }
]
const fieldScene = 'SEARCH'
const fieldList = ref<ListFieldConfig[]>([])
const fieldLoading = ref(false)
const fieldSaving = ref(false)
const addingKey = ref('')
async function fetchFields() {
fieldLoading.value = true
try {
fieldList.value = await listFields(fieldScene)
} finally {
fieldLoading.value = false
}
}
/** 上移 / 下移调整展示顺序 */
function moveField(idx: number, delta: number) {
const target = idx + delta
if (target < 0 || target >= fieldList.value.length) return
const arr = [...fieldList.value]
;[arr[idx], arr[target]] = [arr[target], arr[idx]]
arr.forEach((f, i) => (f.sortNo = (i + 1) * 10))
fieldList.value = arr
}
function removeField(idx: number) {
fieldList.value.splice(idx, 1)
fieldList.value.forEach((f, i) => (f.sortNo = (i + 1) * 10))
}
/** 从字段池新增一行(已存在的字段不可重复添加) */
function addField() {
if (!addingKey.value) return
if (fieldList.value.some((f) => f.fieldKey === addingKey.value)) {
ElMessage.warning('该字段已在列表中')
return
}
const src = fieldPool.find((f) => f.key === addingKey.value)
fieldList.value.push({
fieldKey: src!.key,
fieldLabel: src!.label,
sortNo: (fieldList.value.length + 1) * 10,
visible: 1
})
addingKey.value = ''
}
const availableOptions = computed(() =>
fieldPool.filter((f) => !fieldList.value.some((c) => c.fieldKey === f.key))
)
async function saveFields() {
if (!fieldList.value.length) {
ElMessage.warning('至少保留一个展示字段')
return
}
if (fieldList.value.some((f) => !f.fieldLabel?.trim())) {
ElMessage.warning('请填写完整的显示名')
return
}
fieldSaving.value = true
try {
await saveListFields(fieldScene, fieldList.value)
ElMessage.success('字段配置已保存,检索结果列表即时生效')
} finally {
fieldSaving.value = false
}
}
async function resetFields() {
await ElMessageBox.confirm('确认恢复默认的展示字段配置?', '提示', { type: 'warning' })
fieldList.value = await resetListFields(fieldScene)
ElMessage.success('已恢复默认配置')
}
onMounted(() => {
fetchWords()
fetchHits()
fetchNodes()
fetchStats()
fetchFields()
})
</script>
@@ -477,6 +581,52 @@ onMounted(() => {
</div>
</div>
</el-tab-pane>
<!-- ==================== 列表显示字段配置 ==================== -->
<el-tab-pane label="列表字段配置" name="fields">
<el-alert
class="hint"
type="info"
:closable="false"
title="配置检索结果列表显示哪些元数据字段:可新增、删除、上下调整顺序、修改显示名,或临时隐藏。保存后对全部用户的检索结果列表与导出字段即时生效。"
/>
<div class="toolbar">
<el-select v-model="addingKey" placeholder="选择要新增的字段" style="width: 240px" clearable>
<el-option v-for="f in availableOptions" :key="f.key" :label="f.label" :value="f.key" />
</el-select>
<el-button type="primary" plain :disabled="!addingKey" @click="addField">新增字段</el-button>
<el-button @click="resetFields">恢复默认</el-button>
<el-button type="primary" :loading="fieldSaving" @click="saveFields">保存配置</el-button>
</div>
<el-table v-loading="fieldLoading" :data="fieldList" border stripe max-height="520">
<el-table-column label="顺序" width="110" align="center">
<template #default="{ $index }">
<el-button-group>
<el-button size="small" :disabled="$index === 0" @click="moveField($index, -1)">↑</el-button>
<el-button size="small" :disabled="$index === fieldList.length - 1" @click="moveField($index, 1)">↓</el-button>
</el-button-group>
</template>
</el-table-column>
<el-table-column prop="fieldKey" label="字段键" width="150" />
<el-table-column label="显示名" min-width="200">
<template #default="{ row }">
<el-input v-model="row.fieldLabel" size="small" maxlength="32" />
</template>
</el-table-column>
<el-table-column label="是否显示" width="100" align="center">
<template #default="{ row }">
<el-switch v-model="row.visible" :active-value="1" :inactive-value="0" />
</template>
</el-table-column>
<el-table-column label="操作" width="100" align="center">
<template #default="{ $index }">
<el-button link type="danger" size="small" @click="removeField($index)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
<!-- 敏感词 新增/编辑 弹窗 -->
+259 -11
View File
@@ -1,7 +1,10 @@
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getSearchDetail, getSimilar, unifiedSearch, type FacetItem, type SearchQuery } from '@/api/search'
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 type { VersionResource } from '@/api/resource'
const route = useRoute()
@@ -22,6 +25,28 @@ const query = reactive<SearchQuery>({
/** 概览页每页条数选项(默认 50,可切 100/200;20 便于数据量小时演示翻页) */
const pageSizes = [20, 50, 100, 200]
// ==================== 检索结果展示字段(管理员在后台配置) ====================
/** 展示字段配置:取不到配置时用默认的 8 个字段兜底 */
const listFieldConfig = ref<ListFieldConfig[]>([])
const metaFields = computed(() =>
listFieldConfig.value.filter((f) => f.visible !== 0 && f.fieldKey !== 'title')
)
async function fetchListFields() {
try {
listFieldConfig.value = await listFields('SEARCH')
} catch {
listFieldConfig.value = []
}
}
/** 按字段键取值(含高亮所需原文),未配置值返回空串 */
function metaValue(row: VersionResource, key: string): string {
const v = (row as unknown as Record<string, unknown>)[key]
return v === null || v === undefined ? '' : String(v)
}
/** 输入框内容(与查询参数分离,避免按途径分发后被清空) */
const input = ref('')
const field = ref('综合')
@@ -138,6 +163,125 @@ function scrollToListTop() {
window.scrollTo({ top: 0, behavior: 'smooth' })
}
// ==================== 批量导出 Excel ====================
/** 可导出字段(key 对应 VersionResource 字段) */
const exportFields: { key: string; label: string }[] = [
{ key: 'processNo', label: '加工编号' },
{ key: 'title', label: '正题名' },
{ key: 'otherTitle', label: '其他题名' },
{ key: 'author', label: '责任者' },
{ key: 'authorOrg', label: '作者单位' },
{ key: 'responsibility', label: '责任方式' },
{ key: 'publisher', label: '出版者' },
{ key: 'sourceRepo', label: '来源资源库名' },
{ key: 'publishPlace', label: '出版地' },
{ key: 'publishDate', label: '出版日期' },
{ key: 'publishYear', label: '出版年份' },
{ key: 'edition', label: '版本' },
{ key: 'isbn', label: 'ISBN' },
{ key: 'clcCode', label: '中图分类' },
{ key: 'keywords', label: '关键词' },
{ key: 'summary', label: '摘要' },
{ key: 'mediaType', label: '媒体类型' },
{ key: 'format', label: '格式' },
{ key: 'language', label: '语种' },
{ key: 'pageCount', label: '页码' },
{ key: 'sourceLibrary', label: '来源馆' },
{ key: 'cdoi', label: 'CDOI' },
{ key: 'governStatus', label: '治理状态' },
{ key: 'fulltextUrl', label: '全文地址' }
]
/** 默认勾选的常用字段 */
const defaultFields = ['processNo', 'title', 'author', 'publisher', 'publishYear', 'isbn', 'clcCode', 'mediaType', 'sourceLibrary']
const exportVisible = ref(false)
const exporting = ref(false)
const exportScope = ref<'selected' | 'page' | 'all'>('all')
const exportFieldsChecked = ref<string[]>([...defaultFields])
/** 已勾选的记录(跨当前页保留) */
const selectedIds = ref<Set<number>>(new Set())
const selectedCount = computed(() => selectedIds.value.size)
function toggleSelect(row: VersionResource) {
const id = row.id as number
const next = new Set(selectedIds.value)
next.has(id) ? next.delete(id) : next.add(id)
selectedIds.value = next
}
function selectAllOnPage() {
const next = new Set(selectedIds.value)
rows.value.forEach((r) => next.add(r.id as number))
selectedIds.value = next
}
function clearSelection() {
selectedIds.value = new Set()
}
/** 取值:兼容 undefined / null,并按字段做必要的展示转换 */
function cellText(row: VersionResource, key: string): string {
const raw = ((row as unknown as Record<string, unknown>) || {})[key]
if (raw === null || raw === undefined) return ''
if (typeof raw === 'object') return JSON.stringify(raw)
// Excel 单格上限 32767 字符,超长摘要做截断保护
const s = String(raw)
return s.length > 32000 ? s.slice(0, 32000) + '…' : s
}
async function confirmExport() {
if (!exportFieldsChecked.value.length) {
ElMessage.warning('请至少选择一个导出字段')
return
}
exporting.value = true
try {
let list: VersionResource[] = []
if (exportScope.value === 'selected') {
if (!selectedIds.value.size) {
ElMessage.warning('请先在结果列表中勾选要导出的记录')
return
}
list = rows.value.filter((r) => selectedIds.value.has(r.id as number))
if (list.length < selectedIds.value.size) {
// 勾选可能来自其他页,需从后端按当前检索条件捞取后过滤
const all = await searchList({ ...query })
const wanted = new Set([...selectedIds.value])
list = all.filter((r) => wanted.has(r.id as number))
}
} else if (exportScope.value === 'page') {
list = rows.value
} else {
list = await searchList({ ...query })
}
if (!list.length) {
ElMessage.warning('没有可导出的数据')
return
}
const fields = exportFields.filter((f) => exportFieldsChecked.value.includes(f.key))
const aoa: (string | number)[][] = [fields.map((f) => f.label)]
for (const row of list) {
aoa.push(fields.map((f) => cellText(row, f.key)))
}
const sheet = XLSX.utils.aoa_to_sheet(aoa)
sheet['!cols'] = fields.map((f) => ({ wch: Math.max(12, f.label.length * 2 + 6) }))
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, sheet, '检索结果')
const name = `${input.value.trim() || '全部资源'}-${list.length}条-${new Date().toISOString().slice(0, 10)}.xlsx`
XLSX.writeFile(wb, name)
ElMessage.success(`已导出 ${list.length} 条 / ${fields.length} 个字段`)
exportVisible.value = false
} finally {
exporting.value = false
}
}
const drawer = ref(false)
const detail = ref<VersionResource | null>(null)
const similar = ref<VersionResource[]>([])
@@ -158,11 +302,21 @@ onMounted(() => {
if (f && fields.includes(f)) {
field.value = f
}
if (kw) {
input.value = kw
// 检索条件为空时也执行检索 → 后端返回全部版本资源
input.value = kw || ''
fetchListFields()
doSearch()
})
// 已在检索页时,主页/热词再次带词跳转也能触发新检索
watch(
() => route.query.keyword,
(kw) => {
if (route.path !== '/search') return
input.value = (kw as string) || ''
doSearch()
}
})
)
</script>
<template>
@@ -193,16 +347,33 @@ onMounted(() => {
</aside>
<section class="list">
<div class="meta">共 <b>{{ total }}</b> 条,耗时 {{ costMs }} ms</div>
<div class="toolbar">
<div class="count">共 <b>{{ total }}</b> 条,耗时 {{ costMs }} ms<template v-if="selectedCount">,已勾选 <b>{{ selectedCount }}</b> 条</template></div>
<div class="ops">
<el-checkbox
:model-value="rows.length > 0 && rows.every((r) => selectedIds.has(r.id as number))"
:indeterminate="selectedCount > 0 && selectedCount < rows.length"
@change="(v: boolean) => (v ? selectAllOnPage() : clearSelection())"
>全选本页</el-checkbox>
<el-button size="small" :disabled="!selectedCount" @click="clearSelection">清空选择</el-button>
<el-button size="small" type="primary" @click="exportVisible = true">批量导出 Excel</el-button>
</div>
</div>
<div v-loading="loading" class="cards">
<div v-for="r in rows" :key="r.id" class="card" @click="openDetail(r)">
<div v-for="r in rows" :key="r.id" class="card" :class="{ checked: selectedIds.has(r.id as number) }" @click="openDetail(r)">
<div class="pick" @click.stop>
<el-checkbox :model-value="selectedIds.has(r.id as number)" @change="toggleSelect(r)" />
</div>
<div class="cover">{{ (r.title || '').slice(0, 2) }}</div>
<div class="info">
<div class="t" v-html="highlight(r.title)"></div>
<div class="d">
<span v-html="highlight(r.author || '-')"></span> · {{ r.publishYear || '-' }} · {{ r.mediaType || '-' }}
<div class="metas">
<div v-for="f in metaFields" :key="f.fieldKey" class="cmeta">
<span class="cmeta-k">{{ f.fieldLabel }}:</span>
<span v-if="metaValue(r, f.fieldKey)" v-html="highlight(metaValue(r, f.fieldKey))"></span>
<span v-else class="empty">-</span>
</div>
</div>
<div class="d" v-html="highlight(r.sourceLibrary || '-')"></div>
<div v-for="s in snippets(r)" :key="s.label" class="snippet">
<span class="snippet-label">{{ s.label }}:</span>
<span v-html="s.html"></span>
@@ -224,6 +395,35 @@ onMounted(() => {
<el-empty v-else description="请输入检索词开始检索" />
<!-- 批量导出:选择范围与字段 -->
<el-dialog v-model="exportVisible" title="批量导出检索结果" width="620px">
<el-form label-width="88px">
<el-form-item label="导出范围">
<el-radio-group v-model="exportScope">
<el-radio value="all">全部检索结果(最多 1 万条,约 {{ total }} 条)</el-radio>
<el-radio value="page">当前页({{ rows.length }} 条)</el-radio>
<el-radio value="selected" :disabled="!selectedCount">仅勾选记录({{ selectedCount }} 条)</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="导出字段">
<div class="field-box">
<div class="field-ops">
<el-button link type="primary" @click="exportFieldsChecked = exportFields.map((f) => f.key)">全选</el-button>
<el-button link @click="exportFieldsChecked = [...defaultFields]">恢复默认</el-button>
<span class="hint">已选 {{ exportFieldsChecked.length }} / {{ exportFields.length }}</span>
</div>
<el-checkbox-group v-model="exportFieldsChecked">
<el-checkbox v-for="f in exportFields" :key="f.key" :value="f.key" class="field-item">{{ f.label }}</el-checkbox>
</el-checkbox-group>
</div>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="exportVisible = false">取消</el-button>
<el-button type="primary" :loading="exporting" @click="confirmExport">导出 Excel</el-button>
</template>
</el-dialog>
<el-drawer v-model="drawer" title="文献细览" size="46%">
<template v-if="detail">
<h3 class="dt">{{ detail.title }}</h3>
@@ -309,7 +509,18 @@ onMounted(() => {
}
.list { flex: 1; }
.meta { font-size: 13px; color: #6b7280; margin-bottom: 10px; }
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
flex-wrap: wrap;
margin-bottom: 10px;
.count { font-size: 13px; color: #6b7280; }
.ops { display: flex; align-items: center; gap: 10px; }
}
.cards {
display: flex;
@@ -328,6 +539,14 @@ onMounted(() => {
transition: box-shadow 0.2s;
&:hover { box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1); }
&.checked { border-color: var(--brand-primary); box-shadow: inset 0 0 0 1px var(--brand-primary); }
}
.pick {
display: flex;
align-items: center;
padding-right: 2px;
cursor: default;
}
.cover {
@@ -350,6 +569,25 @@ onMounted(() => {
.d { font-size: 12px; color: #8b93a1; }
}
/* 按管理员配置的字段渲染出的元信息区(两列自适应) */
.metas {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 2px 16px;
}
.cmeta {
font-size: 12px;
color: #6b7280;
line-height: 1.7;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
.cmeta-k { color: #8b93a1; }
.empty { color: #b6bcc7; }
}
/* 命中关键词高亮(v-html 动态插入的节点不带 scoped 属性,需用 :deep 穿透) */
:deep(.hl) {
background: #ffe6a3;
@@ -370,6 +608,16 @@ onMounted(() => {
color: #4b5563;
}
.field-box {
border: 1px solid var(--card-border);
border-radius: var(--radius-sm);
padding: 8px 10px;
.field-ops { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.hint { font-size: 12px; color: #8b93a1; }
.field-item { width: 25% !important; margin-right: 0 !important; }
}
.pager {
margin-top: 18px;
justify-content: center;