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