From 8c4c54a56f99f2f9a28cf7b949690ad9c31308ef Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 29 Feb 2024 22:42:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=B0=E4=BD=8F=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 1bc4eefeabf58c3a40c7205cd6ee4b53ea48ce49) --- src/hooks/web/useCache.ts | 12 ++++++--- src/store/modules/user.ts | 13 ---------- src/utils/auth.ts | 33 +++++------------------- src/views/Login/components/LoginForm.vue | 8 +++--- 4 files changed, 19 insertions(+), 47 deletions(-) diff --git a/src/hooks/web/useCache.ts b/src/hooks/web/useCache.ts index 2850bd5c..4f39f307 100644 --- a/src/hooks/web/useCache.ts +++ b/src/hooks/web/useCache.ts @@ -7,13 +7,18 @@ import WebStorageCache from 'web-storage-cache' type CacheType = 'localStorage' | 'sessionStorage' export const CACHE_KEY = { - IS_DARK: 'isDark', + // 用户相关 + ROLE_ROUTERS: 'roleRouters', USER: 'user', + // 系统设置 + IS_DARK: 'isDark', LANG: 'lang', THEME: 'theme', LAYOUT: 'layout', - ROLE_ROUTERS: 'roleRouters', - DICT_CACHE: 'dictCache' + DICT_CACHE: 'dictCache', + // 登录表单 + LoginForm: 'loginForm', + TenantId: 'tenantId' } export const useCache = (type: CacheType = 'localStorage') => { @@ -30,4 +35,5 @@ export const deleteUserCache = () => { const { wsCache } = useCache() wsCache.delete(CACHE_KEY.USER) wsCache.delete(CACHE_KEY.ROLE_ROUTERS) + // 注意,不要清理 LoginForm 登录表单 } diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 2f225228..019722e4 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -13,20 +13,12 @@ interface UserVO { deptId: number } -interface RememberMeInfo { - enable: boolean // 是否记住我 - username: string - password: string -} - interface UserInfoVO { // USER 缓存 permissions: string[] roles: string[] isSetUser: boolean user: UserVO - // REMEMBER_ME 缓存 - rememberMe: RememberMeInfo } export const useUserStore = defineStore('admin-user', { @@ -39,11 +31,6 @@ export const useUserStore = defineStore('admin-user', { avatar: '', nickname: '', deptId: 0 - }, - rememberMe: { - enable: true, - username: '', - password: '' } }), getters: { diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 3ac66e60..c68a67a9 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,4 +1,4 @@ -import { useCache } from '@/hooks/web/useCache' +import { useCache, CACHE_KEY } from '@/hooks/web/useCache' import { TokenType } from '@/api/login/types' import { decrypt, encrypt } from '@/utils/jsencrypt' @@ -36,8 +36,6 @@ export const formatToken = (token: string): string => { } // ========== 账号相关 ========== -const LoginFormKey = 'LOGINFORM' - export type LoginFormType = { tenantName: string username: string @@ -46,7 +44,7 @@ export type LoginFormType = { } export const getLoginForm = () => { - const loginForm: LoginFormType = wsCache.get(LoginFormKey) + const loginForm: LoginFormType = wsCache.get(CACHE_KEY.LoginForm) if (loginForm) { loginForm.password = decrypt(loginForm.password) as string } @@ -55,38 +53,19 @@ export const getLoginForm = () => { export const setLoginForm = (loginForm: LoginFormType) => { loginForm.password = encrypt(loginForm.password) as string - wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 }) + wsCache.set(CACHE_KEY.LoginForm, loginForm, { exp: 30 * 24 * 60 * 60 }) } export const removeLoginForm = () => { - wsCache.delete(LoginFormKey) + wsCache.delete(CACHE_KEY.LoginForm) } // ========== 租户相关 ========== -const TenantIdKey = 'TENANT_ID' -const TenantNameKey = 'TENANT_NAME' - -export const getTenantName = () => { - return wsCache.get(TenantNameKey) -} - -export const setTenantName = (tenantName: string) => { - wsCache.set(TenantNameKey, tenantName, { exp: 30 * 24 * 60 * 60 }) -} - -export const removeTenantName = () => { - wsCache.delete(TenantNameKey) -} - export const getTenantId = () => { - return wsCache.get(TenantIdKey) + return wsCache.get(CACHE_KEY.TenantId) } export const setTenantId = (username: string) => { - wsCache.set(TenantIdKey, username) -} - -export const removeTenantId = () => { - wsCache.delete(TenantIdKey) + wsCache.set(CACHE_KEY.TenantId, username) } diff --git a/src/views/Login/components/LoginForm.vue b/src/views/Login/components/LoginForm.vue index 66a639b9..a6777204 100644 --- a/src/views/Login/components/LoginForm.vue +++ b/src/views/Login/components/LoginForm.vue @@ -188,7 +188,7 @@ const loginData = reactive({ username: 'admin', password: 'admin123', captchaVerification: '', - rememberMe: false + rememberMe: true // 默认记录我。如果不需要,可手动修改 } }) @@ -219,14 +219,14 @@ const getTenantId = async () => { } } // 记住我 -const getCookie = () => { +const getLoginFormCache = () => { const loginForm = authUtil.getLoginForm() if (loginForm) { loginData.loginForm = { ...loginData.loginForm, username: loginForm.username ? loginForm.username : loginData.loginForm.username, password: loginForm.password ? loginForm.password : loginData.loginForm.password, - rememberMe: loginForm.rememberMe ? true : false, + rememberMe: loginForm.rememberMe, tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName } } @@ -323,7 +323,7 @@ watch( } ) onMounted(() => { - getCookie() + getLoginFormCache() getTenantByWebsite() })