This commit is contained in:
qsh
2024-08-01 18:41:37 +08:00
parent feccb2a1fa
commit 605151c5c8
17 changed files with 143 additions and 137 deletions

View File

@@ -1,8 +1,6 @@
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 +8,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 +47,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 +56,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 +69,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)
}

View File

@@ -1,4 +1,5 @@
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { CACHE_KEY } from '@/hooks/web/useCache'
import cache from '@/plugins/cache'
const { t } = useI18n() // 国际化
@@ -9,10 +10,9 @@ const { t } = useI18n() // 国际化
*/
export function checkPermi(value: string[]) {
if (value && value instanceof Array && value.length > 0) {
const { wsCache } = useCache()
const permissionDatas = value
const all_permission = '*:*:*'
const permissions = wsCache.get(CACHE_KEY.USER).permissions
const permissions = cache.local.get(CACHE_KEY.USER).permissions
const hasPermission = permissions.some((permission) => {
return all_permission === permission || permissionDatas.includes(permission)
})
@@ -30,10 +30,9 @@ export function checkPermi(value: string[]) {
*/
export function checkRole(value: string[]) {
if (value && value instanceof Array && value.length > 0) {
const { wsCache } = useCache()
const permissionRoles = value
const super_admin = 'admin'
const roles = wsCache.get(CACHE_KEY.USER).roles
const roles = cache.local.get(CACHE_KEY.USER).roles
const hasRole = roles.some((role) => {
return super_admin === role || permissionRoles.includes(role)
})