mirror of
https://github.com/PlayEdu/backend
synced 2025-12-23 02:41:19 +08:00
登录跳转以及登录相关store
This commit is contained in:
7
src/store/hooks.ts
Normal file
7
src/store/hooks.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {
|
||||
useSelector as useReduxSelector,
|
||||
TypedUseSelectorHook,
|
||||
} from "react-redux";
|
||||
import { RootState } from "./store";
|
||||
|
||||
export const useSelector: TypedUseSelectorHook<RootState> = useReduxSelector;
|
||||
@@ -1,30 +1,7 @@
|
||||
import { createStore, applyMiddleware } from "redux";
|
||||
import userReducer from "./user/userReducer";
|
||||
import thunk from "redux-thunk";
|
||||
|
||||
import { createStore } from "redux";
|
||||
interface IAction {
|
||||
type: string;
|
||||
payload?: any;
|
||||
}
|
||||
interface IState {
|
||||
isshow: boolean;
|
||||
}
|
||||
const reducer = (
|
||||
preState: IState = {
|
||||
isshow: false,
|
||||
},
|
||||
action: IAction
|
||||
) => {
|
||||
const { type } = action;
|
||||
const newState = { ...preState };
|
||||
switch (type) {
|
||||
case "show":
|
||||
newState.isshow = true;
|
||||
return newState;
|
||||
case "hidden":
|
||||
newState.isshow = false;
|
||||
return newState;
|
||||
default:
|
||||
return preState;
|
||||
}
|
||||
};
|
||||
const store = createStore(reducer);
|
||||
const store = createStore(userReducer, applyMiddleware(thunk));
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export default store;
|
||||
|
||||
59
src/store/user/userActions.ts
Normal file
59
src/store/user/userActions.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { message } from "antd";
|
||||
|
||||
export const IS_LOGIN = "IS_LOGIN";
|
||||
export const LOGIN_OUT = "LOGIN_OUT";
|
||||
export const SET_USER = "SET_USER";
|
||||
export const SET_PERMISSSIONS = "SET_PERMISSSIONS";
|
||||
|
||||
interface IsLoginAction {
|
||||
type: typeof IS_LOGIN;
|
||||
}
|
||||
|
||||
interface LoginOutAction {
|
||||
type: typeof LOGIN_OUT;
|
||||
}
|
||||
|
||||
interface SetUserAction {
|
||||
type: typeof SET_USER;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
interface SetPermisssionsAction {
|
||||
type: typeof SET_PERMISSSIONS;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
export type UserLoginAction =
|
||||
| IsLoginAction
|
||||
| LoginOutAction
|
||||
| SetUserAction
|
||||
| SetPermisssionsAction;
|
||||
|
||||
export const IsLoginActionCreator = (): IsLoginAction => {
|
||||
return {
|
||||
type: IS_LOGIN,
|
||||
};
|
||||
};
|
||||
|
||||
export const LoginOutActionCreator = (): LoginOutAction => {
|
||||
message.success("已退出登录");
|
||||
return {
|
||||
type: LOGIN_OUT,
|
||||
};
|
||||
};
|
||||
|
||||
export const SetUserActionCreator = (data: any): SetUserAction => {
|
||||
return {
|
||||
type: SET_USER,
|
||||
payload: data,
|
||||
};
|
||||
};
|
||||
|
||||
export const SetPermisssionsActionCreator = (
|
||||
data: any
|
||||
): SetPermisssionsAction => {
|
||||
return {
|
||||
type: SET_PERMISSSIONS,
|
||||
payload: data,
|
||||
};
|
||||
};
|
||||
33
src/store/user/userReducer.ts
Normal file
33
src/store/user/userReducer.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
UserLoginAction,
|
||||
IS_LOGIN,
|
||||
LOGIN_OUT,
|
||||
SET_USER,
|
||||
SET_PERMISSSIONS,
|
||||
} from "./userActions";
|
||||
|
||||
interface UserState {
|
||||
permisssions: any[];
|
||||
user: any[];
|
||||
isLogin: boolean;
|
||||
}
|
||||
export const defaultState: UserState = {
|
||||
isLogin: false,
|
||||
permisssions: [],
|
||||
user: [],
|
||||
};
|
||||
|
||||
export default (state = defaultState, action: UserLoginAction) => {
|
||||
switch (action.type) {
|
||||
case IS_LOGIN:
|
||||
return { ...state, isLogin: true };
|
||||
case LOGIN_OUT:
|
||||
return { ...state, isLogin: false, user: [], permisssions: [] };
|
||||
case SET_USER:
|
||||
return { ...state, user: action.payload };
|
||||
case SET_PERMISSSIONS:
|
||||
return { ...state, permisssions: action.payload };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user