This commit is contained in:
qsh
2024-08-01 18:55:41 +08:00
parent 6c035fa0d8
commit e59ba74c12
16 changed files with 136 additions and 130 deletions

View File

@@ -1,8 +1,7 @@
import { useCache } from '@/hooks/web/useCache'
import { TokenType } from '@/api/login/types'
import { decrypt, encrypt } from '@/utils/jsencrypt'
const { wsCache } = useCache()
import cache from '@/plugins/cache'
const AccessTokenKey = 'ACCESS_TOKEN'
const RefreshTokenKey = 'REFRESH_TOKEN'
@@ -10,24 +9,26 @@ const RefreshTokenKey = 'REFRESH_TOKEN'
// 获取token
export const getAccessToken = () => {
// 此处与TokenKey相同此写法解决初始化时Cookies中不存在TokenKey报错
return wsCache.get(AccessTokenKey) ? wsCache.get(AccessTokenKey) : wsCache.get('ACCESS_TOKEN')
return cache.local.get(AccessTokenKey)
? cache.local.get(AccessTokenKey)
: cache.local.get('ACCESS_TOKEN')
}
// 刷新token
export const getRefreshToken = () => {
return wsCache.get(RefreshTokenKey)
return cache.local.get(RefreshTokenKey)
}
// 设置token
export const setToken = (token: TokenType) => {
wsCache.set(RefreshTokenKey, token.refreshToken, { exp: token.expiresTime })
wsCache.set(AccessTokenKey, token.accessToken)
cache.local.set(RefreshTokenKey, token.refreshToken)
cache.local.set(AccessTokenKey, token.accessToken)
}
// 删除token
export const removeToken = () => {
wsCache.delete(AccessTokenKey)
wsCache.delete(RefreshTokenKey)
cache.local.delete(AccessTokenKey)
cache.local.delete(RefreshTokenKey)
}
/** 格式化tokenjwt格式 */
@@ -47,7 +48,7 @@ export type LoginFormType = {
}
export const getLoginForm = () => {
const loginForm: LoginFormType = wsCache.get(LoginFormKey)
const loginForm: LoginFormType = cache.local.get(LoginFormKey)
if (loginForm) {
loginForm.password = decrypt(loginForm.password) as string
}
@@ -56,11 +57,11 @@ export const getLoginForm = () => {
export const setLoginForm = (loginForm: LoginFormType) => {
loginForm.password = encrypt(loginForm.password) as string
wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 })
cache.local.set(LoginFormKey, loginForm)
}
export const removeLoginForm = () => {
wsCache.delete(LoginFormKey)
cache.local.delete(LoginFormKey)
}
// ========== 租户相关 ==========
@@ -69,52 +70,52 @@ const TenantIdKey = 'TENANT_ID'
const TenantNameKey = 'TENANT_NAME'
export const getTenantName = () => {
return wsCache.get(TenantNameKey)
return cache.local.get(TenantNameKey)
}
export const setTenantName = (username: string) => {
wsCache.set(TenantNameKey, username, { exp: 30 * 24 * 60 * 60 })
cache.local.set(TenantNameKey, username)
}
export const removeTenantName = () => {
wsCache.delete(TenantNameKey)
cache.local.delete(TenantNameKey)
}
export const getTenantId = () => {
return wsCache.get(TenantIdKey)
return cache.local.get(TenantIdKey)
}
export const setTenantId = (username: string) => {
wsCache.set(TenantIdKey, username)
cache.local.set(TenantIdKey, username)
}
export const removeTenantId = () => {
wsCache.delete(TenantIdKey)
cache.local.delete(TenantIdKey)
}
const AppIdKey = 'App_ID'
const AppNameKey = 'App_NAME'
export const getAPPName = () => {
return wsCache.get(AppNameKey)
return cache.local.get(AppNameKey)
}
export const setAppName = (name: string) => {
wsCache.set(AppNameKey, name, { exp: 30 * 24 * 60 * 60 })
cache.local.set(AppNameKey, name)
}
export const removeAppName = () => {
wsCache.delete(AppNameKey)
cache.local.delete(AppNameKey)
}
export const getAppId = () => {
return wsCache.get(AppIdKey)
return cache.local.get(AppIdKey)
}
export const setAppId = (id: number) => {
wsCache.set(AppIdKey, id)
cache.local.set(AppIdKey, id)
}
export const removeAppId = () => {
wsCache.delete(AppIdKey)
cache.local.delete(AppIdKey)
}