|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, useTemplateRef, reactive } from 'vue' |
| 3 | +import { KNOWLEDGE_TYPE_MAP } from '@/constants/knowledge' |
| 4 | +import { useRoute, useRouter } from 'vue-router' |
| 5 | +import type { FormInstance, FormRules } from 'element-plus' |
| 6 | +import KnowledgeApi from '@/api/admin/workspace/knowledge/knowledge' |
| 7 | +import { KNOWLEDGE_TYPE } from '@/api/enums' |
| 8 | +import { useStore } from '@/stores' |
| 9 | +import { MsgSuccess } from '@/utils/message' |
| 10 | +import DocumentStrategyForm from './components/DocumentStrategyForm.vue' |
| 11 | +import KnowledgeBaseForm from './components/KnowledgeBaseForm.vue' |
| 12 | +
|
| 13 | +defineOptions({ name: 'CreateWebKnowledgeDrawer' }) |
| 14 | +const props = defineProps<{ folderId: string }>() |
| 15 | +const emit = defineEmits<{ refresh: [] }>() |
| 16 | +const { auth } = useStore() |
| 17 | +const route = useRoute() |
| 18 | +const router = useRouter() |
| 19 | +
|
| 20 | +/* 创建表单 */ |
| 21 | +const baseFormRef = useTemplateRef<InstanceType<typeof KnowledgeBaseForm>>('baseFormRef') |
| 22 | +const drawerVisible = ref(false) |
| 23 | +const loading = ref(false) |
| 24 | +const activeStep = ref(0) |
| 25 | +const strategyFormRef = useTemplateRef<InstanceType<typeof DocumentStrategyForm>>('strategyFormRef') |
| 26 | +const knowledgeFormRef = ref<FormInstance>() |
| 27 | +const knowledgeForm = reactive({ source_url: '', selector: '' }) |
| 28 | +const knowledgeFormRules: FormRules<typeof knowledgeForm> = { |
| 29 | + source_url: [{ required: true, whitespace: true, message: '请输入 Web 根地址', trigger: 'blur' }], |
| 30 | +} |
| 31 | +
|
| 32 | +function resetData() { |
| 33 | + baseFormRef.value?.reset() |
| 34 | + Object.assign(knowledgeForm, { source_url: '', selector: '' }) |
| 35 | + knowledgeFormRef.value?.clearValidate() |
| 36 | + loading.value = false |
| 37 | + activeStep.value = 0 |
| 38 | +} |
| 39 | +
|
| 40 | +function open() { |
| 41 | + drawerVisible.value = true |
| 42 | +} |
| 43 | +
|
| 44 | +function handleNext() { |
| 45 | + if (loading.value) return |
| 46 | + loading.value = true |
| 47 | + return Promise.all([baseFormRef.value?.validate(), knowledgeFormRef.value?.validate().catch(() => false)]) |
| 48 | + .then((results) => { |
| 49 | + if (results.every(Boolean)) activeStep.value = 1 |
| 50 | + }) |
| 51 | + .finally(() => { |
| 52 | + loading.value = false |
| 53 | + }) |
| 54 | +} |
| 55 | +
|
| 56 | +/* 校验并创建,成功后刷新资料并进入文档列表 */ |
| 57 | +function submit() { |
| 58 | + if (loading.value) return |
| 59 | + loading.value = true |
| 60 | + return Promise.all([baseFormRef.value?.validate(), knowledgeFormRef.value?.validate().catch(() => false), strategyFormRef.value?.validate()]) |
| 61 | + .then((validationResults) => { |
| 62 | + if (!validationResults.every(Boolean) || !baseFormRef.value) return |
| 63 | + const baseForm = baseFormRef.value.form |
| 64 | + return KnowledgeApi.postWebKnowledge({ |
| 65 | + name: baseForm.name.trim(), |
| 66 | + desc: baseForm.desc.trim(), |
| 67 | + embedding_model_id: baseForm.embedding_model_id, |
| 68 | + folder_id: props.folderId, |
| 69 | + type: KNOWLEDGE_TYPE.WEB, |
| 70 | + source_url: knowledgeForm.source_url.trim(), |
| 71 | + selector: knowledgeForm.selector.trim(), |
| 72 | + doc_strategy: strategyFormRef.value?.getStrategy(), |
| 73 | + }).then((knowledge) => { |
| 74 | + return auth.loadAuthBaseProfile().then(() => { |
| 75 | + MsgSuccess('创建成功') |
| 76 | + drawerVisible.value = false |
| 77 | + emit('refresh') |
| 78 | + return router.push({ |
| 79 | + name: 'workspace-knowledge-document-list', |
| 80 | + params: { knowledgeId: knowledge.id, type: KNOWLEDGE_TYPE_MAP[knowledge.type], workspaceId: route.params.workspaceId }, |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | + }) |
| 85 | + .finally(() => { |
| 86 | + loading.value = false |
| 87 | + }) |
| 88 | +} |
| 89 | +
|
| 90 | +defineExpose({ open }) |
| 91 | +</script> |
| 92 | + |
| 93 | +<template> |
| 94 | + <MkDrawer v-model="drawerVisible" direction="btt" content-class="h-full p-0!" @closed="resetData"> |
| 95 | + <template #header> |
| 96 | + <div class="flex w-full"> |
| 97 | + <h4>创建 Web 知识库</h4> |
| 98 | + <el-steps :active="activeStep" finish-status="success" class="absolute-center w-85!"> |
| 99 | + <el-step title="基本信息" /> |
| 100 | + <el-step title="文档处理策略" /> |
| 101 | + </el-steps> |
| 102 | + </div> |
| 103 | + </template> |
| 104 | + <MkViewLayout title=""> |
| 105 | + <template #default="{ Footer }"> |
| 106 | + <div class="mx-auto w-full max-w-200 pt-6"> |
| 107 | + <section v-show="activeStep === 0"> |
| 108 | + <h4 class="mb-5 border-l-2 border-primary pl-2">基本信息</h4> |
| 109 | + <KnowledgeBaseForm ref="baseFormRef" :disabled="loading" /> |
| 110 | + <el-form |
| 111 | + class="mt-4" |
| 112 | + ref="knowledgeFormRef" |
| 113 | + :model="knowledgeForm" |
| 114 | + :rules="knowledgeFormRules" |
| 115 | + :disabled="loading" |
| 116 | + label-position="top" |
| 117 | + require-asterisk-position="right" |
| 118 | + @submit.prevent |
| 119 | + > |
| 120 | + <el-form-item label="Web 根地址" prop="source_url"> |
| 121 | + <el-input |
| 122 | + v-model="knowledgeForm.source_url" |
| 123 | + placeholder="请输入 Web 根地址" |
| 124 | + @blur="knowledgeForm.source_url = knowledgeForm.source_url.trim()" |
| 125 | + /> |
| 126 | + </el-form-item> |
| 127 | + <el-form-item label="选择器" prop="selector"> |
| 128 | + <el-input |
| 129 | + v-model="knowledgeForm.selector" |
| 130 | + placeholder="默认为 body,可输入 .classname/#idname/tagname" |
| 131 | + @blur="knowledgeForm.selector = knowledgeForm.selector.trim()" |
| 132 | + /> |
| 133 | + </el-form-item> |
| 134 | + </el-form> |
| 135 | + </section> |
| 136 | + <section v-show="activeStep === 1"> |
| 137 | + <h4 class="mb-5 mk-title-decoration">文档处理策略</h4> |
| 138 | + <DocumentStrategyForm ref="strategyFormRef" /> |
| 139 | + </section> |
| 140 | + </div> |
| 141 | + <component :is="Footer"> |
| 142 | + <!-- 取消创建 --> |
| 143 | + <el-button plain :disabled="loading" @click="drawerVisible = false">取消</el-button> |
| 144 | + <!-- 返回基本信息 --> |
| 145 | + <el-button v-if="activeStep === 1" plain :disabled="loading" @click="activeStep = 0">上一步</el-button> |
| 146 | + <!-- 校验基本信息并进入策略配置 --> |
| 147 | + <el-button v-if="activeStep === 0" type="primary" :loading="loading" @click="handleNext">下一步</el-button> |
| 148 | + <!-- 创建知识库 --> |
| 149 | + <el-button v-else type="primary" :loading="loading" @click="submit">创建</el-button> |
| 150 | + </component> |
| 151 | + </template> |
| 152 | + </MkViewLayout> |
| 153 | + </MkDrawer> |
| 154 | +</template> |
0 commit comments