2
2
import type { VbenFormSchema } from ' @vben/common-ui' ;
3
3
import type { Recordable } from ' @vben/types' ;
4
4
5
- import { computed , ref } from ' vue' ;
5
+ import { computed , ref , useTemplateRef } from ' vue' ;
6
6
7
7
import { AuthenticationCodeLogin , z } from ' @vben/common-ui' ;
8
8
import { $t } from ' @vben/locales' ;
9
9
10
+ import { message } from ' ant-design-vue' ;
11
+
10
12
defineOptions ({ name: ' CodeLogin' });
11
13
12
14
const loading = ref (false );
13
15
const CODE_LENGTH = 6 ;
14
-
16
+ const loginRef =
17
+ useTemplateRef <InstanceType <typeof AuthenticationCodeLogin >>(' loginRef' );
18
+ function sendCodeApi(phoneNumber : string ) {
19
+ message .loading ({
20
+ content: $t (' page.auth.sendingCode' ),
21
+ duration: 0 ,
22
+ key: ' sending-code' ,
23
+ });
24
+ return new Promise ((resolve ) => {
25
+ setTimeout (() => {
26
+ message .success ({
27
+ content: $t (' page.auth.codeSentTo' , [phoneNumber ]),
28
+ duration: 3 ,
29
+ key: ' sending-code' ,
30
+ });
31
+ resolve ({ code: ' 123456' , phoneNumber });
32
+ }, 3000 );
33
+ });
34
+ }
15
35
const formSchema = computed ((): VbenFormSchema [] => {
16
36
return [
17
37
{
@@ -39,6 +59,25 @@ const formSchema = computed((): VbenFormSchema[] => {
39
59
: $t (' authentication.sendCode' );
40
60
return text ;
41
61
},
62
+ handleSendCode : async () => {
63
+ // 模拟发送验证码
64
+ // Simulate sending verification code
65
+ loading .value = true ;
66
+ const formApi = loginRef .value ?.getFormApi ();
67
+ if (! formApi ) {
68
+ loading .value = false ;
69
+ throw new Error (' formApi is not ready' );
70
+ }
71
+ await formApi .validateField (' phoneNumber' );
72
+ const isPhoneReady = await formApi .isFieldValid (' phoneNumber' );
73
+ if (! isPhoneReady ) {
74
+ loading .value = false ;
75
+ throw new Error (' Phone number is not Ready' );
76
+ }
77
+ const { phoneNumber } = await formApi .getValues ();
78
+ await sendCodeApi (phoneNumber );
79
+ loading .value = false ;
80
+ },
42
81
placeholder: $t (' authentication.code' ),
43
82
},
44
83
fieldName: ' code' ,
@@ -62,6 +101,7 @@ async function handleLogin(values: Recordable<any>) {
62
101
63
102
<template >
64
103
<AuthenticationCodeLogin
104
+ ref =" loginRef"
65
105
:form-schema =" formSchema"
66
106
:loading =" loading"
67
107
@submit =" handleLogin"
0 commit comments