parent
6b320e9d72
commit
8c4c54a56f
@ -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 登录表单
|
||||
}
|
||||
|
@ -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: {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
})
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user