login.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script lang="ts" setup>
  2. import type { VbenFormSchema } from '@vben/common-ui';
  3. import type { Recordable } from '@vben-core/typings';
  4. import { computed, markRaw, ref, useTemplateRef } from 'vue';
  5. import { useRouter } from 'vue-router';
  6. import { AuthenticationLogin, SliderCaptcha, z } from '@vben/common-ui';
  7. import { $t } from '@vben/locales';
  8. import { message } from 'ant-design-vue';
  9. import { useAuthStore } from '#/store';
  10. defineOptions({ name: 'Login' });
  11. const authStore = useAuthStore();
  12. const formSchema = computed((): VbenFormSchema[] => {
  13. return [
  14. {
  15. component: 'VbenInput',
  16. componentProps: {
  17. placeholder: $t('authentication.usernameTip'),
  18. },
  19. fieldName: 'username',
  20. label: $t('authentication.username'),
  21. rules: z.string().min(1, { message: $t('authentication.usernameTip') }),
  22. },
  23. {
  24. component: 'VbenInputPassword',
  25. componentProps: {
  26. placeholder: $t('authentication.password'),
  27. },
  28. fieldName: 'password',
  29. label: $t('authentication.password'),
  30. rules: z.string().min(1, { message: $t('authentication.passwordTip') }),
  31. },
  32. {
  33. component: markRaw(SliderCaptcha),
  34. fieldName: 'captcha',
  35. rules: z.boolean().refine((value) => value, {
  36. message: $t('authentication.verifyRequiredTip'),
  37. }),
  38. },
  39. ];
  40. });
  41. const router = useRouter();
  42. const loginRef = useTemplateRef('loginRef');
  43. const loading = ref(false);
  44. async function handle(data: Recordable<any>) {
  45. loading.value = true;
  46. try {
  47. await authStore.login(data as any);
  48. } catch (error: any) {
  49. message.error(error?.message ?? `登录失败`);
  50. const formApi = loginRef.value?.getFormApi();
  51. formApi?.setFieldValue('captcha', false, false);
  52. formApi
  53. ?.getFieldComponentRef<InstanceType<typeof SliderCaptcha>>('captcha')
  54. ?.resume();
  55. } finally {
  56. loading.value = false;
  57. }
  58. }
  59. </script>
  60. <template>
  61. <AuthenticationLogin
  62. ref="loginRef"
  63. :form-schema="formSchema"
  64. :loading="loading"
  65. @submit="handle"
  66. :show-remember-me="false"
  67. :show-forget-password="false"
  68. :show-code-login="false"
  69. :show-qrcode-login="false"
  70. :show-third-party-login="false"
  71. :show-register="false"
  72. />
  73. </template>