diff --git a/frontend/src/hooks/useKnowledgeBase.ts b/frontend/src/hooks/useKnowledgeBase.ts index 8fc646e..ac30701 100644 --- a/frontend/src/hooks/useKnowledgeBase.ts +++ b/frontend/src/hooks/useKnowledgeBase.ts @@ -1,4 +1,4 @@ -import { ref, reactive, onMounted } from "vue"; +import { ref, reactive } from "vue"; import { storeToRefs } from "pinia"; import { formatStringDate, kbFileTypeVerification } from "../utils/index"; import { MessagePlugin } from "tdesign-vue-next"; @@ -11,6 +11,7 @@ import { } from "@/api/knowledge-base/index"; import { knowledgeStore } from "@/stores/knowledge"; import { useRoute } from 'vue-router'; + const usemenuStore = knowledgeStore(); export default function (knowledgeBaseId?: string) { const route = useRoute(); @@ -23,34 +24,32 @@ export default function (knowledgeBaseId?: string) { id: "", total: 0 }); - const getKnowled = (query = { page: 1, page_size: 35 }) => { - if (!knowledgeBaseId) return; - listKnowledgeFiles(knowledgeBaseId, query) + const getKnowled = (query = { page: 1, page_size: 35 }, kbId?: string) => { + const targetKbId = kbId || knowledgeBaseId; + if (!targetKbId) return; + + listKnowledgeFiles(targetKbId, query) .then((result: any) => { - let { data, total: totalResult } = result; - let cardList_ = data.map((item: any) => { - item["file_name"] = item.file_name.substring( - 0, - item.file_name.lastIndexOf(".") - ); - return { - ...item, - updated_at: formatStringDate(new Date(item.updated_at)), - isMore: false, - file_type: item.file_type.toLocaleUpperCase(), - }; - }); - if (query.page == 1) { - cardList.value = cardList_ as any[]; + const { data, total: totalResult } = result; + const cardList_ = data.map((item: any) => ({ + ...item, + file_name: item.file_name.substring(0, item.file_name.lastIndexOf(".")), + updated_at: formatStringDate(new Date(item.updated_at)), + isMore: false, + file_type: item.file_type.toLocaleUpperCase(), + })); + + if (query.page === 1) { + cardList.value = cardList_; } else { - (cardList.value as any[]).push(...cardList_); + cardList.value.push(...cardList_); } total.value = totalResult; }) - .catch((_err) => {}); + .catch(() => {}); }; const delKnowledge = (index: number, item: any) => { - (cardList.value as any[])[index].isMore = false; + cardList.value[index].isMore = false; moreIndex.value = -1; delKnowledgeDetails(item.id) .then((result: any) => { @@ -61,7 +60,7 @@ export default function (knowledgeBaseId?: string) { MessagePlugin.error("知识删除失败!"); } }) - .catch((_err) => { + .catch(() => { MessagePlugin.error("知识删除失败!"); }); }; @@ -74,63 +73,45 @@ export default function (knowledgeBaseId?: string) { } }; const requestMethod = (file: any, uploadInput: any) => { - if (file instanceof File && uploadInput) { - if (kbFileTypeVerification(file)) { - return; - } - // 每次上传时动态解析当前 kbId(优先:路由 -> URL -> 初始参数) - let currentKbId: string | undefined; - try { - currentKbId = (route.params as any)?.kbId as string; - } catch {} - if (!currentKbId && typeof window !== 'undefined') { - try { - const match = window.location.pathname.match(/knowledge-bases\/([^/]+)/); - if (match && match[1]) currentKbId = match[1]; - } catch {} - } - if (!currentKbId) { - currentKbId = knowledgeBaseId; - } - if (!currentKbId) { - MessagePlugin.error("缺少知识库ID"); - return; - } - uploadKnowledgeFile(currentKbId, { file }) - .then((result: any) => { - if (result.success) { - MessagePlugin.info("上传成功!"); - getKnowled(); - } else { - // 改进错误信息提取逻辑 - let errorMessage = "上传失败!"; - if (result.error && result.error.message) { - errorMessage = result.error.message; - } else if (result.message) { - errorMessage = result.message; - } - if (result.code === 'duplicate_file' || (result.error && result.error.code === 'duplicate_file')) { - errorMessage = "文件已存在"; - } - MessagePlugin.error(errorMessage); - } - uploadInput.value.value = ""; - }) - .catch((err: any) => { - let errorMessage = "上传失败!"; - if (err.code === 'duplicate_file') { - errorMessage = "文件已存在"; - } else if (err.error && err.error.message) { - errorMessage = err.error.message; - } else if (err.message) { - errorMessage = err.message; - } - MessagePlugin.error(errorMessage); - uploadInput.value.value = ""; - }); - } else { - MessagePlugin.error("file文件类型错误!"); + if (!(file instanceof File) || !uploadInput) { + MessagePlugin.error("文件类型错误!"); + return; } + + if (kbFileTypeVerification(file)) { + return; + } + + // 获取当前知识库ID + let currentKbId: string | undefined = (route.params as any)?.kbId as string; + if (!currentKbId && typeof window !== 'undefined') { + const match = window.location.pathname.match(/knowledge-bases\/([^/]+)/); + if (match?.[1]) currentKbId = match[1]; + } + if (!currentKbId) { + currentKbId = knowledgeBaseId; + } + if (!currentKbId) { + MessagePlugin.error("缺少知识库ID"); + return; + } + + uploadKnowledgeFile(currentKbId, { file }) + .then((result: any) => { + if (result.success) { + MessagePlugin.info("上传成功!"); + getKnowled({ page: 1, page_size: 35 }, currentKbId); + } else { + const errorMessage = result.error?.message || result.message || "上传失败!"; + MessagePlugin.error(result.code === 'duplicate_file' ? "文件已存在" : errorMessage); + } + uploadInput.value.value = ""; + }) + .catch((err: any) => { + const errorMessage = err.error?.message || err.message || "上传失败!"; + MessagePlugin.error(err.code === 'duplicate_file' ? "文件已存在" : errorMessage); + uploadInput.value.value = ""; + }); }; const getCardDetails = (item: any) => { Object.assign(details, { @@ -142,7 +123,7 @@ export default function (knowledgeBaseId?: string) { getKnowledgeDetails(item.id) .then((result: any) => { if (result.success && result.data) { - let { data } = result; + const { data } = result; Object.assign(details, { title: data.file_name, time: formatStringDate(new Date(data.updated_at)), @@ -150,23 +131,24 @@ export default function (knowledgeBaseId?: string) { }); } }) - .catch((_err) => {}); - getfDetails(item.id, 1); + .catch(() => {}); + getfDetails(item.id, 1); }; + const getfDetails = (id: string, page: number) => { getKnowledgeDetailsCon(id, page) .then((result: any) => { if (result.success && result.data) { - let { data, total: totalResult } = result; - if (page == 1) { - (details.md as any[]) = data; + const { data, total: totalResult } = result; + if (page === 1) { + details.md = data; } else { - (details.md as any[]).push(...data); + details.md.push(...data); } details.total = totalResult; } }) - .catch((_err) => {}); + .catch(() => {}); }; return { cardList, diff --git a/frontend/src/views/platform/index.vue b/frontend/src/views/platform/index.vue index eab6e86..8ce426e 100644 --- a/frontend/src/views/platform/index.vue +++ b/frontend/src/views/platform/index.vue @@ -1,5 +1,5 @@