登录及相关逻辑

This commit is contained in:
禺狨 2023-03-23 17:13:57 +08:00
parent 4b965f26f5
commit a3da93be82
3 changed files with 8 additions and 3 deletions

View File

@ -6,7 +6,7 @@ import LoadingPage from "./pages/loading";
import { user } from "./api/index"; import { user } from "./api/index";
import { getToken } from "./utils/index"; import { getToken } from "./utils/index";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import { logoutAction } from "./store/user/loginUserSlice"; import { loginAction } from "./store/user/loginUserSlice";
function App() { function App() {
const Views = () => useRoutes(routes); const Views = () => useRoutes(routes);
@ -14,7 +14,7 @@ function App() {
const getUser = () => { const getUser = () => {
user.detail().then((res: any) => { user.detail().then((res: any) => {
const data = res.data; const data = res.data;
dispatch(logoutAction(data.user)); dispatch(loginAction(data));
}); });
}; };
if (getToken()) { if (getToken()) {

View File

@ -80,7 +80,7 @@ const LoginPage: React.FC = () => {
const getUser = () => { const getUser = () => {
user.detail().then((res: any) => { user.detail().then((res: any) => {
const data = res.data; const data = res.data;
dispatch(loginAction(data.user)); dispatch(loginAction(data));
setLoading(false); setLoading(false);
navigate("/"); navigate("/");
}); });

View File

@ -2,11 +2,13 @@ import { createSlice } from "@reduxjs/toolkit";
type UserStoreInterface = { type UserStoreInterface = {
user: any; user: any;
departments: any;
isLogin: boolean; isLogin: boolean;
}; };
let defaultValue: UserStoreInterface = { let defaultValue: UserStoreInterface = {
user: null, user: null,
departments: null,
isLogin: false, isLogin: false,
}; };
@ -17,11 +19,14 @@ const loginUserSlice = createSlice({
}, },
reducers: { reducers: {
loginAction(stage, e) { loginAction(stage, e) {
console.log(e);
stage.value.user = e.payload.user; stage.value.user = e.payload.user;
stage.value.departments = e.payload.departments;
stage.value.isLogin = true; stage.value.isLogin = true;
}, },
logoutAction(stage) { logoutAction(stage) {
stage.value.user = null; stage.value.user = null;
stage.value.departments = null;
stage.value.isLogin = false; stage.value.isLogin = false;
}, },
}, },