1
0
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:
2023-05-05 20:59:09 +08:00
parent a8dce8f8e0
commit 60b8460e03
32 changed files with 1022 additions and 151 deletions

61
ui/src/utils/auth.ts Normal file
View 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 }