Explorar o código

Merge branch 'master' of http://173.18.12.196:3000/front/self-constructing_graph

yangdr hai 1 mes
pai
achega
b8f5a0ab38
Modificáronse 2 ficheiros con 101 adicións e 13 borrados
  1. 20 1
      src/components/LayoutHeader.vue
  2. 81 12
      src/dialogs/OCRDialog.vue

+ 20 - 1
src/components/LayoutHeader.vue

@@ -6,7 +6,7 @@
     </div> -->
     <div class="menu">
       <el-menu :default-active="currentPath" ref="menuRef" class="el-menu-demo" mode="horizontal">
-        <el-menu-item :index="item.path" v-for="item in routeList" :key="item.name"
+        <el-menu-item :index="item.path" v-for="item in routeList" :key="item.name" :class="{ 'external-link-item': isExternalLink(item.path) }"
           @click="handleMenuClick(item.path)">{{ item.title }}</el-menu-item>
       </el-menu>
     </div>
@@ -69,6 +69,11 @@ const handleLogout = () => {
   clearSessionVar()
 }
 
+// 判断是否为外部链接的方法
+const isExternalLink = (path) => {
+  return /^https?/g.test(path);
+};
+
 
 function handleMenuClick(path) {
   if (/^https?/g.test(path)) {
@@ -79,6 +84,7 @@ function handleMenuClick(path) {
     }, 1000)
     menuRef.value.updateActiveIndex(currentPath.value)
   } else {
+    if (route.path.includes(path)) return
     router.push({ path: path })
   }
 }
@@ -133,6 +139,19 @@ onBeforeUnmount(() => {
         font-size: 20px;
       }
     }
+    .external-link-item {
+      // 覆盖激活效果
+      &.is-active {
+        background-color: transparent !important;
+        color: inherit !important;
+      }
+
+      // 覆盖点击效果
+      &:focus {
+        background-color: transparent !important;
+        color: inherit !important;
+      }
+    }
   }
 }
 </style>

+ 81 - 12
src/dialogs/OCRDialog.vue

@@ -220,6 +220,9 @@ const form = ref({
   status: 'queue'
 })
 const fileList = ref<any>([])
+// 记录已导入文件的哈希值
+const importedFileHashes = ref<string[]>([])
+
 const props = defineProps({
   title: { type: String, required: true, default: '工作' },
   queue_category: { type: String, required: true, default: 'SYSTEM' },
@@ -230,7 +233,7 @@ const props = defineProps({
   job_creator: { type: String, required: false, default: 'admin' },
   status: { type: Number, required: false, default: 0 },
 })
-const emit = defineEmits(['update:modelValue', 'success', 'cancel'])
+const emit = defineEmits(['update:modelValue', 'success', 'cancel','closed'])
 const showDialog = (visible: boolean = true) => {
   // console.log("OCRDialog showDialog")
   if (upload.value) {
@@ -238,6 +241,15 @@ const showDialog = (visible: boolean = true) => {
   }
   dialogFormVisible.value = visible
 }
+
+// 清除上传文件列表
+const clearFileList = () => {
+    fileList.value = []
+    if (upload.value) {
+        upload.value.clearFiles()
+    }
+    importedFileHashes.value = [] // 清空已导入文件的哈希值记录
+}
 function handleGetKnowledgeBase() {
   getKnowledgeBase().then((response: any) => {
     // console.log('getKnowledgeBase', response)
@@ -295,7 +307,7 @@ function fetchFile(fileName: string, fileUrl: string) {
 function handleSelectedImport() {
   const SelectionRows = KBTableRef.value.getSelectionRows()
   handleImportFiles(toRaw(SelectionRows))
-  KBTableRef.value.clearSelection()
+  knowledgeBase.value.visible = false
   // console.log(SelectionRows)
 }
 // 使用原生方法计算文件的hash值,该方法兼容性较差
@@ -337,13 +349,12 @@ async function calculateFileHashForCryptoJS(file: File) {
 }
 
 
-function handleImportFiles(filesList: any[]) {
-  for (let i = 0; i < filesList.length; i++) {
-    // if (i > 0) break;
-    fetchFile(filesList[i].file_name, filesList[i].minio_url)
-  }
-  knowledgeBase.value.visible = false
-}
+// function handleImportFiles(filesList: any[]) {
+//   for (let i = 0; i < filesList.length; i++) {
+//     // if (i > 0) break;
+//     fetchFile(filesList[i].file_name, filesList[i].minio_url)
+//   }
+// }
 
 /**
  * 用于计算文件的hash值,包括sha256值和md5值
@@ -406,9 +417,61 @@ const handleRemove = (file: any, fileList: any) => {
   console.log(file, fileList)
 }
 
+async function calculateFileHash(file: File) {
+  const arrayBuffer = await file.arrayBuffer();
+  const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
+  const hashArray = Array.from(new Uint8Array(hashBuffer));
+  const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
+  return hashHex;
+}
+
+function handleImportFiles(filesList: any[]) {
+  filesList.forEach(async (fileInfo) => {
+    const { file_name, minio_url } = fileInfo;
+    try {
+      const response = await axios.get(minio_url, { responseType: 'blob' });
+      const rawFile = new File([response.data], file_name, { type: response.data.type });
+      const fileHash = await calculateFileHash(rawFile);
+
+      if (importedFileHashes.value.includes(fileHash)) {
+        ElMessage({
+          message: `文件“${file_name}”已导入过,不能重复导入`,
+          type: 'info',
+          plain: true,
+        });
+        return;
+      }
+
+      const file = rawFile as UploadRawFile;
+      file.uid = genFileId();
+      upload.value!.handleStart(file);
+      importedFileHashes.value.push(fileHash);
+      ElMessage({
+        message: `文件“${file_name}”导入成功`,
+        type: 'success',
+        plain: true,
+      });
+    } catch (error) {
+      ElMessage({
+        message: `从知识库获取文件“${file_name}”失败`,
+        type: 'error'
+      });
+      console.log(error);
+    }
+  });
+}
+
 const handleSuccess = (response: any, file: any, fileList: any) => {
-  console.log(response, file, fileList)
-  emit('success')
+  console.log(response, file, fileList);
+  const fileHash = importedFileHashes.value.find(hash => {
+    // 这里简单通过文件名匹配,实际可根据需求优化
+    return file.name === file.raw.name;
+  });
+  if (fileHash) {
+    // 上传成功后保留哈希记录
+    importedFileHashes.value.push(fileHash);
+  }
+  emit('success');
 }
 
 const handleConfirm = () => {
@@ -431,16 +494,22 @@ const handleConfirm = () => {
     }
   })
 }
-defineExpose({ showDialog })
+defineExpose({ showDialog,clearFileList })
 
 function handleClosed() {
   knowledgeBase.value.visible = false
+  clearFileList ()
 }
 
 watch(() => knowledgeBase.value.activeId, (newValue) => {
   debounceGetKBfileList()
   // console.log('activeId', newValue)
 })
+watch(() =>knowledgeBase.value.visible, (newValue) => {
+  if (!newValue) {
+    KBTableRef.value.clearSelection() // 清除选择
+  }
+})
 
 async function checkLinkValidity(index: number, url: string) {
   try {