Просмотр исходного кода

Merge branch 'all-lhf' into 'all'

All lhf

See merge request skyeye/skyeye_frontend/skyeye-admin!150
楼航飞 1 год назад
Родитель
Сommit
6fd1bd19ad

+ 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;

+ 22 - 2
src/utils/http/axios/index.ts

@@ -16,7 +16,7 @@ import { setObjToUrlParams } from '@/utils/urlUtils';
 
 import { RequestOptions, Result, CreateAxiosOptions } from './types';
 
-import { useUserStoreWidthOut } from '@/store/modules/user';
+import { useUserStore, useUserStoreWidthOut } from '@/store/modules/user';
 import urlJoin from 'url-join';
 
 const globSetting = useGlobSetting();
@@ -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
@@ -321,3 +323,21 @@ export const http = createAxios();
 //     urlPrefix: 'api',
 //   },
 // });
+
+/** 生成所需要的headers */
+export const getHeaders = () => {
+  const userStore = useUserStore();
+  const { getValue } = useTargetTenantIdSetting();
+  const localTId = getValue();
+  const baseHeaders = {
+    Satoken: userStore.getToken,
+    Tenantid: userStore.getTenantId,
+  };
+  if (localTId) {
+    return {
+      ...baseHeaders,
+      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 };
+}

+ 4 - 11
src/views/cameras/overview/components/BatchImportCamera.vue

@@ -22,7 +22,7 @@
         <el-upload
           ref="upload"
           style="width: 384px; height: 192px; border-radius: 8px"
-          :headers="headers"
+          :headers="getHeaders()"
           :multiple="false"
           :limit="1"
           drag
@@ -103,22 +103,15 @@
 <script setup lang="ts">
   import { computed, ref } from 'vue';
   import axios, { AxiosRequestConfig } from 'axios';
-  import { useUserStore } from '@/store/modules/user';
   import { genFileId, ElMessage } from 'element-plus';
   import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
   import { Close, Document, WarnTriangleFilled, Download } from '@element-plus/icons-vue';
   import { useGlobSetting } from '@/hooks/setting';
   import urlJoin from 'url-join';
+  import { getHeaders } from '@/utils/http/axios';
 
   const emits = defineEmits(['close', 'update']);
 
-  const userStore = useUserStore();
-  const headers = {
-    Satoken: userStore.getToken,
-    Tenantid: userStore.getTenantId,
-    targetTenantId: localStorage.getItem('targetTenantId') || '',
-  };
-
   const upload = ref<UploadInstance>();
   const cardVisible = ref<boolean>(true);
   const isImportEnable = ref<boolean>(true);
@@ -137,7 +130,7 @@
   const handleDownloadSceneCode = async () => {
     try {
       const config: AxiosRequestConfig = {
-        headers,
+        headers: getHeaders(),
         responseType: 'blob',
       };
       const response = await axios.get(
@@ -165,7 +158,7 @@
   const handleDownloadTemplate = async () => {
     try {
       const config: AxiosRequestConfig = {
-        headers,
+        headers: getHeaders(),
         responseType: 'blob',
       };
       const response = await axios.get(

+ 1 - 6
src/views/datamanager/alertformdata/components/common/QuestionFormBase.vue

@@ -182,7 +182,7 @@
   import { IssueState, sourceOptions, issueStateOptionsAdd } from './constant.question';
   import { useIssueType } from '../../hooks/useIssueType';
   import { useWorkLocation } from '../../hooks/useWorkLocation';
-  import { useUserStore } from '@/store/modules/user';
+  import { getHeaders } from '@/utils/http/axios';
   import urlJoin from 'url-join';
   import { useGlobSetting } from '@/hooks/setting';
 
@@ -321,11 +321,6 @@
     });
   };
 
-  // 图片上传
-  const userStore = useUserStore();
-  const getHeaders = () => {
-    return { Satoken: userStore.getToken, Tenantid: userStore.getTenantId };
-  };
   const handleAvatarSuccess = (res) => {
     if (!formData.pictures) formData.pictures = [];
     formData.pictures.push(res.data.url);

+ 2 - 5
src/views/datamanager/alertformdata/components/default/Default.vue

@@ -105,6 +105,7 @@
   import { useUserStore } from '@/store/modules/user';
   import { useGlobSetting } from '@/hooks/setting';
   import urlJoin from 'url-join';
+  import { getHeaders } from '@/utils/http/axios';
 
   const userStore = useUserStore();
   // const { aiOptions, manualOptions, getAIOptions, getManualOptions } = useIssueType();
@@ -112,10 +113,6 @@
   const { aiMainOptions, manualMainOptions, getAIMainOptions, getManualMainOptions } =
     useIssueMainType();
 
-  const headers = {
-    Satoken: userStore.getToken,
-    Tenantid: userStore.getTenantId,
-  };
   const { urlPrefix } = useGlobSetting();
 
   const alertTableRef = ref<typeof AlertTable>();
@@ -182,7 +179,7 @@
       };
 
       const config: AxiosRequestConfig = {
-        headers,
+        headers: getHeaders(),
         responseType: 'blob',
       };
       const response = await axios.post(urlPrefix + '/issue/export', requestBody, config);

+ 1 - 6
src/views/map-config/mini-map/MiniMapConfig.vue

@@ -144,20 +144,15 @@
   import { computed } from 'vue';
   import { Search, Refresh, ArrowLeft } from '@element-plus/icons-vue';
   import KonvaMap from './MapBase/KonvaMap.vue';
-  import { useUserStore } from '@/store/modules/user';
   import useCameraStatus from '@/views/cameras/preview/store/useCameraStatus';
   import { onBeforeRouteLeave } from 'vue-router';
   import urlJoin from 'url-join';
   import { useGlobSetting } from '@/hooks/setting';
+  import { getHeaders } from '@/utils/http/axios';
 
   const cameraStatus = useCameraStatus();
   const { openInterval, closeInterval } = cameraStatus;
 
-  const userStore = useUserStore();
-  const getHeaders = () => {
-    return { Satoken: userStore.getToken, Tenantid: userStore.getTenantId };
-  };
-
   const miniMap = useMiniMap();
   const { scenesTree, shopCameraList, selectedShopCode, selectedShopDetail } = storeToRefs(miniMap);
   const { getScenesTree, getShowCameras, getMapLayout } = miniMap;

+ 1 - 6
src/views/page-config/ConfigEdit.vue

@@ -165,14 +165,9 @@
   import { uploadCompanyLayout, updateCompanyLayout, getCompanyLayoutApi } from '@/api/scene/scene';
   import safeParse from '@/utils/safeParse';
   import { useRouter } from 'vue-router';
-  import { useUserStore } from '@/store/modules/user';
   import urlJoin from 'url-join';
   import { useGlobSetting } from '@/hooks/setting';
-
-  const userStore = useUserStore();
-  const getHeaders = () => {
-    return { Satoken: userStore.getToken, Tenantid: userStore.getTenantId };
-  };
+  import { getHeaders } from '@/utils/http/axios';
 
   const mapEditor = useMapEditor();
   const { bgImg, addedShops, activeShopId, showShops } = storeToRefs(mapEditor);

+ 1 - 6
src/views/page-config/component/ConfigDrawer.vue

@@ -141,12 +141,7 @@
   import { cloneDeep } from 'lodash-es';
   import { useGlobSetting } from '@/hooks/setting';
   import urlJoin from 'url-join';
-  import { useUserStore } from '@/store/modules/user';
-
-  const userStore = useUserStore();
-  const getHeaders = () => {
-    return { Satoken: userStore.getToken, Tenantid: userStore.getTenantId };
-  };
+  import { getHeaders } from '@/utils/http/axios';
 
   const mapEditor = useMapEditor();
   const { addedShops, showShops, activeShopId } = storeToRefs(mapEditor);

+ 4 - 8
src/views/system/user/component/AddUser.vue

@@ -15,7 +15,7 @@
           :limit="1"
           drag
           :action="importUrl"
-          :headers="headers"
+          :headers="getHeaders()"
           :with-credentials="true"
           :auto-upload="false"
           :on-exceed="handleExceed"
@@ -169,6 +169,7 @@
   import axios, { AxiosRequestConfig } from 'axios';
   import { useGlobSetting } from '@/hooks/setting';
   import urlJoin from 'url-join';
+  import { getHeaders } from '@/utils/http/axios';
 
   const userStore = useUserStore();
 
@@ -180,11 +181,6 @@
     cardVisible.value = props.modelValue;
   });
 
-  const headers = {
-    Satoken: userStore.getToken,
-    Tenantid: userStore.getTenantId,
-  };
-
   const cardVisible = ref<boolean>(true);
 
   //对话框
@@ -221,7 +217,7 @@
     //调用后端接口
     try {
       const config: AxiosRequestConfig = {
-        headers,
+        headers: getHeaders(),
         responseType: 'blob',
       };
       const response = await axios.get(
@@ -249,7 +245,7 @@
     //调用后端接口
     try {
       const config: AxiosRequestConfig = {
-        headers,
+        headers: getHeaders(),
         responseType: 'blob',
       };
       const response = await axios.get(urlPrefix + '/user/downloadInfoForm', config);

+ 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');