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

167 lines
3.4 KiB
Vue
Raw Normal View History

2023-07-10 20:40:36 +08:00
<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { reactive, ref } from 'vue'
2022-06-22 23:03:21 +08:00
import { useI18n } from '@/hooks/web/useI18n'
import { useForm } from '@/hooks/web/useForm'
import { ElInput, FormRules } from 'element-plus'
2022-06-23 22:37:49 +08:00
import { useValidator } from '@/hooks/web/useValidator'
import { BaseButton } from '@/components/Button'
2022-06-22 23:03:21 +08:00
const emit = defineEmits(['to-login'])
2023-07-10 20:40:36 +08:00
const { formRegister, formMethods } = useForm()
const { getElFormExpose } = formMethods
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
2023-07-10 20:40:36 +08:00
},
formItemProps: {
slots: {
default: () => {
return <h2 class="text-2xl font-bold text-center w-[100%]">{t('login.register')}</h2>
}
}
2022-06-22 23:03:21 +08:00
}
},
{
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
2023-07-10 20:40:36 +08:00
},
formItemProps: {
slots: {
default: (formData) => {
return (
<div class="w-[100%] flex">
<ElInput v-model={formData.code} placeholder={t('login.codePlaceholder')} />
</div>
)
}
}
2022-06-23 22:37:49 +08:00
}
},
2022-06-22 23:03:21 +08:00
{
field: 'register',
colProps: {
span: 24
2023-07-10 20:40:36 +08:00
},
formItemProps: {
slots: {
default: () => {
return (
<>
<div class="w-[100%]">
<BaseButton
2023-07-10 20:40:36 +08:00
type="primary"
class="w-[100%]"
loading={loading.value}
onClick={loginRegister}
>
{t('login.register')}
</BaseButton>
2023-07-10 20:40:36 +08:00
</div>
<div class="w-[100%] mt-15px">
<BaseButton class="w-[100%]" onClick={toLogin}>
2023-07-10 20:40:36 +08:00
{t('login.hasUser')}
</BaseButton>
2023-07-10 20:40:36 +08:00
</div>
</>
)
}
}
2022-06-22 23:03:21 +08:00
}
}
])
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 () => {
2023-07-10 20:40:36 +08:00
const formRef = await getElFormExpose()
2022-06-23 22:37:49 +08:00
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-18 17:09:03 +08:00
class="dark:(border-1 border-[var(--el-border-color)] border-solid)"
2023-07-10 20:40:36 +08:00
@register="formRegister"
/>
2022-06-22 23:03:21 +08:00
</template>