2024-05-19 21:20:42 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import {
|
|
|
|
VbenButton,
|
|
|
|
VbenCheckbox,
|
|
|
|
VbenInput,
|
|
|
|
VbenInputPassword,
|
|
|
|
} from '@vben-core/shadcn-ui';
|
|
|
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
import { computed, reactive } from 'vue';
|
|
|
|
import { useRouter } from 'vue-router';
|
|
|
|
|
|
|
|
import Title from './auth-title.vue';
|
|
|
|
import ThirdPartyLogin from './third-party-login.vue';
|
|
|
|
|
|
|
|
import type { LoginEmits } from './typings';
|
|
|
|
|
|
|
|
interface Props {
|
2024-06-02 21:21:34 +08:00
|
|
|
/**
|
|
|
|
* @zh_CN 验证码登录路径
|
|
|
|
*/
|
|
|
|
codeLoginPath?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 忘记密码路径
|
|
|
|
*/
|
|
|
|
forgetPasswordPath?: string;
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
/**
|
|
|
|
* @zh_CN 是否处于加载处理状态
|
|
|
|
*/
|
|
|
|
loading?: boolean;
|
2024-06-02 21:21:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 密码占位符
|
|
|
|
*/
|
|
|
|
passwordPlaceholder?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 二维码登录路径
|
|
|
|
*/
|
|
|
|
qrCodeLoginPath?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 注册路径
|
|
|
|
*/
|
|
|
|
registerPath?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 是否显示验证码登录
|
|
|
|
*/
|
|
|
|
showCodeLogin?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 是否显示忘记密码
|
|
|
|
*/
|
|
|
|
showForgetPassword?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 是否显示二维码登录
|
|
|
|
*/
|
|
|
|
showQrcodeLogin?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 是否显示注册按钮
|
|
|
|
*/
|
|
|
|
showRegister?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 是否显示第三方登录
|
|
|
|
*/
|
|
|
|
showThirdPartyLogin?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @zh_CN 用户名占位符
|
|
|
|
*/
|
|
|
|
usernamePlaceholder?: string;
|
2024-05-19 21:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'AuthenticationLogin',
|
|
|
|
});
|
|
|
|
|
|
|
|
withDefaults(defineProps<Props>(), {
|
2024-06-02 21:21:34 +08:00
|
|
|
codeLoginPath: '/auth/code-login',
|
|
|
|
forgetPasswordPath: '/auth/forget-password',
|
2024-05-19 21:20:42 +08:00
|
|
|
loading: false,
|
2024-06-02 21:21:34 +08:00
|
|
|
passwordPlaceholder: '',
|
|
|
|
qrCodeLoginPath: '/auth/qrcode-login',
|
|
|
|
registerPath: '/auth/register',
|
|
|
|
showCodeLogin: true,
|
|
|
|
showForgetPassword: true,
|
|
|
|
showQrcodeLogin: true,
|
|
|
|
showRegister: true,
|
|
|
|
showThirdPartyLogin: true,
|
|
|
|
usernamePlaceholder: '',
|
2024-05-19 21:20:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
submit: LoginEmits['submit'];
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const REMEMBER_ME_KEY = 'REMEMBER_ME_USERNAME';
|
|
|
|
|
|
|
|
const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || '';
|
2024-06-02 21:21:34 +08:00
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
const formState = reactive({
|
|
|
|
password: '',
|
|
|
|
rememberMe: !!localUsername,
|
|
|
|
submitted: false,
|
|
|
|
username: localUsername,
|
|
|
|
});
|
|
|
|
|
|
|
|
const usernameStatus = computed(() => {
|
|
|
|
return formState.submitted && !formState.username ? 'error' : 'default';
|
|
|
|
});
|
|
|
|
|
|
|
|
const passwordStatus = computed(() => {
|
|
|
|
return formState.submitted && !formState.password ? 'error' : 'default';
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
REMEMBER_ME_KEY,
|
|
|
|
formState.rememberMe ? formState.username : '',
|
|
|
|
);
|
|
|
|
|
|
|
|
emit('submit', {
|
|
|
|
password: formState.password,
|
|
|
|
username: formState.username,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleGo(path: string) {
|
|
|
|
router.push(path);
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2024-06-02 21:21:34 +08:00
|
|
|
<div @keypress.enter.prevent="handleSubmit">
|
2024-05-19 21:20:42 +08:00
|
|
|
<Title>
|
|
|
|
{{ $t('authentication.welcome-back') }} 👋🏻
|
|
|
|
<template #desc>
|
|
|
|
<span class="text-muted-foreground">
|
|
|
|
{{ $t('authentication.login-subtitle') }}
|
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
</Title>
|
|
|
|
|
|
|
|
<VbenInput
|
|
|
|
v-model="formState.username"
|
|
|
|
:status="usernameStatus"
|
|
|
|
:error-tip="$t('authentication.username-tip')"
|
|
|
|
:label="$t('authentication.username')"
|
|
|
|
name="username"
|
2024-06-02 21:21:34 +08:00
|
|
|
:placeholder="usernamePlaceholder || $t('authentication.username')"
|
2024-05-19 21:20:42 +08:00
|
|
|
type="text"
|
2024-05-21 23:23:48 +08:00
|
|
|
required
|
2024-05-19 21:20:42 +08:00
|
|
|
:autofocus="false"
|
|
|
|
/>
|
|
|
|
<VbenInputPassword
|
|
|
|
v-model="formState.password"
|
|
|
|
:status="passwordStatus"
|
|
|
|
:error-tip="$t('authentication.password-tip')"
|
|
|
|
:label="$t('authentication.password')"
|
|
|
|
name="password"
|
2024-06-02 21:21:34 +08:00
|
|
|
:placeholder="passwordPlaceholder || $t('authentication.password')"
|
2024-05-19 21:20:42 +08:00
|
|
|
required
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div class="mb-6 mt-4 flex justify-between">
|
|
|
|
<div class="flex-center flex">
|
|
|
|
<VbenCheckbox v-model:checked="formState.rememberMe" name="rememberMe">
|
|
|
|
{{ $t('authentication.remember-me') }}
|
|
|
|
</VbenCheckbox>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<span
|
2024-06-02 21:21:34 +08:00
|
|
|
v-if="showForgetPassword"
|
2024-05-19 21:20:42 +08:00
|
|
|
class="text-primary hover:text-primary/80 cursor-pointer text-sm font-normal"
|
2024-06-02 21:21:34 +08:00
|
|
|
@click="handleGo(forgetPasswordPath)"
|
2024-05-19 21:20:42 +08:00
|
|
|
>
|
|
|
|
{{ $t('authentication.forget-password') }}
|
|
|
|
</span>
|
|
|
|
<!-- <VbenButton variant="ghost" @click="handleGo('/auth/forget-password')">
|
|
|
|
忘记密码?
|
|
|
|
</VbenButton> -->
|
|
|
|
</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
|
2024-06-02 21:21:34 +08:00
|
|
|
v-if="showCodeLogin"
|
2024-05-19 21:20:42 +08:00
|
|
|
variant="outline"
|
|
|
|
class="w-1/2"
|
2024-06-02 21:21:34 +08:00
|
|
|
@click="handleGo(codeLoginPath)"
|
2024-05-19 21:20:42 +08:00
|
|
|
>
|
|
|
|
{{ $t('authentication.mobile-login') }}
|
|
|
|
</VbenButton>
|
|
|
|
<VbenButton
|
2024-06-02 21:21:34 +08:00
|
|
|
v-if="showQrcodeLogin"
|
2024-05-19 21:20:42 +08:00
|
|
|
variant="outline"
|
|
|
|
class="ml-4 w-1/2"
|
2024-06-02 21:21:34 +08:00
|
|
|
@click="handleGo(qrCodeLoginPath)"
|
2024-05-19 21:20:42 +08:00
|
|
|
>
|
|
|
|
{{ $t('authentication.qrcode-login') }}
|
|
|
|
</VbenButton>
|
|
|
|
<!-- <VbenButton
|
|
|
|
:loading="loading"
|
|
|
|
variant="outline"
|
|
|
|
class="w-1/3"
|
|
|
|
@click="handleGo('/auth/register')"
|
|
|
|
>
|
|
|
|
创建账号
|
|
|
|
</VbenButton> -->
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- 第三方登录 -->
|
2024-06-02 21:21:34 +08:00
|
|
|
<ThirdPartyLogin v-if="showThirdPartyLogin" />
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-06-02 21:21:34 +08:00
|
|
|
<div v-if="showRegister" class="text-center text-sm">
|
2024-05-19 21:20:42 +08:00
|
|
|
{{ $t('authentication.account-tip') }}
|
|
|
|
<span
|
|
|
|
class="text-primary hover:text-primary/80 cursor-pointer text-sm font-normal"
|
2024-06-02 21:21:34 +08:00
|
|
|
@click="handleGo(registerPath)"
|
2024-05-19 21:20:42 +08:00
|
|
|
>
|
|
|
|
{{ $t('authentication.create-account') }}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|