mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-29 07:52:50 +00:00
fix: update getCaptcha function to include accessToken and simplify captcha display logic
This commit is contained in:
parent
0406fbcaf7
commit
6f3ad77fba
|
|
@ -115,8 +115,8 @@ const ldapLogin: (
|
|||
* @param username
|
||||
* @param loading 接口加载器
|
||||
*/
|
||||
const getCaptcha: (username?: string, loading?: Ref<boolean>) => Promise<Result<any>> = (username, loading) => {
|
||||
return get('/captcha', {username: username}, loading)
|
||||
const getCaptcha: (username?: string, accessToken?: string, loading?: Ref<boolean>) => Promise<Result<any>> = (username, accessToken, loading) => {
|
||||
return get('/captcha', {username: username, accessToken: accessToken}, loading)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@
|
|||
</el-input>
|
||||
</el-form-item>
|
||||
</div>
|
||||
<div class="mb-24" v-if="loginMode !== 'LDAP'&& showCaptcha">
|
||||
<div class="mb-24" v-if="loginMode !== 'LDAP'&& identifyCode">
|
||||
<el-form-item prop="captcha">
|
||||
<div class="flex-between w-full">
|
||||
<el-input
|
||||
|
|
@ -262,25 +262,15 @@ const loginHandle = () => {
|
|||
params: {accessToken: chatUser.accessToken},
|
||||
query: route.query,
|
||||
})
|
||||
localStorage.removeItem('chat_' + loginForm.value.username)
|
||||
}).catch(() => {
|
||||
const username = loginForm.value.username
|
||||
localStorage.setItem('chat_' + username, String(Number(localStorage.getItem('chat_' + username) || '0') + 1))
|
||||
loading.value = false
|
||||
loginForm.value.username = ''
|
||||
loginForm.value.password = ''
|
||||
loginForm.value.captcha = ''
|
||||
const timestampKey = `${username}_chat_first_fail_timestamp`
|
||||
if (!localStorage.getItem(timestampKey)) {
|
||||
localStorage.setItem(timestampKey, Date.now().toString())
|
||||
}
|
||||
makeCode(loginForm.value.username)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function makeCode(username?: string) {
|
||||
loginApi.getCaptcha(username).then((res: any) => {
|
||||
loginApi.getCaptcha(username, accessToken).then((res: any) => {
|
||||
identifyCode.value = res.data.captcha
|
||||
})
|
||||
}
|
||||
|
|
@ -385,53 +375,8 @@ function changeMode(val: string) {
|
|||
loginFormRef.value?.clearValidate()
|
||||
}
|
||||
|
||||
const showCaptcha = computed<boolean>(() => {
|
||||
// -1 表示一直不显示
|
||||
if (max_attempts.value === -1) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 0 表示一直显示
|
||||
if (max_attempts.value === 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 大于 0,根据登录失败次数决定
|
||||
const username = loginForm.value.username?.trim()
|
||||
if (!username) {
|
||||
return false // 没有输入用户名时不显示
|
||||
}
|
||||
|
||||
const timestampKey = `${username}_chat_first_fail_timestamp`
|
||||
const firstFailTimestamp = localStorage.getItem(timestampKey)
|
||||
|
||||
if (firstFailTimestamp) {
|
||||
const expirationTime = 60 * 60 * 1000 // 10分钟毫秒数
|
||||
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
|
||||
// 过期则清除记录
|
||||
localStorage.removeItem('chat_' + username)
|
||||
localStorage.removeItem(timestampKey)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
// 如果没有时间戳但有失败次数,可能是旧数据,清除失败次数
|
||||
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
|
||||
if (failCount > 0) {
|
||||
localStorage.removeItem('chat_' + username)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
|
||||
console.log('failCount', failCount)
|
||||
|
||||
return failCount >= max_attempts.value
|
||||
})
|
||||
|
||||
function handleUsernameBlur(username: string) {
|
||||
if (showCaptcha.value) {
|
||||
makeCode(username)
|
||||
}
|
||||
makeCode(username)
|
||||
}
|
||||
|
||||
onBeforeMount(() => {
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@
|
|||
</el-input>
|
||||
</el-form-item>
|
||||
</div>
|
||||
<div class="mb-24" v-if="loginMode !== 'LDAP' && showCaptcha">
|
||||
<div class="mb-24" v-if="loginMode !== 'LDAP' && identifyCode">
|
||||
<el-form-item prop="captcha">
|
||||
<div class="flex-between w-full">
|
||||
<el-input
|
||||
|
|
@ -208,73 +208,18 @@ const loginHandle = () => {
|
|||
.then(() => {
|
||||
locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US'
|
||||
localStorage.setItem('workspace_id', 'default')
|
||||
localStorage.removeItem(loginForm.value.username)
|
||||
router.push({name: 'home'})
|
||||
})
|
||||
.catch(() => {
|
||||
const username = loginForm.value.username
|
||||
localStorage.setItem(username, String(Number(localStorage.getItem(username) || '0') + 1))
|
||||
loading.value = false
|
||||
loginForm.value.username = ''
|
||||
loginForm.value.password = ''
|
||||
loginForm.value.captcha = ''
|
||||
const timestampKey = `${username}_first_fail_timestamp`
|
||||
if (!localStorage.getItem(timestampKey)) {
|
||||
localStorage.setItem(timestampKey, Date.now().toString())
|
||||
}
|
||||
makeCode(username)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const showCaptcha = computed<boolean>(() => {
|
||||
if (!authSetting.value) return true
|
||||
|
||||
const maxAttempts = authSetting.value.max_attempts
|
||||
|
||||
// -1 表示一直不显示
|
||||
if (maxAttempts === -1) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 0 表示一直显示
|
||||
if (maxAttempts === 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 大于 0,根据登录失败次数决定
|
||||
const username = loginForm.value.username?.trim()
|
||||
if (!username) {
|
||||
return false // 没有输入用户名时不显示
|
||||
}
|
||||
|
||||
const timestampKey = `${username}_first_fail_timestamp`
|
||||
const firstFailTimestamp = localStorage.getItem(timestampKey)
|
||||
|
||||
if (firstFailTimestamp) {
|
||||
const expirationTime = 10 * 60 * 1000 // 10分钟毫秒数
|
||||
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
|
||||
// 过期则清除记录
|
||||
localStorage.removeItem(username)
|
||||
localStorage.removeItem(timestampKey)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
// 如果没有时间戳但有失败次数,可能是旧数据,清除失败次数
|
||||
const failCount = Number(localStorage.getItem(username) || '0')
|
||||
if (failCount > 0) {
|
||||
localStorage.removeItem(username)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const failCount = Number(localStorage.getItem(username) || '0')
|
||||
console.log('failCount', failCount)
|
||||
|
||||
return failCount >= maxAttempts
|
||||
})
|
||||
|
||||
function makeCode(username?: string) {
|
||||
loginApi.getCaptcha(username).then((res: any) => {
|
||||
if (res && res.data && res.data.captcha) {
|
||||
|
|
@ -286,9 +231,7 @@ function makeCode(username?: string) {
|
|||
}
|
||||
|
||||
function handleUsernameBlur(username: string) {
|
||||
if (showCaptcha.value) {
|
||||
makeCode(username)
|
||||
}
|
||||
makeCode(username)
|
||||
}
|
||||
|
||||
onBeforeMount(() => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue