ruoyi-plus-vben5/packages/effects/common-ui/src/ui/authentication/login.vue

178 lines
4.4 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script setup lang="ts">
import type { VbenFormSchema } from '@vben-core/form-ui';
import type { AuthenticationProps, LoginEmits } from './types';
import { computed, onMounted, reactive, ref } from 'vue';
2024-06-08 19:49:06 +08:00
import { useRouter } from 'vue-router';
import { $t } from '@vben/locales';
import { useVbenForm } from '@vben-core/form-ui';
import { VbenButton, VbenCheckbox } from '@vben-core/shadcn-ui';
2024-05-19 21:20:42 +08:00
import Title from './auth-title.vue';
import ThirdPartyLogin from './third-party-login.vue';
interface Props extends AuthenticationProps {
formSchema: VbenFormSchema[];
}
2024-05-19 21:20:42 +08:00
defineOptions({
name: 'AuthenticationLogin',
});
const props = withDefaults(defineProps<Props>(), {
codeLoginPath: '/auth/code-login',
forgetPasswordPath: '/auth/forget-password',
formSchema: () => [],
2024-05-19 21:20:42 +08:00
loading: false,
qrCodeLoginPath: '/auth/qrcode-login',
registerPath: '/auth/register',
showCodeLogin: true,
showForgetPassword: true,
showQrcodeLogin: true,
showRegister: true,
showRememberMe: true,
showThirdPartyLogin: true,
submitButtonText: '',
subTitle: '',
title: '',
2024-05-19 21:20:42 +08:00
});
const emit = defineEmits<{
submit: LoginEmits['submit'];
}>();
const [Form, { setFieldValue, validate }] = useVbenForm(
reactive({
commonConfig: {
hideLabel: true,
hideRequiredMark: true,
},
schema: computed(() => props.formSchema),
showDefaultActions: false,
}),
);
2024-05-19 21:20:42 +08:00
const router = useRouter();
const REMEMBER_ME_KEY = `REMEMBER_ME_USERNAME_${location.hostname}`;
2024-05-19 21:20:42 +08:00
const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || '';
const rememberMe = ref(!!localUsername);
2024-05-19 21:20:42 +08:00
async function handleSubmit() {
const { valid, values } = await validate();
if (valid) {
localStorage.setItem(
REMEMBER_ME_KEY,
rememberMe.value ? values?.username : '',
);
emit('submit', values as { password: string; username: string });
2024-05-19 21:20:42 +08:00
}
}
function handleGo(path: string) {
router.push(path);
}
onMounted(() => {
if (localUsername) {
setFieldValue('username', localUsername);
}
});
2024-05-19 21:20:42 +08:00
</script>
<template>
<div @keydown.enter.prevent="handleSubmit">
<slot name="title">
<Title>
<slot name="title">
{{ title || `${$t('authentication.welcomeBack')} 👋🏻` }}
</slot>
<template #desc>
<span class="text-muted-foreground">
<slot name="subTitle">
{{ subTitle || $t('authentication.loginSubtitle') }}
</slot>
</span>
</template>
</Title>
</slot>
2024-05-19 21:20:42 +08:00
<Form />
2024-05-19 21:20:42 +08:00
<div
v-if="showRememberMe || showForgetPassword"
class="mb-6 flex justify-between"
>
<div class="flex-center">
<VbenCheckbox
v-if="showRememberMe"
v-model:checked="rememberMe"
name="rememberMe"
>
{{ $t('authentication.rememberMe') }}
2024-05-19 21:20:42 +08:00
</VbenCheckbox>
</div>
<span
v-if="showForgetPassword"
2024-06-23 19:17:31 +08:00
class="text-primary hover:text-primary-hover active:text-primary-active cursor-pointer text-sm font-normal"
@click="handleGo(forgetPasswordPath)"
2024-05-19 21:20:42 +08:00
>
{{ $t('authentication.forgetPassword') }}
2024-05-19 21:20:42 +08:00
</span>
</div>
2024-09-12 22:03:58 +08:00
<VbenButton
:class="{
'cursor-wait': loading,
}"
:loading="loading"
class="w-full"
@click="handleSubmit"
>
{{ submitButtonText || $t('common.login') }}
2024-05-19 21:20:42 +08:00
</VbenButton>
<div
v-if="showCodeLogin || showQrcodeLogin"
class="mb-2 mt-4 flex items-center justify-between"
>
2024-05-19 21:20:42 +08:00
<VbenButton
v-if="showCodeLogin"
2024-05-19 21:20:42 +08:00
class="w-1/2"
2024-06-09 13:31:43 +08:00
variant="outline"
@click="handleGo(codeLoginPath)"
2024-05-19 21:20:42 +08:00
>
{{ $t('authentication.mobileLogin') }}
2024-05-19 21:20:42 +08:00
</VbenButton>
<VbenButton
v-if="showQrcodeLogin"
2024-05-19 21:20:42 +08:00
class="ml-4 w-1/2"
2024-06-09 13:31:43 +08:00
variant="outline"
@click="handleGo(qrCodeLoginPath)"
2024-05-19 21:20:42 +08:00
>
{{ $t('authentication.qrcodeLogin') }}
2024-05-19 21:20:42 +08:00
</VbenButton>
</div>
<!-- 第三方登录 -->
<slot name="third-party-login">
<ThirdPartyLogin v-if="showThirdPartyLogin" />
</slot>
<slot name="to-register">
<div v-if="showRegister" class="mt-3 text-center text-sm">
{{ $t('authentication.accountTip') }}
<span
class="text-primary hover:text-primary-hover active:text-primary-active cursor-pointer text-sm font-normal"
@click="handleGo(registerPath)"
>
{{ $t('authentication.createAccount') }}
</span>
</div>
</slot>
2024-05-19 21:20:42 +08:00
</div>
</template>