登录成功请求系统配置

This commit is contained in:
none 2023-04-11 11:01:15 +08:00
parent 7bc01276b5
commit 86964737d3
3 changed files with 59 additions and 43 deletions

View File

@ -3,3 +3,7 @@ import client from "./internal/httpClient";
export function getImageCaptcha() { export function getImageCaptcha() {
return client.get("/backend/v1/system/image-captcha", {}); return client.get("/backend/v1/system/image-captcha", {});
} }
export function getSystemConfig() {
return client.get("/backend/v1/system/config", {});
}

View File

@ -1,7 +1,7 @@
import React, { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import styles from "./index.module.less"; import styles from "./index.module.less";
import { Spin, Input, Button, message } from "antd"; import { Spin, Input, Button, message } from "antd";
import { login, system } from "../../api/index"; import { login as loginApi, system } from "../../api/index";
import { setToken } from "../../utils/index"; import { setToken } from "../../utils/index";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
@ -9,6 +9,10 @@ import banner from "../../assets/images/login/banner.png";
import icon from "../../assets/images/login/icon.png"; import icon from "../../assets/images/login/icon.png";
import "./login.less"; import "./login.less";
import { loginAction } from "../../store/user/loginUserSlice"; import { loginAction } from "../../store/user/loginUserSlice";
import {
SystemConfigStoreInterface,
saveConfigAction,
} from "../../store/system/systemConfigSlice";
const LoginPage = () => { const LoginPage = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -22,6 +26,7 @@ const LoginPage = () => {
const [captchaLoading, setCaptchaLoading] = useState(true); const [captchaLoading, setCaptchaLoading] = useState(true);
const fetchImageCaptcha = () => { const fetchImageCaptcha = () => {
setCaptchaVal("");
setCaptchaLoading(true); setCaptchaLoading(true);
system.getImageCaptcha().then((res: any) => { system.getImageCaptcha().then((res: any) => {
setImage(res.data.image); setImage(res.data.image);
@ -30,7 +35,7 @@ const LoginPage = () => {
}); });
}; };
const loginSubmit = (e: any) => { const loginSubmit = async () => {
if (!email) { if (!email) {
message.error("请输入管理员邮箱账号"); message.error("请输入管理员邮箱账号");
return; return;
@ -43,47 +48,60 @@ const LoginPage = () => {
message.error("请输入图形验证码"); message.error("请输入图形验证码");
return; return;
} }
if (captchaVal.length < 4) { if (captchaVal.length !== 4) {
message.error("图形验证码错误"); message.error("图形验证码错误");
return; return;
} }
if (loading) { await handleSubmit();
return;
}
handleSubmit();
}; };
const handleSubmit = () => { const handleSubmit = async () => {
if (loading) { if (loading) {
return; return;
} }
setLoading(true); setLoading(true);
login try {
.login(email, password, captchaKey, captchaVal) let res: any = await loginApi.login(
.then((res: any) => { email,
const token = res.data.token; password,
setToken(token); captchaKey,
getUser(); captchaVal
}) );
.catch((e) => { setToken(res.data.token); //将token写入本地
setLoading(false); await getSystemConfig(); //获取系统配置并写入store
setCaptchaVal(""); await getUser(); //获取登录用户的信息并写入store
fetchImageCaptcha();
}); navigate("/");
} catch (e) {
message.error("登录出现错误");
console.error("错误信息", e);
setLoading(false);
fetchImageCaptcha(); //刷新图形验证码
}
}; };
const getUser = () => { const getUser = async () => {
login.getUser().then((res: any) => { let res: any = await loginApi.getUser();
const data = res.data; dispatch(loginAction(res.data));
dispatch(loginAction(data)); };
setLoading(false);
navigate("/"); const getSystemConfig = async () => {
}); let res: any = await system.getSystemConfig();
let data: SystemConfigStoreInterface = {
systemName: res.data["system.name"],
systemLogo: res.data["system.logo"],
systemApiUrl: res.data["system.api_url"],
systemPcUrl: res.data["system.pc_url"],
systemH5Url: res.data["system.h5_url"],
memberDefaultAvatar: res.data["member.default_avatar"],
courseDefaultThumbs: res.data["default.course_thumbs"],
};
dispatch(saveConfigAction(data));
}; };
const keyUp = (e: any) => { const keyUp = (e: any) => {
if (e.keyCode === 13) { if (e.keyCode === 13) {
loginSubmit(e); loginSubmit();
} }
}; };

View File

@ -1,25 +1,19 @@
import { createSlice } from "@reduxjs/toolkit"; import { createSlice } from "@reduxjs/toolkit";
type SystemConfigStoreInterface = { type SystemConfigStoreInterface = {
systemApiUrl: string; systemApiUrl?: string;
systemPcUrl: string; systemPcUrl?: string;
systemH5Url: string; systemH5Url?: string;
systemLogo: string; systemLogo?: string;
systemName: string; systemName?: string;
}; memberDefaultAvatar?: string;
courseDefaultThumbs?: string[];
let defaultValue: SystemConfigStoreInterface = {
systemApiUrl: "",
systemPcUrl: "",
systemH5Url: "",
systemLogo: "",
systemName: "",
}; };
const systemConfigSlice = createSlice({ const systemConfigSlice = createSlice({
name: "systemConfig", name: "systemConfig",
initialState: { initialState: {
value: defaultValue, value: {},
}, },
reducers: { reducers: {
saveConfigAction(stage, e) { saveConfigAction(stage, e) {