code-login.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <script setup lang="ts">
  2. import type { Recordable } from '@vben/types';
  3. import type { VbenFormSchema } from '@vben-core/form-ui';
  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: [Recordable<any>];
  45. }>();
  46. const router = useRouter();
  47. const [Form, { validate, getValues }] = 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 } = await validate();
  59. const values = await getValues();
  60. if (valid) {
  61. emit('submit', {
  62. code: values?.code,
  63. phoneNumber: values?.phoneNumber,
  64. });
  65. }
  66. }
  67. function goToLogin() {
  68. router.push(props.loginPath);
  69. }
  70. </script>
  71. <template>
  72. <div>
  73. <Title>
  74. <slot name="title">
  75. {{ title || $t('authentication.welcomeBack') }} 📲
  76. </slot>
  77. <template #desc>
  78. <span class="text-muted-foreground">
  79. <slot name="subTitle">
  80. {{ subTitle || $t('authentication.codeSubtitle') }}
  81. </slot>
  82. </span>
  83. </template>
  84. </Title>
  85. <Form />
  86. <VbenButton
  87. :class="{
  88. 'cursor-wait': loading,
  89. }"
  90. :loading="loading"
  91. class="w-full"
  92. @click="handleSubmit"
  93. >
  94. <slot name="submitButtonText">
  95. {{ submitButtonText || $t('common.login') }}
  96. </slot>
  97. </VbenButton>
  98. <VbenButton class="mt-4 w-full" variant="outline" @click="goToLogin()">
  99. {{ $t('common.back') }}
  100. </VbenButton>
  101. </div>
  102. </template>