gemercheung пре 10 месеци
родитељ
комит
8186c417b7
1 измењених фајлова са 22 додато и 8 уклоњено
  1. 22 8
      src/request/index.ts

+ 22 - 8
src/request/index.ts

@@ -26,7 +26,7 @@ export type AuthHook = () => {
   clear: () => void;
 };
 export const setAuthHook = (hook: AuthHook) => (getAuth = hook);
-let getAuth: AuthHook = () => ({ token: "", userId: "0", clear: () => {} });
+let getAuth: AuthHook = () => ({ token: "", userId: "0", clear: () => { } });
 
 axios.defaults.baseURL = baseURL;
 
@@ -45,14 +45,17 @@ axios.interceptors.request.use(async (config) => {
   }
 
   const { token, userId } = getAuth();
-  if (!token && !~notLoginUrls.indexOf(config.url)) {
-    router.replace({ name: RouteName.login });
-    throw "用户未登录";
-  }
-
   config.headers.token = token;
   config.headers.userid = userId;
 
+  const hasIgnore = config.params ? "ingoreRes" in config.params : false;
+  if (!hasIgnore) {
+    if (!token && !~notLoginUrls.indexOf(config.url)) {
+      router.replace({ name: RouteName.login });
+      throw "用户未登录";
+    }
+  }
+
   if (~GetUrls.indexOf(config.url)) {
     config.method = "GET";
   } else if (~PostUrls.indexOf(config.url)) {
@@ -71,7 +74,15 @@ axios.interceptors.request.use(async (config) => {
     const fromData = new FormData();
 
     Object.keys(config.data).forEach((key) => {
-      fromData.append(key, config.data[key]);
+      if (key === 'files') {
+        Array.from(config.data[key]).forEach(file => {
+          fromData.append('files', file as any as File);
+        })
+      } else {
+        fromData.append(key, config.data[key]);
+      }
+
+
     });
     config.data = fromData;
     config.headers["Content-Type"] = "multipart/form-data";
@@ -83,7 +94,10 @@ axios.interceptors.request.use(async (config) => {
 
 const responseInterceptor = (res: AxiosResponse<any, any>) => {
   closeLoading();
-  if (!successCode.includes(res.data.code)) {
+  const hasIgnore = res.config.params
+    ? "ingoreRes" in res.config.params
+    : false;
+  if (!successCode.includes(res.data.code) && !hasIgnore) {
     let errMsg = res.data.msg || res.data.message;
     openErrorMsg(errMsg);