Quellcode durchsuchen

21. 优化多租户的传递逻辑

YunaiV vor 2 Jahren
Ursprung
Commit
2242bc3069
2 geänderte Dateien mit 10 neuen und 4 gelöschten Zeilen
  1. 6 3
      src/api/axios.ts
  2. 4 1
      src/api/http.ts

+ 6 - 3
src/api/axios.ts

@@ -27,9 +27,12 @@ axiosInstance.interceptors.request.use(
   (config: AxiosRequestConfig) => {
     // 获取 tenantId
     const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
-    config.headers = {
-      ...config.headers,
-      'tenant-id': info ? info[SystemStoreEnum.TENANT_INFO]['tenantId'] : undefined
+    const tenantId = info ? info[SystemStoreEnum.TENANT_INFO]['tenantId'] : undefined
+    if (tenantId) {
+      config.headers = {
+        ...config.headers,
+        'tenant-id': tenantId
+      }
     }
 
     // 白名单校验

+ 4 - 1
src/api/http.ts

@@ -121,7 +121,10 @@ export const appendTokenAndTenant = (headers: RequestParamsObjType, requestUrl:
     return headers;
   }
   // ① 获取 tenantId
-  headers['tenant-id'] = info[SystemStoreEnum.TENANT_INFO]['tenantId']
+  const tenantId = info ? info[SystemStoreEnum.TENANT_INFO]['tenantId'] : undefined
+  if (tenantId) {
+    headers['tenant-id'] = tenantId
+  }
   // ② 获取 token
   const userInfo = info[SystemStoreEnum.USER_INFO]
   if (!userInfo) {