code-login.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <script setup lang="ts">
  2. import type { VbenFormSchema } from '@vben-core/form-ui';
  3. import type { LoginCodeEmits } from './types';
  4. import { computed, reactive } from 'vue';
  5. import { useRouter } from 'vue-router';
  6. import { $t } from '@vben/locales';
  7. import { useVbenForm } from '@vben-core/form-ui';
  8. import { VbenButton } from '@vben-core/shadcn-ui';
  9. import Title from './auth-title.vue';
  10. interface Props {
  11. formSchema: VbenFormSchema[];
  12. /**
  13. * @zh_CN 是否处于加载处理状态
  14. */
  15. loading?: boolean;
  16. /**
  17. * @zh_CN 登陆路径
  18. */
  19. loginPath?: string;
  20. /**
  21. * @zh_CN 标题
  22. */
  23. title?: string;
  24. /**
  25. * @zh_CN 描述
  26. */
  27. subTitle?: string;
  28. /**
  29. * @zh_CN 按钮文本
  30. */
  31. submitButtonText?: string;
  32. }
  33. defineOptions({
  34. name: 'AuthenticationCodeLogin',
  35. });
  36. const props = withDefaults(defineProps<Props>(), {
  37. loading: false,
  38. loginPath: '/auth/login',
  39. submitButtonText: '',
  40. subTitle: '',
  41. title: '',
  42. });
  43. const emit = defineEmits<{
  44. submit: LoginCodeEmits['submit'];
  45. }>();
  46. const router = useRouter();
  47. const [Form, { validate }] = useVbenForm(
  48. reactive({
  49. commonConfig: {
  50. hideLabel: true,
  51. hideRequiredMark: true,
  52. },
  53. schema: computed(() => props.formSchema),
  54. showDefaultActions: false,
  55. }),
  56. );
  57. async function handleSubmit() {
  58. const { valid, values } = await validate();
  59. if (valid) {
  60. emit('submit', {
  61. code: values?.code,
  62. phoneNumber: values?.phoneNumber,
  63. });
  64. }
  65. }
  66. function goToLogin() {
  67. router.push(props.loginPath);
  68. }
  69. </script>
  70. <template>
  71. <div>
  72. <Title>
  73. <slot name="title">
  74. {{ title || $t('authentication.welcomeBack') }} 📲
  75. </slot>
  76. <template #desc>
  77. <span class="text-muted-foreground">
  78. <slot name="subTitle">
  79. {{ subTitle || $t('authentication.codeSubtitle') }}
  80. </slot>
  81. </span>
  82. </template>
  83. </Title>
  84. <Form />
  85. <VbenButton
  86. :class="{
  87. 'cursor-wait': loading,
  88. }"
  89. :loading="loading"
  90. class="w-full"
  91. @click="handleSubmit"
  92. >
  93. <slot name="submitButtonText">
  94. {{ submitButtonText || $t('common.login') }}
  95. </slot>
  96. </VbenButton>
  97. <VbenButton class="mt-4 w-full" variant="outline" @click="goToLogin()">
  98. {{ $t('common.back') }}
  99. </VbenButton>
  100. </div>
  101. </template>