refactor(frontend): 重构前端第三版

This commit is contained in:
sophie committed 2026-06-11 20:11:51 +08:00
1 parent bf32f8b954
commit dc141d19a2
49 files changed
+5333 -4234

No files matched your search

@@ -0,0 +1,380 @@
<script setup lang="ts">
/**
* features/kb/components/KbAdminPanel.vue
* 知识库配置面板(治理面:分组、Dify、任务队列)。
*/
import { computed, onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { Activity, FolderPlus, Pencil, Play, Settings, Trash2, Wrench, X } from 'lucide-vue-next';
import DifyHealthDialog from './DifyHealthDialog.vue';
import DifySettingsDialog from './DifySettingsDialog.vue';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import { WORKSPACE_PATHS } from '@/features/workspace/constants/routes';
import {
createKbGroup,
deleteKbGroup,
getKbGroupsSafe,
getKbJobsSafe,
runKbJob,
updateKbGroup,
type KbGroup,
type KbJob,
} from '../api/kb.api';
const router = useRouter();
const { t } = useI18n();
const groups = ref<KbGroup[]>([]);
const jobs = ref<KbJob[]>([]);
const isSubmitting = ref(false);
const isRefreshing = ref(false);
const editingGroupId = ref<number | null>(null);
const form = ref({
name: '',
description: '',
});
const feedback = ref('');
const feedbackType = ref<'success' | 'error' | ''>('');
const runningJobId = ref<number | string | null>(null);
const settingsDialogOpen = ref(false);
const healthDialogOpen = ref(false);
const deleteDialogOpen = ref(false);
const deletingGroup = ref<KbGroup | null>(null);
const formTitle = computed(() => editingGroupId.value ? t('kbAdmin.form.editTitle') : t('kbAdmin.form.createTitle'));
const formDescription = computed(() => editingGroupId.value
? t('kbAdmin.form.editDesc')
: t('kbAdmin.form.createDesc'));
const submitText = computed(() => {
if (isSubmitting.value) return editingGroupId.value ? t('kbAdmin.form.saving') : t('kbAdmin.form.submitting');
return editingGroupId.value ? t('kbAdmin.form.saveChanges') : t('kbAdmin.form.create');
});
const loadData = async () => {
isRefreshing.value = true;
const [groupsResult, jobsResult] = await Promise.all([getKbGroupsSafe(), getKbJobsSafe()]);
if (groupsResult.ok) {
groups.value = groupsResult.data;
} else {
feedback.value = groupsResult.error || t('kbAdmin.msg.loadGroupsFailed');
feedbackType.value = 'error';
}
if (jobsResult.ok) {
jobs.value = jobsResult.data;
} else {
feedback.value = jobsResult.error || t('kbAdmin.msg.loadJobsFailed');
feedbackType.value = 'error';
}
isRefreshing.value = false;
};
const resetForm = () => {
editingGroupId.value = null;
form.value = { name: '', description: '' };
};
const handleCreateGroup = 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';
resetForm();
await loadData();
};
const handleEditGroup = (group: KbGroup) => {
editingGroupId.value = group.id;
form.value = {
name: group.name,
description: group.description || '',
};
feedback.value = '';
feedbackType.value = '';
};
const handleDeleteGroup = async (group: KbGroup) => {
deletingGroup.value = group;
deleteDialogOpen.value = true;
};
const handleDeleteGroupConfirm = async () => {
if (!deletingGroup.value) return;
const result = await deleteKbGroup(deletingGroup.value.id);
if (!result.ok) {
feedback.value = result.error || t('kbAdmin.msg.deleteFailed');
feedbackType.value = 'error';
return;
}
if (editingGroupId.value === deletingGroup.value.id) {
resetForm();
}
feedback.value = t('kbAdmin.msg.deleted');
feedbackType.value = 'success';
deleteDialogOpen.value = false;
deletingGroup.value = null;
await loadData();
};
const handleRunJob = async (job: KbJob) => {
runningJobId.value = job.id;
const result = await runKbJob(job.id);
runningJobId.value = null;
if (!result.ok) {
feedback.value = result.error || t('kbAdmin.msg.runFailed');
feedbackType.value = 'error';
return;
}
feedback.value = result.run_result || t('kbAdmin.msg.runDone');
feedbackType.value = 'success';
await loadData();
};
onMounted(async () => {
await loadData();
});
defineExpose({ loadData });
</script>
<template>
<div class="animate-fade-in-up">
<div class="kb-admin-header">
<div class="min-w-0 flex-1">
<h2 class="text-xl font-bold text-primary">{{ t('kbAdmin.title') }}</h2>
<p class="section-desc mt-1">
{{ t('kbAdmin.subtitle') }}
</p>
</div>
<div class="flex w-full shrink-0 flex-col gap-2 sm:w-auto sm:flex-row sm:items-center sm:justify-end">
<div
class="kb-admin-toolbar"
role="toolbar"
:aria-label="t('kbAdmin.toolbar')"
>
<button
type="button"
class="kb-admin-toolbar-btn"
@click="settingsDialogOpen = true"
:title="t('kbAdmin.difySettings')"
>
<Settings class="h-4 w-4 shrink-0" />
<span class="whitespace-nowrap">{{ t('kbAdmin.difySettings') }}</span>
</button>
<button
type="button"
class="kb-admin-toolbar-btn"
@click="healthDialogOpen = true"
:title="t('kbAdmin.healthCheck')"
>
<Activity class="h-4 w-4 shrink-0" />
<span class="whitespace-nowrap">{{ t('kbAdmin.healthCheck') }}</span>
</button>
</div>
<button
type="button"
class="btn-primary inline-flex w-full items-center justify-center gap-2 px-5 py-2.5 text-sm sm:w-auto"
@click="router.push(WORKSPACE_PATHS.knowledgeBase)"
>
<Wrench class="h-4 w-4 shrink-0" />
<span class="whitespace-nowrap">{{ t('kbAdmin.goWorkspace') }}</span>
</button>
</div>
</div>
<div class="kb-admin-body">
<section class="kb-admin-section">
<div class="mb-5 flex items-center gap-3">
<div class="flex h-10 w-10 items-center justify-center rounded-2xl bg-brand-50 text-brand-600">
<FolderPlus class="h-5 w-5" />
</div>
<div>
<h3 class="section-title">{{ formTitle }}</h3>
<p class="section-desc">{{ formDescription }}</p>
</div>
</div>
<div class="space-y-4">
<div>
<label class="label-field mb-2 block">{{ t('kbAdmin.form.groupName') }}</label>
<input v-model="form.name" class="input-field" :placeholder="t('kbAdmin.form.groupNamePlaceholder')" />
</div>
<div>
<label class="label-field mb-2 block">{{ t('kbAdmin.form.groupDesc') }}</label>
<textarea
v-model="form.description"
rows="4"
class="input-field"
:placeholder="t('kbAdmin.form.groupDescPlaceholder')"
/>
</div>
<div class="flex flex-wrap items-center justify-end gap-2 border-t border-subtle pt-4">
<button
type="button"
class="btn-primary inline-flex items-center gap-1.5 px-5 py-2 text-sm disabled:cursor-not-allowed disabled:opacity-60"
@click="handleCreateGroup"
:disabled="isSubmitting"
>
<FolderPlus v-if="!isSubmitting" class="h-4 w-4 shrink-0" />
{{ submitText }}
</button>
<button
v-if="editingGroupId"
type="button"
class="btn-secondary inline-flex items-center gap-1.5 px-4 py-2 text-sm"
@click="resetForm"
>
<X class="h-4 w-4 shrink-0" />
{{ t('kbAdmin.form.cancelEdit') }}
</button>
</div>
<p
v-if="feedback"
class="text-sm"
:class="feedbackType === 'error' ? 'text-rose-500' : 'text-emerald-600'"
>
{{ feedback }}
</p>
</div>
</section>
<div class="space-y-6">
<section class="kb-admin-section">
<div class="mb-4 flex items-center justify-between">
<h3 class="section-title">{{ t('kbAdmin.groupsTitle') }}</h3>
<span class="text-xs text-muted">{{ isRefreshing ? t('common.loading') : '/api/kb/groups' }}</span>
</div>
<div v-if="groups.length === 0" class="kb-admin-empty">
{{ t('kbAdmin.groupsEmpty') }}
</div>
<div v-else class="grid grid-cols-1 gap-3 md:grid-cols-2">
<div
v-for="group in groups"
:key="group.id"
class="kb-admin-card"
>
<div class="flex items-start justify-between gap-3">
<div class="min-w-0">
<div class="font-semibold text-primary">{{ group.name }}</div>
<div class="mt-1 text-sm text-muted">{{ group.description || t('kbAdmin.noDescription') }}</div>
</div>
<div class="kb-admin-badge">
{{ t('kbAdmin.documentCount', { count: group.document_count ?? 0 }) }}
</div>
</div>
<div class="mt-4 flex flex-wrap items-center justify-between gap-3">
<div class="text-xs text-muted">
{{ t('kbAdmin.updatedAt') }}{{ group.updated_at || '--' }}
</div>
<div class="flex items-center gap-2">
<button
type="button"
class="btn-secondary inline-flex items-center gap-1 px-3 py-2 text-xs"
@click="handleEditGroup(group)"
>
<Pencil class="h-3.5 w-3.5" />
{{ t('common.edit') }}
</button>
<button
type="button"
class="inline-flex items-center gap-1 rounded-xl border border-rose-200 px-3 py-2 text-xs font-medium text-rose-600 transition-colors hover:bg-rose-50"
@click="handleDeleteGroup(group)"
>
<Trash2 class="h-3.5 w-3.5" />
{{ t('common.delete') }}
</button>
</div>
</div>
</div>
</div>
</section>
<section class="kb-admin-section">
<div class="mb-4 flex items-center justify-between">
<h3 class="section-title">{{ t('kbAdmin.jobsTitle') }}</h3>
<span class="text-xs text-muted">/api/kb/jobs</span>
</div>
<div v-if="jobs.length === 0" class="kb-admin-empty">
{{ t('kbAdmin.jobsEmpty') }}
</div>
<div v-else class="space-y-3">
<div
v-for="job in jobs"
:key="job.id"
class="kb-admin-card"
>
<div class="flex items-start justify-between gap-3">
<div class="min-w-0">
<div class="font-semibold text-primary">{{ job.job_type }}</div>
<div class="mt-1 text-sm text-muted">{{ job.created_at || '--' }}</div>
<div class="mt-2 text-sm text-muted">
{{ job.message || t('kbAdmin.noJobMessage') }}
</div>
<div class="mt-2 text-xs text-muted">
{{ t('kbAdmin.jobMeta', { docId: job.document_id ?? '--', versionId: job.document_version_id ?? '--', progress: job.progress ?? 0 }) }}
</div>
</div>
<span class="kb-admin-badge">
{{ job.status }}
</span>
</div>
<div v-if="['clean', 'sync_dify'].includes(job.job_type) && job.status !== 'success'" class="mt-3 flex justify-end">
<button
type="button"
class="inline-flex items-center gap-1 rounded-xl border border-brand-200 px-3 py-2 text-xs font-medium text-brand-700 transition-colors hover:bg-brand-50 disabled:cursor-not-allowed disabled:opacity-60"
@click="handleRunJob(job)"
:disabled="runningJobId === job.id"
>
<Play class="h-3.5 w-3.5" />
{{ runningJobId === job.id ? t('kbAdmin.running') : t('kbAdmin.runJob') }}
</button>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<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'; }"
/>
<DeleteConfirmDialog
v-model="deleteDialogOpen"
:title="t('kbAdmin.deleteDialog.title')"
:message="t('kbAdmin.deleteDialog.message', { name: deletingGroup?.name || '' })"
@confirm="handleDeleteGroupConfirm"
@cancel="() => { deleteDialogOpen = false; deletingGroup = null; }"
/>
</template>