Files
timi-tdesign-pc/src/components/authorize-form/login.vue
T

128 lines
2.5 KiB
Vue

<template>
<t-form class="tui-login-form" :data="loginRequest" :rules="rules" @submit="doSubmit">
<t-form-item label="用户" name="user">
<t-input class="user" v-model="loginRequest.data.user" placeholder="UID、邮箱或用户名" />
</t-form-item>
<t-form-item label="密码" name="password">
<t-input type="password" v-model="loginRequest.data.password" placeholder="" />
</t-form-item>
<t-form-item v-if="captcha" label="验证码" name="captcha">
<div class="captcha-box">
<t-input class="captcha" v-model="loginRequest.captcha" placeholder="" />
<captcha :request="captcha" :width="90" :height="28" @update="onCaptchaUpdate" />
</div>
</t-form-item>
<div class="exec">
<t-button
class="button"
type="submit"
:loading="doSubmitting"
:disabled="doSubmitting"
>登录</t-button>
</div>
</t-form>
</template>
<script lang="ts" setup>
import { Captcha } from "@timi/web/components/captcha";
import { Emits, useHandler } from "~/components/authorize-form/emits";
import type {
CaptchaData,
CaptchaRequest,
CaptchaResult,
LoginHandler,
LoginRequest
} from "./types";
import { MessagePlugin } from "tdesign-vue-next";
const props = defineProps<{
captcha?: CaptchaRequest;
login?: LoginHandler;
}>();
const rules = {
user: [
{
required: true,
message: "请输入用户名"
}
],
password: [
{
required: true,
message: "请输入密码"
}
],
captcha: [
{
required: true,
message: "请输入验证码"
}
]
}
const emits = defineEmits<Emits>();
const { onLoginSuccess } = useHandler(emits);
const doSubmitting = ref<boolean>(false);
const loginRequest = reactive<CaptchaData<LoginRequest>>({
captchaId: "",
captcha: "",
data: {
user: "",
password: ""
}
});
function onCaptchaUpdate(result: CaptchaResult) {
loginRequest.captchaId = result.id;
}
const doSubmit = async () => {
const login = props.login;
if (!login) {
MessagePlugin.error("未配置登录处理函数", 5E3);
return;
}
doSubmitting.value = true;
try {
await login(loginRequest);
await onLoginSuccess();
} catch (error) {
MessagePlugin.error(String(error), 5E3);
} finally {
doSubmitting.value = false;
}
};
</script>
<style lang="less" scoped>
.tui-login-form {
padding: 1rem;
margin-left: -32px;
.captcha-box {
width: 100%;
display: flex;
justify-content: space-between;
.captcha {
width: 7rem;
margin-right: 1rem;
}
}
.exec {
display: flex;
margin-top: 2rem;
justify-content: center;
.button {
width: 8rem;
margin-left: 32px;
}
}
}
</style>