Skip to content

Commit 6f3ad77

Browse files
committed
fix: update getCaptcha function to include accessToken and simplify captcha display logic
1 parent 0406fbc commit 6f3ad77

File tree

3 files changed

+9
-121
lines changed

3 files changed

+9
-121
lines changed

ui/src/api/chat/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ const ldapLogin: (
115115
* @param username
116116
* @param loading 接口加载器
117117
*/
118-
const getCaptcha: (username?: string, loading?: Ref<boolean>) => Promise<Result<any>> = (username, loading) => {
119-
return get('/captcha', {username: username}, loading)
118+
const getCaptcha: (username?: string, accessToken?: string, loading?: Ref<boolean>) => Promise<Result<any>> = (username, accessToken, loading) => {
119+
return get('/captcha', {username: username, accessToken: accessToken}, loading)
120120
}
121121

122122
/**

ui/src/views/chat/user-login/index.vue

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
</el-input>
8686
</el-form-item>
8787
</div>
88-
<div class="mb-24" v-if="loginMode !== 'LDAP'&& showCaptcha">
88+
<div class="mb-24" v-if="loginMode !== 'LDAP'&& identifyCode">
8989
<el-form-item prop="captcha">
9090
<div class="flex-between w-full">
9191
<el-input
@@ -262,25 +262,15 @@ const loginHandle = () => {
262262
params: {accessToken: chatUser.accessToken},
263263
query: route.query,
264264
})
265-
localStorage.removeItem('chat_' + loginForm.value.username)
266265
}).catch(() => {
267-
const username = loginForm.value.username
268-
localStorage.setItem('chat_' + username, String(Number(localStorage.getItem('chat_' + username) || '0') + 1))
269-
loading.value = false
270-
loginForm.value.username = ''
271-
loginForm.value.password = ''
272-
loginForm.value.captcha = ''
273-
const timestampKey = `${username}_chat_first_fail_timestamp`
274-
if (!localStorage.getItem(timestampKey)) {
275-
localStorage.setItem(timestampKey, Date.now().toString())
276-
}
266+
makeCode(loginForm.value.username)
277267
})
278268
}
279269
})
280270
}
281271
282272
function makeCode(username?: string) {
283-
loginApi.getCaptcha(username).then((res: any) => {
273+
loginApi.getCaptcha(username, accessToken).then((res: any) => {
284274
identifyCode.value = res.data.captcha
285275
})
286276
}
@@ -385,53 +375,8 @@ function changeMode(val: string) {
385375
loginFormRef.value?.clearValidate()
386376
}
387377
388-
const showCaptcha = computed<boolean>(() => {
389-
// -1 表示一直不显示
390-
if (max_attempts.value === -1) {
391-
return false
392-
}
393-
394-
// 0 表示一直显示
395-
if (max_attempts.value === 0) {
396-
return true
397-
}
398-
399-
// 大于 0,根据登录失败次数决定
400-
const username = loginForm.value.username?.trim()
401-
if (!username) {
402-
return false // 没有输入用户名时不显示
403-
}
404-
405-
const timestampKey = `${username}_chat_first_fail_timestamp`
406-
const firstFailTimestamp = localStorage.getItem(timestampKey)
407-
408-
if (firstFailTimestamp) {
409-
const expirationTime = 60 * 60 * 1000 // 10分钟毫秒数
410-
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
411-
// 过期则清除记录
412-
localStorage.removeItem('chat_' + username)
413-
localStorage.removeItem(timestampKey)
414-
return false
415-
}
416-
} else {
417-
// 如果没有时间戳但有失败次数,可能是旧数据,清除失败次数
418-
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
419-
if (failCount > 0) {
420-
localStorage.removeItem('chat_' + username)
421-
return false
422-
}
423-
}
424-
425-
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
426-
console.log('failCount', failCount)
427-
428-
return failCount >= max_attempts.value
429-
})
430-
431378
function handleUsernameBlur(username: string) {
432-
if (showCaptcha.value) {
433-
makeCode(username)
434-
}
379+
makeCode(username)
435380
}
436381
437382
onBeforeMount(() => {

ui/src/views/login/index.vue

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</el-input>
3636
</el-form-item>
3737
</div>
38-
<div class="mb-24" v-if="loginMode !== 'LDAP' && showCaptcha">
38+
<div class="mb-24" v-if="loginMode !== 'LDAP' && identifyCode">
3939
<el-form-item prop="captcha">
4040
<div class="flex-between w-full">
4141
<el-input
@@ -208,73 +208,18 @@ const loginHandle = () => {
208208
.then(() => {
209209
locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US'
210210
localStorage.setItem('workspace_id', 'default')
211-
localStorage.removeItem(loginForm.value.username)
212211
router.push({name: 'home'})
213212
})
214213
.catch(() => {
215214
const username = loginForm.value.username
216-
localStorage.setItem(username, String(Number(localStorage.getItem(username) || '0') + 1))
217215
loading.value = false
218-
loginForm.value.username = ''
219-
loginForm.value.password = ''
220-
loginForm.value.captcha = ''
221-
const timestampKey = `${username}_first_fail_timestamp`
222-
if (!localStorage.getItem(timestampKey)) {
223-
localStorage.setItem(timestampKey, Date.now().toString())
224-
}
216+
makeCode(username)
225217
})
226218
}
227219
}
228220
})
229221
}
230222
231-
const showCaptcha = computed<boolean>(() => {
232-
if (!authSetting.value) return true
233-
234-
const maxAttempts = authSetting.value.max_attempts
235-
236-
// -1 表示一直不显示
237-
if (maxAttempts === -1) {
238-
return false
239-
}
240-
241-
// 0 表示一直显示
242-
if (maxAttempts === 0) {
243-
return true
244-
}
245-
246-
// 大于 0,根据登录失败次数决定
247-
const username = loginForm.value.username?.trim()
248-
if (!username) {
249-
return false // 没有输入用户名时不显示
250-
}
251-
252-
const timestampKey = `${username}_first_fail_timestamp`
253-
const firstFailTimestamp = localStorage.getItem(timestampKey)
254-
255-
if (firstFailTimestamp) {
256-
const expirationTime = 10 * 60 * 1000 // 10分钟毫秒数
257-
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
258-
// 过期则清除记录
259-
localStorage.removeItem(username)
260-
localStorage.removeItem(timestampKey)
261-
return false
262-
}
263-
} else {
264-
// 如果没有时间戳但有失败次数,可能是旧数据,清除失败次数
265-
const failCount = Number(localStorage.getItem(username) || '0')
266-
if (failCount > 0) {
267-
localStorage.removeItem(username)
268-
return false
269-
}
270-
}
271-
272-
const failCount = Number(localStorage.getItem(username) || '0')
273-
console.log('failCount', failCount)
274-
275-
return failCount >= maxAttempts
276-
})
277-
278223
function makeCode(username?: string) {
279224
loginApi.getCaptcha(username).then((res: any) => {
280225
if (res && res.data && res.data.captcha) {
@@ -286,9 +231,7 @@ function makeCode(username?: string) {
286231
}
287232
288233
function handleUsernameBlur(username: string) {
289-
if (showCaptcha.value) {
290-
makeCode(username)
291-
}
234+
makeCode(username)
292235
}
293236
294237
onBeforeMount(() => {

0 commit comments

Comments
 (0)