You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.1 KiB
Vue

3 years ago
<script setup lang="ts">
import { Form } from '@/components/Form'
3 years ago
import { reactive, ref, unref } from 'vue'
3 years ago
import { useI18n } from '@/hooks/web/useI18n'
import { useForm } from '@/hooks/web/useForm'
3 years ago
import { ElButton, ElInput, FormRules, ElMessage } from 'element-plus'
import { getCodeApi, registerApi } from '@/api/register'
import { useValidator } from '@/hooks/web/useValidator'
import { IUserModel } from '@/api-types/user'
3 years ago
import md5 from 'js-md5'
import { cloneDeep } from 'lodash-es'
3 years ago
const emit = defineEmits(['to-login'])
3 years ago
const { register, methods, elFormRef } = useForm()
const { getFormData } = methods
3 years ago
const { t } = useI18n()
3 years ago
const { required, lengthRange, notSpace, notSpecialCharacters, isEqual } = useValidator()
3 years ago
const schema = reactive<FormSchema[]>([
{
field: 'title',
colProps: {
span: 24
}
},
{
field: 'user_name',
label: t('login.username'),
value: '',
component: 'Input',
colProps: {
span: 24
},
componentProps: {
placeholder: t('login.usernamePlaceholder')
}
},
{
field: 'password',
label: t('login.password'),
3 years ago
value: '',
3 years ago
component: 'InputPassword',
colProps: {
span: 24
},
componentProps: {
style: {
width: '100%'
},
3 years ago
strength: true,
3 years ago
placeholder: t('login.passwordPlaceholder')
}
},
{
field: 'check_password',
label: t('login.checkPassword'),
3 years ago
value: '',
3 years ago
component: 'InputPassword',
colProps: {
span: 24
},
componentProps: {
style: {
width: '100%'
},
3 years ago
strength: true,
3 years ago
placeholder: t('login.passwordPlaceholder')
}
},
3 years ago
{
field: 'code',
label: t('login.code'),
colProps: {
span: 24
}
},
3 years ago
{
field: 'register',
colProps: {
span: 24
}
}
])
3 years ago
const rules: FormRules = {
user_name: [
required('用户名不能为空'),
{
validator: (_, value, callback) =>
lengthRange(value, callback, { min: 2, max: 10, message: '用户名长度必须在2-10之间' })
},
{
validator: (_, value, callback) => notSpace(value, callback, '用户名不能有空格')
}
],
password: [
required('密码不能为空'),
{
validator: (_, value, callback) =>
3 years ago
lengthRange(value, callback, { min: 5, max: 20, message: '密码长度必须在5-20之间' })
3 years ago
},
{
validator: (_, value, callback) => notSpecialCharacters(value, callback, '密码不能是特殊字符')
}
],
check_password: [
required('确认密码不能为空'),
{
validator: (_, value, callback) =>
3 years ago
lengthRange(value, callback, { min: 5, max: 20, message: '确认密码长度必须在5-20之间' })
3 years ago
},
{
validator: (_, value, callback) =>
notSpecialCharacters(value, callback, '确认密码不能是特殊字符')
},
{
validator: async (_, value, callback) => {
const formData = await getFormData<Omit<IUserModel, 'is_admin'>>()
return isEqual(value, formData.password, callback, '两次密码不一致')
}
}
],
code: [required('验证码不能为空')]
3 years ago
}
const toLogin = () => {
emit('to-login')
}
3 years ago
const codeUrl = ref('')
3 years ago
const codeUuid = ref('')
3 years ago
const getCode = async () => {
3 years ago
const res = await getCodeApi()
if (res) {
const { url, uuid } = res.result
codeUrl.value = url
codeUuid.value = uuid
}
3 years ago
}
getCode()
const loading = ref(false)
const loginRegister = async () => {
const formRef = unref(elFormRef)
formRef?.validate(async (valid) => {
if (valid) {
try {
loading.value = true
const formData = await getFormData<Omit<IUserModel, 'is_admin'>>()
3 years ago
const res = await registerApi(
Object.assign(cloneDeep(formData), {
uuid: codeUuid.value,
password: md5(formData.password),
check_password: md5(formData.check_password)
})
)
if (res) {
3 years ago
ElMessage.success('注册成功')
toLogin()
}
} finally {
loading.value = false
}
}
})
}
3 years ago
</script>
<template>
<Form
:schema="schema"
:rules="rules"
label-position="top"
hide-required-asterisk
size="large"
class="dark:(border-1 border-[var(--el-border-color)] border-solid)"
@register="register"
>
<template #title>
<h2 class="text-2xl font-bold text-center w-[100%]">{{ t('login.register') }}</h2>
</template>
3 years ago
<template #code="form">
<div class="w-[100%] flex">
<ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" class="flex-2" />
3 years ago
<div v-html="codeUrl" class="h-38px flex-1 cursor-pointer w-150px" @click="getCode"></div>
3 years ago
</div>
</template>
3 years ago
<template #register>
<div class="w-[100%]">
3 years ago
<ElButton type="primary" class="w-[100%]" :loading="loading" @click="loginRegister">
3 years ago
{{ 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>