Optimize code
This commit is contained in:
@@ -1,30 +1,31 @@
|
||||
import { getCaptcha, getLocalStorage, removeToken, setLocalStorage } from './common'
|
||||
import { SYSTEM_OK, TOKEN_NAME } from '@/constants/Common.constants'
|
||||
import { SYSTEM_OK, STORAGE_TOKEN_KEY, STORAGE_USER_INFO_KEY } from '@/constants/common.constants'
|
||||
import request from '@/services'
|
||||
import { URL_API_LOGIN, URL_API_LOGOUT } from '@/constants/urls.constants'
|
||||
|
||||
let captcha: Captcha
|
||||
|
||||
export const login = async (username: string, password: string) => {
|
||||
return await request.post<Token>('/login', {
|
||||
return await request.post<Token>(URL_API_LOGIN, {
|
||||
username,
|
||||
password
|
||||
})
|
||||
}
|
||||
|
||||
export const logout = async () => {
|
||||
return request.post('/logout').finally(() => {
|
||||
return request.post(URL_API_LOGOUT).finally(() => {
|
||||
removeToken()
|
||||
})
|
||||
}
|
||||
|
||||
export const getLoginStatus = () => {
|
||||
return getLocalStorage(TOKEN_NAME) !== null
|
||||
return getLocalStorage(STORAGE_TOKEN_KEY) !== null
|
||||
}
|
||||
|
||||
export const getUser = async (): Promise<User> => {
|
||||
if (getLocalStorage('userInfo') !== null) {
|
||||
if (getLocalStorage(STORAGE_USER_INFO_KEY) !== null) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(JSON.parse(getLocalStorage('userInfo') as string) as User)
|
||||
resolve(JSON.parse(getLocalStorage(STORAGE_USER_INFO_KEY) as string) as User)
|
||||
})
|
||||
}
|
||||
return requestUser()
|
||||
@@ -37,7 +38,7 @@ export const requestUser = async () => {
|
||||
const response = value.data
|
||||
if (response.code === SYSTEM_OK) {
|
||||
user = response.data
|
||||
setLocalStorage('userInfo', JSON.stringify(user))
|
||||
setLocalStorage(STORAGE_USER_INFO_KEY, JSON.stringify(user))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TOKEN_NAME } from '@/constants/Common.constants'
|
||||
import { STORAGE_TOKEN_KEY, STORAGE_USER_INFO_KEY } from '@/constants/common.constants'
|
||||
|
||||
export const getQueryVariable = (variable: string) => {
|
||||
const query = window.location.search.substring(1)
|
||||
@@ -36,7 +36,7 @@ export const setLocalStorage = (name: string, value: string) => {
|
||||
}
|
||||
|
||||
export const setToken = (token: string) => {
|
||||
setLocalStorage(TOKEN_NAME, token)
|
||||
setLocalStorage(STORAGE_TOKEN_KEY, token)
|
||||
}
|
||||
|
||||
export const getCookie = (name: string) => {
|
||||
@@ -57,7 +57,7 @@ export const getLocalStorage = (name: string) => {
|
||||
}
|
||||
|
||||
export const getToken = () => {
|
||||
return getLocalStorage(TOKEN_NAME)
|
||||
return getLocalStorage(STORAGE_TOKEN_KEY)
|
||||
}
|
||||
|
||||
export const removeCookie = (name: string) => {
|
||||
@@ -69,8 +69,8 @@ export const removeLocalStorage = (name: string) => {
|
||||
}
|
||||
|
||||
export const removeToken = () => {
|
||||
removeLocalStorage('userInfo')
|
||||
removeLocalStorage(TOKEN_NAME)
|
||||
removeLocalStorage(STORAGE_USER_INFO_KEY)
|
||||
removeLocalStorage(STORAGE_TOKEN_KEY)
|
||||
}
|
||||
|
||||
export const clearLocalStorage = () => {
|
||||
|
||||
Reference in New Issue
Block a user