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

348 lines
9.8 KiB
Vue
Raw Normal View History

2025-06-12 04:03:43 +00:00
<template>
<UserLoginLayout v-if="!loading" v-loading="loading">
2025-06-19 12:02:52 +00:00
<LoginContainer
v-if="chatUser.chat_profile?.authentication_type == 'password'"
:subTitle="theme.themeInfo?.slogan || $t('theme.defaultSlogan')"
>
<PasswordAuth></PasswordAuth>
</LoginContainer>
<LoginContainer v-else :subTitle="theme.themeInfo?.slogan || $t('theme.defaultSlogan')">
2025-06-12 09:14:34 +00:00
<h2 class="mb-24" v-if="!showQrCodeTab">
2025-06-17 12:26:20 +00:00
{{ loginMode == 'LOCAL' ? $t('views.login.title') : loginMode }}
</h2>
2025-06-12 04:03:43 +00:00
<div v-if="!showQrCodeTab">
<el-form
class="login-form"
:rules="rules"
:model="loginForm"
ref="loginFormRef"
@keyup.enter="login"
>
<div class="mb-24">
<el-form-item prop="username">
<el-input
size="large"
class="input-item"
v-model="loginForm.username"
:placeholder="$t('views.login.loginForm.username.placeholder')"
>
</el-input>
</el-form-item>
</div>
<div class="mb-24">
<el-form-item prop="password">
<el-input
type="password"
size="large"
class="input-item"
v-model="loginForm.password"
:placeholder="$t('views.login.loginForm.password.placeholder')"
show-password
>
</el-input>
</el-form-item>
</div>
<div class="mb-24" v-if="loginMode !== 'LDAP'">
<el-form-item prop="captcha">
<div class="flex-between w-full">
<el-input
size="large"
class="input-item"
v-model="loginForm.captcha"
:placeholder="$t('views.login.loginForm.captcha.placeholder')"
>
</el-input>
<img
:src="identifyCode"
alt=""
height="38"
class="ml-8 cursor border border-r-4"
@click="makeCode"
/>
</div>
</el-form-item>
</div>
</el-form>
<el-button
size="large"
type="primary"
class="w-full"
@click="loginHandle"
:loading="loading"
>
{{ $t('views.login.buttons.login') }}
</el-button>
<div class="operate-container flex-between mt-12">
<el-button
:loading="loading"
class="forgot-password"
@click="router.push('/forgot_password')"
link
type="primary"
>
{{ $t('views.login.forgotPassword') }}?
</el-button>
</div>
</div>
<div v-if="showQrCodeTab">
2025-06-17 12:26:20 +00:00
<QrCodeTab :tabs="orgOptions" />
2025-06-12 04:03:43 +00:00
</div>
<div class="login-gradient-divider lighter mt-24" v-if="modeList.length > 1">
<span>{{ $t('views.login.moreMethod') }}</span>
</div>
<div class="text-center mt-16">
<template v-for="item in modeList">
<el-button
2025-06-12 09:14:34 +00:00
v-if="item !== 'LOCAL' && loginMode !== item && item !== 'QR_CODE'"
2025-06-12 04:03:43 +00:00
circle
:key="item"
class="login-button-circle color-secondary"
@click="changeMode(item)"
>
<span
:style="{
'font-size': item === 'OAUTH2' ? '8px' : '10px',
color: user.themeInfo?.theme,
}"
2025-06-17 12:26:20 +00:00
>{{ item }}</span
2025-06-12 04:03:43 +00:00
>
</el-button>
<el-button
v-if="item === 'QR_CODE' && loginMode !== item"
circle
:key="item"
class="login-button-circle color-secondary"
@click="changeMode('QR_CODE')"
>
2025-06-17 12:26:20 +00:00
<img src="@/assets/scan/icon_qr_outlined.svg" width="25px" />
2025-06-12 04:03:43 +00:00
</el-button>
<el-button
2025-06-12 09:14:34 +00:00
v-if="item === 'LOCAL' && loginMode != 'LOCAL'"
2025-06-12 04:03:43 +00:00
circle
:key="item"
class="login-button-circle color-secondary"
style="font-size: 24px"
icon="UserFilled"
2025-06-12 09:14:34 +00:00
@click="changeMode('LOCAL')"
2025-06-12 04:03:43 +00:00
/>
</template>
</div>
</LoginContainer>
</UserLoginLayout>
</template>
<script setup lang="ts">
2025-06-17 12:26:20 +00:00
import { onMounted, ref, onBeforeMount } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import type { LoginRequest } from '@/api/type/login'
2025-06-12 04:03:43 +00:00
import LoginContainer from '@/layout/login-layout/LoginContainer.vue'
import UserLoginLayout from '@/layout/login-layout/UserLoginLayout.vue'
2025-06-17 08:31:07 +00:00
import loginApi from '@/api/chat/chat.ts'
2025-06-17 12:26:20 +00:00
import { t, getBrowserLang } from '@/locales'
2025-06-12 04:03:43 +00:00
import useStore from '@/stores'
2025-06-17 12:26:20 +00:00
import { useI18n } from 'vue-i18n'
2025-06-12 04:03:43 +00:00
import QrCodeTab from '@/views/login/scanCompinents/QrCodeTab.vue'
2025-06-17 12:26:20 +00:00
import { MsgConfirm, MsgError } from '@/utils/message.ts'
2025-06-19 12:02:52 +00:00
import PasswordAuth from '@/views/chat/auth/component/password.vue'
2025-06-17 12:26:20 +00:00
import useUserStore from '@/stores/modules/user.ts'
2025-06-12 08:15:02 +00:00
2025-06-12 04:03:43 +00:00
const router = useRouter()
2025-06-17 12:26:20 +00:00
const { login, user, theme, chatUser } = useStore()
const { locale } = useI18n({ useScope: 'global' })
2025-06-12 04:03:43 +00:00
const loading = ref<boolean>(false)
2025-06-12 08:15:02 +00:00
const route = useRoute()
2025-06-12 04:03:43 +00:00
const identifyCode = ref<string>('')
2025-06-12 08:15:02 +00:00
const {
2025-06-17 12:26:20 +00:00
params: { accessToken },
2025-06-12 08:15:02 +00:00
} = route as any
2025-06-12 04:03:43 +00:00
const loginFormRef = ref<FormInstance>()
const loginForm = ref<LoginRequest>({
username: '',
password: '',
captcha: '',
})
const rules = ref<FormRules<LoginRequest>>({
username: [
{
required: true,
message: t('views.login.loginForm.username.requiredMessage'),
trigger: 'blur',
},
],
password: [
{
required: true,
message: t('views.login.loginForm.password.requiredMessage'),
trigger: 'blur',
},
],
captcha: [
{
required: true,
message: t('views.login.loginForm.captcha.requiredMessage'),
trigger: 'blur',
},
],
})
const loginHandle = () => {
loginFormRef.value?.validate().then(() => {
if (loginMode.value === 'LDAP') {
2025-06-17 12:26:20 +00:00
chatUser.ldapLogin(loginForm.value).then((ok) => {
router.push({ name: 'chat', params: { accessToken: chatUser.accessToken } })
2025-06-12 04:03:43 +00:00
})
} else {
2025-06-17 12:26:20 +00:00
chatUser.login(loginForm.value).then((ok) => {
router.push({ name: 'chat', params: { accessToken: chatUser.accessToken } })
2025-06-12 04:03:43 +00:00
})
}
})
}
function makeCode() {
loginApi.getCaptcha().then((res: any) => {
identifyCode.value = res.data.captcha
})
}
onBeforeMount(() => {
makeCode()
})
2025-06-12 09:14:34 +00:00
const modeList = ref<string[]>([])
2025-06-12 08:15:02 +00:00
//const QrList = ref<any[]>([''])
2025-06-12 04:03:43 +00:00
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)
})
}
2025-06-12 08:15:02 +00:00
function redirectAuth(authType: string, needMessage: boolean = false) {
2025-06-12 04:03:43 +00:00
if (authType === 'LDAP' || authType === '') {
return
}
2025-06-17 08:31:07 +00:00
loginApi.getAuthSetting(authType, loading).then((res: any) => {
2025-06-12 08:15:02 +00:00
if (!res.data || !res.data.config) {
2025-06-17 12:26:20 +00:00
return
2025-06-12 04:03:43 +00:00
}
2025-06-12 08:15:02 +00:00
2025-06-17 12:26:20 +00:00
const config = res.data.config
const redirectUrl = eval(`\`${config.redirectUrl}/${accessToken}\``)
let url
2025-06-12 08:15:02 +00:00
if (authType === 'CAS') {
2025-06-17 12:26:20 +00:00
url = config.ldpUri
url +=
url.indexOf('?') !== -1
? `&service=${encodeURIComponent(redirectUrl)}`
: `?service=${encodeURIComponent(redirectUrl)}`
2025-06-12 08:15:02 +00:00
} else if (authType === 'OIDC') {
2025-06-17 12:26:20 +00:00
const scope = config.scope || 'openid+profile+email'
url = `${config.authEndpoint}?client_id=${config.clientId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}`
2025-06-12 08:15:02 +00:00
if (config.state) {
2025-06-17 12:26:20 +00:00
url += `&state=${config.state}`
2025-06-12 08:15:02 +00:00
}
} else if (authType === 'OAuth2') {
2025-06-17 12:26:20 +00:00
url = `${config.authEndpoint}?client_id=${config.clientId}&response_type=code&redirect_uri=${redirectUrl}&state=${uuidv4()}`
2025-06-12 08:15:02 +00:00
if (config.scope) {
2025-06-17 12:26:20 +00:00
url += `&scope=${config.scope}`
2025-06-12 08:15:02 +00:00
}
}
if (!url) {
2025-06-17 12:26:20 +00:00
return
2025-06-12 08:15:02 +00:00
}
if (needMessage) {
MsgConfirm(t('views.login.jump_tip'), '', {
confirmButtonText: t('views.login.jump'),
cancelButtonText: t('common.cancel'),
confirmButtonClass: '',
2025-06-12 04:03:43 +00:00
})
2025-06-12 08:15:02 +00:00
.then(() => {
2025-06-17 12:26:20 +00:00
window.location.href = url
2025-06-12 08:15:02 +00:00
})
2025-06-17 12:26:20 +00:00
.catch(() => {})
2025-06-12 08:15:02 +00:00
} else {
2025-06-17 12:26:20 +00:00
console.log('url', url)
window.location.href = url
2025-06-12 08:15:02 +00:00
}
2025-06-17 12:26:20 +00:00
})
2025-06-12 04:03:43 +00:00
}
function changeMode(val: string) {
2025-06-12 09:14:34 +00:00
loginMode.value = val === 'LDAP' ? val : 'LOCAL'
2025-06-12 04:03:43 +00:00
if (val === 'QR_CODE') {
loginMode.value = val
showQrCodeTab.value = true
return
}
showQrCodeTab.value = false
loginForm.value = {
username: '',
password: '',
captcha: '',
}
redirectAuth(val)
loginFormRef.value?.clearValidate()
}
onBeforeMount(() => {
2025-06-17 08:18:41 +00:00
if (chatUser.chat_profile?.login_value) {
modeList.value = chatUser.chat_profile.login_value
loginMode.value = modeList.value[0] || 'LOCAL'
if (modeList.value.length == 1 && ['CAS', 'OIDC', 'OAuth2'].includes(modeList.value[0])) {
redirectAuth(modeList.value[0])
2025-06-12 04:03:43 +00:00
}
2025-06-17 08:18:41 +00:00
}
2025-06-12 04:03:43 +00:00
})
</script>
<style lang="scss" scoped>
.login-gradient-divider {
position: relative;
text-align: center;
color: var(--el-color-info);
::before {
content: '';
width: 25%;
height: 1px;
background: linear-gradient(90deg, rgba(222, 224, 227, 0) 0%, #dee0e3 100%);
position: absolute;
left: 16px;
top: 50%;
}
::after {
content: '';
width: 25%;
height: 1px;
background: linear-gradient(90deg, #dee0e3 0%, rgba(222, 224, 227, 0) 100%);
position: absolute;
right: 16px;
top: 50%;
}
}
.login-button-circle {
padding: 20px !important;
margin: 0 4px;
width: 32px;
height: 32px;
text-align: center;
}
</style>