1- import { ref , reactive , onMounted } from "vue" ;
1+ import { ref , reactive } from "vue" ;
22import { storeToRefs } from "pinia" ;
33import { formatStringDate , kbFileTypeVerification } from "../utils/index" ;
44import { MessagePlugin } from "tdesign-vue-next" ;
@@ -11,6 +11,7 @@ import {
1111} from "@/api/knowledge-base/index" ;
1212import { knowledgeStore } from "@/stores/knowledge" ;
1313import { useRoute } from 'vue-router' ;
14+
1415const usemenuStore = knowledgeStore ( ) ;
1516export default function ( knowledgeBaseId ?: string ) {
1617 const route = useRoute ( ) ;
@@ -23,34 +24,32 @@ export default function (knowledgeBaseId?: string) {
2324 id : "" ,
2425 total : 0
2526 } ) ;
26- const getKnowled = ( query = { page : 1 , page_size : 35 } ) => {
27- if ( ! knowledgeBaseId ) return ;
28- listKnowledgeFiles ( knowledgeBaseId , query )
27+ const getKnowled = ( query = { page : 1 , page_size : 35 } , kbId ?: string ) => {
28+ const targetKbId = kbId || knowledgeBaseId ;
29+ if ( ! targetKbId ) return ;
30+
31+ listKnowledgeFiles ( targetKbId , query )
2932 . then ( ( result : any ) => {
30- let { data, total : totalResult } = result ;
31- let cardList_ = data . map ( ( item : any ) => {
32- item [ "file_name" ] = item . file_name . substring (
33- 0 ,
34- item . file_name . lastIndexOf ( "." )
35- ) ;
36- return {
37- ...item ,
38- updated_at : formatStringDate ( new Date ( item . updated_at ) ) ,
39- isMore : false ,
40- file_type : item . file_type . toLocaleUpperCase ( ) ,
41- } ;
42- } ) ;
43- if ( query . page == 1 ) {
44- cardList . value = cardList_ as any [ ] ;
33+ const { data, total : totalResult } = result ;
34+ const cardList_ = data . map ( ( item : any ) => ( {
35+ ...item ,
36+ file_name : item . file_name . substring ( 0 , item . file_name . lastIndexOf ( "." ) ) ,
37+ updated_at : formatStringDate ( new Date ( item . updated_at ) ) ,
38+ isMore : false ,
39+ file_type : item . file_type . toLocaleUpperCase ( ) ,
40+ } ) ) ;
41+
42+ if ( query . page === 1 ) {
43+ cardList . value = cardList_ ;
4544 } else {
46- ( cardList . value as any [ ] ) . push ( ...cardList_ ) ;
45+ cardList . value . push ( ...cardList_ ) ;
4746 }
4847 total . value = totalResult ;
4948 } )
50- . catch ( ( _err ) => { } ) ;
49+ . catch ( ( ) => { } ) ;
5150 } ;
5251 const delKnowledge = ( index : number , item : any ) => {
53- ( cardList . value as any [ ] ) [ index ] . isMore = false ;
52+ cardList . value [ index ] . isMore = false ;
5453 moreIndex . value = - 1 ;
5554 delKnowledgeDetails ( item . id )
5655 . then ( ( result : any ) => {
@@ -61,7 +60,7 @@ export default function (knowledgeBaseId?: string) {
6160 MessagePlugin . error ( "知识删除失败!" ) ;
6261 }
6362 } )
64- . catch ( ( _err ) => {
63+ . catch ( ( ) => {
6564 MessagePlugin . error ( "知识删除失败!" ) ;
6665 } ) ;
6766 } ;
@@ -74,63 +73,45 @@ export default function (knowledgeBaseId?: string) {
7473 }
7574 } ;
7675 const requestMethod = ( file : any , uploadInput : any ) => {
77- if ( file instanceof File && uploadInput ) {
78- if ( kbFileTypeVerification ( file ) ) {
79- return ;
80- }
81- // 每次上传时动态解析当前 kbId(优先:路由 -> URL -> 初始参数)
82- let currentKbId : string | undefined ;
83- try {
84- currentKbId = ( route . params as any ) ?. kbId as string ;
85- } catch { }
86- if ( ! currentKbId && typeof window !== 'undefined' ) {
87- try {
88- const match = window . location . pathname . match ( / k n o w l e d g e - b a s e s \/ ( [ ^ / ] + ) / ) ;
89- if ( match && match [ 1 ] ) currentKbId = match [ 1 ] ;
90- } catch { }
91- }
92- if ( ! currentKbId ) {
93- currentKbId = knowledgeBaseId ;
94- }
95- if ( ! currentKbId ) {
96- MessagePlugin . error ( "缺少知识库ID" ) ;
97- return ;
98- }
99- uploadKnowledgeFile ( currentKbId , { file } )
100- . then ( ( result : any ) => {
101- if ( result . success ) {
102- MessagePlugin . info ( "上传成功!" ) ;
103- getKnowled ( ) ;
104- } else {
105- // 改进错误信息提取逻辑
106- let errorMessage = "上传失败!" ;
107- if ( result . error && result . error . message ) {
108- errorMessage = result . error . message ;
109- } else if ( result . message ) {
110- errorMessage = result . message ;
111- }
112- if ( result . code === 'duplicate_file' || ( result . error && result . error . code === 'duplicate_file' ) ) {
113- errorMessage = "文件已存在" ;
114- }
115- MessagePlugin . error ( errorMessage ) ;
116- }
117- uploadInput . value . value = "" ;
118- } )
119- . catch ( ( err : any ) => {
120- let errorMessage = "上传失败!" ;
121- if ( err . code === 'duplicate_file' ) {
122- errorMessage = "文件已存在" ;
123- } else if ( err . error && err . error . message ) {
124- errorMessage = err . error . message ;
125- } else if ( err . message ) {
126- errorMessage = err . message ;
127- }
128- MessagePlugin . error ( errorMessage ) ;
129- uploadInput . value . value = "" ;
130- } ) ;
131- } else {
132- MessagePlugin . error ( "file文件类型错误!" ) ;
76+ if ( ! ( file instanceof File ) || ! uploadInput ) {
77+ MessagePlugin . error ( "文件类型错误!" ) ;
78+ return ;
79+ }
80+
81+ if ( kbFileTypeVerification ( file ) ) {
82+ return ;
83+ }
84+
85+ // 获取当前知识库ID
86+ let currentKbId : string | undefined = ( route . params as any ) ?. kbId as string ;
87+ if ( ! currentKbId && typeof window !== 'undefined' ) {
88+ const match = window . location . pathname . match ( / k n o w l e d g e - b a s e s \/ ( [ ^ / ] + ) / ) ;
89+ if ( match ?. [ 1 ] ) currentKbId = match [ 1 ] ;
90+ }
91+ if ( ! currentKbId ) {
92+ currentKbId = knowledgeBaseId ;
93+ }
94+ if ( ! currentKbId ) {
95+ MessagePlugin . error ( "缺少知识库ID" ) ;
96+ return ;
13397 }
98+
99+ uploadKnowledgeFile ( currentKbId , { file } )
100+ . then ( ( result : any ) => {
101+ if ( result . success ) {
102+ MessagePlugin . info ( "上传成功!" ) ;
103+ getKnowled ( { page : 1 , page_size : 35 } , currentKbId ) ;
104+ } else {
105+ const errorMessage = result . error ?. message || result . message || "上传失败!" ;
106+ MessagePlugin . error ( result . code === 'duplicate_file' ? "文件已存在" : errorMessage ) ;
107+ }
108+ uploadInput . value . value = "" ;
109+ } )
110+ . catch ( ( err : any ) => {
111+ const errorMessage = err . error ?. message || err . message || "上传失败!" ;
112+ MessagePlugin . error ( err . code === 'duplicate_file' ? "文件已存在" : errorMessage ) ;
113+ uploadInput . value . value = "" ;
114+ } ) ;
134115 } ;
135116 const getCardDetails = ( item : any ) => {
136117 Object . assign ( details , {
@@ -142,31 +123,32 @@ export default function (knowledgeBaseId?: string) {
142123 getKnowledgeDetails ( item . id )
143124 . then ( ( result : any ) => {
144125 if ( result . success && result . data ) {
145- let { data } = result ;
126+ const { data } = result ;
146127 Object . assign ( details , {
147128 title : data . file_name ,
148129 time : formatStringDate ( new Date ( data . updated_at ) ) ,
149130 id : data . id ,
150131 } ) ;
151132 }
152133 } )
153- . catch ( ( _err ) => { } ) ;
154- getfDetails ( item . id , 1 ) ;
134+ . catch ( ( ) => { } ) ;
135+ getfDetails ( item . id , 1 ) ;
155136 } ;
137+
156138 const getfDetails = ( id : string , page : number ) => {
157139 getKnowledgeDetailsCon ( id , page )
158140 . then ( ( result : any ) => {
159141 if ( result . success && result . data ) {
160- let { data, total : totalResult } = result ;
161- if ( page == 1 ) {
162- ( details . md as any [ ] ) = data ;
142+ const { data, total : totalResult } = result ;
143+ if ( page === 1 ) {
144+ details . md = data ;
163145 } else {
164- ( details . md as any [ ] ) . push ( ...data ) ;
146+ details . md . push ( ...data ) ;
165147 }
166148 details . total = totalResult ;
167149 }
168150 } )
169- . catch ( ( _err ) => { } ) ;
151+ . catch ( ( ) => { } ) ;
170152 } ;
171153 return {
172154 cardList,
0 commit comments