Skip to content

Commit de41b6c

Browse files
committed
【功能新增】AI:知识库文档上传:60%,SplitStep 完成度更高
1 parent da91896 commit de41b6c

File tree

7 files changed

+130
-97
lines changed

7 files changed

+130
-97
lines changed

src/api/ai/knowledge/document/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ export const KnowledgeDocumentApi = {
2424
return await request.get({ url: `/ai/knowledge/document/get?id=` + id })
2525
},
2626

27-
// 新增知识库文档
27+
// 新增知识库文档(单个)
2828
createKnowledgeDocument: async (data: KnowledgeDocumentVO) => {
2929
return await request.post({ url: `/ai/knowledge/document/create`, data })
3030
},
3131

32+
// 新增知识库文档(批量)
33+
createKnowledgeDocumentList: async (data: any) => {
34+
return await request.post({ url: `/ai/knowledge/document/create-list`, data })
35+
},
36+
3237
// // 修改AI 知识库文档
3338
// updateKnowledgeDocument: async (data: KnowledgeDocumentVO) => {
3439
// return await request.put({ url: `/ai/knowledge/document/update`, data })

src/router/modules/remaining.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ const remainingRouter: AppRouteRecordRaw[] = [
633633
},
634634
{
635635
path: 'console/knowledge/document/create',
636-
component: () => import('@/views/ai/knowledge/document/create/index.vue'),
636+
component: () => import('@/views/ai/knowledge/document/form/index.vue'),
637637
name: 'AiKnowledgeDocumentCreate',
638638
meta: {
639639
title: '创建文档',
@@ -642,6 +642,18 @@ const remainingRouter: AppRouteRecordRaw[] = [
642642
hidden: true,
643643
activeMenu: '/ai/console/knowledge/document'
644644
}
645+
},
646+
{
647+
path: 'console/knowledge/document/update',
648+
component: () => import('@/views/ai/knowledge/document/form/index.vue'),
649+
name: 'AiKnowledgeDocumentUpdate',
650+
meta: {
651+
title: '修改文档',
652+
icon: 'ep:edit',
653+
noCache: true,
654+
hidden: true,
655+
activeMenu: '/ai/console/knowledge/document'
656+
}
645657
}
646658
]
647659
},

src/views/ai/knowledge/document/create/SplitStep.vue renamed to src/views/ai/knowledge/document/form/SplitStep.vue

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,24 @@
7878

7979
<!-- 添加底部按钮 -->
8080
<div class="mt-20px flex justify-between">
81-
<el-button @click="handlePrevStep">上一步</el-button>
82-
<el-button type="primary" @click="handleNextStep">保存并处理</el-button>
81+
<div>
82+
<el-button v-if="!modelData.id" @click="handlePrevStep">上一步</el-button>
83+
</div>
84+
<div>
85+
<el-button type="primary" :loading="submitLoading" @click="handleSave">
86+
保存并处理
87+
</el-button>
88+
</div>
8389
</div>
8490
</div>
8591
</template>
8692

