cyywl_server/yudao-ui-admin-vue3/src/utils/auth.ts

30 lines
786 B
TypeScript
Raw Normal View History

2022-07-18 19:06:37 +08:00
import { useCache } from '@/hooks/web/useCache'
import { TokenType } from '@/api/login/types'
const { wsCache } = useCache()
const AccessTokenKey = 'ACCESS_TOKEN'
const RefreshTokenKey = 'REFRESH_TOKEN'
// 获取token
export function getAccessToken() {
// 此处与TokenKey相同此写法解决初始化时Cookies中不存在TokenKey报错
return wsCache.get('ACCESS_TOKEN')
}
// 刷新token
export function getRefreshToken() {
return wsCache.get(RefreshTokenKey)
}
// 设置token
export function setToken(token: TokenType) {
wsCache.set(RefreshTokenKey, token.refreshToken, { exp: token.expiresTime })
wsCache.set(AccessTokenKey, token.accessToken)
}
// 删除token
export function removeToken() {
wsCache.delete(AccessTokenKey)
wsCache.delete(RefreshTokenKey)
}