Skip to content

Commit 680502f

Browse files
committed
feat: enhance login logic with dynamic captcha display and add API for fetching authentication settings
1 parent b96b499 commit 680502f

File tree

3 files changed

+295
-214
lines changed

3 files changed

+295
-214
lines changed

ui/src/api/system-settings/auth-setting.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ const getLoginAuthSetting: (loading?: Ref<boolean>) => Promise<Result<any>> = (l
5050
return get(`login/auth/setting`, undefined, loading)
5151
}
5252

53+
/**
54+
* 获取认证设置
55+
*/
56+
const getLoginViewAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (auth_type, loading) => {
57+
return get(`login/${prefix}/${auth_type}/detail`, undefined, loading)
58+
}
59+
5360
export default {
5461
getAuthSetting,
5562
postAuthSetting,
5663
putAuthSetting,
5764
putLoginSetting,
5865
getLoginSetting,
59-
getLoginAuthSetting
66+
getLoginAuthSetting,
67+
getLoginViewAuthSetting
6068
}

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

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
size="large"
6767
class="input-item"
6868
v-model="loginForm.username"
69+
@blur="handleUsernameBlur(loginForm.username)"
6970
:placeholder="$t('views.login.loginForm.username.placeholder')"
7071
>
7172
</el-input>
@@ -84,7 +85,7 @@
8485
</el-input>
8586
</el-form-item>
8687
</div>
87-
<div class="mb-24" v-if="loginMode !== 'LDAP'">
88+
<div class="mb-24" v-if="loginMode !== 'LDAP'&& showCaptcha">
8889
<el-form-item prop="captcha">
8990
<div class="flex-between w-full">
9091
<el-input
@@ -100,7 +101,7 @@
100101
alt=""
101102
height="38"
102103
class="ml-8 cursor border border-r-6"
103-
@click="makeCode"
104+
@click="makeCode(loginForm.username)"
104105
/>
105106
</div>
106107
</el-form-item>
@@ -212,6 +213,7 @@ const loginForm = ref<LoginRequest>({
212213
captcha: '',
213214
})
214215
216+
const max_attempts = ref<number>(1) // 声明为 ref
215217
const rules = ref<FormRules<LoginRequest>>({
216218
username: [
217219
{
@@ -253,20 +255,30 @@ const loginHandle = () => {
253255
params: {accessToken: chatUser.accessToken},
254256
query: route.query,
255257
})
258+
localStorage.removeItem('chat_' + loginForm.value.username)
259+
}).catch(() => {
260+
const username = loginForm.value.username
261+
localStorage.setItem('chat_' + username, String(Number(localStorage.getItem('chat_' + username) || '0') + 1))
262+
loading.value = false
263+
loginForm.value.username = ''
264+
loginForm.value.password = ''
265+
const timestampKey = `${username}_chat_first_fail_timestamp`
266+
if (!localStorage.getItem(timestampKey)) {
267+
localStorage.setItem(timestampKey, Date.now().toString())
268+
}
256269
})
257270
}
258271
})
259272
}
260273
261-
function makeCode(userrname?: string) {
262-
loginApi.getCaptcha(userrname).then((res: any) => {
274+
function makeCode(username?: string) {
275+
loginApi.getCaptcha(username).then((res: any) => {
263276
identifyCode.value = res.data.captcha
264277
})
265278
}
266279
267280
onBeforeMount(() => {
268281
locale.value = chatUser.getLanguage()
269-
makeCode()
270282
})
271283
272284
const modeList = ref<string[]>([])
@@ -365,7 +377,59 @@ function changeMode(val: string) {
365377
loginFormRef.value?.clearValidate()
366378
}
367379
380+
const showCaptcha = computed<boolean>(() => {
381+
// -1 表示一直不显示
382+
if (max_attempts.value === -1) {
383+
return false
384+
}
385+
386+
// 0 表示一直显示
387+
if (max_attempts.value === 0) {
388+
return true
389+
}
390+
391+
// 大于 0,根据登录失败次数决定
392+
const username = loginForm.value.username?.trim()
393+
if (!username) {
394+
return false // 没有输入用户名时不显示
395+
}
396+
397+
const timestampKey = `${username}_chat_first_fail_timestamp`
398+
const firstFailTimestamp = localStorage.getItem(timestampKey)
399+
400+
if (firstFailTimestamp) {
401+
const expirationTime = 60 * 60 * 1000 // 10分钟毫秒数
402+
if (Date.now() - parseInt(firstFailTimestamp) > expirationTime) {
403+
// 过期则清除记录
404+
localStorage.removeItem('chat_' + username)
405+
localStorage.removeItem(timestampKey)
406+
return false
407+
}
408+
} else {
409+
// 如果没有时间戳但有失败次数,可能是旧数据,清除失败次数
410+
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
411+
if (failCount > 0) {
412+
localStorage.removeItem('chat_' + username)
413+
return false
414+
}
415+
}
416+
417+
const failCount = Number(localStorage.getItem('chat_' + username) || '0')
418+
console.log('failCount', failCount)
419+
420+
return failCount >= max_attempts.value
421+
})
422+
423+
function handleUsernameBlur(username: string) {
424+
if (showCaptcha.value) {
425+
makeCode(username)
426+
}
427+
}
428+
368429
onBeforeMount(() => {
430+
if (chatUser.chat_profile?.max_attempts) {
431+
max_attempts.value = chatUser.chat_profile.max_attempts
432+
}
369433
if (chatUser.chat_profile?.login_value) {
370434
modeList.value = chatUser.chat_profile.login_value
371435
if (modeList.value.includes('LOCAL')) {

0 commit comments

Comments
 (0)