import { useState, useEffect } from "react"; import { Button, Toast, SpinLoading, Input, Image } from "antd-mobile"; import styles from "./index.module.scss"; import { useDispatch } from "react-redux"; import { useNavigate } from "react-router-dom"; import { login, system, user } from "../../api/index"; import { setToken } from "../../utils/index"; import { loginAction } from "../../store/user/loginUserSlice"; import { SystemConfigStoreInterface, saveConfigAction, } from "../../store/system/systemConfigSlice"; import banner from "../../assets/images/login/banner.png"; const LoginPage = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [image, setImage] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [captchaVal, setCaptchaVal] = useState(""); const [captchaKey, setCaptchaKey] = useState(""); const [captchaLoading, setCaptchaLoading] = useState(true); useEffect(() => { fetchImageCaptcha(); document.title = "登录"; }, []); const fetchImageCaptcha = () => { setCaptchaLoading(true); system.imageCaptcha().then((res: any) => { setImage(res.data.image); setCaptchaKey(res.data.key); setCaptchaLoading(false); }); }; const loginSubmit = async (e: any) => { if (!email) { Toast.show({ content: "请输入学员邮箱账号", }); return; } if (!password) { Toast.show({ content: "请输入密码", }); return; } if (!captchaVal) { Toast.show({ content: "请输入图形验证码", }); return; } if (captchaVal.length < 4) { Toast.show({ content: "图形验证码错误", }); return; } await handleSubmit(); }; const handleSubmit = async () => { if (loading) { return; } setLoading(true); try { let res: any = await login.login(email, password, captchaKey, captchaVal); setToken(res.data.token); //将token写入本地 await getSystemConfig(); //获取系统配置并写入store await getUser(); //获取登录用户的信息并写入store setLoading(false); navigate("/member", { replace: true }); } catch (e) { console.error("错误信息", e); setLoading(false); setCaptchaVal(""); fetchImageCaptcha(); //刷新图形验证码 } }; const getUser = async () => { let res: any = await user.detail(); dispatch(loginAction(res.data)); }; const getSystemConfig = async () => { let configRes: any = await system.config(); if (configRes.data) { let config: SystemConfigStoreInterface = { //系统配置 systemApiUrl: configRes.data["system-api-url"], systemH5Url: configRes.data["system-h5-url"], systemLogo: configRes.data["system-logo"], systemName: configRes.data["system-name"], systemPcUrl: configRes.data["system-pc-url"], pcIndexFooterMsg: configRes.data["system-pc-index-footer-msg"], //播放器配置 playerPoster: configRes.data["player-poster"], playerIsEnabledBulletSecret: configRes.data["player-is-enabled-bullet-secret"] && configRes.data["player-is-enabled-bullet-secret"] === "1" ? true : false, playerIsDisabledDrag: configRes.data["player-disabled-drag"] && configRes.data["player-disabled-drag"] === "1" ? true : false, playerBulletSecretText: configRes.data["player-bullet-secret-text"], playerBulletSecretColor: configRes.data["player-bullet-secret-color"], playerBulletSecretOpacity: configRes.data["player-bullet-secret-opacity"], }; dispatch(saveConfigAction(config)); } }; return (
学员登录
{ setEmail(val); }} />
{ setPassword(val); }} />
{ setCaptchaVal(val); }} />
{captchaLoading && (
)} {!captchaLoading && ( )}
); }; export default LoginPage;