Browse Source

请求添加event

zhaoke 1 year ago
parent
commit
a8f87c91e9
1 changed files with 62 additions and 60 deletions
  1. 62 60
      src/utils/axiosReq.ts

+ 62 - 60
src/utils/axiosReq.ts

@@ -1,121 +1,123 @@
-import axios from "axios";
-import router from "@/router";
-import { ElLoading, ElMessage, ElMessageBox } from "element-plus";
-import { getToken, setToken, removeToken } from "@/utils/auth";
-import { AxiosConfigTy, AxiosReqTy, ObjTy } from "~/common";
-import { useUserStore } from "@/store/user";
-let reqConfig: any;
-let loadingE: any;
+import axios from 'axios'
+import router from '@/router'
+import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
+import { getToken, setToken, removeToken } from '@/utils/auth'
+import { AxiosConfigTy, AxiosReqTy, ObjTy } from '~/common'
+import { useUserStore } from '@/store/user'
+import { da } from 'element-plus/es/locale'
+let reqConfig: any
+let loadingE: any
 
-const service: any = axios.create();
+const service: any = axios.create()
 
 // 请求拦截
 service.interceptors.request.use(
   (request: AxiosReqTy) => {
-    const authMap = useUserStore().authMap;
-    const data = request.data!;
+    const authMap = useUserStore().authMap
+    const data = request.data!
     if (data.btnAuth) {
       if (authMap[data.btnAuth]) {
-        request.headers["auth-id"] = authMap[data.btnAuth];
-        sessionStorage.setItem("auth-id", authMap[data.btnAuth]);
+        request.headers['auth-id'] = authMap[data.btnAuth]
+        sessionStorage.setItem('auth-id', authMap[data.btnAuth])
       }
-      delete data.btnAuth;
+      delete data.btnAuth
     } else {
-      const route = router.currentRoute;
+      const route = router.currentRoute
       if (!data.id || data.id !== DATACONTENT_ID.loginId) {
-        const role = route.value.meta?.roles?.[0];
+        const role = route.value.meta?.roles?.[0]
         if (role && authMap[role]) {
-          request.headers["auth-id"] = authMap[role];
+          request.headers['auth-id'] = authMap[role]
         }
       }
     }
     // token setting
-    request.headers["token"] = getToken();
+    request.headers['token'] = getToken()
+    request.headers['event'] = data.event || '0'
     /* download file*/
     if (request.isDownLoadFile) {
-      request.responseType = "blob";
+      request.responseType = 'blob'
     }
     /* upload file*/
     if (request.isUploadFile) {
-      request.headers["Content-Type"] = "application/json";
+      request.headers['Content-Type'] = 'application/json'
     }
-    reqConfig = request;
+    reqConfig = request
     if (request.bfLoading) {
       loadingE = ElLoading.service({
         lock: true,
-        text: "数据载入中",
+        text: '数据载入中',
         // spinner: 'el-icon-ElLoading',
-        background: "rgba(0, 0, 0, 0.1)",
-      });
+        background: 'rgba(0, 0, 0, 0.1)',
+      })
     }
     /*
      *params会拼接到url上
      * */
     if (request.isParams) {
-      request.params = request.data;
-      request.data = {};
+      request.params = request.data
+      request.data = {}
     }
-    return request;
+    return request
   },
   (err: any) => {
-    Promise.reject(err);
+    Promise.reject(err)
   }
-);
+)
 // 响应拦截
 service.interceptors.response.use(
   (response) => {
-    const res = response.data;
+    const res = response.data
     // if the custom code is not 20000, it is judged as an error.
-    if (res.code != "0") {
-      if (res.message && res.message.includes("Duplicate entry")) {
+    if (res.code != '0') {
+      if (res.message && res.message.includes('Duplicate entry')) {
         ElMessage({
-          message: "执行失败",
-          type: "error",
+          message: '执行失败',
+          type: 'error',
           duration: 5 * 1000,
-        });
+        })
       } else {
         ElMessage({
-          message: res.message ?? "执行失败",
-          type: "error",
+          message: res.message ?? '执行失败',
+          type: 'error',
           duration: 5 * 1000,
-        });
+        })
       }
-      return Promise.reject(res.message || res.returnData?.message || "Error");
+      return Promise.reject(res.message || res.returnData?.message || 'Error')
     } else {
-      return res;
+      return res
     }
   },
   (error) => {
-    const des500 = `${error}`.split(" ").includes("500");
+    const des500 = `${error}`.split(' ').includes('500')
     if (des500) {
       ElMessage({
-        message: "身份令牌过期或失效,即将重新登录",
-        type: "error",
+        message: '身份令牌过期或失效,即将重新登录',
+        type: 'error',
         duration: 3 * 1000,
         onClose: async () => {
-          sessionStorage.clear();
-          removeToken(); // must remove  token  first
-          removeToken("codeToken");
-          removeToken("systemSet");
-          removeToken("userid");
-          removeToken("auth-id");
+          sessionStorage.clear()
+          removeToken() // must remove  token  first
+          removeToken('codeToken')
+          removeToken('systemSet')
+          removeToken('userid')
+          removeToken('auth-id')
           // store.dispatch('app/toggleOutflag', false)
           // await store.dispatch('user/resetToken')
-          useUserStore().resetState();
-          router.push(`/login`);
-          location.reload();
+          useUserStore().resetState()
+          router.push(`/login`)
+          location.reload()
         },
-      });
+      })
     } else {
       ElMessage({
         message: error.message,
-        type: "error",
+        type: 'error',
         duration: 5 * 1000,
-      });
+      })
     }
-    return Promise.reject(error);
+    return Promise.reject(error)
   }
-);
+)
 
 export function axiosReq({
   url,
@@ -133,7 +135,7 @@ export function axiosReq({
 }: AxiosConfigTy): any {
   return service({
     url: url,
-    method: method ?? "get",
+    method: method ?? 'get',
     data: data ?? {},
     headers: headers ?? {},
     isParams: isParams ?? false,
@@ -145,7 +147,7 @@ export function axiosReq({
     // baseURL: baseURL ?? import.meta.env.VITE_APP_BASE_URL,
     baseURL: baseURL ?? PLATFROM_CONFIG.baseNewUrl,
     timeout: timeout ?? 15000,
-  });
+  })
 }
 
-export default axiosReq;
+export default axiosReq