Browse Source

开启接口500报错

lph 1 year ago
parent
commit
6305ad5101
1 changed files with 57 additions and 57 deletions
  1. 57 57
      zkqy-ui/src/utils/request.js

+ 57 - 57
zkqy-ui/src/utils/request.js

@@ -1,16 +1,16 @@
 import axios from 'axios'
 import axios from 'axios'
-import {Notification, MessageBox, Message, Loading} from 'element-ui'
+import { Notification, MessageBox, Message, Loading } from 'element-ui'
 import store from '@/store'
 import store from '@/store'
-import {getToken} from '@/utils/auth'
+import { getToken } from '@/utils/auth'
 import errorCode from '@/utils/errorCode'
 import errorCode from '@/utils/errorCode'
-import {tansParams, blobValidate} from "@/utils/zkqy";
+import { tansParams, blobValidate } from "@/utils/zkqy";
 import cache from '@/plugins/cache'
 import cache from '@/plugins/cache'
-import {saveAs} from 'file-saver'
-import {throttle} from '@/utils/index'
+import { saveAs } from 'file-saver'
+import { throttle } from '@/utils/index'
 
 
 let downloadLoadingInstance;
 let downloadLoadingInstance;
 // 是否显示重新登录
 // 是否显示重新登录
-export let isRelogin = {show: false};
+export let isRelogin = { show: false };
 
 
 
 
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
@@ -69,70 +69,70 @@ service.interceptors.request.use(config => {
 
 
 // 响应拦截器
 // 响应拦截器
 service.interceptors.response.use(res => {
 service.interceptors.response.use(res => {
-    // 未设置状态码则默认成功状态
-    const code = res.data.code || 200;
-    // 获取错误信息
-    const msg = errorCode[code] || res.data.msg || errorCode['default']
-    // 二进制数据则直接返回
-    if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
-      return res.data
-    }
-    if (code === 401) {
-      if (!isRelogin.show) {
-        isRelogin.show = true;
-        MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
-          confirmButtonText: '重新登录',
-          cancelButtonText: '取消',
-          type: 'warning'
-        }).then(() => {
-          isRelogin.show = false;
-          store.dispatch('LogOut')
-          // .then(() => {
-          //   location.href = '/index';
-          // })
-        }).catch(() => {
-          isRelogin.show = false;
-        });
-      }
-      return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
-    } else if (code === 500) {
-      let resUrl = res.config.url;
-      if (resUrl == '/login') {
-        Message({message: msg, type: 'error'})
-        return Promise.reject(new Error(msg))
-      } else {
-        return Promise.resolve()
-      }
-
-    } else if (code === 601) {
-      Message({message: msg, type: 'warning'})
-      return Promise.reject('error')
-    } else if (code == 602) {
-      if (store.state.user.name == 'admin') return
-      MessageBox.confirm('数据源信息异常!请您联系管理员或尝试重新登录系统。', '系统提示', {
+  // 未设置状态码则默认成功状态
+  const code = res.data.code || 200;
+  // 获取错误信息
+  const msg = errorCode[code] || res.data.msg || errorCode['default']
+  // 二进制数据则直接返回
+  if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
+    return res.data
+  }
+  if (code === 401) {
+    if (!isRelogin.show) {
+      isRelogin.show = true;
+      MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
         confirmButtonText: '重新登录',
         confirmButtonText: '重新登录',
         cancelButtonText: '取消',
         cancelButtonText: '取消',
         type: 'warning'
         type: 'warning'
       }).then(() => {
       }).then(() => {
         isRelogin.show = false;
         isRelogin.show = false;
         store.dispatch('LogOut')
         store.dispatch('LogOut')
-        //   .then(() => {
+        // .then(() => {
         //   location.href = '/index';
         //   location.href = '/index';
         // })
         // })
       }).catch(() => {
       }).catch(() => {
         isRelogin.show = false;
         isRelogin.show = false;
       });
       });
-      return Promise.reject('数据源信息异常!请您联系管理员或尝试重新登录系统。')
-    } else if (code !== 200) {
-      Notification.error({title: msg})
-      return Promise.reject('error')
+    }
+    return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
+  } else if (code === 500) {
+    let resUrl = res.config.url;
+    if (resUrl == '/login') {
+      Message({ message: msg, type: 'error' })
+      return Promise.reject(new Error(msg))
     } else {
     } else {
-      return res.data
+      return Promise.reject('code:500')
     }
     }
-  },
+
+  } else if (code === 601) {
+    Message({ message: msg, type: 'warning' })
+    return Promise.reject('error')
+  } else if (code == 602) {
+    if (store.state.user.name == 'admin') return
+    MessageBox.confirm('数据源信息异常!请您联系管理员或尝试重新登录系统。', '系统提示', {
+      confirmButtonText: '重新登录',
+      cancelButtonText: '取消',
+      type: 'warning'
+    }).then(() => {
+      isRelogin.show = false;
+      store.dispatch('LogOut')
+      //   .then(() => {
+      //   location.href = '/index';
+      // })
+    }).catch(() => {
+      isRelogin.show = false;
+    });
+    return Promise.reject('数据源信息异常!请您联系管理员或尝试重新登录系统。')
+  } else if (code !== 200) {
+    Notification.error({ title: msg })
+    return Promise.reject('error')
+  } else {
+    return res.data
+  }
+},
   error => {
   error => {
     console.log('err' + error)
     console.log('err' + error)
-    let {message} = error;
+    let { message } = error;
     if (message == "Network Error") {
     if (message == "Network Error") {
       message = "后端接口连接异常";
       message = "后端接口连接异常";
     } else if (message.includes("timeout")) {
     } else if (message.includes("timeout")) {
@@ -140,7 +140,7 @@ service.interceptors.response.use(res => {
     } else if (message.includes("Request failed with status code")) {
     } else if (message.includes("Request failed with status code")) {
       message = "系统接口" + message.substr(message.length - 3) + "异常";
       message = "系统接口" + message.substr(message.length - 3) + "异常";
     }
     }
-    Message({message: message, type: 'error', duration: 5 * 1000})
+    Message({ message: message, type: 'error', duration: 5 * 1000 })
     return Promise.reject(error)
     return Promise.reject(error)
   }
   }
 )
 )
@@ -156,7 +156,7 @@ export function download(url, params, filename, config) {
     transformRequest: [(params) => {
     transformRequest: [(params) => {
       return tansParams(params)
       return tansParams(params)
     }],
     }],
-    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
+    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
     responseType: 'blob',
     responseType: 'blob',
     ...config
     ...config
   }).then(async (data) => {
   }).then(async (data) => {