gohttpdUi/src/views/Login/components/RegisterForm.vue

146 lines
3.0 KiB
Vue
Raw Normal View History

2022-06-22 23:03:21 +08:00
<script setup lang="ts">
import { Form } from '@/components/Form'
2022-06-23 22:37:49 +08:00
import { reactive, ref, unref } from 'vue'
2022-06-22 23:03:21 +08:00
import { useI18n } from '@/hooks/web/useI18n'
import { useForm } from '@/hooks/web/useForm'
2022-06-25 21:56:24 +08:00
import { ElButton, ElInput, FormRules } from 'element-plus'
2022-06-23 22:37:49 +08:00
import { useValidator } from '@/hooks/web/useValidator'
2022-10-09 17:12:03 +08:00
import { FormSchema } from '@/types/form'
2022-06-22 23:03:21 +08:00
const emit = defineEmits(['to-login'])
2022-06-25 21:56:24 +08:00
const { register, elFormRef } = useForm()
2022-06-22 23:03:21 +08:00
const { t } = useI18n()
2022-06-25 21:56:24 +08:00
const { required } = useValidator()
2022-06-23 22:37:49 +08:00
2022-06-22 23:03:21 +08:00
const schema = reactive<FormSchema[]>([
{
field: 'title',
colProps: {
span: 24
}
},
{
2022-06-25 21:56:24 +08:00
field: 'username',
2022-06-22 23:03:21 +08:00
label: t('login.username'),
value: '',
component: 'Input',
colProps: {
span: 24
},
componentProps: {
placeholder: t('login.usernamePlaceholder')
}
},
{
field: 'password',
label: t('login.password'),
2022-06-23 22:37:49 +08:00
value: '',
2022-06-22 23:03:21 +08:00
component: 'InputPassword',
colProps: {
span: 24
},
componentProps: {
style: {
width: '100%'
},
2022-06-23 22:37:49 +08:00
strength: true,
2022-06-22 23:03:21 +08:00
placeholder: t('login.passwordPlaceholder')
}
},
{
field: 'check_password',
label: t('login.checkPassword'),
2022-06-23 22:37:49 +08:00
value: '',
2022-06-22 23:03:21 +08:00
component: 'InputPassword',
colProps: {
span: 24
},
componentProps: {
style: {
width: '100%'
},
2022-06-23 22:37:49 +08:00
strength: true,
2022-06-22 23:03:21 +08:00
placeholder: t('login.passwordPlaceholder')
}
},
2022-06-23 22:37:49 +08:00
{
field: 'code',
label: t('login.code'),
colProps: {
span: 24
}
},
2022-06-22 23:03:21 +08:00
{
field: 'register',
colProps: {
span: 24
}
}
])
2022-06-23 22:37:49 +08:00
const rules: FormRules = {
2022-06-25 21:56:24 +08:00
username: [required()],
password: [required()],
check_password: [required()],
code: [required()]
2022-06-22 23:03:21 +08:00
}
const toLogin = () => {
emit('to-login')
}
2022-06-23 22:37:49 +08:00
const loading = ref(false)
const loginRegister = async () => {
const formRef = unref(elFormRef)
formRef?.validate(async (valid) => {
if (valid) {
try {
loading.value = true
2022-06-25 21:56:24 +08:00
toLogin()
2022-06-23 22:37:49 +08:00
} finally {
loading.value = false
}
}
})
}
2022-06-22 23:03:21 +08:00
</script>
<template>
<Form
:schema="schema"
:rules="rules"
label-position="top"
hide-required-asterisk
size="large"
2023-04-17 09:23:03 +08:00
class="dark:border-1 dark:border-[var(--el-border-color)] dark:border-solid"
2022-06-22 23:03:21 +08:00
@register="register"
>
<template #title>
<h2 class="text-2xl font-bold text-center w-[100%]">{{ t('login.register') }}</h2>
</template>
2022-06-23 22:37:49 +08:00
<template #code="form">
<div class="w-[100%] flex">
2022-06-25 21:56:24 +08:00
<ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" />
2022-06-23 22:37:49 +08:00
</div>
</template>
2022-06-22 23:03:21 +08:00
<template #register>
<div class="w-[100%]">
2022-06-23 22:37:49 +08:00
<ElButton type="primary" class="w-[100%]" :loading="loading" @click="loginRegister">
2022-06-22 23:03:21 +08:00
{{ t('login.register') }}
</ElButton>
</div>
<div class="w-[100%] mt-15px">
<ElButton class="w-[100%]" @click="toLogin">
{{ t('login.hasUser') }}
</ElButton>
</div>
</template>
</Form>
</template>