From e6be6598d70f1fc4e6d4bf33d6e43dc6492187ad Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@gmail.com>
Date: 星期四, 13 二月 2025 22:49:04 +0800
Subject: [PATCH] #

---
 rsf-admin/src/page/login/Register.jsx |   61 +++++++++++++++++++++---------
 1 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/rsf-admin/src/page/login/Register.jsx b/rsf-admin/src/page/login/Register.jsx
index 881f5be..f927d52 100644
--- a/rsf-admin/src/page/login/Register.jsx
+++ b/rsf-admin/src/page/login/Register.jsx
@@ -15,11 +15,13 @@
     useTranslate,
     useLogin,
     useNotify,
+    email as validEmail,
 } from 'react-admin';
 import { useForm, Controller } from 'react-hook-form';
 import ProviderChoices from "./ProviderChoices";
 import Visibility from '@mui/icons-material/Visibility';
 import VisibilityOff from '@mui/icons-material/VisibilityOff';
+import { sendEmailCode } from '@/api/auth';
 
 const Register = (props) => {
     const translate = useTranslate();
@@ -28,7 +30,7 @@
     const location = useLocation();
     const { systemInfo } = props;
 
-    const { control, watch, handleSubmit, setValue } = useForm();
+    const { control, watch, handleSubmit, setValue, setError, clearErrors } = useForm();
 
     const email = watch('email');
     const username = watch('username');
@@ -36,20 +38,40 @@
     const confirmPassword = watch('confirmPassword');
 
     const [loading, setLoading] = useState(false);
+    const [codeLoading, setCodeLoading] = useState(false);
     const [showPassword, setShowPassword] = useState(true);
-
-
     const [isCounting, setIsCounting] = useState(false);
     const [countdown, setCountdown] = useState(60);
 
-    // 澶勭悊楠岃瘉鐮佹寜閽偣鍑�
     const handleSendCode = async () => {
-        // 杩欓噷鍋囪鍙戦�侀獙璇佺爜鐨勮姹�
-        const response = await fetch('/api/send-code');
-        if (response.ok) {
-            setIsCounting(true);
-            localStorage.setItem('codeCountdown', 60); // 瀛樺偍鍊掕鏃跺埌鏈湴
+        if (!email) {
+            setError("email", {
+                message: translate('ra.validation.required')
+            })
+            return;
         }
+        const emailError = validEmail()(email);
+        if (emailError) {
+            setError("email", {
+                message: translate("ra.validation.email")
+            })
+            return;
+        }
+        clearErrors("email");
+        setCodeLoading(true);
+        sendEmailCode({ email }).then(res => {
+            setCodeLoading(false);
+            const { code, msg, data } = res;
+            if (code === 200) {
+                notify(msg, { type: 'success', messageArgs: { _: msg } });
+            } else {
+                notify(msg, { type: 'error', messageArgs: { _: msg } });
+            }
+        }).catch((error) => {
+            setCodeLoading(false);
+            notify(error.message, { type: 'error', messageArgs: { _: error.message } });
+            console.error(error);
+        })
     };
 
     // 鍊掕鏃跺姛鑳�
@@ -260,19 +282,22 @@
                         <Button
                             variant="outlined"
                             onClick={handleSendCode}
-                            disabled={isCounting || loading}
+                            disabled={codeLoading || isCounting}
                             sx={{
+                                width: '35%',
                                 mt: 1,
+                                whiteSpace: 'nowrap',
                             }}
                         >
-                            {isCounting ? (
-                                <>
-                                    <CircularProgress size={20} color="primary" sx={{ marginRight: 1 }} />
-                                    {`${countdown}s`}
-                                </>
-                            ) : (
-                                translate('page.login.button.code')
-                            )}
+                            {codeLoading ? (
+                                <CircularProgress size={20} color="primary" sx={{ marginRight: 1 }} />
+                            ) :
+                                isCounting ? (
+                                    `${countdown}s`
+                                ) : (
+                                    translate('page.login.button.code')
+                                )
+                            }
                         </Button>
                     </Box>
 

--
Gitblit v1.9.1