Purpose 4 месяцев назад
Родитель
Сommit
180da5f180
2 измененных файлов с 21 добавлено и 2 удалено
  1. 10 1
      src/utils/api.js
  2. 11 1
      src/utils/apiCreator.js

+ 10 - 1
src/utils/api.js

@@ -1,4 +1,13 @@
 import apiCreator from './apiCreator'
 import Constant from './Constant'
 
-export const api = apiCreator(Constant.REQUEST_PREFIX)
+import Vue from 'vue'
+
+export const api = apiCreator(Constant.REQUEST_PREFIX, (errorMsg) => {
+  Vue.prototype.$notify({
+    title: '失败',
+    message: errorMsg,
+    type: 'error',
+    position: 'bottom-right'
+  })
+})

+ 11 - 1
src/utils/apiCreator.js

@@ -14,7 +14,7 @@ export class AjaxError extends Error {
 // const RESPONSE_ERROR = 'ResponseError'
 const REQUEST_CANCEL = 'RequestCancel'
 
-export default (baseApiPath) => {
+export default (baseApiPath, onError) => {
   const link = document.createElement('a')
 
   const request = axios.create({
@@ -62,6 +62,16 @@ export default (baseApiPath) => {
       return
     }
 
+    if (typeof onError === 'function') {
+      const { response } = error
+      if (response) {
+        const { data } = response
+        if (data && data.type === 'failure') {
+          onError(data.message)
+        }
+      }
+    }
+
     if (!(error instanceof AjaxError)) {
       error = new AjaxError('服务器正忙', 500)
     }