Преглед на файлове

feat: add cancel tag (isCancel) to abnormal response

Casper Dai преди 3 години
родител
ревизия
13a797e119
променени са 4 файла, в които са добавени 23 реда и са изтрити 15 реда
  1. 6 4
      src/api/calendar.js
  2. 3 2
      src/api/program.js
  3. 4 3
      src/utils/request.js
  4. 10 6
      src/views/dashboard/components/Device.vue

+ 6 - 4
src/api/calendar.js

@@ -24,10 +24,11 @@ export function getSchedules (query) {
   })
 }
 
-export function getSchedule (id) {
+export function getSchedule (id, options) {
   return request({
     url: `/content/calendar/${id}`,
-    method: 'get'
+    method: 'get',
+    ...options
   }).then(({ data }) => {
     const { id, type, name, resolutionRatio, eventDetail } = data
     return { id, type, name, resolutionRatio, programs: eventDetail }
@@ -167,9 +168,10 @@ export function rejectRelease ({ id, name }, remark) {
   }, name)
 }
 
-export function getTimeline (deviceId) {
+export function getTimeline (deviceId, options) {
   return request({
     url: `/content/deviceCalender/${deviceId}`,
-    method: 'get'
+    method: 'get',
+    ...options
   })
 }

+ 3 - 2
src/api/program.js

@@ -69,10 +69,11 @@ export function updateProgramName (data) {
   })
 }
 
-export function getProgram (id) {
+export function getProgram (id, options) {
   return request({
     url: `/item/getById/${id}`,
-    method: 'get'
+    method: 'get',
+    ...options
   })
 }
 

+ 4 - 3
src/utils/request.js

@@ -73,6 +73,7 @@ service.interceptors.response.use(
     return Promise.reject(res)
   },
   error => {
+    const isCancel = axios.isCancel(error)
     const { response, config } = error
     if (response) {
       const { status } = response
@@ -101,11 +102,11 @@ service.interceptors.response.use(
           message: errMessage || '请求异常'
         })
       }
-      return Promise.reject(response.data)
+      return Promise.reject({ isCancel, ...response.data })
     }
 
     let { message } = error
-    if (!config?.custom && !axios.isCancel(error)) {
+    if (!config?.custom && !isCancel) {
       if (message) {
         if (/timeout/.test(message)) {
           message = '请求超时'
@@ -118,7 +119,7 @@ service.interceptors.response.use(
         message
       })
     }
-    return Promise.reject({ errCode: -1, errMessage: message })
+    return Promise.reject({ errCode: -1, errMessage: message, isCancel })
   }
 )
 

+ 10 - 6
src/views/dashboard/components/Device.vue

@@ -341,11 +341,13 @@ export default {
     },
     getTimeline () {
       this.loadingTimeline = true
-      getTimeline(this.device.id).then(({ data }) => {
+      getTimeline(this.device.id, { custom: true }).then(({ data }) => {
         this.timeline = (JSON.parse(data.eventDetail) || []).map(this.createItem)
         this.checkTimeline()
-      }).catch(() => {
-        this.$timer = setTimeout(this.getTimeline, 2000)
+      }).catch(({ isCancel }) => {
+        if (!isCancel) {
+          this.$timer = setTimeout(this.getTimeline, 2000)
+        }
       })
     },
     checkTimeline () {
@@ -374,7 +376,7 @@ export default {
         if (item.name) {
           return Promise.resolve(item.name)
         }
-        return (item.type === 1 ? getProgram : getSchedule)(item.id).then(({ name }) => name)
+        return (item.type === 1 ? getProgram : getSchedule)(item.id, { custom: true }).then(({ name }) => name)
       }
       return Promise.resolve('未知')
     },
@@ -383,8 +385,10 @@ export default {
         this.getName(this.current).then(name => {
           this.current.name = name
           this.finishTimeline()
-        }).catch(() => {
-          this.$timer = setTimeout(this.getDetail, 2000)
+        }).catch(({ isCancel }) => {
+          if (!isCancel) {
+            this.$timer = setTimeout(this.getDetail, 2000)
+          }
         })
       } else {
         this.finishTimeline()