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

461 lines
13 KiB
Vue
Raw Normal View History

2025-04-16 11:10:00 +00:00
<template>
<login-layout v-if="!loading" v-loading="loading">
2025-05-12 10:19:28 +00:00
<LoginContainer :subTitle="theme.themeInfo?.slogan || $t('theme.defaultSlogan')">
2025-06-05 11:47:47 +00:00
<h2 class="mb-24" v-if="!showQrCodeTab">{{ loginMode || $t('views.login.title') }}</h2>
<div v-if="!showQrCodeTab">
2025-04-16 11:10:00 +00:00
<el-form
class="login-form"
:rules="rules"
:model="loginForm"
ref="loginFormRef"
2025-07-03 11:05:08 +00:00
@keyup.enter="loginHandle"
2025-04-16 11:10:00 +00:00
>
<div class="mb-24">
<el-form-item prop="username">
<el-input
size="large"
class="input-item"
v-model="loginForm.username"
2025-04-25 08:27:13 +00:00
:placeholder="$t('views.login.loginForm.username.placeholder')"
2025-04-16 11:10:00 +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-04-25 08:27:13 +00:00
:placeholder="$t('views.login.loginForm.password.placeholder')"
2025-04-16 11:10:00 +00:00
show-password
>
</el-input>
</el-form-item>
</div>
2025-06-05 11:47:47 +00:00
<div class="mb-24" v-if="loginMode !== 'LDAP'">
2025-04-25 08:27:13 +00:00
<el-form-item prop="captcha">
2025-04-17 03:28:48 +00:00
<div class="flex-between w-full">
<el-input
size="large"
class="input-item"
2025-04-25 08:27:13 +00:00
v-model="loginForm.captcha"
:placeholder="$t('views.login.loginForm.captcha.placeholder')"
2025-04-17 03:28:48 +00:00
>
</el-input>
2025-04-25 08:27:13 +00:00
2025-06-13 09:17:01 +00:00
<img
:src="identifyCode"
alt=""
height="38"
2025-06-26 08:36:08 +00:00
class="ml-8 cursor border border-r-6"
2025-06-13 09:17:01 +00:00
@click="makeCode"
/>
2025-04-17 03:28:48 +00:00
</div>
</el-form-item>
</div>
2025-04-16 11:10:00 +00:00
</el-form>
2025-04-25 08:27:13 +00:00
<el-button
size="large"
type="primary"
class="w-full"
@click="loginHandle"
:loading="loading"
>
2025-04-27 03:34:36 +00:00
{{ $t('views.login.buttons.login') }}
2025-04-16 11:10:00 +00:00
</el-button>
<div class="operate-container flex-between mt-12">
<el-button
2025-04-25 08:27:13 +00:00
:loading="loading"
2025-04-16 11:10:00 +00:00
class="forgot-password"
@click="router.push('/forgot_password')"
link
type="primary"
>
{{ $t('views.login.forgotPassword') }}?
</el-button>
</div>
</div>
2025-06-05 11:47:47 +00:00
<div v-if="showQrCodeTab">
<QrCodeTab :tabs="orgOptions"/>
2025-06-05 11:47:47 +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
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',
2025-06-20 19:55:48 +00:00
color: theme.themeInfo?.theme,
2025-06-05 11:47:47 +00:00
}"
>{{ item }}</span
2025-06-05 11:47:47 +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')"
>
<img src="@/assets/scan/icon_qr_outlined.svg" width="25px"/>
2025-06-05 11:47:47 +00:00
</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('')"
/>
</template>
</div>
2025-04-16 11:10:00 +00:00
</LoginContainer>
</login-layout>
</template>
<script setup lang="ts">
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 LoginLayout from '@/layout/login-layout/LoginLayout.vue'
2025-04-25 08:27:13 +00:00
import loginApi from '@/api/user/login'
2025-06-11 09:41:53 +00:00
import authApi from '@/api/system-settings/auth-setting'
import {t, getBrowserLang} from '@/locales'
2025-04-16 11:10:00 +00:00
import useStore from '@/stores'
import {useI18n} from 'vue-i18n'
2025-06-13 09:17:01 +00:00
import QrCodeTab from '@/views/login/scanCompinents/QrCodeTab.vue'
import {MsgConfirm, MsgError} from '@/utils/message.ts'
2025-06-05 11:47:47 +00:00
import * as dd from 'dingtalk-jsapi'
import {loadScript} from '@/utils/common'
import {hasPermission} from "@/utils/permission";
import {RoleConst} from "@/utils/permission/data.ts";
2025-06-17 12:35:24 +00:00
2025-04-16 11:10:00 +00:00
const router = useRouter()
const {login, user, theme} = useStore()
const {locale} = useI18n({useScope: 'global'})
2025-04-16 11:10:00 +00:00
const loading = ref<boolean>(false)
2025-04-25 08:27:13 +00:00
const identifyCode = ref<string>('')
2025-04-16 11:10:00 +00:00
const loginFormRef = ref<FormInstance>()
const loginForm = ref<LoginRequest>({
username: '',
password: '',
2025-04-25 08:27:13 +00:00
captcha: '',
2025-04-16 11:10:00 +00:00
})
const rules = ref<FormRules<LoginRequest>>({
username: [
{
required: true,
2025-04-25 08:27:13 +00:00
message: t('views.login.loginForm.username.requiredMessage'),
2025-04-16 11:10:00 +00:00
trigger: 'blur',
},
],
password: [
{
required: true,
2025-04-25 08:27:13 +00:00
message: t('views.login.loginForm.password.requiredMessage'),
trigger: 'blur',
},
],
captcha: [
{
required: true,
message: t('views.login.loginForm.captcha.requiredMessage'),
2025-04-16 11:10:00 +00:00
trigger: 'blur',
},
],
})
2025-04-25 08:27:13 +00:00
const loginHandle = () => {
loginFormRef.value?.validate().then(() => {
2025-06-05 11:47:47 +00:00
if (loginMode.value === 'LDAP') {
login.asyncLdapLogin(loginForm.value, loading).then(() => {
locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US'
router.push({name: 'home'})
2025-06-05 11:47:47 +00:00
})
} else {
login.asyncLogin(loginForm.value, loading).then(() => {
locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US'
2025-06-06 01:39:01 +00:00
localStorage.setItem('workspace_id', 'default')
if (hasPermission([RoleConst.ADMIN, RoleConst.EXTENDS_ADMIN], 'OR')) {
router.push({name: 'user'})
} else {
router.push({name: 'home'})
}
2025-06-05 11:47:47 +00:00
})
}
2025-04-25 08:27:13 +00:00
})
}
2025-06-05 11:47:47 +00:00
2025-04-25 08:27:13 +00:00
function makeCode() {
loginApi.getCaptcha().then((res: any) => {
2025-05-07 02:10:10 +00:00
identifyCode.value = res.data.captcha
2025-04-25 08:27:13 +00:00
})
2025-04-16 11:10:00 +00:00
}
2025-06-05 11:47:47 +00:00
2025-04-16 11:10:00 +00:00
onBeforeMount(() => {
2025-04-25 08:27:13 +00:00
makeCode()
2025-04-16 11:10:00 +00:00
})
2025-06-05 11:47:47 +00:00
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)
})
}
function redirectAuth(authType: string) {
if (authType === 'LDAP' || authType === '') {
return
}
authApi.getAuthSetting(authType, loading).then((res: any) => {
if (!res.data) {
return
}
MsgConfirm(t('views.login.jump_tip'), '', {
confirmButtonText: t('views.login.jump'),
cancelButtonText: t('common.cancel'),
2025-06-13 09:17:01 +00:00
confirmButtonClass: '',
2025-06-05 11:47:47 +00:00
})
.then(() => {
2025-06-12 08:15:02 +00:00
if (!res.data.config) {
2025-06-05 11:47:47 +00:00
return
}
2025-06-12 08:15:02 +00:00
const config = res.data.config
2025-06-05 11:47:47 +00:00
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)}`
}
}
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}`
}
}
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) {
window.location.href = url
}
})
.catch(() => {
})
2025-06-05 11:47:47 +00:00
})
}
function changeMode(val: string) {
loginMode.value = val === 'LDAP' ? val : ''
if (val === 'QR_CODE') {
loginMode.value = val
showQrCodeTab.value = true
return
}
showQrCodeTab.value = false
loginForm.value = {
username: '',
password: '',
2025-06-13 09:17:01 +00:00
captcha: '',
2025-06-05 11:47:47 +00:00
}
redirectAuth(val)
loginFormRef.value?.clearValidate()
}
onBeforeMount(() => {
loading.value = true
user.asyncGetProfile().then((res) => {
2025-06-17 12:35:24 +00:00
// 企业版和专业版:第三方登录
2025-06-24 10:21:09 +00:00
if (user.isPE() || user.isEE()) {
2025-06-13 09:17:01 +00:00
login
2025-06-05 11:47:47 +00:00
.getAuthType()
.then((res) => {
//如果结果包含LDAP把LDAP放在第一个
const ldapIndex = res.indexOf('LDAP')
if (ldapIndex !== -1) {
const [ldap] = res.splice(ldapIndex, 1)
res.unshift(ldap)
}
modeList.value = [...modeList.value, ...res]
})
.finally(() => (loading.value = false))
2025-06-13 09:17:01 +00:00
login
2025-06-05 11:47:47 +00:00
.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')
2025-06-13 09:17:01 +00:00
: t('views.system.authentication.scanTheQRCode.lark'),
2025-06-05 11:47:47 +00:00
})
})
}
})
.finally(() => (loading.value = false))
} else {
loading.value = false
}
})
})
declare const window: any
onMounted(() => {
makeCode()
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) => {
2025-06-05 11:47:47 +00:00
console.log('DingTalk client request success:', res)
2025-06-13 09:17:01 +00:00
login.dingOauth2Callback(res.code).then(() => {
router.push({name: 'home'})
2025-06-05 11:47:47 +00:00
})
})
}
}
const handleLark = () => {
const appId = params.get('appId')
const callRequestAuthCode = () => {
window.tt?.requestAuthCode({
appId: appId,
success: (res: any) => {
2025-06-13 09:17:01 +00:00
login.larkCallback(res.code).then(() => {
router.push({name: 'home'})
2025-06-05 11:47:47 +00:00
})
},
fail: (error: any) => {
MsgError(error)
2025-06-13 09:17:01 +00:00
},
2025-06-05 11:47:47 +00:00
})
}
loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', {
jsId: 'lark-sdk',
2025-06-13 09:17:01 +00:00
forceReload: true,
2025-06-05 11:47:47 +00:00
})
.then(() => {
if (window.tt) {
window.tt.requestAccess({
appID: appId,
scopeList: [],
success: (res: any) => {
2025-06-13 09:17:01 +00:00
login.larkCallback(res.code).then(() => {
router.push({name: 'home'})
2025-06-05 11:47:47 +00:00
})
},
fail: (error: any) => {
const {errno} = error
2025-06-05 11:47:47 +00:00
if (errno === 103) {
callRequestAuthCode()
}
2025-06-13 09:17:01 +00:00
},
2025-06-05 11:47:47 +00:00
})
} else {
callRequestAuthCode()
}
})
.catch((error) => {
console.error('SDK 加载失败:', error)
})
}
switch (client) {
case 'dingtalk':
handleDingTalk()
break
case 'lark':
handleLark()
break
default:
break
}
})
2025-04-16 11:10:00 +00:00
</script>
2025-06-05 11:47:47 +00:00
<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>