mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-06 07:21:24 +08:00
Added login, logout and getUserinfo (Include ui and server)
This commit is contained in:
61
ui/src/utils/auth.ts
Normal file
61
ui/src/utils/auth.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { Captcha } from './common'
|
||||
import {
|
||||
getCaptcha,
|
||||
getLocalStorage,
|
||||
getToken,
|
||||
removeLocalStorage,
|
||||
setLocalStorage,
|
||||
setToken
|
||||
} from './common'
|
||||
import { TOKEN_NAME } from '@/constants/Common.constants'
|
||||
import _ from 'lodash'
|
||||
import request from '@/services'
|
||||
|
||||
let captcha: Captcha
|
||||
|
||||
async function login(username: string, passwd: string): Promise<boolean> {
|
||||
removeLocalStorage('username')
|
||||
await request.post('/login', { username, passwd }).then((res: any) => {
|
||||
const response = res.data
|
||||
if (response.code === 20010) {
|
||||
setToken(response.data.token)
|
||||
}
|
||||
})
|
||||
|
||||
return !_.isEmpty(getToken())
|
||||
}
|
||||
|
||||
function logout(): void {
|
||||
removeLocalStorage(TOKEN_NAME)
|
||||
removeLocalStorage('username')
|
||||
}
|
||||
|
||||
function getLoginStatus(): boolean {
|
||||
return getLocalStorage(TOKEN_NAME) != null
|
||||
}
|
||||
|
||||
async function getUsername(): Promise<string | null> {
|
||||
if (!_.isEmpty(getLocalStorage('username'))) {
|
||||
return getLocalStorage('username')
|
||||
}
|
||||
|
||||
let username = ''
|
||||
|
||||
await request.get('/userInfo').then((res) => {
|
||||
username = res.data.data.user.username
|
||||
})
|
||||
|
||||
setLocalStorage('username', username)
|
||||
return username
|
||||
}
|
||||
|
||||
function getCaptchaSrc(): string {
|
||||
captcha = getCaptcha(300, 150, 4)
|
||||
return captcha.base64Src
|
||||
}
|
||||
|
||||
function verifyCaptcha(value: string): boolean {
|
||||
return captcha.value === value.replace(/\s*/g, '').toUpperCase()
|
||||
}
|
||||
|
||||
export { login, logout, getLoginStatus, getUsername, getCaptchaSrc, verifyCaptcha }
|
||||
Reference in New Issue
Block a user