Browse Source

fix(Student): 添加对iframe内容获取失败的备用处理方案

当api.getHTML失败时,尝试使用getFile作为备用方案获取iframe内容。如果两者都失败,则抛出错误提示。
lsc 2 months ago
parent
commit
b59b09858a
1 changed files with 22 additions and 3 deletions
  1. 22 3
      src/views/Student/index.vue

+ 22 - 3
src/views/Student/index.vue

@@ -1440,9 +1440,28 @@ const processIframeLinks = async () => {
                   }
                   }
                   
                   
                   // 如果无法获取contentWindow,使用HTML方式
                   // 如果无法获取contentWindow,使用HTML方式
-                  const html = await api.getHTML(iframeSrc)
-                  // const html = await api.getHTML('https://knowledge.cocorobo.cn/zh-CN/story-telling/a7fa08b8-cf60-11ef-93e3-12e77c4cb76b')
-                  console.log('html', html)
+                  let html = null
+                  try {
+                    html = await api.getHTML(iframeSrc)
+                    // const html = await api.getHTML('https://knowledge.cocorobo.cn/zh-CN/story-telling/a7fa08b8-cf60-11ef-93e3-12e77c4cb76b')
+                    console.log('html', html)
+                  }
+                  catch (error) {
+                    console.log(`getHTML 失败,尝试使用 getFile:`, error)
+                    try {
+                      const fileData = await getFile(iframeSrc)
+                      if (fileData && fileData.data) {
+                        // 将 ArrayBuffer 转为字符串
+                        const uint8Array = new Uint8Array(fileData.data)
+                        html = new TextDecoder('utf-8').decode(uint8Array)
+                        console.log('getFile 成功获取内容:', html)
+                      }
+                    }
+                    catch (fileError) {
+                      console.error('getFile 也失败:', fileError)
+                      throw new Error(`无法获取内容: getHTML 和 getFile 都失败了`)
+                    }
+                  }
                   console.log(`处理幻灯片 ${slideIndex + 1} 中的iframe链接:`, iframeSrc)
                   console.log(`处理幻灯片 ${slideIndex + 1} 中的iframe链接:`, iframeSrc)
                   return {
                   return {
                     ...element,
                     ...element,