FormItem.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div class="form-title">
  3. {{ form_type ? $t('login.registerModel') : $t('login.loginModel') }}
  4. </div>
  5. <a-form
  6. ref="ref_form"
  7. :model="form"
  8. :rules="rules"
  9. :style="{ width: 'calc(100% - 40px)' }"
  10. layout="validate"
  11. @submit-success="handleSubmit"
  12. >
  13. <a-form-item field="username">
  14. <a-input v-model="form.username" :placeholder="$t('login.usernameTips')">
  15. <template #prefix> <icon-user /> </template>
  16. </a-input>
  17. </a-form-item>
  18. <a-form-item v-if="form_type" field="email">
  19. <a-input v-model="form.email" :placeholder="$t('login.emailTips')">
  20. <template #prefix> <icon-email /> </template>
  21. </a-input>
  22. </a-form-item>
  23. <a-form-item field="password">
  24. <a-input-password
  25. v-model="form.password"
  26. :placeholder="$t('login.passwordTips')"
  27. :defaultVisibility="true"
  28. allow-clear
  29. >
  30. <template #prefix>
  31. <icon-user />
  32. </template>
  33. </a-input-password>
  34. </a-form-item>
  35. <a-form-item>
  36. <a-button v-if="!form_type" class="submit-btn" type="primary" html-type="submit" :loading="loading"
  37. >{{ $t('login.loginBtn') }}
  38. </a-button>
  39. <a-button v-if="form_type" class="submit-btn" type="primary" html-type="submit" :loading="loading"
  40. >{{ $t('login.registerBtn') }}
  41. </a-button>
  42. </a-form-item>
  43. </a-form>
  44. <div v-if="!form_type" class="tips-register">
  45. {{ $t('login.noAccount') }} <a class="reg-link" href="#" @click="changeFormType">{{ $t('login.registerBtn') }}</a>
  46. </div>
  47. </template>
  48. <script setup>
  49. import { ref, reactive, getCurrentInstance, nextTick } from 'vue';
  50. import { useRouter } from 'vue-router';
  51. import { useCurrentUserStore } from '../../base/store/use-current-user';
  52. import { getLoginFormRules } from './auth';
  53. import { register } from '../../apis/login';
  54. import { useI18n } from 'vue-i18n';
  55. const router = useRouter();
  56. const { appContext } = getCurrentInstance();
  57. const userStore = useCurrentUserStore();
  58. const { t } = useI18n();
  59. const $message = appContext.config.globalProperties.$message;
  60. const ref_form = ref();
  61. const form = reactive({
  62. username: '',
  63. email: '', // 非必填
  64. password: ''
  65. });
  66. const rules = getLoginFormRules(t);
  67. /**
  68. * 表单类型
  69. * 0:登录
  70. * 1:注册
  71. */
  72. const form_type = ref(0);
  73. const loading = ref(false);
  74. const handleSubmit = (data) => {
  75. loading.value = true;
  76. if (form_type.value) {
  77. // 注册
  78. toRegister();
  79. } else {
  80. // 登录
  81. userStore
  82. .toLogin({ username: data.username, password: data.password })
  83. .then((res) => {
  84. $message.success(res);
  85. router.push({ path: '/knowledge' });
  86. })
  87. .catch((err) => {
  88. $message.error(err);
  89. })
  90. .finally(() => {
  91. loading.value = false;
  92. });
  93. }
  94. };
  95. const toRegister = () => {
  96. register({ username: form.username, email: form.email, password: form.password })
  97. .then((res) => {
  98. if (res.code == 200 && res.data.username) {
  99. $message.success(res.msg);
  100. form_type.value = 0;
  101. ref_form.value.resetFields();
  102. nextTick(() => {
  103. form.username = res.data.username;
  104. });
  105. } else {
  106. $message.error(res.msg);
  107. }
  108. })
  109. .finally(() => {
  110. loading.value = false;
  111. });
  112. };
  113. const changeFormType = (e) => {
  114. e.preventDefault();
  115. ref_form.value.resetFields();
  116. form_type.value = 1;
  117. };
  118. </script>
  119. <style scoped>
  120. .form-title {
  121. line-height: 42px;
  122. font-size: 30px;
  123. font-weight: 500;
  124. }
  125. .submit-btn {
  126. width: 100%;
  127. height: 54px;
  128. background-color: rgb(var(--arcoblue-6));
  129. color: var(--color-neutral-1);
  130. font-size: 20px;
  131. border-radius: 3px;
  132. }
  133. .submit-btn:hover {
  134. background-color: rgba(var(--arcoblue-6), 0.8);
  135. color: var(--color-neutral-1);
  136. }
  137. .tips-register {
  138. position: absolute;
  139. right: 20px;
  140. bottom: 100px;
  141. font-size: 20px;
  142. color: var(--color-neutral-4);
  143. }
  144. .reg-link {
  145. color: rgb(var(--arcoblue-6));
  146. }
  147. </style>
  148. <style>
  149. .arco-form-item-label-col {
  150. padding: 0px;
  151. }
  152. .arco-input-wrapper {
  153. height: 54px;
  154. background-color: var(--color-bg-1);
  155. border-color: var(--color-neutral-3);
  156. }
  157. .arco-form-item-label {
  158. display: none;
  159. }
  160. </style>