import React, { useState, useEffect } from "react"; import styles from "./Login.module.css"; import { Typography, Spin, Input, Button, message } from "antd"; import { login, system } from "../../api/index"; import { setToken } from "../../utils/index"; export const Login: React.FC = () => { const [loading, setLoading] = useState(true); const [image, setImage] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [captcha_val, setCaptcha_val] = useState(""); const [captcha_key, setCaptcha_key] = useState(""); const fetchData = () => { setLoading(true); system.getImageCaptcha().then((res: any) => { setImage(res.data.image); setCaptcha_key(res.data.key); setLoading(false); }); }; const loginSubmit = (e: any) => { if (!email) { message.error("请输入账号"); return; } if (!password) { message.error("请输入密码号"); return; } if (!captcha_val) { message.error("请输入图片验证码"); return; } if (loading) { return; } handleSubmit(); }; const handleSubmit = () => { setLoading(true); login .login(email, password, captcha_key, captcha_val) .then((res: any) => { const token = res.data.token; setToken(token); setLoading(false); getUser(); }) .catch((e) => { setLoading(false); setCaptcha_val(""); fetchData(); }); }; const getUser = () => { login.getUser().then((res) => { console.log(res); }); }; useEffect(() => { fetchData(); }, []); return (
登录后台
{ setEmail(e.target.value); }} style={{ width: 300 }} placeholder="请输入账号" />
{ setPassword(e.target.value); }} style={{ width: 300 }} placeholder="请输入密码" />
{ setCaptcha_val(e.target.value); }} />
); };