8793
<script lang="ts" setup>
88-
import { PropType, ref, computed, inject, onMounted, getCurrentInstance } from 'vue'
94+
import { computed, getCurrentInstance, inject, onMounted, PropType, ref } from 'vue'
8995
import { Icon } from '@/components/Icon'
9096
import { KnowledgeSegmentApi } from '@/api/ai/knowledge/segment'
9197
import { useMessage } from '@/hooks/web/useMessage'
98+
import { KnowledgeDocumentApi } from '@/api/ai/knowledge/document'
9299
93100
const props = defineProps({
94101
modelValue: {
@@ -108,6 +115,7 @@ const modelData = computed({
108115
109116
const splitLoading = ref(false) // 分段加载状态
110117
const currentFile = ref<any>(null) // 当前选中的文件
118+
const submitLoading = ref(false) // 提交按钮加载状态
111119
112120
/** 选择文件 */
113121
const selectFile = async (index: number) => {
@@ -124,8 +132,11 @@ const splitContent = async (file: any) => {
124132
125133
splitLoading.value = true
126134
try {
127-
const data = await KnowledgeSegmentApi.splitContent(file.url, modelData.value.segmentMaxTokens) // 调用后端分段接口,获取文档的分段内容、字符数和 Token 数
128-
file.segments = data
135+
// 调用后端分段接口,获取文档的分段内容、字符数和 Token 数
136+
file.segments = await KnowledgeSegmentApi.splitContent(
137+
file.url,
138+
modelData.value.segmentMaxTokens
139+
)
129140
} catch (error) {
130141
console.error('获取分段内容失败:', file, error)
131142
message.error('获取分段内容失败')
@@ -158,22 +169,46 @@ const handlePrevStep = () => {
158169
}
159170
}
160171
161-
/** 下一步按钮处理 */
162-
const handleNextStep = () => {
163-
const parentEl = parent || getCurrentInstance()?.parent
164-
if (parentEl && typeof parentEl.exposed?.goToNextStep === 'function') {
165-
parentEl.exposed.goToNextStep()
166-
}
167-
}
168-
169-
/** 组件激活时自动调用分段接口 TODO 芋艿:需要看下 */
170-
const activated = async () => {
171-
if (!currentFile.value && modelData.value.list && modelData.value.list.length > 0) {
172-
currentFile.value = modelData.value.list[0] // 如果没有选中文件,默认选中第一个
172+
/** 保存操作 */
173+
const handleSave = async () => {
174+
// 保存前验证
175+
if (!currentFile?.value?.segments || currentFile.value.segments.length === 0) {
176+
message.warning('请先预览分段内容')
177+
return
173178
}
174179
175-
if (currentFile.value) {
176-
await splitContent(currentFile.value) // 如果有选中的文件,获取分段内容
180+
// 设置按钮加载状态
181+
submitLoading.value = true
182+
try {
183+
if (modelData.value.id) {
184+
// 修改场景
185+
modelData.value.ids = await KnowledgeDocumentApi.createKnowledgeDocumentList({
186+
id: modelData.value.id,
187+
segmentMaxTokens: modelData.value.segmentMaxTokens
188+
})
189+
} else {
190+
// 新增场景
191+
modelData.value.ids = await KnowledgeDocumentApi.createKnowledgeDocumentList({
192+
knowledgeId: modelData.value.knowledgeId,
193+
segmentMaxTokens: modelData.value.segmentMaxTokens,
194+
list: modelData.value.list.map((item: any) => ({
195+
name: item.name,
196+
url: item.url
197+
}))
198+
})
199+
}
200+
201+
// 进入下一步
202+
const parentEl = parent || getCurrentInstance()?.parent
203+
if (parentEl && typeof parentEl.exposed?.goToNextStep === 'function') {
204+
parentEl.exposed.goToNextStep()
205+
}
206+
} catch (error: any) {
207+
console.error('保存失败:', modelData.value, error)
208+
message.error(error.message)
209+
} finally {
210+
// 关闭按钮加载状态
211+
submitLoading.value = false
177212
}
178213
}
179214

src/views/ai/knowledge/document/create/index.vue renamed to src/views/ai/knowledge/document/form/index.vue

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<!-- 左侧标题 -->
99
<div class="w-200px flex items-center overflow-hidden">
1010
<Icon icon="ep:arrow-left" class="cursor-pointer flex-shrink-0" @click="handleBack" />
11-
<span class="ml-10px text-16px truncate" :title="formData.name || '创建知识库文档'">
12-
{{ formData.name || '创建知识库文档' }}
11+
<span class="ml-10px text-16px truncate">
12+
{{ formData.id ? '编辑知识库文档' : '创建知识库文档' }}
1313
</span>
1414
</div>
1515

@@ -68,29 +68,25 @@
6868

6969
<script lang="ts" setup>
7070
import { useRoute, useRouter } from 'vue-router'
71-
import { useMessage } from '@/hooks/web/useMessage'
7271
import { useTagsViewStore } from '@/store/modules/tagsView'
7372
import UploadStep from './UploadStep.vue'
7473
import SplitStep from './SplitStep.vue'
7574
import ProcessStep from './ProcessStep.vue'
75+
import { KnowledgeDocumentApi } from '@/api/ai/knowledge/document'
7676
77-
const router = useRouter()
7877
const { delView } = useTagsViewStore() // 视图操作
79-
const route = useRoute()
80-
const message = useMessage()
78+
const route = useRoute() // 路由
79+
const router = useRouter() // 路由
8180
8281
// 组件引用
8382
const uploadDocumentRef = ref()
8483
const documentSegmentRef = ref()
8584
const processCompleteRef = ref()
86-
8785
const currentStep = ref(0) // 步骤控制
8886
const steps = [{ title: '上传文档' }, { title: '文档分段' }, { title: '处理并完成' }]
89-
90-
// 表单数据
9187
const formData = ref({
92-
knowlegeId: undefined, // 知识库编号
93-
id: undefined, // 文档编号(documentId)
88+
knowledgeId: undefined, // 知识库编号
89+
id: undefined, // 编辑的文档编号(documentId)
9490
segmentMaxTokens: 500, // 分段最大 token 数
9591
list: [] as Array<{
9692
name: string
@@ -101,17 +97,35 @@ const formData = ref({
10197
tokens?: number
10298
}>
10399
}>, // 用于存储上传的文件列表
100+
documentIds: [], // 最终提交的创建/修改的文档编号,用于 ProcessStep 组件的轮询
104101
status: 0 // 0: 草稿, 1: 处理中, 2: 已完成
105-
})
102+
}) // 表单数据
103+
104+
provide('parent', getCurrentInstance()) // 提供 parent 给子组件使用
106105
107106
/** 初始化数据 */
108107
const initData = async () => {
109-
// TODO @芋艿:knowlegeId 解析
110-
const documentId = route.params.id as string
108+
// 【新增场景】从路由参数中获取知识库 ID
109+
if (route.query.knowledgeId) {
110+
formData.value.knowledgeId = route.query.knowledgeId as any
111+
}
112+
113+
// 【修改场景】从路由参数中获取文档 ID
114+
const documentId = route.query.id
111115
if (documentId) {
112-
// 修改场景
113-
// 这里需要调用API获取文档数据
114-
// formData.value = await DocumentApi.getDocument(documentId)
116+
// 获取文档信息
117+
formData.value.id = documentId as any
118+
const document = await KnowledgeDocumentApi.getKnowledgeDocument(documentId as any)
119+
formData.value.segmentMaxTokens = document.segmentMaxTokens
120+
formData.value.list = [
121+
{
122+
name: document.name,
123+
url: document.url,
124+
segments: []
125+
}
126+
]
127+
// 进入下一步
128+
goToNextStep()
115129
}
116130
117131
// TODO @芋艿:为了开发方便,强制设置
@@ -141,75 +155,31 @@ const goToPrevStep = () => {
141155
}
142156
}
143157
144-
/** 保存操作 */
145-
const handleSave = async () => {
146-
try {
147-
// 更新表单数据
148-
const documentData = {
149-
...formData.value
150-
}
151-
152-
if (formData.value.id) {
153-
// 修改场景
154-
// await DocumentApi.updateDocument(documentData)
155-
message.success('修改成功')
156-
} else {
157-
// 新增场景
158-
// formData.value.id = await DocumentApi.createDocument(documentData)
159-
message.success('新增成功')
160-
try {
161-
await message.confirm('创建文档成功,是否继续编辑?')
162-
// 用户点击继续编辑,跳转到编辑页面
163-
await nextTick()
164-
// 先删除当前页签
165-
delView(unref(router.currentRoute))
166-
// 跳转到编辑页面
167-
await router.push({
168-
name: 'AiKnowledgeDocumentUpdate',
169-
params: { id: formData.value.id }
170-
})
171-
} catch {
172-
// 先删除当前页签
173-
delView(unref(router.currentRoute))
174-
// 用户点击返回列表
175-
await router.push({ name: 'AiKnowledgeDocument' })
176-
}
177-
}
178-
} catch (error: any) {
179-
console.error('保存失败:', error)
180-
message.warning(error.message || '请完善所有步骤的必填信息')
181-
}
182-
}
183-
184158
/** 返回列表页 */
185159
const handleBack = () => {
186160
// 先删除当前页签
187161
delView(unref(router.currentRoute))
188162
// 跳转到列表页
189-
router.push({ name: 'AiKnowledgeDocument' })
163+
router.push({ name: 'AiKnowledgeDocument', query: { knowledgeId: formData.value.knowledgeId } })
190164
}
191165
192166
/** 初始化 */
193167
onMounted(async () => {
194168
await initData()
195169
})
196170
197-
// 提供parent给子组件使用
198-
provide('parent', getCurrentInstance())
199-
200-
// 添加组件卸载前的清理代码
171+
/** 添加组件卸载前的清理代码 */
201172
onBeforeUnmount(() => {
202173
// 清理所有的引用
203174
uploadDocumentRef.value = null
204175
documentSegmentRef.value = null
205176
processCompleteRef.value = null
206177
})
207178
208-
// 暴露方法给子组件使用
179+
/** 暴露方法给子组件使用 */
209180
defineExpose({
210181
goToNextStep,
211-
goToPrevStep,
212-
handleSave
182+
goToPrevStep
213183
})
214184
</script>
215185

src/views/ai/knowledge/document/index.vue

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<el-button
6969
link
7070
type="primary"
71-
@click="openForm('update', scope.row.id)"
71+
@click="handleUpdate(scope.row.id)"
7272
v-hasPermi="['ai:knowledge:update']"
7373
>
7474
编辑
@@ -148,15 +148,20 @@ const resetQuery = () => {
148148
handleQuery()
149149
}
150150
151-
/** 添加/修改操作 */
152-
const formRef = ref()
153-
const openForm = (type: string, id?: number) => {
154-
formRef.value.open(type, id)
155-
}
156-
157151
/** 跳转到创建文档页面 */
158152
const handleCreate = () => {
159-
router.push({ name: 'AiKnowledgeDocumentCreate' })
153+
router.push({
154+
name: 'AiKnowledgeDocumentCreate',
155+
query: { knowledgeId: queryParams.knowledgeId }
156+
})
157+
}
158+
159+
/** 跳转到更新文档页面 */
160+
const handleUpdate = (id: number) => {
161+
router.push({
162+
name: 'AiKnowledgeDocumentUpdate',
163+
query: { id, knowledgeId: queryParams.knowledgeId }
164+
})
160165
}
161166
162167
/** 删除按钮操作 */
@@ -174,10 +179,16 @@ const handleDelete = async (id: number) => {
174179
175180
/** 初始化 **/
176181
onMounted(() => {
177-
// 从路由参数中获取知识库 ID
178-
if (route.query.knowledgeId) {
179-
queryParams.knowledgeId = route.query.knowledgeId as any
182+
// 如果知识库 ID 不存在,显示错误提示并关闭页面
183+
if (!route.query.knowledgeId) {
184+
message.error('知识库 ID 不存在,无法查看文档列表')
185+
// 关闭当前路由,返回到知识库列表页面
186+
router.push({ name: 'AiKnowledge' })
187+
return
180188
}
189+
190+
// 从路由参数中获取知识库 ID
191+
queryParams.knowledgeId = route.query.knowledgeId as any
181192
getList()
182193
})
183194
</script>

0 commit comments

Comments
 (0)