RegisterForm.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <script setup lang="ts">
  2. import { Form } from '@/components/Form'
  3. import { reactive, ref, unref } from 'vue'
  4. import { useI18n } from '@/hooks/web/useI18n'
  5. import { useForm } from '@/hooks/web/useForm'
  6. import { ElButton, ElInput, FormRules } from 'element-plus'
  7. import { useValidator } from '@/hooks/web/useValidator'
  8. import { FormSchema } from '@/types/form'
  9. const emit = defineEmits(['to-login'])
  10. const { register, elFormRef } = useForm()
  11. const { t } = useI18n()
  12. const { required } = useValidator()
  13. const schema = reactive<FormSchema[]>([
  14. {
  15. field: 'title',
  16. colProps: {
  17. span: 24
  18. }
  19. },
  20. {
  21. field: 'username',
  22. label: t('login.username'),
  23. value: '',
  24. component: 'Input',
  25. colProps: {
  26. span: 24
  27. },
  28. componentProps: {
  29. placeholder: t('login.usernamePlaceholder')
  30. }
  31. },
  32. {
  33. field: 'password',
  34. label: t('login.password'),
  35. value: '',
  36. component: 'InputPassword',
  37. colProps: {
  38. span: 24
  39. },
  40. componentProps: {
  41. style: {
  42. width: '100%'
  43. },
  44. strength: true,
  45. placeholder: t('login.passwordPlaceholder')
  46. }
  47. },
  48. {
  49. field: 'check_password',
  50. label: t('login.checkPassword'),
  51. value: '',
  52. component: 'InputPassword',
  53. colProps: {
  54. span: 24
  55. },
  56. componentProps: {
  57. style: {
  58. width: '100%'
  59. },
  60. strength: true,
  61. placeholder: t('login.passwordPlaceholder')
  62. }
  63. },
  64. {
  65. field: 'code',
  66. label: t('login.code'),
  67. colProps: {
  68. span: 24
  69. }
  70. },
  71. {
  72. field: 'register',
  73. colProps: {
  74. span: 24
  75. }
  76. }
  77. ])
  78. const rules: FormRules = {
  79. username: [required()],
  80. password: [required()],
  81. check_password: [required()],
  82. code: [required()]
  83. }
  84. const toLogin = () => {
  85. emit('to-login')
  86. }
  87. const loading = ref(false)
  88. const loginRegister = async () => {
  89. const formRef = unref(elFormRef)
  90. formRef?.validate(async (valid) => {
  91. if (valid) {
  92. try {
  93. loading.value = true
  94. toLogin()
  95. } finally {
  96. loading.value = false
  97. }
  98. }
  99. })
  100. }
  101. </script>
  102. <template>
  103. <Form
  104. :schema="schema"
  105. :rules="rules"
  106. label-position="top"
  107. hide-required-asterisk
  108. size="large"
  109. class="dark:(border-1 border-[var(--el-border-color)] border-solid)"
  110. @register="register"
  111. >
  112. <template #title>
  113. <h2 class="text-2xl font-bold text-center w-[100%]">{{ t('login.register') }}</h2>
  114. </template>
  115. <template #code="form">
  116. <div class="w-[100%] flex">
  117. <ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" />
  118. </div>
  119. </template>
  120. <template #register>
  121. <div class="w-[100%]">
  122. <ElButton type="primary" class="w-[100%]" :loading="loading" @click="loginRegister">
  123. {{ t('login.register') }}
  124. </ElButton>
  125. </div>
  126. <div class="w-[100%] mt-15px">
  127. <ElButton class="w-[100%]" @click="toLogin">
  128. {{ t('login.hasUser') }}
  129. </ElButton>
  130. </div>
  131. </template>
  132. </Form>
  133. </template>