Files
ss-crm-manage-web/src/utils/auth.ts

129 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-04-28 16:20:45 +08:00
import { TokenType } from '@/api/login/types'
import { decrypt, encrypt } from '@/utils/jsencrypt'
2024-08-01 18:41:37 +08:00
import cache from '@/plugins/cache'
2024-04-28 16:20:45 +08:00
const AccessTokenKey = 'ACCESS_TOKEN'
const RefreshTokenKey = 'REFRESH_TOKEN'
// 获取token
export const getAccessToken = () => {
// 此处与TokenKey相同此写法解决初始化时Cookies中不存在TokenKey报错
2025-05-19 11:41:27 +08:00
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')
2024-04-28 16:20:45 +08:00
}
// 刷新token
export const getRefreshToken = () => {
2025-05-19 11:41:27 +08:00
return localStorage.getItem(RefreshTokenKey)
// return cache.local.get(RefreshTokenKey)
2024-04-28 16:20:45 +08:00
}
// 设置token
export const setToken = (token: TokenType) => {
2025-05-19 11:41:27 +08:00
localStorage.setItem(AccessTokenKey, token.accessToken)
localStorage.setItem(RefreshTokenKey, token.refreshToken)
// cache.local.set(RefreshTokenKey, token.refreshToken)
// cache.local.set(AccessTokenKey, token.accessToken)
2024-04-28 16:20:45 +08:00
}
// 删除token
export const removeToken = () => {
2025-05-19 11:41:27 +08:00
localStorage.removeItem(AccessTokenKey)
localStorage.removeItem(RefreshTokenKey)
// cache.local.delete(AccessTokenKey)
// cache.local.delete(RefreshTokenKey)
2024-04-28 16:20:45 +08:00
}
/** 格式化tokenjwt格式 */
export const formatToken = (token: string): string => {
return 'Bearer ' + token
}
// ========== 账号相关 ==========
const LoginFormKey = 'LOGINFORM'
export type LoginFormType = {
2024-05-23 14:08:08 +08:00
tenantId: number
appId: number
2024-04-28 16:20:45 +08:00
username: string
password: string
rememberMe: boolean
}
export const getLoginForm = () => {
2024-08-01 18:41:37 +08:00
const loginForm: LoginFormType = cache.local.get(LoginFormKey)
2024-04-28 16:20:45 +08:00
if (loginForm) {
loginForm.password = decrypt(loginForm.password) as string
}
return loginForm
}
export const setLoginForm = (loginForm: LoginFormType) => {
loginForm.password = encrypt(loginForm.password) as string
2024-08-01 18:41:37 +08:00
cache.local.set(LoginFormKey, loginForm)
2024-04-28 16:20:45 +08:00
}
export const removeLoginForm = () => {
2024-08-01 18:41:37 +08:00
cache.local.delete(LoginFormKey)
2024-04-28 16:20:45 +08:00
}
// ========== 租户相关 ==========
const TenantIdKey = 'TENANT_ID'
const TenantNameKey = 'TENANT_NAME'
export const getTenantName = () => {
2024-08-01 18:41:37 +08:00
return cache.local.get(TenantNameKey)
2024-04-28 16:20:45 +08:00
}
export const setTenantName = (username: string) => {
2024-08-01 18:41:37 +08:00
cache.local.set(TenantNameKey, username)
2024-04-28 16:20:45 +08:00
}
export const removeTenantName = () => {
2024-08-01 18:41:37 +08:00
cache.local.delete(TenantNameKey)
2024-04-28 16:20:45 +08:00
}
export const getTenantId = () => {
2024-08-01 18:41:37 +08:00
return cache.local.get(TenantIdKey)
2024-04-28 16:20:45 +08:00
}
export const setTenantId = (username: string) => {
2024-08-01 18:41:37 +08:00
cache.local.set(TenantIdKey, username)
2024-04-28 16:20:45 +08:00
}
export const removeTenantId = () => {
2024-08-01 18:41:37 +08:00
cache.local.delete(TenantIdKey)
2024-04-28 16:20:45 +08:00
}
2024-05-23 14:08:08 +08:00
const AppIdKey = 'App_ID'
const AppNameKey = 'App_NAME'
export const getAPPName = () => {
2024-08-01 18:41:37 +08:00
return cache.local.get(AppNameKey)
2024-05-23 14:08:08 +08:00
}
export const setAppName = (name: string) => {
2024-08-01 18:41:37 +08:00
cache.local.set(AppNameKey, name)
2024-05-23 14:08:08 +08:00
}
export const removeAppName = () => {
2024-08-01 18:41:37 +08:00
cache.local.delete(AppNameKey)
2024-05-23 14:08:08 +08:00
}
export const getAppId = () => {
2024-08-01 18:41:37 +08:00
return cache.local.get(AppIdKey)
2024-05-23 14:08:08 +08:00
}
export const setAppId = (id: number) => {
2024-08-01 18:41:37 +08:00
cache.local.set(AppIdKey, id)
2024-05-23 14:08:08 +08:00
}
export const removeAppId = () => {
2024-08-01 18:41:37 +08:00
cache.local.delete(AppIdKey)
2024-05-23 14:08:08 +08:00
}