This commit is contained in:
qsh
2025-05-21 18:49:14 +08:00
parent cfa352ecb7
commit 2c1ab31f73
21 changed files with 1594 additions and 17 deletions

View File

@@ -1,6 +1,5 @@
import { TokenType } from '@/api/login/types'
import { decrypt, encrypt } from '@/utils/jsencrypt'
import cache from '@/plugins/cache'
const AccessTokenKey = 'ACCESS_TOKEN'
@@ -9,26 +8,34 @@ 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 localStorage.getItem(AccessTokenKey)
? localStorage.getItem(AccessTokenKey)
: localStorage.getItem('ACCESS_TOKEN')
// return cache.local.get(AccessTokenKey)
// ? cache.local.get(AccessTokenKey)
// : cache.local.get('ACCESS_TOKEN')
}
// 刷新token
export const getRefreshToken = () => {
return cache.local.get(RefreshTokenKey)
return localStorage.getItem(RefreshTokenKey)
// return cache.local.get(RefreshTokenKey)
}
// 设置token
export const setToken = (token: TokenType) => {
cache.local.set(RefreshTokenKey, token.refreshToken)
cache.local.set(AccessTokenKey, token.accessToken)
localStorage.setItem(AccessTokenKey, token.accessToken)
localStorage.setItem(RefreshTokenKey, token.refreshToken)
// cache.local.set(RefreshTokenKey, token.refreshToken)
// cache.local.set(AccessTokenKey, token.accessToken)
}
// 删除token
export const removeToken = () => {
cache.local.delete(AccessTokenKey)
cache.local.delete(RefreshTokenKey)
localStorage.removeItem(AccessTokenKey)
localStorage.removeItem(RefreshTokenKey)
// cache.local.delete(AccessTokenKey)
// cache.local.delete(RefreshTokenKey)
}
/** 格式化tokenjwt格式 */