ソースを参照

refactor: targetTenantId设置和获取统一从useTargetTanantIdSetting走

louhangfei 1 年間 前
コミット
44cd5cb31e

+ 5 - 2
src/layout/components/Header/SwitchTenant.vue

@@ -17,13 +17,16 @@
   import { ref, computed, onMounted } from 'vue';
   import { useUserStore } from '@/store/modules/user';
   import { getListUseZongbu } from '@/api/tenant';
+  import { useTargetTenantIdSetting } from '@/utils/useTargetTenantIdSetting';
 
   interface TenantOption {
     label: string;
     value: string;
   }
 
-  const localTId = window.localStorage.getItem('targetTenantId');
+  const { setValue, getValue } = useTargetTenantIdSetting();
+
+  const localTId = getValue();
 
   const currentTenant = ref(localTId ? Number(localTId) : '');
 
@@ -43,7 +46,7 @@
   });
 
   const handleChange = (targetTenantId: string) => {
-    window.localStorage.setItem('targetTenantId', targetTenantId);
+    setValue(targetTenantId);
     currentTenant.value = targetTenantId;
     window.location.reload();
   };

+ 18 - 4
src/store/modules/user.ts

@@ -12,11 +12,25 @@ import { useGlobSetting } from '@/hooks/setting';
 const { tenantCode } = useGlobSetting();
 export interface IUserState {
   token: string;
-  username: string;
+  // username: string;
   welcome: string;
   avatar: string;
   permissions: any[];
-  info: any;
+  info: {
+    userId: number;
+    username: string;
+    realName: string;
+    avatar: string;
+    desc: string;
+    token: string;
+    roleType: string;
+    nickname: string;
+    mobile: string;
+    email: string;
+    remark: string;
+    tenantName: string;
+    tenantCode: string;
+  };
   tenantId: number;
 }
 
@@ -25,7 +39,7 @@ export const useUserStore = defineStore({
   state: (): IUserState => ({
     token: Storage.get(ACCESS_TOKEN, ''),
     tenantId: Storage.get(TENANT_TOKEN, ''),
-    username: '',
+    // username: '',
     welcome: '',
     avatar: '',
     permissions: [],
@@ -42,7 +56,7 @@ export const useUserStore = defineStore({
       return this.avatar;
     },
     getNickname(): string {
-      return this.username;
+      return this.info.username;
     },
     getPermissions(): [any][] {
       return this.permissions;

+ 6 - 3
src/utils/http/axios/index.ts

@@ -25,6 +25,7 @@ const urlPrefix = globSetting.urlPrefix || '';
 import router from '@/router';
 import { storage } from '@/utils/Storage';
 import { getRedirectUrl } from '@/utils/getRedirectUrl';
+import { useTargetTenantIdSetting } from '@/utils/useTargetTenantIdSetting';
 
 /**
  * @description: 数据处理,方便区分多种处理方式
@@ -205,9 +206,10 @@ const transform: AxiosTransform = {
   requestInterceptors: (config, options) => {
     // 请求之前处理config
     const userStore = useUserStoreWidthOut();
+    const { getValue } = useTargetTenantIdSetting();
     const token = userStore.getToken;
     const tenantId = userStore.getTenantId;
-    const targetTenantId = localStorage.getItem('targetTenantId');
+    const targetTenantId = getValue();
     if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
       // jwt token
       (config as Recordable).headers.satoken = options.authenticationScheme
@@ -325,7 +327,8 @@ export const http = createAxios();
 /** 生成所需要的headers */
 export const getHeaders = () => {
   const userStore = useUserStore();
-  const localTId = localStorage.getItem('targetTenantId');
+  const { getValue } = useTargetTenantIdSetting();
+  const localTId = getValue();
   const baseHeaders = {
     Satoken: userStore.getToken,
     Tenantid: userStore.getTenantId,
@@ -333,7 +336,7 @@ export const getHeaders = () => {
   if (localTId) {
     return {
       ...baseHeaders,
-      targetTenantId: localStorage.getItem('targetTenantId') || '',
+      targetTenantId: localTId,
     };
   }
   return baseHeaders;

+ 35 - 0
src/utils/useTargetTenantIdSetting.ts

@@ -0,0 +1,35 @@
+import { useUserStore } from '@/store/modules/user';
+import { watch } from 'vue';
+
+/** 设置targetTenantId */
+export function useTargetTenantIdSetting() {
+  let fullKey = '';
+  const userStore = useUserStore();
+
+  watch(
+    () => [userStore.info.username, userStore.info.tenantCode],
+    ([username, tenantCode]) => {
+      fullKey = `${tenantCode || ''}_${username}_targetTenantId`;
+      console.log('fullKey', fullKey);
+    },
+    { immediate: true },
+  );
+
+  function setValue(tenantId: string | string) {
+    if (!fullKey) {
+      console.error('请先设置targetTenantId');
+      return;
+    }
+    localStorage.setItem(fullKey, String(tenantId));
+  }
+
+  function getValue() {
+    if (!fullKey) {
+      console.error('请先设置targetTenantId');
+      return;
+    }
+    return localStorage.getItem(fullKey);
+  }
+
+  return { setValue, getValue };
+}

+ 1 - 1
utils/devProxy/zongbu/proxy.ts

@@ -30,6 +30,6 @@ const proxyPrd: PROXY_TYPE = {
 };
 
 // 对外导出的代理
-export const proxy = proxyStaff;
+export const proxy = proxyPrd;
 
 export const appConfigPath = path.resolve(__dirname, 'app.config.js');