Files
ss-oa-manage-web/src/utils/auth.ts
2025-05-19 11:26:55 +08:00

130 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 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 localStorage.getItem(RefreshTokenKey)
// return cache.local.get(RefreshTokenKey)
}
// 设置token
export const setToken = (token: TokenType) => {
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 = () => {
localStorage.removeItem(AccessTokenKey)
localStorage.removeItem(RefreshTokenKey)
// cache.local.delete(AccessTokenKey)
// cache.local.delete(RefreshTokenKey)
}
/** 格式化tokenjwt格式 */
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)
}