From 3e8a9220f6d0ee39ec8ee4590002545a9f6de191 Mon Sep 17 00:00:00 2001 From: qsh <> Date: Mon, 19 May 2025 11:26:55 +0800 Subject: [PATCH] sc --- src/permission.js | 14 +++++++++++++- src/store/modules/dict.ts | 2 +- src/utils/auth.ts | 24 ++++++++++++++++-------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/permission.js b/src/permission.js index 9192a5f..dca738f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -7,7 +7,8 @@ import { usePageLoading } from '@/hooks/web/usePageLoading' import { useDictStoreWithOut } from '@/store/modules/dict' import { useUserStoreWithOut } from '@/store/modules/user' import { usePermissionStoreWithOut } from '@/store/modules/permission' -import { getTenantId, getAppId } from '@/utils/auth' +import { useAppStoreWithOut } from '@/store/modules/app' +import { getTenantId, getAppId, setTenantId, setAppId } from '@/utils/auth' import cache from '@/plugins/cache' const { start, done } = useNProgress() @@ -30,6 +31,9 @@ router.beforeEach(async (to, from, next) => { } else { if (getAccessToken()) { if (to.path === '/login') { + if (to.query?.tenantId && to.query?.appId) { + setApp(to.query.tenantId, to.query.appId) + } next({ path: '/' }) } else { // 获取所有字典 @@ -72,6 +76,14 @@ router.beforeEach(async (to, from, next) => { } }) +function setApp(tenantId, appId) { + setTenantId(tenantId) + + setAppId(appId) + const appStore = useAppStoreWithOut() + appStore.setAppInfo(appId) +} + router.afterEach((to) => { useTitle(to?.meta?.title) done() // 结束Progress diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts index 7433c34..50a7ea3 100644 --- a/src/store/modules/dict.ts +++ b/src/store/modules/dict.ts @@ -45,7 +45,7 @@ export const useDictStore = defineStore('dict', { this.dictMap = dictMap this.isSetDict = true } else { - const res = await listSimpleDictData() + const res = (await listSimpleDictData()) || [] // 设置数据 const dictDataMap = new Map() res.forEach((dictData: DictDataVO) => { diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 8ceb741..4b989a1 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -9,26 +9,34 @@ const RefreshTokenKey = 'REFRESH_TOKEN' // 获取token export const getAccessToken = () => { // 此处与TokenKey相同,此写法解决初始化时Cookies中不存在TokenKey报错 - return cache.local.get(AccessTokenKey) - ? cache.local.get(AccessTokenKey) - : cache.local.get('ACCESS_TOKEN') + return localStorage.getItem(AccessTokenKey) + ? localStorage.getItem(AccessTokenKey) + : localStorage.getItem('ACCESS_TOKEN') + // return cache.local.get(AccessTokenKey) + // ? cache.local.get(AccessTokenKey) + // : cache.local.get('ACCESS_TOKEN') } // 刷新token export const getRefreshToken = () => { - return cache.local.get(RefreshTokenKey) + return localStorage.getItem(RefreshTokenKey) + // return cache.local.get(RefreshTokenKey) } // 设置token export const setToken = (token: TokenType) => { - cache.local.set(RefreshTokenKey, token.refreshToken) - cache.local.set(AccessTokenKey, token.accessToken) + localStorage.setItem(AccessTokenKey, token.accessToken) + localStorage.setItem(RefreshTokenKey, token.refreshToken) + // cache.local.set(RefreshTokenKey, token.refreshToken) + // cache.local.set(AccessTokenKey, token.accessToken) } // 删除token export const removeToken = () => { - cache.local.delete(AccessTokenKey) - cache.local.delete(RefreshTokenKey) + localStorage.removeItem(AccessTokenKey) + localStorage.removeItem(RefreshTokenKey) + // cache.local.delete(AccessTokenKey) + // cache.local.delete(RefreshTokenKey) } /** 格式化token(jwt格式) */