Browse Source

通用请求类工具新增用户类型请求异常拦截

yangxiao 3 years ago
parent
commit
6da098944b
1 changed files with 7 additions and 5 deletions
  1. 7 5
      src/api/axios.js

+ 7 - 5
src/api/axios.js

@@ -69,15 +69,17 @@ export function requestData (options) {
       if (options.showLoading) {
         store.state.loading = false;
       }
-      if (response.code === 200) {
-        options.success && options.success(resolve(response));
-      } else {
+      if (response.code === 501) { // 用户类请求错误code (账号密码错误、用户锁定、token过期等)
         localStorage.removeItem('authToken');
-        Message.error((response.data && response.data.msg) || ("请求出错[" + response.data.code + "]"));
+        Message.error(response.data.msg);
         setTimeout(() => {
-          // window.location.reload();
+          window.location.reload();
           // window.__STATICVUE__.$router.replace('/login');
         }, 1000);
+      } else if (response.code === 200) { // 请求成功 code
+        options.success && options.success(resolve(response));
+      } else { // 其他code
+        Message.error((response.data && response.data.msg) || ("请求出错[" + response.data.code + "]"));
       }
     }).catch(error => {
       if (options.showLoading) {