sv
This commit is contained in:
121
src/utils/auth.ts
Normal file
121
src/utils/auth.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { TokenType } from '@/api/login/types'
|
||||
import { decrypt, encrypt } from '@/utils/jsencrypt'
|
||||
|
||||
import cache from '@/plugins/cache'
|
||||
|
||||
const AccessTokenKey = 'ACCESS_TOKEN'
|
||||
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')
|
||||
}
|
||||
|
||||
// 刷新token
|
||||
export const getRefreshToken = () => {
|
||||
return cache.local.get(RefreshTokenKey)
|
||||
}
|
||||
|
||||
// 设置token
|
||||
export const setToken = (token: TokenType) => {
|
||||
cache.local.set(RefreshTokenKey, token.refreshToken)
|
||||
cache.local.set(AccessTokenKey, token.accessToken)
|
||||
}
|
||||
|
||||
// 删除token
|
||||
export const removeToken = () => {
|
||||
cache.local.delete(AccessTokenKey)
|
||||
cache.local.delete(RefreshTokenKey)
|
||||
}
|
||||
|
||||
/** 格式化token(jwt格式) */
|
||||
export const formatToken = (token: string): string => {
|
||||
return 'Bearer ' + token
|
||||
}
|
||||
// ========== 账号相关 ==========
|
||||
|
||||
const LoginFormKey = 'LOGINFORM'
|
||||
|
||||
export type LoginFormType = {
|
||||
tenantId: number
|
||||
appId: number
|
||||
username: string
|
||||
password: string
|
||||
rememberMe: boolean
|
||||
}
|
||||
|
||||
export const getLoginForm = () => {
|
||||
const loginForm: LoginFormType = cache.local.get(LoginFormKey)
|
||||
if (loginForm) {
|
||||
loginForm.password = decrypt(loginForm.password) as string
|
||||
}
|
||||
return loginForm
|
||||
}
|
||||
|
||||
export const setLoginForm = (loginForm: LoginFormType) => {
|
||||
loginForm.password = encrypt(loginForm.password) as string
|
||||
cache.local.set(LoginFormKey, loginForm)
|
||||
}
|
||||
|
||||
export const removeLoginForm = () => {
|
||||
cache.local.delete(LoginFormKey)
|
||||
}
|
||||
|
||||
// ========== 租户相关 ==========
|
||||
|
||||
const TenantIdKey = 'TENANT_ID'
|
||||
const TenantNameKey = 'TENANT_NAME'
|
||||
|
||||
export const getTenantName = () => {
|
||||
return cache.local.get(TenantNameKey)
|
||||
}
|
||||
|
||||
export const setTenantName = (username: string) => {
|
||||
cache.local.set(TenantNameKey, username)
|
||||
}
|
||||
|
||||
export const removeTenantName = () => {
|
||||
cache.local.delete(TenantNameKey)
|
||||
}
|
||||
|
||||
export const getTenantId = () => {
|
||||
return cache.local.get(TenantIdKey)
|
||||
}
|
||||
|
||||
export const setTenantId = (username: string) => {
|
||||
cache.local.set(TenantIdKey, username)
|
||||
}
|
||||
|
||||
export const removeTenantId = () => {
|
||||
cache.local.delete(TenantIdKey)
|
||||
}
|
||||
|
||||
const AppIdKey = 'App_ID'
|
||||
const AppNameKey = 'App_NAME'
|
||||
|
||||
export const getAPPName = () => {
|
||||
return cache.local.get(AppNameKey)
|
||||
}
|
||||
|
||||
export const setAppName = (name: string) => {
|
||||
cache.local.set(AppNameKey, name)
|
||||
}
|
||||
|
||||
export const removeAppName = () => {
|
||||
cache.local.delete(AppNameKey)
|
||||
}
|
||||
|
||||
export const getAppId = () => {
|
||||
return cache.local.get(AppIdKey)
|
||||
}
|
||||
|
||||
export const setAppId = (id: number) => {
|
||||
cache.local.set(AppIdKey, id)
|
||||
}
|
||||
|
||||
export const removeAppId = () => {
|
||||
cache.local.delete(AppIdKey)
|
||||
}
|
||||
Reference in New Issue
Block a user