Browse Source

feat(请求): 为getHTML添加错误显示控制参数

添加showError参数控制是否显示请求错误信息
修改axios拦截器以支持该参数配置
lsc 2 days ago
parent
commit
9b3c513dfe
3 changed files with 19 additions and 13 deletions
  1. 7 1
      src/services/config.ts
  2. 2 2
      src/services/course.ts
  3. 10 10
      src/views/Student/index.vue

+ 7 - 1
src/services/config.ts

@@ -79,7 +79,7 @@ instance.interceptors.response.use(
     if (response.status >= 200 && response.status < 400) {
       return Promise.resolve(response.data)
     }
-    message.error(response.config.url || "")
+    message.error(response.config.url || '')
     message.error('未知的请求错误!')
     return Promise.reject(response)
   },
@@ -100,6 +100,12 @@ instance.interceptors.response.use(
           fullUrl += '?' + params
         }
       }
+
+      // 检查是否需要显示错误信息
+      const showError = config.showError !== false
+      if (!showError) {
+        return Promise.reject(error)
+      }
     }
 
     if (error && error.response) {

+ 2 - 2
src/services/course.ts

@@ -83,8 +83,8 @@ export const selectWorksStudent = (oid: string, cid: string): Promise<any> => {
  * @param url 目标URL
  * @returns Promise<any>
  */
-export const getHTML = (url: string): Promise<any> => {
-  return axios.get(`${url}`)
+export const getHTML = (url: string, showError: boolean = false): Promise<any> => {
+  return axios.get(`${url}`, { showError })
 }
 
 /**

+ 10 - 10
src/views/Student/index.vue

@@ -1503,16 +1503,16 @@ const processIframeLinks = async () => {
                   // 如果无法获取contentWindow,使用HTML方式
                   let html = null
                   try {
-                      console.log(`getFile2 失败,尝试使用 getHTML:`, error2)
-                      try {
-                        html = await api.getHTML(iframeSrc)
-                        console.log('getHTML 成功获取内容:', html)
-                      }
-                      catch (htmlError) {
-                        console.error('getHTML 也失败:', htmlError)
-                        console.error('无法获取内容: getFile、getFile2 和 getHTML 都失败了')
-                        // throw new Error(`无法获取内容: getFile、getFile2 和 getHTML 都失败了`)
-                      }
+                    console.log(`getFile2 失败,尝试使用 getHTML:`, error2)
+                    try {
+                      html = await api.getHTML(iframeSrc)
+                      console.log('getHTML 成功获取内容:', html)
+                    }
+                    catch (htmlError) {
+                      console.error('getHTML 也失败:', htmlError)
+                      console.error('无法获取内容: getFile、getFile2 和 getHTML 都失败了')
+                      // throw new Error(`无法获取内容: getFile、getFile2 和 getHTML 都失败了`)
+                    }
                   }
                   catch (error) {
                     console.log(`getFile 失败,尝试使用 getFile2:`, error)