From 6f3ad77fba3cf26926e7a78719e3e9cecb159f32 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 24 Sep 2025 18:55:22 +0800 Subject: [PATCH] fix: update getCaptcha function to include accessToken and simplify captcha display logic --- ui/src/api/chat/chat.ts | 4 +- ui/src/views/chat/user-login/index.vue | 63 ++------------------------ ui/src/views/login/index.vue | 63 ++------------------------ 3 files changed, 9 insertions(+), 121 deletions(-) diff --git a/ui/src/api/chat/chat.ts b/ui/src/api/chat/chat.ts index 4bb591576..26c5aab6c 100644 --- a/ui/src/api/chat/chat.ts +++ b/ui/src/api/chat/chat.ts @@ -115,8 +115,8 @@ const ldapLogin: ( * @param username * @param loading 接口加载器 */ -const getCaptcha: (username?: string, loading?: Ref) => Promise> = (username, loading) => { - return get('/captcha', {username: username}, loading) +const getCaptcha: (username?: string, accessToken?: string, loading?: Ref) => Promise> = (username, accessToken, loading) => { + return get('/captcha', {username: username, accessToken: accessToken}, loading) } /** diff --git a/ui/src/views/chat/user-login/index.vue b/ui/src/views/chat/user-login/index.vue index c783e8e70..68034cb2c 100644 --- a/ui/src/views/chat/user-login/index.vue +++ b/ui/src/views/chat/user-login/index.vue @@ -85,7 +85,7 @@ -
+
{ 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(() => { - // -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(() => { diff --git a/ui/src/views/login/index.vue b/ui/src/views/login/index.vue index 14420b63e..735d2b506 100644 --- a/ui/src/views/login/index.vue +++ b/ui/src/views/login/index.vue @@ -35,7 +35,7 @@
-
+
{ .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(() => { - 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(() => {