Ver Fonte

fix: 模板解析进度实时更新(轮询+监听任务中心)

何文松 há 4 semanas atrás
pai
commit
2ff13e4490
1 ficheiros alterados com 74 adições e 1 exclusões
  1. 74 1
      frontend/vue-demo/src/views/Home.vue

+ 74 - 1
frontend/vue-demo/src/views/Home.vue

@@ -222,7 +222,7 @@
 </template>
 
 <script setup>
-import { ref, reactive, computed, onMounted } from 'vue'
+import { ref, reactive, computed, onMounted, onUnmounted, watch } from 'vue'
 import { useRouter } from 'vue-router'
 import { Promotion, UploadFilled, CircleCheckFilled, Delete, Loading, WarningFilled } from '@element-plus/icons-vue'
 import { ElMessage, ElMessageBox } from 'element-plus'
@@ -341,10 +341,83 @@ async function refreshRecommendTemplates() {
   }
 }
 
+// 监听任务中心的任务列表变化,更新模板解析状态
+watch(
+  () => taskCenterStore.list,
+  async (newList) => {
+    if (!newList || newList.length === 0) return
+    
+    // 遍历推荐模板,更新解析状态
+    for (const tpl of recommendTemplates.value) {
+      if (tpl.baseDocumentId) {
+        const task = newList.find(t => t.documentId === tpl.baseDocumentId)
+        if (task) {
+          tpl.parseStatus = task.status
+          tpl.parseProgress = task.progress || 0
+        }
+      }
+    }
+  },
+  { deep: true }
+)
+
 onMounted(async () => {
   await Promise.all([refreshStats(), refreshRecommendTemplates()])
+  
+  // 如果有解析中的模板,启动轮询刷新状态
+  startParseStatusPolling()
+})
+
+onUnmounted(() => {
+  stopParseStatusPolling()
 })
 
+// 解析状态轮询
+let parseStatusTimer = null
+
+function startParseStatusPolling() {
+  stopParseStatusPolling()
+  
+  parseStatusTimer = setInterval(async () => {
+    // 检查是否有解析中的模板
+    const parsingTemplates = recommendTemplates.value.filter(
+      t => t.parseStatus === 'processing' || t.parseStatus === 'pending'
+    )
+    
+    if (parsingTemplates.length === 0) {
+      stopParseStatusPolling()
+      return
+    }
+    
+    // 更新解析中模板的状态
+    for (const tpl of parsingTemplates) {
+      if (tpl.baseDocumentId) {
+        try {
+          const task = await taskCenterApi.getByDocumentId(tpl.baseDocumentId)
+          if (task) {
+            tpl.parseStatus = task.status
+            tpl.parseProgress = task.progress || 0
+            
+            // 如果解析完成,刷新整个列表
+            if (task.status === 'completed' || task.status === 'failed') {
+              await refreshRecommendTemplates()
+            }
+          }
+        } catch (e) {
+          // 忽略错误
+        }
+      }
+    }
+  }, 3000) // 每3秒轮询一次
+}
+
+function stopParseStatusPolling() {
+  if (parseStatusTimer) {
+    clearInterval(parseStatusTimer)
+    parseStatusTimer = null
+  }
+}
+
 function handleAiSubmit() {
   if (!aiInput.value.trim()) {
     ElMessage.warning('请输入您的需求')