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

218 lines
5.3 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';
2024-08-07 08:57:56 +08:00
interface Props extends AuthenticationProps {
formSchema: VbenFormSchema[];
2024-08-07 08:57:56 +08:00
}
2024-05-19 21:20:42 +08:00
defineOptions({
name: 'AuthenticationLogin',
});
2024-08-07 08:57:56 +08:00
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,
subTitle: '',
2024-08-07 08:57:56 +08:00
tenantOptions: () => [],
title: '',
2024-05-19 21:20:42 +08:00
});
const emit = defineEmits<{
2024-08-07 08:57:56 +08:00
/**
* 验证码点击
*/
captchaClick: [];
/**
* 第三方登录 platfrom 对应平台的string
*/
oauthLogin: [plateform: string];
2024-05-19 21:20:42 +08:00
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
2024-08-07 08:57:56 +08:00
const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || 'admin';
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-08-07 08:57:56 +08:00
}
2024-05-19 21:20:42 +08:00
}
function handleGo(path: string) {
router.push(path);
}
2024-08-07 08:57:56 +08:00
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>
{{ title || `${$t('authentication.welcomeBack')} 👋🏻` }}
<template #desc>
<span class="text-muted-foreground">
{{ subTitle || $t('authentication.loginSubtitle') }}
</span>
</template>
</Title>
</slot>
2024-05-19 21:20:42 +08:00
<Form />
2024-05-19 21:20:42 +08:00
2024-08-07 08:57:56 +08:00
<!-- 图片验证码 -->
<div v-if="useCaptcha" class="flex">
<div class="flex-1">
<VbenInput
v-model="formState.code"
:error-tip="$t('authentication.captchaTip')"
:label="$t('authentication.captcha')"
:placeholder="$t('authentication.captcha')"
:status="captchaStatus"
name="code"
required
type="text"
/>
</div>
<img
:src="captchaBase64"
2024-08-09 10:08:59 +08:00
class="h-[40px] w-[115px] rounded-r-md"
2024-08-07 08:57:56 +08:00
@click="emit('captchaClick')"
/>
</div>
<div
v-if="showRememberMe || showForgetPassword"
class="mb-6 flex justify-between"
>
<div v-if="showRememberMe" class="flex-center">
<VbenCheckbox 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>
<VbenButton :loading="loading" class="w-full" @click="handleSubmit">
{{ $t('common.login') }}
</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"
@oauth-login="(e) => emit('oauthLogin', e)"
/>
</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>
<style lang="scss">
/**
2024-08-09 10:08:59 +08:00
tenant-picker 跟下面的输入框样式保持一致
*/
.tenant-picker > button[role='combobox'] {
height: 40px;
background-color: hsl(var(--input-background));
2024-08-09 10:08:59 +08:00
&:focus {
@apply border-primary;
}
}
/**
验证码输入框样式
去除右边的圆角
*/
input[id='code'] {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
</style>