Skip to content

Commit

Permalink
chore: improve code login demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetfan committed Dec 16, 2024
1 parent d6cb803 commit 6d1b3a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion playground/src/locales/langs/en-US/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"register": "Register",
"codeLogin": "Code Login",
"qrcodeLogin": "Qr Code Login",
"forgetPassword": "Forget Password"
"forgetPassword": "Forget Password",
"sendingCode": "SMS Code is sending...",
"codeSentTo": "Code has been sent to {0}"
},
"dashboard": {
"title": "Dashboard",
Expand Down
4 changes: 3 additions & 1 deletion playground/src/locales/langs/zh-CN/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"register": "注册",
"codeLogin": "验证码登录",
"qrcodeLogin": "二维码登录",
"forgetPassword": "忘记密码"
"forgetPassword": "忘记密码",
"sendingCode": "正在发送验证码",
"codeSentTo": "验证码已发送至{0}"
},
"dashboard": {
"title": "概览",
Expand Down
44 changes: 42 additions & 2 deletions playground/src/views/_core/authentication/code-login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
import type { VbenFormSchema } from '@vben/common-ui';
import type { Recordable } from '@vben/types';
import { computed, ref } from 'vue';
import { computed, ref, useTemplateRef } from 'vue';
import { AuthenticationCodeLogin, z } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { message } from 'ant-design-vue';
defineOptions({ name: 'CodeLogin' });
const loading = ref(false);
const CODE_LENGTH = 6;
const loginRef =
useTemplateRef<InstanceType<typeof AuthenticationCodeLogin>>('loginRef');
function sendCodeApi(phoneNumber: string) {
message.loading({
content: $t('page.auth.sendingCode'),
duration: 0,
key: 'sending-code',
});
return new Promise((resolve) => {
setTimeout(() => {
message.success({
content: $t('page.auth.codeSentTo', [phoneNumber]),
duration: 3,
key: 'sending-code',
});
resolve({ code: '123456', phoneNumber });
}, 3000);
});
}
const formSchema = computed((): VbenFormSchema[] => {
return [
{
Expand Down Expand Up @@ -39,6 +59,25 @@ const formSchema = computed((): VbenFormSchema[] => {
: $t('authentication.sendCode');
return text;
},
handleSendCode: async () => {
// 模拟发送验证码
// Simulate sending verification code
loading.value = true;
const formApi = loginRef.value?.getFormApi();
if (!formApi) {
loading.value = false;
throw new Error('formApi is not ready');
}
await formApi.validateField('phoneNumber');
const isPhoneReady = await formApi.isFieldValid('phoneNumber');
if (!isPhoneReady) {
loading.value = false;
throw new Error('Phone number is not Ready');
}
const { phoneNumber } = await formApi.getValues();
await sendCodeApi(phoneNumber);
loading.value = false;
},
placeholder: $t('authentication.code'),
},
fieldName: 'code',
Expand All @@ -62,6 +101,7 @@ async function handleLogin(values: Recordable<any>) {

<template>
<AuthenticationCodeLogin
ref="loginRef"
:form-schema="formSchema"
:loading="loading"
@submit="handleLogin"
Expand Down

0 comments on commit 6d1b3a4

Please sign in to comment.