387 lines
14 KiB
Vue
387 lines
14 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* features/kb/components/KbAdminPanel.vue
|
|
* 知识库配置面板(治理面:分组、Dify)。
|
|
* 依赖:kb.api、Element Plus、AdminListPage、DifySettingsDialog、DifyHealthDialog、Vue I18n。
|
|
* 逻辑:管理控制台加载知识库分组。列表骨架用 AdminListPage。表头排序、文档数筛选、分页和删除确认用 Element。排序和筛选都作用在全量数据上,再分页。新增/编辑走 KbGroupDrawer。
|
|
*/
|
|
|
|
import { computed, onMounted, ref, watch } from 'vue';
|
|
import { useRouter } from 'vue-router';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { CircleCheck, Plus, Search, Setting, Tools } from '@element-plus/icons-vue';
|
|
import {
|
|
ElAlert,
|
|
ElButton,
|
|
ElConfigProvider,
|
|
ElInput,
|
|
ElLoading,
|
|
ElMessageBox,
|
|
ElPagination,
|
|
ElSpace,
|
|
ElTable,
|
|
ElTableColumn,
|
|
} from 'element-plus';
|
|
import 'element-plus/dist/index.css';
|
|
import 'element-plus/theme-chalk/dark/css-vars.css';
|
|
import '@/features/admin/styles/element-trial.css';
|
|
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
|
|
import AdminListPage from '@/features/admin/components/AdminListPage.vue';
|
|
import KbGroupDrawer, { type KbGroupFormValue } from './KbGroupDrawer.vue';
|
|
import DifyHealthDialog from './DifyHealthDialog.vue';
|
|
import DifySettingsDialog from './DifySettingsDialog.vue';
|
|
import { WORKSPACE_PATHS } from '@/features/workspace/constants/routes';
|
|
import {
|
|
createKbGroup,
|
|
deleteKbGroup,
|
|
getKbGroupsSafe,
|
|
updateKbGroup,
|
|
type KbGroup,
|
|
} from '../api/kb.api';
|
|
import { formatDateTime } from '../utils/kb-display.utils';
|
|
|
|
type GroupFilterMode = 'all' | 'withDocs' | 'empty';
|
|
type GroupSortKey = 'name' | 'document_count' | 'updated_at';
|
|
|
|
const router = useRouter();
|
|
const { t } = useI18n();
|
|
const { elementLocale } = useElementProvider();
|
|
const vLoading = ElLoading.directive;
|
|
|
|
const groups = ref<KbGroup[]>([]);
|
|
const isSubmitting = ref(false);
|
|
const isDeleting = ref(false);
|
|
const isRefreshing = ref(true);
|
|
const editingGroupId = ref<number | null>(null);
|
|
const form = ref<KbGroupFormValue>({ name: '', description: '' });
|
|
const feedback = ref('');
|
|
const feedbackType = ref<'success' | 'error' | ''>('');
|
|
const settingsDialogOpen = ref(false);
|
|
const healthDialogOpen = ref(false);
|
|
const drawerOpen = ref(false);
|
|
const searchKeyword = ref('');
|
|
const filterMode = ref<GroupFilterMode>('all');
|
|
const sortKey = ref<GroupSortKey | ''>('updated_at');
|
|
const sortDir = ref<'asc' | 'desc' | ''>('desc');
|
|
const currentPage = ref(1);
|
|
const pageSize = ref(20);
|
|
|
|
const pageSizeOptions = [10, 20, 50, 100];
|
|
|
|
/**
|
|
* 文档数表头筛选项。
|
|
* 核心逻辑:不放「全部」;Element 筛选面板的重置会清空条件,等同于全部。
|
|
*/
|
|
const documentFilters = computed(() => [
|
|
{ text: t('kbAdmin.filters.withDocs'), value: 'withDocs' },
|
|
{ text: t('kbAdmin.filters.empty'), value: 'empty' },
|
|
]);
|
|
|
|
const filteredGroups = computed(() => {
|
|
const keyword = searchKeyword.value.trim().toLowerCase();
|
|
const matched = groups.value.filter((group) => {
|
|
const documentCount = group.document_count ?? 0;
|
|
const matchesKeyword = !keyword
|
|
|| group.name.toLowerCase().includes(keyword)
|
|
|| (group.description || '').toLowerCase().includes(keyword);
|
|
const matchesFilter = filterMode.value === 'all'
|
|
|| (filterMode.value === 'withDocs' && documentCount > 0)
|
|
|| (filterMode.value === 'empty' && documentCount === 0);
|
|
return matchesKeyword && matchesFilter;
|
|
});
|
|
|
|
if (!sortKey.value || !sortDir.value) return matched;
|
|
return [...matched].sort((a, b) => {
|
|
let result = 0;
|
|
if (sortKey.value === 'name') {
|
|
result = a.name.localeCompare(b.name, undefined, { numeric: true });
|
|
} else if (sortKey.value === 'document_count') {
|
|
result = (a.document_count ?? 0) - (b.document_count ?? 0);
|
|
} else {
|
|
result = (Date.parse(a.updated_at || '') || 0) - (Date.parse(b.updated_at || '') || 0);
|
|
}
|
|
return sortDir.value === 'asc' ? result : -result;
|
|
});
|
|
});
|
|
const pagedGroups = computed(() => {
|
|
const start = (currentPage.value - 1) * pageSize.value;
|
|
return filteredGroups.value.slice(start, start + pageSize.value);
|
|
});
|
|
|
|
/**
|
|
* 表格空状态文案。
|
|
* 核心逻辑:一个分组都没有时提示去创建;有分组但筛选后为空时提示调整条件。
|
|
*/
|
|
const tableEmptyText = computed(() => (
|
|
groups.value.length === 0 ? t('kbAdmin.groupsEmpty') : t('kbAdmin.filters.emptyResult')
|
|
));
|
|
|
|
/** 格式化分组更新时间,避免管理台表格直接暴露后端 ISO 时间字符串。 */
|
|
const formatGroupUpdatedAt = (value?: string | null) => formatDateTime(value);
|
|
|
|
/**
|
|
* 加载知识库治理页的分组数据,供页面初始渲染和操作后刷新复用。
|
|
* 核心逻辑:只等分组列表;失败写入页面提示,并用 finally 关掉表格加载态。
|
|
*/
|
|
const loadData = async () => {
|
|
isRefreshing.value = true;
|
|
try {
|
|
const groupsResult = await getKbGroupsSafe();
|
|
if (groupsResult.ok) groups.value = groupsResult.data;
|
|
else {
|
|
feedback.value = groupsResult.error || t('kbAdmin.msg.loadGroupsFailed');
|
|
feedbackType.value = 'error';
|
|
}
|
|
} finally {
|
|
isRefreshing.value = false;
|
|
}
|
|
};
|
|
|
|
/** 重置分组表单到新建状态,关闭抽屉或切换新建时清空当前编辑对象。 */
|
|
const resetForm = () => {
|
|
editingGroupId.value = null;
|
|
form.value = { name: '', description: '' };
|
|
};
|
|
|
|
/** 打开新建抽屉,保持主列表当前位置和筛选条件不变。 */
|
|
const openCreateDrawer = () => {
|
|
resetForm();
|
|
feedback.value = '';
|
|
feedbackType.value = '';
|
|
drawerOpen.value = true;
|
|
};
|
|
|
|
/** 将列表中的分组带入右侧抽屉,避免大列表编辑时跳转到页面顶部。 */
|
|
const openEditDrawer = (group: KbGroup) => {
|
|
editingGroupId.value = group.id;
|
|
form.value = { name: group.name, description: group.description || '' };
|
|
feedback.value = '';
|
|
feedbackType.value = '';
|
|
drawerOpen.value = true;
|
|
};
|
|
|
|
/** 关闭分组抽屉并恢复表单,列表筛选和分页状态保留。 */
|
|
const closeDrawer = () => {
|
|
drawerOpen.value = false;
|
|
resetForm();
|
|
};
|
|
|
|
/** 提交分组新建或编辑;根据 editingGroupId 自动选择创建或更新接口。 */
|
|
const handleSubmitGroup = async () => {
|
|
if (!form.value.name.trim()) {
|
|
feedback.value = t('kbAdmin.msg.nameRequired');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
isSubmitting.value = true;
|
|
const payload = { name: form.value.name.trim(), description: form.value.description.trim() };
|
|
const result = editingGroupId.value ? await updateKbGroup(editingGroupId.value, payload) : await createKbGroup(payload);
|
|
isSubmitting.value = false;
|
|
|
|
if (!result.ok) {
|
|
feedback.value = result.error || t('kbAdmin.msg.createFailed');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
|
|
feedback.value = editingGroupId.value ? t('kbAdmin.msg.updated') : t('kbAdmin.msg.created');
|
|
feedbackType.value = 'success';
|
|
closeDrawer();
|
|
await loadData();
|
|
};
|
|
|
|
/**
|
|
* 确认后删除分组,并刷新列表。
|
|
* 核心逻辑:Element 确认框取消会 reject,这里直接返回,不调用删除接口。
|
|
* @param group - 待删除的分组
|
|
*/
|
|
const handleDeleteGroup = async (group: KbGroup) => {
|
|
try {
|
|
await ElMessageBox.confirm(
|
|
t('kbAdmin.deleteDialog.message', { name: group.name }),
|
|
t('kbAdmin.deleteDialog.title'),
|
|
{
|
|
type: 'warning',
|
|
confirmButtonText: t('common.deleteDialog.confirm'),
|
|
cancelButtonText: t('common.deleteDialog.cancel'),
|
|
},
|
|
);
|
|
} catch {
|
|
return;
|
|
}
|
|
isDeleting.value = true;
|
|
const result = await deleteKbGroup(group.id);
|
|
isDeleting.value = false;
|
|
if (!result.ok) {
|
|
feedback.value = result.error || t('kbAdmin.msg.deleteFailed');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
|
|
if (editingGroupId.value === group.id) closeDrawer();
|
|
feedback.value = t('kbAdmin.msg.deleted');
|
|
feedbackType.value = 'success';
|
|
await loadData();
|
|
};
|
|
|
|
/**
|
|
* 按文档数表头筛选全部分组,并回到第一页。
|
|
* 核心逻辑:筛选发生在分页之前。未选或重置时显示全部;单选时只留有文档或空分组。
|
|
* @param filters - Element 按 column-key 给出的已选值
|
|
*/
|
|
function onFilterChange(filters: Record<string, string[]>): void {
|
|
const selected = filters.document_count ?? [];
|
|
if (selected.length === 1 && (selected[0] === 'withDocs' || selected[0] === 'empty')) {
|
|
filterMode.value = selected[0];
|
|
} else {
|
|
filterMode.value = 'all';
|
|
}
|
|
currentPage.value = 1;
|
|
}
|
|
|
|
/**
|
|
* 按表头把筛选后的全部分组重排,并回到第一页。
|
|
* 核心逻辑:排序发生在分页之前,所以翻页后顺序不变。第三次点击表头时 Element 清空排序,列表回到接口原顺序。
|
|
* @param payload.prop - 列字段
|
|
* @param payload.order - ascending、descending 或空
|
|
*/
|
|
function onSortChange(payload: { prop: string | null; order: string | null }): void {
|
|
const field = payload.prop;
|
|
if ((field === 'name' || field === 'document_count' || field === 'updated_at') && payload.order) {
|
|
sortKey.value = field;
|
|
sortDir.value = payload.order === 'ascending' ? 'asc' : 'desc';
|
|
} else {
|
|
sortKey.value = '';
|
|
sortDir.value = '';
|
|
}
|
|
currentPage.value = 1;
|
|
}
|
|
|
|
watch([searchKeyword, pageSize], () => {
|
|
currentPage.value = 1;
|
|
});
|
|
|
|
onMounted(async () => {
|
|
await loadData();
|
|
});
|
|
|
|
defineExpose({ loadData });
|
|
</script>
|
|
|
|
<template>
|
|
<ElConfigProvider :locale="elementLocale">
|
|
<AdminListPage>
|
|
<template #title>
|
|
<h2 class="section-title">{{ t('kbAdmin.groupsTitle') }}</h2>
|
|
<p class="section-desc mt-1">{{ t('kbAdmin.groupManageSubtitle') }}</p>
|
|
</template>
|
|
<template #actions>
|
|
<ElSpace wrap>
|
|
<ElButton :icon="Setting" @click="settingsDialogOpen = true">{{ t('kbAdmin.difySettings') }}</ElButton>
|
|
<ElButton :icon="CircleCheck" @click="healthDialogOpen = true">{{ t('kbAdmin.healthCheck') }}</ElButton>
|
|
<ElButton :icon="Tools" @click="router.push(WORKSPACE_PATHS.knowledgeBase)">{{ t('kbAdmin.goWorkspace') }}</ElButton>
|
|
<ElButton type="primary" :icon="Plus" @click="openCreateDrawer">{{ t('kbAdmin.form.create') }}</ElButton>
|
|
</ElSpace>
|
|
</template>
|
|
<template #filters>
|
|
<ElInput
|
|
v-model="searchKeyword"
|
|
clearable
|
|
class="kb-filter-search"
|
|
:prefix-icon="Search"
|
|
:placeholder="t('kbAdmin.filters.searchPlaceholder')"
|
|
/>
|
|
</template>
|
|
|
|
<ElAlert
|
|
v-if="feedback"
|
|
class="kb-admin-alert"
|
|
:title="feedback"
|
|
:type="feedbackType === 'error' ? 'error' : 'success'"
|
|
show-icon
|
|
closable
|
|
@close="feedback = ''"
|
|
/>
|
|
|
|
<ElTable
|
|
v-loading="isRefreshing"
|
|
class="hx-admin-table"
|
|
:data="pagedGroups"
|
|
row-key="id"
|
|
:default-sort="{ prop: 'updated_at', order: 'descending' }"
|
|
:empty-text="tableEmptyText"
|
|
@sort-change="onSortChange"
|
|
@filter-change="onFilterChange"
|
|
>
|
|
<ElTableColumn prop="name" :label="t('kbAdmin.table.name')" min-width="160" sortable="custom">
|
|
<template #default="{ row }">
|
|
<div class="truncate" :title="(row as KbGroup).name">{{ (row as KbGroup).name }}</div>
|
|
</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn :label="t('kbAdmin.table.description')" min-width="220">
|
|
<template #default="{ row }">
|
|
<div class="truncate hx-element-hint" :title="(row as KbGroup).description || t('kbAdmin.noDescription')">
|
|
{{ (row as KbGroup).description || t('kbAdmin.noDescription') }}
|
|
</div>
|
|
</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn
|
|
prop="document_count"
|
|
column-key="document_count"
|
|
:label="t('kbAdmin.table.documents')"
|
|
width="160"
|
|
sortable="custom"
|
|
:filters="documentFilters"
|
|
:filter-multiple="false"
|
|
>
|
|
<template #default="{ row }">{{ (row as KbGroup).document_count ?? 0 }}</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn prop="updated_at" :label="t('kbAdmin.table.updatedAt')" min-width="180" sortable="custom">
|
|
<template #default="{ row }">{{ formatGroupUpdatedAt((row as KbGroup).updated_at) }}</template>
|
|
</ElTableColumn>
|
|
<ElTableColumn :label="t('kbAdmin.table.actions')" width="160" align="right">
|
|
<template #default="{ row }">
|
|
<ElButton link type="primary" @click="openEditDrawer(row as KbGroup)">{{ t('common.edit') }}</ElButton>
|
|
<ElButton link type="danger" :disabled="isDeleting" @click="handleDeleteGroup(row as KbGroup)">
|
|
{{ t('common.delete') }}
|
|
</ElButton>
|
|
</template>
|
|
</ElTableColumn>
|
|
</ElTable>
|
|
<template v-if="filteredGroups.length > 0" #pagination>
|
|
<ElPagination
|
|
v-if="filteredGroups.length > 0"
|
|
v-model:current-page="currentPage"
|
|
v-model:page-size="pageSize"
|
|
:total="filteredGroups.length"
|
|
:page-sizes="pageSizeOptions"
|
|
layout="total, sizes, prev, pager, next"
|
|
background
|
|
/>
|
|
</template>
|
|
</AdminListPage>
|
|
|
|
<KbGroupDrawer
|
|
v-model:form="form"
|
|
:open="drawerOpen"
|
|
:mode="editingGroupId ? 'edit' : 'create'"
|
|
:submitting="isSubmitting"
|
|
@submit="handleSubmitGroup"
|
|
@close="closeDrawer"
|
|
/>
|
|
|
|
<DifySettingsDialog :open="settingsDialogOpen" @close="settingsDialogOpen = false" @saved="(message) => { feedback = message; feedbackType = 'success'; void loadData(); }" @error="(message) => { feedback = message; feedbackType = 'error'; }" />
|
|
<DifyHealthDialog :open="healthDialogOpen" @close="healthDialogOpen = false" @error="(message) => { feedback = message; feedbackType = 'error'; }" />
|
|
</ElConfigProvider>
|
|
</template>
|
|
|
|
<style scoped>
|
|
:deep(.kb-filter-search) {
|
|
width: 16rem;
|
|
}
|
|
|
|
.kb-admin-alert {
|
|
margin: 16px 32px 0;
|
|
}
|
|
</style>
|