UnisKB/ui/src/views/login/index.vue

387 lines
11 KiB
Vue
Raw Normal View History

2023-09-15 09:40:35 +00:00
<template>
<login-layout v-if="!loading" v-loading="loading">
2025-01-21 07:25:21 +00:00
<LoginContainer :subTitle="user.themeInfo?.slogan || $t('views.system.theme.defaultSlogan')">
<h2 class="mb-24" v-if="!showQrCodeTab">{{ loginMode || $t('views.login.title') }}</h2>
2024-10-09 02:59:45 +00:00
<div v-if="!showQrCodeTab">
<el-form
class="login-form"
:rules="rules"
:model="loginForm"
ref="loginFormRef"
@keyup.enter="login"
2024-10-09 02:59:45 +00:00
>
<div class="mb-24">
<el-form-item prop="username">
<el-input
size="large"
class="input-item"
v-model="loginForm.username"
2025-01-21 07:25:21 +00:00
:placeholder="$t('views.user.userForm.form.username.placeholder')"
2024-10-09 02:59:45 +00:00
>
</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"
2025-01-21 07:25:21 +00:00
:placeholder="$t('views.user.userForm.form.password.placeholder')"
show-password
2024-10-09 02:59:45 +00:00
>
</el-input>
</el-form-item>
</div>
</el-form>
<el-button size="large" type="primary" class="w-full" @click="login"
>{{ $t('views.login.buttons.login') }}
</el-button>
2024-10-09 02:59:45 +00:00
<div class="operate-container flex-between mt-12">
<!-- <el-button class="register" @click="router.push('/register')" link type="primary">
2023-10-12 08:36:16 +00:00
注册
2024-03-21 06:19:01 +00:00
</el-button> -->
2024-10-09 02:59:45 +00:00
<el-button
class="forgot-password"
@click="router.push('/forgot_password')"
link
type="primary"
2024-10-09 02:59:45 +00:00
>
2025-01-21 07:25:21 +00:00
{{ $t('views.login.forgotPassword') }}?
2024-10-09 02:59:45 +00:00
</el-button>
</div>
</div>
<div v-if="showQrCodeTab">
<QrCodeTab :tabs="orgOptions" />
2023-10-12 08:36:16 +00:00
</div>
2024-07-11 06:44:18 +00:00
2024-07-11 11:51:28 +00:00
<div class="login-gradient-divider lighter mt-24" v-if="modeList.length > 1">
2025-01-21 07:25:21 +00:00
<span>{{ $t('views.login.moreMethod') }}</span>
2024-07-11 06:44:18 +00:00
</div>
<div class="text-center mt-16">
2024-07-11 11:51:28 +00:00
<template v-for="item in modeList">
<el-button
2024-10-09 02:59:45 +00:00
v-if="item !== '' && loginMode !== item && item !== 'QR_CODE'"
circle
:key="item"
class="login-button-circle color-secondary"
@click="changeMode(item)"
>
<span
:style="{
'font-size': item === 'OAUTH2' ? '8px' : '10px',
color: user.themeInfo?.theme
}"
>{{ item }}</span
>
2024-07-11 11:51:28 +00:00
</el-button>
<el-button
2024-10-09 02:59:45 +00:00
v-if="item === 'QR_CODE' && loginMode !== item"
circle
:key="item"
class="login-button-circle color-secondary"
@click="changeMode('QR_CODE')"
>
<img src="@/assets/icon_qr_outlined.svg" width="25px" />
</el-button>
<el-button
v-if="item === '' && loginMode !== ''"
circle
:key="item"
class="login-button-circle color-secondary"
style="font-size: 24px"
icon="UserFilled"
@click="changeMode('')"
2024-07-11 11:51:28 +00:00
/>
</template>
</div>
2023-10-12 08:36:16 +00:00
</LoginContainer>
</login-layout>
2023-09-15 09:40:35 +00:00
</template>
<script setup lang="ts">
2024-10-09 02:59:45 +00:00
import { onMounted, ref, onBeforeMount } from 'vue'
import type { LoginRequest } from '@/api/type/user'
import { useRoute, useRouter } from 'vue-router'
2024-10-09 02:59:45 +00:00
import type { FormInstance, FormRules } from 'element-plus'
2023-10-19 10:18:45 +00:00
import useStore from '@/stores'
2024-10-09 02:59:45 +00:00
import authApi from '@/api/auth-setting'
import { MsgConfirm, MsgError, MsgSuccess } from '@/utils/message'
2025-01-20 12:20:44 +00:00
2025-02-05 10:55:05 +00:00
import { t, getBrowserLang } from '@/locales'
2024-10-09 02:59:45 +00:00
import QrCodeTab from '@/views/login/components/QrCodeTab.vue'
2025-01-20 12:20:44 +00:00
import { useI18n } from 'vue-i18n'
import * as dd from 'dingtalk-jsapi'
import { loadScript } from '@/utils/utils'
2025-01-20 12:20:44 +00:00
const { locale } = useI18n({ useScope: 'global' })
2023-10-12 08:36:16 +00:00
const loading = ref<boolean>(false)
2024-10-09 02:59:45 +00:00
const { user } = useStore()
2023-09-15 09:40:35 +00:00
const router = useRouter()
const loginForm = ref<LoginRequest>({
2023-10-12 08:36:16 +00:00
username: '',
password: ''
})
2023-09-15 09:40:35 +00:00
const rules = ref<FormRules<LoginRequest>>({
2023-10-12 08:36:16 +00:00
username: [
{
required: true,
2025-01-21 07:25:21 +00:00
message: t('views.user.userForm.form.username.requiredMessage'),
2023-10-12 08:36:16 +00:00
trigger: 'blur'
}
],
password: [
{
required: true,
2025-01-21 07:25:21 +00:00
message: t('views.user.userForm.form.password.requiredMessage'),
2023-10-12 08:36:16 +00:00
trigger: 'blur'
2024-03-21 06:19:01 +00:00
}
2023-10-12 08:36:16 +00:00
]
2023-09-15 09:40:35 +00:00
})
const loginFormRef = ref<FormInstance>()
2024-07-16 09:32:27 +00:00
const modeList = ref<string[]>([''])
2024-10-09 02:59:45 +00:00
const QrList = ref<any[]>([''])
2024-07-11 06:44:18 +00:00
const loginMode = ref('')
2024-10-09 02:59:45 +00:00
const showQrCodeTab = ref(false)
2024-10-09 02:59:45 +00:00
interface qrOption {
key: string
value: string
}
2024-10-09 02:59:45 +00:00
const orgOptions = ref<qrOption[]>([])
function redirectAuth(authType: string) {
if (authType === 'LDAP' || authType === '') {
2024-10-09 02:59:45 +00:00
return
}
authApi.getAuthSetting(authType, loading).then((res: any) => {
if (!res.data) {
2024-10-09 02:59:45 +00:00
return
}
2025-01-23 04:10:03 +00:00
MsgConfirm(t('views.login.jump_tip'), '', {
confirmButtonText: t('views.login.jump'),
2025-01-13 03:15:51 +00:00
cancelButtonText: t('common.cancel'),
2024-10-09 02:59:45 +00:00
confirmButtonClass: ''
})
.then(() => {
if (!res.data.config_data) {
return
}
2024-10-09 02:59:45 +00:00
const config = res.data.config_data
const redirectUrl = eval(`\`${config.redirectUrl}\``)
let url
if (authType === 'CAS') {
url = config.ldpUri
if (url.indexOf('?') !== -1) {
url = `${config.ldpUri}&service=${encodeURIComponent(redirectUrl)}`
} else {
url = `${config.ldpUri}?service=${encodeURIComponent(redirectUrl)}`
}
2024-10-09 02:59:45 +00:00
}
if (authType === 'OIDC') {
2025-02-17 11:00:34 +00:00
const scope = config.scope || 'openid+profile+email'
url = `${config.authEndpoint}?client_id=${config.clientId}&redirect_uri=${redirectUrl}&response_type=code&scope=${scope}`
2024-10-09 02:59:45 +00:00
}
if (authType === 'OAuth2') {
url =
`${config.authEndpoint}?client_id=${config.clientId}&response_type=code` +
`&redirect_uri=${redirectUrl}&state=${res.data.id}`
if (config.scope) {
url += `&scope=${config.scope}`
}
}
2024-10-09 02:59:45 +00:00
if (url) {
window.location.href = url
}
})
.catch(() => {})
})
}
2024-07-11 06:44:18 +00:00
function changeMode(val: string) {
2024-10-09 02:59:45 +00:00
loginMode.value = val === 'LDAP' ? val : ''
if (val === 'QR_CODE') {
loginMode.value = val
showQrCodeTab.value = true
return
}
showQrCodeTab.value = false
2024-07-11 06:44:18 +00:00
loginForm.value = {
username: '',
password: ''
}
redirectAuth(val)
2024-07-11 06:44:18 +00:00
loginFormRef.value?.clearValidate()
}
2023-09-15 09:40:35 +00:00
const login = () => {
2023-10-12 08:36:16 +00:00
loginFormRef.value?.validate().then(() => {
loading.value = true
2023-10-19 10:18:45 +00:00
user
2024-10-09 02:59:45 +00:00
.login(loginMode.value, loginForm.value.username, loginForm.value.password)
.then(() => {
locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US'
2024-10-09 02:59:45 +00:00
router.push({ name: 'home' })
})
.finally(() => (loading.value = false))
2023-10-12 08:36:16 +00:00
})
2023-09-15 09:40:35 +00:00
}
2024-07-11 11:51:28 +00:00
onBeforeMount(() => {
loading.value = true
2024-07-11 11:51:28 +00:00
user.asyncGetProfile().then((res) => {
2024-07-30 03:02:49 +00:00
if (user.isEnterprise()) {
2024-07-16 09:32:27 +00:00
user
2024-10-09 02:59:45 +00:00
.getAuthType()
.then((res) => {
//如果结果包含LDAP把LDAP放在第一个
const ldapIndex = res.indexOf('LDAP')
if (ldapIndex !== -1) {
const [ldap] = res.splice(ldapIndex, 1)
res.unshift(ldap)
}
2024-10-09 02:59:45 +00:00
modeList.value = [...modeList.value, ...res]
})
.finally(() => (loading.value = false))
user
.getQrType()
.then((res) => {
if (res.length > 0) {
modeList.value = ['QR_CODE', ...modeList.value]
QrList.value = res
QrList.value.forEach((item) => {
orgOptions.value.push({
key: item,
value:
item === 'wecom'
? t('views.system.authentication.scanTheQRCode.wecom')
: item === 'dingtalk'
? t('views.system.authentication.scanTheQRCode.dingtalk')
: t('views.system.authentication.scanTheQRCode.lark')
2024-10-09 02:59:45 +00:00
})
})
}
})
.finally(() => (loading.value = false))
} else {
loading.value = false
2024-07-11 11:51:28 +00:00
}
})
})
declare const window: any
onMounted(() => {
const route = useRoute()
const currentUrl = ref(route.fullPath)
const params = new URLSearchParams(currentUrl.value.split('?')[1])
const client = params.get('client')
const handleDingTalk = () => {
const code = params.get('corpId')
if (code) {
dd.runtime.permission.requestAuthCode({ corpId: code }).then((res) => {
console.log('DingTalk client request success:', res)
user.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) => {
user.larkCallback(res.code).then(() => {
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', {
jsId: 'lark-sdk',
forceReload: true
})
.then(() => {
if (window.tt) {
window.tt.requestAccess({
appID: appId,
scopeList: [],
success: (res: any) => {
user.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
}
})
2023-09-15 09:40:35 +00:00
</script>
2024-07-11 06:44:18 +00:00
<style lang="scss" scope>
.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%;
}
}
2024-07-11 11:51:28 +00:00
2024-07-11 06:44:18 +00:00
.login-button-circle {
2024-10-09 02:59:45 +00:00
padding: 20px !important;
margin: 0 4px;
width: 32px;
height: 32px;
text-align: center;
2024-07-11 06:44:18 +00:00
}
</style>