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

292 lines
7.0 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script setup lang="ts">
import type { AuthenticationProps, LoginEmits } from './typings';
2024-08-07 08:57:56 +08:00
import { computed, reactive, watch } from 'vue';
2024-06-08 19:49:06 +08:00
import { useRouter } from 'vue-router';
import { $t } from '@vben/locales';
2024-05-19 21:20:42 +08:00
import {
2024-08-07 08:57:56 +08:00
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
2024-05-19 21:20:42 +08:00
VbenButton,
VbenCheckbox,
VbenInput,
VbenInputPassword,
} from '@vben-core/shadcn-ui';
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 {
/**
* @zh_CN 验证码图片base64
*/
captchaBase64?: string;
/**
* 租户信息options
*/
tenantOptions?: { companyName: string; domain?: string; tenantId: string }[];
/**
* @zh_CN 是否启用验证码
*/
useCaptcha?: boolean;
/**
* @zh_CN 是否启用租户
*/
useTenant?: boolean;
}
2024-05-19 21:20:42 +08:00
defineOptions({
name: 'AuthenticationLogin',
});
2024-08-07 08:57:56 +08:00
const props = withDefaults(defineProps<Props>(), {
captchaBase64: '',
codeLoginPath: '/auth/code-login',
forgetPasswordPath: '/auth/forget-password',
2024-05-19 21:20:42 +08:00
loading: false,
passwordPlaceholder: '',
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: '',
usernamePlaceholder: '',
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 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';
2024-05-19 21:20:42 +08:00
const formState = reactive({
2024-08-07 08:57:56 +08:00
code: '',
password: 'admin123',
2024-05-19 21:20:42 +08:00
rememberMe: !!localUsername,
submitted: false,
2024-08-07 08:57:56 +08:00
// 默认租户
tenantId: '000000',
2024-05-19 21:20:42 +08:00
username: localUsername,
});
2024-08-07 08:57:56 +08:00
/**
* 默认选中第一项租户
*/
const stop = watch(props.tenantOptions, (options) => {
if (options.length > 0) {
formState.tenantId = options[0]!.tenantId;
stop();
}
});
2024-05-19 21:20:42 +08:00
const usernameStatus = computed(() => {
return formState.submitted && !formState.username ? 'error' : 'default';
});
const passwordStatus = computed(() => {
return formState.submitted && !formState.password ? 'error' : 'default';
});
2024-08-07 08:57:56 +08:00
const captchaStatus = computed(() => {
return formState.submitted && !formState.code ? 'error' : 'default';
});
2024-05-19 21:20:42 +08:00
function handleSubmit() {
formState.submitted = true;
2024-05-21 22:42:25 +08:00
2024-05-19 21:20:42 +08:00
if (
usernameStatus.value !== 'default' ||
passwordStatus.value !== 'default'
) {
return;
}
2024-08-07 08:57:56 +08:00
// 验证码
if (props.useCaptcha && captchaStatus.value !== 'default') {
return;
}
2024-05-19 21:20:42 +08:00
localStorage.setItem(
REMEMBER_ME_KEY,
formState.rememberMe ? formState.username : '',
);
emit('submit', {
2024-08-07 08:57:56 +08:00
code: formState.code,
grantType: 'password',
2024-05-19 21:20:42 +08:00
password: formState.password,
2024-08-07 08:57:56 +08:00
tenantId: formState.tenantId,
2024-05-19 21:20:42 +08:00
username: formState.username,
});
}
function handleGo(path: string) {
router.push(path);
}
2024-08-07 08:57:56 +08:00
/**
* 重置验证码
*/
function resetCaptcha() {
emit('captchaClick');
formState.code = '';
// todo 获取焦点
// VbenInput并没有提供focus方法
}
defineExpose({ resetCaptcha });
2024-05-19 21:20:42 +08:00
</script>
<template>
<div @keypress.enter.prevent="handleSubmit">
2024-05-19 21:20:42 +08:00
<Title>
{{ title || `${$t('authentication.welcomeBack')} 👋🏻` }}
2024-05-19 21:20:42 +08:00
<template #desc>
<span class="text-muted-foreground">
{{ subTitle || $t('authentication.loginSubtitle') }}
2024-05-19 21:20:42 +08:00
</span>
</template>
</Title>
2024-08-07 08:57:56 +08:00
<!-- 租户 -->
<div v-if="useTenant" class="mb-6">
<Select v-model="formState.tenantId">
<SelectTrigger>
<SelectValue placeholder="选择公司" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem
v-for="item in tenantOptions"
:key="item.tenantId"
:value="item.tenantId"
>
{{ item.companyName }}
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>
2024-05-19 21:20:42 +08:00
<VbenInput
v-model="formState.username"
2024-06-09 13:31:43 +08:00
:autofocus="false"
:error-tip="$t('authentication.usernameTip')"
2024-05-19 21:20:42 +08:00
:label="$t('authentication.username')"
:placeholder="usernamePlaceholder || $t('authentication.username')"
2024-06-09 13:31:43 +08:00
:status="usernameStatus"
name="username"
2024-05-21 23:23:48 +08:00
required
2024-06-09 13:31:43 +08:00
type="text"
2024-05-19 21:20:42 +08:00
/>
<VbenInputPassword
v-model="formState.password"
:error-tip="$t('authentication.passwordTip')"
2024-05-19 21:20:42 +08:00
:label="$t('authentication.password')"
:placeholder="passwordPlaceholder || $t('authentication.password')"
2024-06-09 13:31:43 +08:00
:status="passwordStatus"
name="password"
2024-05-19 21:20:42 +08:00
required
type="password"
/>
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"
class="h-[38px] w-[115px] rounded-r-md"
@click="emit('captchaClick')"
/>
</div>
2024-05-19 21:20:42 +08:00
<div class="mb-6 mt-4 flex justify-between">
<div v-if="showRememberMe" class="flex-center">
2024-05-19 21:20:42 +08:00
<VbenCheckbox v-model:checked="formState.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 class="mb-2 mt-4 flex items-center justify-between">
<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>
<!-- 第三方登录 -->
<ThirdPartyLogin
v-if="showThirdPartyLogin"
@oauth-login="(e) => emit('oauthLogin', e)"
/>
2024-05-19 21:20:42 +08:00
<div v-if="showRegister" class="text-center text-sm">
{{ $t('authentication.accountTip') }}
2024-05-19 21:20:42 +08:00
<span
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(registerPath)"
2024-05-19 21:20:42 +08:00
>
{{ $t('authentication.createAccount') }}
2024-05-19 21:20:42 +08:00
</span>
</div>
</div>
</template>