lsc 1 天之前
父節點
當前提交
2e0af57466
共有 1 個文件被更改,包括 95 次插入11 次删除
  1. 95 11
      src/views/Student/index.vue

+ 95 - 11
src/views/Student/index.vue

@@ -239,6 +239,31 @@ const visibleAI = ref(false)
 // 作业区收缩状态
 const workPanelCollapsed = ref(false)
 
+// 定时器相关
+const workTimer = ref<number | null>(null)
+const workUpdateInterval = 5000 // 5秒更新一次
+
+// 启动作业更新定时器
+const startWorkTimer = () => {
+  if (workTimer.value) {
+    clearInterval(workTimer.value)
+  }
+  workTimer.value = setInterval(() => {
+    console.log('定时器触发,检查作业更新')
+    getWork(true) // 传入 true 表示是更新模式
+  }, workUpdateInterval)
+  console.log('作业更新定时器已启动,间隔:', workUpdateInterval, 'ms')
+}
+
+// 停止作业更新定时器
+const stopWorkTimer = () => {
+  if (workTimer.value) {
+    clearInterval(workTimer.value)
+    workTimer.value = null
+    console.log('作业更新定时器已停止')
+  }
+}
+
 // 收缩/展开后重新计算中间画布尺寸(在 DOM 更新并完成过渡后)
 watch(() => workPanelCollapsed.value, async () => {
   // 等待本次 DOM 更新
@@ -400,12 +425,23 @@ const nextSlide = () => {
   }
 }
 
-// 监听slideIndex变化,调用getWork
+// 监听slideIndex变化,调用getWork并管理定时器
 watch(() => slideIndex.value, (newIndex, oldIndex) => {
   console.log('slideIndex变化,调用getWork', { newIndex, oldIndex })
-  if (newIndex !== oldIndex && typeof newIndex === 'number' && currentSlideHasIframe.value) {
-    console.log('触发getWork,当前幻灯片索引:', newIndex)
-    getWork()
+  if (newIndex !== oldIndex && typeof newIndex === 'number') {
+    // 检查新页面是否有iframe
+    const hasIframe = currentSlideHasIframe.value
+    
+    if (hasIframe) {
+      console.log('当前页面有iframe,启动作业更新定时器')
+      startWorkTimer()
+      console.log('触发getWork,当前幻灯片索引:', newIndex)
+      getWork()
+    }
+    else {
+      console.log('当前页面无iframe,停止作业更新定时器')
+      stopWorkTimer()
+    }
   }
 }, { immediate: false, deep: false })
 
@@ -1028,18 +1064,21 @@ const getCourseDetail = async () => {
   }
 }
 
-const getWork = async () => {
+const getWork = async (isUpdate = false) => {
   try {
-    workLoading.value = true
+    if (!isUpdate) {
+      workLoading.value = true
+    }
     console.log('getWork 开始执行,参数:', {
       courseid: props.courseid,
       slideIndex: slideIndex.value,
-      type: props.type
+      type: props.type,
+      isUpdate
     })
     
     if (!props.courseid) {
       console.warn('getWork: courseid 未提供,跳过执行')
-      workLoading.value = false
+      if (!isUpdate) workLoading.value = false
       return
     }
     
@@ -1048,22 +1087,59 @@ const getWork = async () => {
     const frame = elementList.value.find(element => element.type === ElementTypes.FRAME)
     console.log('frame:', frame)
     const toolType = frame?.toolType ?? ''
-    workArray.value = props.cid
+    const newWorkArray = props.cid
       ? res[0].filter((work: any) => {
         return work.type === 1 || (work.type === 2 && work.classid.includes(props.cid)) && (work.atool === toolType || !toolType)
       })
       : res[0]
+    
+    // 如果是更新模式,只有当数据真正变化时才更新
+    if (isUpdate) {
+      const hasChanged = checkWorkArrayChanged(workArray.value, newWorkArray)
+      if (hasChanged) {
+        console.log('检测到作业数据变化,更新显示')
+        workArray.value = newWorkArray
+      }
+      else {
+        console.log('作业数据无变化,跳过更新')
+      }
+    }
+    else {
+      workArray.value = newWorkArray
+    }
+    
     console.log('getWork 执行成功,结果:', workArray.value)
   }
   catch (error) {
     console.error('getWork 执行失败:', error)
-    message.error('获取作业信息失败')
+    if (!isUpdate) {
+      message.error('获取作业信息失败')
+    }
   }
   finally {
-    workLoading.value = false
+    if (!isUpdate) {
+      workLoading.value = false
+    }
   }
 }
 
+// 检查作业数组是否发生变化
+const checkWorkArrayChanged = (oldArray: WorkItem[], newArray: WorkItem[]): boolean => {
+  if (oldArray.length !== newArray.length) return true
+  
+  // 检查每个作业的 id 和 name 是否一致
+  for (let i = 0; i < oldArray.length; i++) {
+    const oldWork = oldArray[i]
+    const newWork = newArray[i]
+    
+    if (oldWork.id !== newWork.id || oldWork.name !== newWork.name) {
+      return true
+    }
+  }
+  
+  return false
+}
+
 onMounted(() => {
   document.addEventListener('keydown', handleKeydown)
 
@@ -1086,6 +1162,11 @@ onMounted(() => {
   getCourseDetail()
   getWork()
 
+  // 只有当当前页面存在iframe时才启动作业更新定时器
+  if (currentSlideHasIframe.value) {
+    startWorkTimer()
+  }
+
   // 计算初始缩放比例
   nextTick(() => {
     calculateScale()
@@ -1139,6 +1220,9 @@ onUnmounted(() => {
   // 移除视口尺寸更新事件监听器
   window.removeEventListener('viewportSizeUpdated', handleViewportSizeUpdated)
 
+  // 停止作业更新定时器
+  stopWorkTimer()
+
   // 清理window上的引用
   if ((window as any).PPTistStudent) {
     delete (window as any).PPTistStudent