#
vincentlu
2025-02-14 9af56f878cc3e22d3bce6ab53d67f3753470a7ac
rsf-admin/src/page/login/Register.jsx
@@ -21,7 +21,7 @@
import ProviderChoices from "./ProviderChoices";
import Visibility from '@mui/icons-material/Visibility';
import VisibilityOff from '@mui/icons-material/VisibilityOff';
import { sendEmailCode } from '@/api/auth';
import { sendEmailCode, register } from '@/api/auth';
const Register = (props) => {
    const translate = useTranslate();
@@ -36,6 +36,7 @@
    const username = watch('username');
    const password = watch('password');
    const confirmPassword = watch('confirmPassword');
    const code = watch('code');
    const [loading, setLoading] = useState(false);
    const [codeLoading, setCodeLoading] = useState(false);
@@ -43,6 +44,7 @@
    const [isCounting, setIsCounting] = useState(false);
    const [countdown, setCountdown] = useState(60);
    // send code
    const handleSendCode = async () => {
        if (!email) {
            setError("email", {
@@ -64,6 +66,9 @@
            const { code, msg, data } = res;
            if (code === 200) {
                notify(msg, { type: 'success', messageArgs: { _: msg } });
                setIsCounting(true);
                setCountdown(60);
                localStorage.setItem('codeCountdown', 60);
            } else {
                notify(msg, { type: 'error', messageArgs: { _: msg } });
            }
@@ -74,7 +79,7 @@
        })
    };
    // 倒计时功能
    // countdown
    useEffect(() => {
        const savedCountdown = localStorage.getItem('codeCountdown');
        if (savedCountdown && !isCounting) {
@@ -89,43 +94,35 @@
            } else if (countdown <= 0) {
                clearInterval(interval);
                setIsCounting(false);
                localStorage.removeItem('codeCountdown'); // 重置
                localStorage.removeItem('codeCountdown');
            }
        }, 1000);
        return () => clearInterval(interval);
    }, [countdown, isCounting]);
    const onSubmit = (data) => {
        notify("Registration is not open yet");
        return;
    // register
    const onSubmit = (params) => {
        // console.log(params);
        setLoading(true);
        // js native confirm && root
        login(
            data,
            location.state ? (location.state).nextPathname : '/'
        ).catch((error) => {
        register(params).then(res => {
            setLoading(false);
            notify(
                typeof error === 'string'
                    ? error
                    : typeof error === 'undefined' || !error.message
                        ? 'ra.auth.sign_in_error'
                        : error.message,
                {
                    type: 'error',
                    messageArgs: {
                        _:
                            typeof error === 'string'
                                ? error
                                : error && error.message
                                    ? error.message
                                    : undefined,
                    },
            const { code, msg, data } = res;
            if (code === 200) {
                console.log(data);
                notify(msg, { type: 'success', messageArgs: { _: msg } });
            } else if (code === 10002) {
                setError("username", {
                    message: msg
                })
            } else {
                notify(msg, { type: 'error', messageArgs: { _: msg } });
                }
            );
        });
        }).catch((error) => {
            setLoading(false);
            notify(error.message, { type: 'error', messageArgs: { _: error.message } });
            console.error(error);
        })
    };
    return (
@@ -142,7 +139,7 @@
                        control={control}
                        defaultValue=""
                        rules={{ required: true }}
                        render={({ field }) => (
                        render={({ field, fieldState: { error } }) => (
                            <TextField
                                {...field}
                                label={translate("page.login.username")}
@@ -150,6 +147,8 @@
                                disabled={loading}
                                autoFocus
                                autoComplete="off"
                                error={!!error}
                                helperText={error?.message || ""}
                            />
                        )}
                    />
@@ -306,7 +305,7 @@
                    <Button
                        type="submit"
                        variant="contained"
                        disabled={loading || !(email && username && password && confirmPassword)}
                        disabled={loading || !(email && username && password && confirmPassword && code)}
                        sx={{
                            backgroundColor: "#3D4BA7"
                        }}