学员登录ldap优化

This commit is contained in:
unknown 2023-09-04 16:57:32 +08:00
parent b01a392b5a
commit 5d03a69888
4 changed files with 40 additions and 13 deletions

View File

@ -10,3 +10,10 @@ export function login(email: string, password: string) {
export function logout() { export function logout() {
return client.post("/api/v1/auth/logout", {}); return client.post("/api/v1/auth/logout", {});
} }
export function loginLdap(email: string, password: string) {
return client.post("/api/v1/auth/login/ldap", {
username: email,
password: password,
});
}

View File

@ -24,6 +24,7 @@ export const InitPage = (props: Props) => {
if (props.configData) { if (props.configData) {
let config: SystemConfigStoreInterface = { let config: SystemConfigStoreInterface = {
//系统配置 //系统配置
"ldap-enabled": props.configData["ldap-enabled"],
systemApiUrl: props.configData["system-api-url"], systemApiUrl: props.configData["system-api-url"],
systemH5Url: props.configData["system-h5-url"], systemH5Url: props.configData["system-h5-url"],
systemLogo: props.configData["system-logo"], systemLogo: props.configData["system-logo"],

View File

@ -1,7 +1,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Button, Toast, Input, Image } from "antd-mobile"; import { Button, Toast, Input, Image } from "antd-mobile";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
import { useDispatch } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { login, system, user } from "../../api/index"; import { login, system, user } from "../../api/index";
import { setToken } from "../../utils/index"; import { setToken } from "../../utils/index";
@ -19,6 +19,7 @@ const LoginPage = () => {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [bodyHeight, setBodyHeight] = useState(0); const [bodyHeight, setBodyHeight] = useState(0);
const systemConfig = useSelector((state: any) => state.systemConfig.value);
useEffect(() => { useEffect(() => {
document.title = "登录"; document.title = "登录";
@ -29,7 +30,7 @@ const LoginPage = () => {
const loginSubmit = async (e: any) => { const loginSubmit = async (e: any) => {
if (!email) { if (!email) {
Toast.show({ Toast.show({
content: "请输入学员邮箱账号", content: "请输入邮箱或uid",
}); });
return; return;
} }
@ -47,17 +48,32 @@ const LoginPage = () => {
return; return;
} }
setLoading(true); setLoading(true);
try { if (systemConfig["ldap-enabled"] === "1") {
let res: any = await login.login(email, password); try {
setToken(res.data.token); //将token写入本地 let res: any = await login.loginLdap(email, password);
await getSystemConfig(); //获取系统配置并写入store setToken(res.data.token); //将token写入本地
await getUser(); //获取登录用户的信息并写入store await getSystemConfig(); //获取系统配置并写入store
setLoading(false); await getUser(); //获取登录用户的信息并写入store
navigate("/member", { replace: true }); setLoading(false);
} catch (e) { navigate("/member", { replace: true });
console.error("错误信息", e); } catch (e) {
setLoading(false); console.error("错误信息", e);
setLoading(false);
}
}else{
try {
let res: any = await login.login(email, password);
setToken(res.data.token); //将token写入本地
await getSystemConfig(); //获取系统配置并写入store
await getUser(); //获取登录用户的信息并写入store
setLoading(false);
navigate("/member", { replace: true });
} catch (e) {
console.error("错误信息", e);
setLoading(false);
}
} }
}; };
const getUser = async () => { const getUser = async () => {
@ -70,6 +86,7 @@ const LoginPage = () => {
if (configRes.data) { if (configRes.data) {
let config: SystemConfigStoreInterface = { let config: SystemConfigStoreInterface = {
//系统配置 //系统配置
"ldap-enabled": configRes.configData["ldap-enabled"],
systemApiUrl: configRes.data["system-api-url"], systemApiUrl: configRes.data["system-api-url"],
systemH5Url: configRes.data["system-h5-url"], systemH5Url: configRes.data["system-h5-url"],
systemLogo: configRes.data["system-logo"], systemLogo: configRes.data["system-logo"],
@ -110,7 +127,7 @@ const LoginPage = () => {
<div className={styles["input-box"]}> <div className={styles["input-box"]}>
<Input <Input
className={styles["input-item"]} className={styles["input-item"]}
placeholder="请输入学员邮箱账号" placeholder="请输入邮箱或uid"
value={email} value={email}
onChange={(val) => { onChange={(val) => {
setEmail(val); setEmail(val);

View File

@ -1,6 +1,7 @@
import { createSlice } from "@reduxjs/toolkit"; import { createSlice } from "@reduxjs/toolkit";
type SystemConfigStoreInterface = { type SystemConfigStoreInterface = {
"ldap-enabled": string;
systemApiUrl: string; systemApiUrl: string;
systemPcUrl: string; systemPcUrl: string;
systemH5Url: string; systemH5Url: string;
@ -16,6 +17,7 @@ type SystemConfigStoreInterface = {
}; };
let defaultValue: SystemConfigStoreInterface = { let defaultValue: SystemConfigStoreInterface = {
"ldap-enabled": "",
systemApiUrl: "", systemApiUrl: "",
systemPcUrl: "", systemPcUrl: "",
systemH5Url: "", systemH5Url: "",