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,245 +287,254 @@ function handleUsernameBlur(username: string) {
} }
onBeforeMount(() => { onBeforeMount(() => {
authApi.getLoginAuthSetting().then((res) => { user.asyncGetProfile().then((res) => {
if (Object.keys(res.data).length > 0) { //
authSetting.value = res.data; if (user.isPE() || user.isEE()) {
authApi.getLoginAuthSetting().then((res) => {
if (Object.keys(res.data).length > 0) {
authSetting.value = res.data;
} else {
authSetting.value = {
max_attempts: 1,
default_value: 'password',
}
}
const params = route.query
if (params.login_mode !== 'manual') {
const defaultMode = authSetting.value.default_value
if (['lark', 'wecom', 'dingtalk'].includes(defaultMode)) {
changeMode('QR_CODE', false)
defaultQrTab.value = defaultMode
} else {
changeMode(defaultMode, false)
}
}
})
} else { } else {
authSetting.value = { authSetting.value = {
max_attempts: 1, max_attempts: 1,
default_value: 'password', default_value: 'password',
} }
} }
const params = route.query })
if (params.login_mode !== 'manual') {
const defaultMode = authSetting.value.default_value const modeList = ref<string[]>([''])
if (['lark', 'wecom', 'dingtalk'].includes(defaultMode)) { const QrList = ref<any[]>([''])
changeMode('QR_CODE', false) const loginMode = ref('')
defaultQrTab.value = defaultMode const showQrCodeTab = ref(false)
interface qrOption {
key: string
value: string
}
const orgOptions = ref<qrOption[]>([])
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
const newDefaultSlogan = computed(() => {
const default_login = '强大易用的企业级智能体平台'
if (!theme.themeInfo?.slogan || default_login == theme.themeInfo?.slogan) {
return t('theme.defaultSlogan')
} else {
return theme.themeInfo?.slogan
}
})
function redirectAuth(authType: string, needMessage: boolean = true) {
if (authType === 'LDAP' || authType === '') {
return
}
authApi.getLoginViewAuthSetting(authType, loading).then((res: any) => {
if (!res.data || !res.data.config) {
return
}
const config = res.data.config
// redirectUrl
const redirectUrl = `${config.redirectUrl}`
let url
if (authType === 'CAS') {
url = config.ldpUri
url +=
url.indexOf('?') !== -1
? `&service=${encodeURIComponent(redirectUrl)}`
: `?service=${encodeURIComponent(redirectUrl)}`
} else if (authType === 'OIDC') {
const scope = config.scope || 'openid+profile+email'
url = `${config.authEndpoint}?client_id=${config.clientId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}`
if (config.state) {
url += `&state=${config.state}`
}
} else if (authType === 'OAuth2') {
url = `${config.authEndpoint}?client_id=${config.clientId}&response_type=code&redirect_uri=${redirectUrl}&state=${uuidv4()}`
if (config.scope) {
url += `&scope=${config.scope}`
}
}
if (!url) {
return
}
if (needMessage) {
MsgConfirm(t('views.login.jump_tip'), '', {
confirmButtonText: t('views.login.jump'),
cancelButtonText: t('common.cancel'),
confirmButtonClass: '',
})
.then(() => {
window.location.href = url
})
.catch(() => {
})
} else { } else {
changeMode(defaultMode, false) console.log('url', url)
window.location.href = url
} }
} })
})
})
const modeList = ref<string[]>([''])
const QrList = ref<any[]>([''])
const loginMode = ref('')
const showQrCodeTab = ref(false)
interface qrOption {
key: string
value: string
}
const orgOptions = ref<qrOption[]>([])
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
const newDefaultSlogan = computed(() => {
const default_login = '强大易用的企业级智能体平台'
if (!theme.themeInfo?.slogan || default_login == theme.themeInfo?.slogan) {
return t('theme.defaultSlogan')
} else {
return theme.themeInfo?.slogan
} }
})
function redirectAuth(authType: string, needMessage: boolean = true) { function changeMode(val: string, needMessage: boolean = true) {
if (authType === 'LDAP' || authType === '') { loginMode.value = val === 'LDAP' ? val : ''
return if (val === 'QR_CODE') {
} loginMode.value = val
authApi.getAuthSetting(authType, loading).then((res: any) => { showQrCodeTab.value = true
if (!res.data || !res.data.config) {
return return
} }
showQrCodeTab.value = false
const config = res.data.config loginForm.value = {
// redirectUrl username: '',
const redirectUrl = `${config.redirectUrl}` password: '',
let url captcha: '',
if (authType === 'CAS') {
url = config.ldpUri
url +=
url.indexOf('?') !== -1
? `&service=${encodeURIComponent(redirectUrl)}`
: `?service=${encodeURIComponent(redirectUrl)}`
} else if (authType === 'OIDC') {
const scope = config.scope || 'openid+profile+email'
url = `${config.authEndpoint}?client_id=${config.clientId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}`
if (config.state) {
url += `&state=${config.state}`
}
} else if (authType === 'OAuth2') {
url = `${config.authEndpoint}?client_id=${config.clientId}&response_type=code&redirect_uri=${redirectUrl}&state=${uuidv4()}`
if (config.scope) {
url += `&scope=${config.scope}`
}
} }
if (!url) { redirectAuth(val, needMessage)
return loginFormRef.value?.clearValidate()
}
if (needMessage) {
MsgConfirm(t('views.login.jump_tip'), '', {
confirmButtonText: t('views.login.jump'),
cancelButtonText: t('common.cancel'),
confirmButtonClass: '',
})
.then(() => {
window.location.href = url
})
.catch(() => {
})
} else {
console.log('url', url)
window.location.href = url
}
})
}
function changeMode(val: string, needMessage: boolean = true) {
loginMode.value = val === 'LDAP' ? val : ''
if (val === 'QR_CODE') {
loginMode.value = val
showQrCodeTab.value = true
return
} }
showQrCodeTab.value = false
loginForm.value = {
username: '',
password: '',
captcha: '',
}
redirectAuth(val, needMessage)
loginFormRef.value?.clearValidate()
}
onBeforeMount(() => { onBeforeMount(() => {
loading.value = true loading.value = true
user.asyncGetProfile().then((res) => { user.asyncGetProfile().then((res) => {
// //
if (user.isPE() || user.isEE()) { if (user.isPE() || user.isEE()) {
login login
.getAuthType() .getAuthType()
.then((res) => { .then((res) => {
//LDAPLDAP //LDAPLDAP
const ldapIndex = res.indexOf('LDAP') const ldapIndex = res.indexOf('LDAP')
if (ldapIndex !== -1) { if (ldapIndex !== -1) {
const [ldap] = res.splice(ldapIndex, 1) const [ldap] = res.splice(ldapIndex, 1)
res.unshift(ldap) res.unshift(ldap)
} }
modeList.value = [...modeList.value, ...res] modeList.value = [...modeList.value, ...res]
}) })
.finally(() => (loading.value = false)) .finally(() => (loading.value = false))
login login
.getQrType() .getQrType()
.then((res) => { .then((res) => {
if (res.length > 0) { if (res.length > 0) {
modeList.value = ['QR_CODE', ...modeList.value] modeList.value = ['QR_CODE', ...modeList.value]
QrList.value = res QrList.value = res
QrList.value.forEach((item) => { QrList.value.forEach((item) => {
orgOptions.value.push({ orgOptions.value.push({
key: item, key: item,
value: value:
item === 'wecom' item === 'wecom'
? t('views.system.authentication.scanTheQRCode.wecom') ? t('views.system.authentication.scanTheQRCode.wecom')
: item === 'dingtalk' : item === 'dingtalk'
? t('views.system.authentication.scanTheQRCode.dingtalk') ? t('views.system.authentication.scanTheQRCode.dingtalk')
: t('views.system.authentication.scanTheQRCode.lark'), : t('views.system.authentication.scanTheQRCode.lark'),
})
}) })
}) }
} })
}) .finally(() => (loading.value = false))
.finally(() => (loading.value = false)) } else {
} else { 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])
const client = params.get('client') const client = params.get('client')
const handleDingTalk = () => { const handleDingTalk = () => {
const code = params.get('corpId') const code = params.get('corpId')
if (code) { if (code) {
dd.runtime.permission.requestAuthCode({corpId: code}).then((res) => { dd.runtime.permission.requestAuthCode({corpId: code}).then((res) => {
console.log('DingTalk client request success:', res) console.log('DingTalk client request success:', res)
login.dingOauth2Callback(res.code).then(() => { login.dingOauth2Callback(res.code).then(() => {
router.push({name: 'home'})
})
})
}
}
const handleLark = () => {
const appId = params.get('appId')
const callRequestAuthCode = () => {
window.tt?.requestAuthCode({
appId: appId,
success: (res: any) => {
login.larkCallback(res.code).then(() => {
router.push({name: 'home'}) router.push({name: 'home'})
}) })
}, })
fail: (error: any) => { }
MsgError(error)
},
})
} }
loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', { const handleLark = () => {
jsId: 'lark-sdk', const appId = params.get('appId')
forceReload: true, const callRequestAuthCode = () => {
}) window.tt?.requestAuthCode({
.then(() => { appId: appId,
if (window.tt) { success: (res: any) => {
window.tt.requestAccess({ login.larkCallback(res.code).then(() => {
appID: appId, router.push({name: 'home'})
scopeList: [], })
success: (res: any) => { },
login.larkCallback(res.code).then(() => { fail: (error: any) => {
router.push({name: 'home'}) MsgError(error)
}) },
}, })
fail: (error: any) => { }
const {errno} = error
if (errno === 103) {
callRequestAuthCode()
}
},
})
} else {
callRequestAuthCode()
}
})
.catch((error) => {
console.error('SDK 加载失败:', error)
})
}
switch (client) { loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', {
case 'dingtalk': jsId: 'lark-sdk',
handleDingTalk() forceReload: true,
break })
case 'lark': .then(() => {
handleLark() if (window.tt) {
break window.tt.requestAccess({
default: appID: appId,
break scopeList: [],
} success: (res: any) => {
}) login.larkCallback(res.code).then(() => {
router.push({name: 'home'})
})
},
fail: (error: any) => {
const {errno} = error
if (errno === 103) {
callRequestAuthCode()
}
},
})
} else {
callRequestAuthCode()
}
})
.catch((error) => {
console.error('SDK 加载失败:', error)
})
}
switch (client) {
case 'dingtalk':
handleDingTalk()
break
case 'lark':
handleLark()
break
default:
break
}
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.login-gradient-divider { .login-gradient-divider {