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 9e5caae8..b3861809 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 7da49b08..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 = (username: string) => { - wsCache.set(TenantNameKey, username, { 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 ef212505..bf102e04 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 // 默认记录我。如果不需要,可手动修改 } }) @@ -218,14 +218,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 } } @@ -320,7 +320,7 @@ watch( } ) onMounted(() => { - getCookie() + getLoginFormCache() getTenantByWebsite() })