Forráskód Böngészése

fix: 状态和工具

fan 2 éve
szülő
commit
4a182fb8be

+ 22 - 0
src/store/modules/user.ts

@@ -12,6 +12,7 @@ import {
   EXPRISEIN_KEY,
   REFRESH_TOKEN_KEY,
   PORTAL_KEY,
+  TENANT_KEY,
 } from '/@/enums/cacheEnum';
 import { getAuthCache, setAuthCache } from '/@/utils/auth';
 import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
@@ -27,6 +28,7 @@ import { isArray } from '/@/utils/is';
 import { h } from 'vue';
 import locales from '/@/utils/locales';
 import { useGlobSetting } from '/@/hooks/setting';
+import { sysTenantQueryCurrent } from '@/api/sys/sysTenantApi';
 
 interface UserState {
   userInfo: Nullable<UserInfo>;
@@ -38,6 +40,7 @@ interface UserState {
   loginType: string | undefined;
   expiresIn?: number;
   portalList?: [];
+  tenant?: undefined;
 }
 const globSetting = useGlobSetting();
 
@@ -62,6 +65,8 @@ export const useUserStore = defineStore({
     expiresIn: 0,
     // 门户列表
     portalList: [],
+    // 当前租户信息
+    tenant: undefined,
   }),
   getters: {
     getUserInfo(): UserInfo {
@@ -88,6 +93,9 @@ export const useUserStore = defineStore({
     getExpiresIn(): number {
       return this.expiresIn || Number(getAuthCache<string>(EXPRISEIN_KEY));
     },
+    getTenant(): number {
+      return this.tenant || getAuthCache(TENANT_KEY);
+    },
   },
   actions: {
     setToken(info: string | undefined) {
@@ -122,11 +130,16 @@ export const useUserStore = defineStore({
     setExpiresIn(info: number | undefined) {
       this.expiresIn = info || 0;
     },
+    setTenant(info: number | undefined) {
+      this.tenant = info || {};
+      setAuthCache(TENANT_KEY, info);
+    },
     resetState() {
       this.userInfo = null;
       this.token = '';
       this.roleList = [];
       this.sessionTimeout = false;
+      this.tenant = {};
     },
     /**
      * @description: login
@@ -213,9 +226,18 @@ export const useUserStore = defineStore({
         this.setPortalList(portalList);
         userInfo.portalList = portalList;
       }
+      this.getTenantCurrent();
       this.setUserInfo(userInfo);
       return userInfo;
     },
+    /**
+     * @description 租户信息
+     */
+    async getTenantCurrent() {
+      const tenantInfo = await sysTenantQueryCurrent();
+      console.log('🚀 ~ file: user.ts:237 ~ getTenantCurrent ~ tenantInfo:', tenantInfo);
+      this.setTenant(tenantInfo || {});
+    },
     /**
      * @description 刷新token
      */

+ 2 - 0
src/utils/cache/persistent.ts

@@ -17,6 +17,7 @@ import {
   REFRESH_TOKEN_KEY,
   EXPRISEIN_KEY,
   PORTAL_KEY,
+  TENANT_KEY,
 } from '/@/enums/cacheEnum';
 import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
 import { toRaw } from 'vue';
@@ -33,6 +34,7 @@ interface BasicStore {
   [REFRESH_TOKEN_KEY]: string | number | null | undefined;
   [EXPRISEIN_KEY]: string | number | null | undefined;
   [PORTAL_KEY]: object[];
+  [TENANT_KEY]: object | null | undefined;
 }
 
 type LocalStore = BasicStore;

+ 1 - 0
src/utils/http/axios/index.ts

@@ -75,6 +75,7 @@ const transform: AxiosTransform = {
     if (code == ResultEnum.NO_LOGIN) {
       const now = new Date().getTime();
       const expired = parseInt(userStore.getExpiresIn) - now <= 0;
+      console.log('🚀 ~ file: index.ts:78 ~ transformResponseHook: ~ expired:', expired);
       if (expired) {
         const res = await userStore.refreshToken();
         if (res?.access_token) {

+ 50 - 5
src/utils/index.ts

@@ -3,7 +3,7 @@ import type { App, Plugin } from 'vue';
 
 import { unref } from 'vue';
 import { isObject } from '/@/utils/is';
-import { cloneDeep } from 'lodash-es';
+import { cloneDeep, intersectionWith } from 'lodash-es';
 
 import dayjs from 'dayjs';
 
@@ -142,7 +142,22 @@ export function formatDictValue(options: Array<any>, value: string) {
   return matchItem && matchItem['label'] ? matchItem['label'] : '';
 }
 
-// 获取字典颜色
+// 获取字典值
+export function formatDictLabel(options: Array<any>, label: string) {
+  if (!label) {
+    if (label != '0') {
+      return '';
+    }
+  }
+  if (!options) {
+    return '';
+  }
+  let matchItem = '';
+  matchItem = options.find(item => (item['label'] as string) == label);
+  return matchItem && matchItem['value'] ? matchItem['value'] : '';
+}
+
+// 获取字典背景颜色
 export function formatDictColor(options: Array<any>, value: string) {
   if (!value) {
     if (value != '0') {
@@ -154,7 +169,37 @@ export function formatDictColor(options: Array<any>, value: string) {
   }
   let matchItem = '';
   matchItem = options.find(item => item['value'] == value);
-  return matchItem && matchItem['color'] ? matchItem['color'] : '';
+  return matchItem && matchItem['bgColor'] ? matchItem['bgColor'] : '';
+}
+
+// 获取字典字体颜色
+export function formatDictFontColor(options: Array<any>, value: string) {
+  if (!value) {
+    if (value != '0') {
+      return '';
+    }
+  }
+  if (!options) {
+    return '';
+  }
+  let matchItem = '';
+  matchItem = options.find(item => item['value'] == value);
+  return matchItem && matchItem['fontColor'] ? matchItem['fontColor'] : '';
+}
+
+// 获取字典数组
+export function formatDictTags(options: Array<any>, value: Array<any>) {
+  if (!value.length) {
+    return [];
+  }
+  if (!options.length) {
+    return [];
+  }
+  const matchItem =
+    intersectionWith(options, value, (arrVal, othVal) => {
+      return arrVal.value == othVal.value;
+    }) || [];
+  return matchItem;
 }
 
 // 转换字典
@@ -210,12 +255,12 @@ export function dateRanges(format = 'YYYY-MM-DD') {
   console.log('🚀 ~ file: index.ts:210 ~ dateRanges ~ format:', format);
   return {
     今天: [dayjs(), dayjs()],
-    本周: [dayjs(), dayjs().endOf('week')],
+    本周: [dayjs().startOf('week'), dayjs().endOf('week')],
     上周: [
       dayjs().add(-1, 'week').startOf('week').add(1, 'day'),
       dayjs().add(-1, 'week').endOf('week').add(1, 'day'),
     ],
-    本月: [dayjs(), dayjs().endOf('month')],
+    本月: [dayjs().startOf('month'), dayjs().endOf('month')],
     上月: [dayjs().add(-1, 'month').startOf('month'), dayjs().add(-1, 'month').endOf('month')],
     // Today: [dayjs(), dayjs()],
     // 'This Month': [dayjs(), dayjs().endOf('month')],