feat: enhance login logic with dynamic captcha display and add API for fetching authentication settings

v3.2
wxg0103 2025-09-17 15:11:26 +08:00
parent b96b499bc7
commit 680502f366
3 changed files with 302 additions and 221 deletions

View File

@ -50,11 +50,19 @@ const getLoginAuthSetting: (loading?: Ref<boolean>) => Promise<Result<any>> = (l
return get(`login/auth/setting`, undefined, loading) return get(`login/auth/setting`, undefined, loading)
} }
/**
*
*/
const getLoginViewAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (auth_type, loading) => {
return get(`login/${prefix}/${auth_type}/detail`, undefined, loading)
}
export default { export default {
getAuthSetting, getAuthSetting,
postAuthSetting, postAuthSetting,
putAuthSetting, putAuthSetting,
putLoginSetting, putLoginSetting,
getLoginSetting, getLoginSetting,
getLoginAuthSetting getLoginAuthSetting,
getLoginViewAuthSetting
} }

View File

@ -66,6 +66,7 @@
size="large" size="large"
class="input-item" class="input-item"
v-model="loginForm.username" v-model="loginForm.username"
@blur="handleUsernameBlur(loginForm.username)"
:placeholder="$t('views.login.loginForm.username.placeholder')" :placeholder="$t('views.login.loginForm.username.placeholder')"
> >
</el-input> </el-input>
@ -84,7 +85,7 @@
</el-input> </el-input>
</el-form-item> </el-form-item>
</div> </div>
<div class="mb-24" v-if="loginMode !== 'LDAP'"> <div class="mb-24" v-if="loginMode !== 'LDAP'&& showCaptcha">
<el-form-item prop="captcha"> <el-form-item prop="captcha">
<div class="flex-between w-full"> <div class="flex-between w-full">
<el-input <el-input
@ -100,7 +101,7 @@
alt="" alt=""
height="38" height="38"
class="ml-8 cursor border border-r-6" class="ml-8 cursor border border-r-6"
@click="makeCode" @click="makeCode(loginForm.username)"
/> />
</div> </div>
</el-form-item> </el-form-item>
@ -212,6 +213,7 @@ const loginForm = ref<LoginRequest>({
captcha: '', captcha: '',
}) })
const max_attempts = ref<number>(1) // ref
const rules = ref<FormRules<LoginRequest>>({ const rules = ref<FormRules<LoginRequest>>({
username: [ username: [
{ {
@ -253,20 +255,30 @@ const loginHandle = () => {
params: {accessToken: chatUser.accessToken}, params: {accessToken: chatUser.accessToken},
query: route.query, 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 = ''
const timestampKey = `${username}_chat_first_fail_timestamp`
if (!localStorage.getItem(timestampKey)) {
localStorage.setItem(timestampKey, Date.now().toString())
}
}) })
} }
}) })
} }
function makeCode(userrname?: string) { function makeCode(username?: string) {
loginApi.getCaptcha(userrname).then((res: any) => { loginApi.getCaptcha(username).then((res: any) => {
identifyCode.value = res.data.captcha identifyCode.value = res.data.captcha
}) })
} }
onBeforeMount(() => { onBeforeMount(() => {
locale.value = chatUser.getLanguage() locale.value = chatUser.getLanguage()
makeCode()
}) })
const modeList = ref<string[]>([]) const modeList = ref<string[]>([])
@ -365,7 +377,59 @@ function changeMode(val: string) {
loginFormRef.value?.clearValidate() 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)
}
}
onBeforeMount(() => { onBeforeMount(() => {
if (chatUser.chat_profile?.max_attempts) {
max_attempts.value = chatUser.chat_profile.max_attempts
}
if (chatUser.chat_profile?.login_value) { if (chatUser.chat_profile?.login_value) {
modeList.value = chatUser.chat_profile.login_value modeList.value = chatUser.chat_profile.login_value
if (modeList.value.includes('LOCAL')) { if (modeList.value.includes('LOCAL')) {

View File

@ -287,6 +287,9 @@ function handleUsernameBlur(username: string) {
} }
onBeforeMount(() => { onBeforeMount(() => {
user.asyncGetProfile().then((res) => {
//
if (user.isPE() || user.isEE()) {
authApi.getLoginAuthSetting().then((res) => { authApi.getLoginAuthSetting().then((res) => {
if (Object.keys(res.data).length > 0) { if (Object.keys(res.data).length > 0) {
authSetting.value = res.data; authSetting.value = res.data;
@ -307,42 +310,48 @@ onBeforeMount(() => {
} }
} }
}) })
}) } else {
authSetting.value = {
max_attempts: 1,
default_value: 'password',
}
}
})
const modeList = ref<string[]>(['']) const modeList = ref<string[]>([''])
const QrList = ref<any[]>(['']) const QrList = ref<any[]>([''])
const loginMode = ref('') const loginMode = ref('')
const showQrCodeTab = ref(false) const showQrCodeTab = ref(false)
interface qrOption { interface qrOption {
key: string key: string
value: string value: string
} }
const orgOptions = ref<qrOption[]>([]) const orgOptions = ref<qrOption[]>([])
function uuidv4() { function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0 const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8 const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16) return v.toString(16)
}) })
} }
const newDefaultSlogan = computed(() => { const newDefaultSlogan = computed(() => {
const default_login = '强大易用的企业级智能体平台' const default_login = '强大易用的企业级智能体平台'
if (!theme.themeInfo?.slogan || default_login == theme.themeInfo?.slogan) { if (!theme.themeInfo?.slogan || default_login == theme.themeInfo?.slogan) {
return t('theme.defaultSlogan') return t('theme.defaultSlogan')
} else { } else {
return theme.themeInfo?.slogan return theme.themeInfo?.slogan
} }
}) })
function redirectAuth(authType: string, needMessage: boolean = true) { function redirectAuth(authType: string, needMessage: boolean = true) {
if (authType === 'LDAP' || authType === '') { if (authType === 'LDAP' || authType === '') {
return return
} }
authApi.getAuthSetting(authType, loading).then((res: any) => { authApi.getLoginViewAuthSetting(authType, loading).then((res: any) => {
if (!res.data || !res.data.config) { if (!res.data || !res.data.config) {
return return
} }
@ -388,9 +397,9 @@ function redirectAuth(authType: string, needMessage: boolean = true) {
window.location.href = url window.location.href = url
} }
}) })
} }
function changeMode(val: string, needMessage: boolean = true) { function changeMode(val: string, needMessage: boolean = true) {
loginMode.value = val === 'LDAP' ? val : '' loginMode.value = val === 'LDAP' ? val : ''
if (val === 'QR_CODE') { if (val === 'QR_CODE') {
loginMode.value = val loginMode.value = val
@ -405,9 +414,9 @@ function changeMode(val: string, needMessage: boolean = true) {
} }
redirectAuth(val, needMessage) redirectAuth(val, needMessage)
loginFormRef.value?.clearValidate() loginFormRef.value?.clearValidate()
} }
onBeforeMount(() => { onBeforeMount(() => {
loading.value = true loading.value = true
user.asyncGetProfile().then((res) => { user.asyncGetProfile().then((res) => {
// //
@ -448,10 +457,10 @@ onBeforeMount(() => {
loading.value = false loading.value = false
} }
}) })
}) })
declare const window: any declare const window: any
onMounted(() => { onMounted(() => {
const route = useRoute() const route = useRoute()
const currentUrl = ref(route.fullPath) const currentUrl = ref(route.fullPath)
const params = new URLSearchParams(currentUrl.value.split('?')[1]) const params = new URLSearchParams(currentUrl.value.split('?')[1])
@ -525,7 +534,7 @@ onMounted(() => {
default: default:
break break
} }
}) })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.login-gradient-divider { .login-gradient-divider {