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