FormItem.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 } 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, asycData } 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(async (res) => {
  84. $message.success(res);
  85. // 登录成功后,获取路由列表
  86. // 阻塞路由的获取,等待用户信息获取成功
  87. await userStore.getRoutesList();
  88. // 登录成功后,获取用户权限
  89. await userStore.getUserInfo();
  90. router.push({ path: '/' });
  91. })
  92. .catch((err) => {
  93. $message.error(err);
  94. })
  95. .finally(() => {
  96. loading.value = false;
  97. });
  98. }
  99. };
  100. const toRegister = () => {
  101. register({ username: form.username, email: form.email, password: form.password })
  102. .then((res) => {
  103. if (res.code == 200) {
  104. $message.success(res.msg);
  105. form_type.value = 0;
  106. ref_form.value.resetFields();
  107. toAsycData(res.data.userFlag);
  108. } else {
  109. $message.error(res.msg);
  110. }
  111. })
  112. .finally(() => {
  113. loading.value = false;
  114. });
  115. };
  116. // 注册后同步数据
  117. const toAsycData = async (userFlag) => {
  118. await asycData(userFlag);
  119. };
  120. const changeFormType = (e) => {
  121. e.preventDefault();
  122. ref_form.value.resetFields();
  123. form_type.value = 1;
  124. };
  125. </script>
  126. <style scoped>
  127. .form-title {
  128. line-height: 42px;
  129. font-size: 30px;
  130. font-weight: 500;
  131. color: var(--color-neutral-10);
  132. }
  133. .submit-btn {
  134. width: 100%;
  135. height: 54px;
  136. background-color: rgb(var(--arcoblue-6));
  137. color: var(--color-neutral-1);
  138. font-size: 20px;
  139. border-radius: 3px;
  140. }
  141. .submit-btn:hover {
  142. background-color: rgba(var(--arcoblue-6), 0.8);
  143. color: var(--color-neutral-1);
  144. }
  145. .tips-register {
  146. position: absolute;
  147. right: 20px;
  148. bottom: 100px;
  149. font-size: 20px;
  150. color: var(--color-neutral-4);
  151. }
  152. .reg-link {
  153. color: rgb(var(--arcoblue-6));
  154. }
  155. .arco-form-item-label-col {
  156. padding: 0px;
  157. }
  158. .arco-input-wrapper {
  159. height: 54px;
  160. background-color: var(--color-bg-1);
  161. border-color: var(--color-neutral-3);
  162. }
  163. :deep .arco-form-item-label-col > .arco-form-item-label {
  164. display: none;
  165. }
  166. </style>