This commit is contained in:
none
2023-03-23 17:29:43 +08:00
parent 9f90bd75d3
commit 89b6307e2d
16 changed files with 163 additions and 161 deletions

View File

@@ -1,7 +0,0 @@
import {
useSelector as useReduxSelector,
TypedUseSelectorHook,
} from "react-redux";
import { RootState } from "./store";
export const useSelector: TypedUseSelectorHook<RootState> = useReduxSelector;

12
src/store/index.ts Normal file
View File

@@ -0,0 +1,12 @@
import { configureStore } from "@reduxjs/toolkit";
import systemConfigReducer from "./system/systemConfigSlice";
import loginUserReducer from "./user/loginUserSlice";
const store = configureStore({
reducer: {
loginUser: loginUserReducer,
systemConfig: systemConfigReducer,
},
});
export default store;

View File

@@ -1,7 +0,0 @@
import { createStore, applyMiddleware } from "redux";
import userReducer from "./user/userReducer";
import thunk from "redux-thunk";
const store = createStore(userReducer, applyMiddleware(thunk));
export type RootState = ReturnType<typeof store.getState>;
export default store;

View File

@@ -0,0 +1,34 @@
import { createSlice } from "@reduxjs/toolkit";
type SystemConfigStoreInterface = {
systemApiUrl: string;
systemPcUrl: string;
systemH5Url: string;
systemLogo: string;
systemName: string;
};
let defaultValue: SystemConfigStoreInterface = {
systemApiUrl: "",
systemPcUrl: "",
systemH5Url: "",
systemLogo: "",
systemName: "",
};
const systemConfigSlice = createSlice({
name: "systemConfig",
initialState: {
value: defaultValue,
},
reducers: {
saveConfigAction(stage, e) {
stage.value = e.payload;
},
},
});
export default systemConfigSlice.reducer;
export const { saveConfigAction } = systemConfigSlice.actions;
export type { SystemConfigStoreInterface };

View File

@@ -0,0 +1,42 @@
import { createSlice } from "@reduxjs/toolkit";
type UserInterface = {
id: number;
name: string;
email: string;
};
type UserStoreInterface = {
user: UserInterface | null;
isLogin: boolean;
permissions: string[];
};
let defaultValue: UserStoreInterface = {
user: null,
isLogin: false,
permissions: [],
};
const loginUserSlice = createSlice({
name: "loginUser",
initialState: {
value: defaultValue,
},
reducers: {
loginAction(stage, e) {
stage.value.user = e.payload.user;
stage.value.permissions = e.payload.permissions;
stage.value.isLogin = true;
},
logoutAction(stage) {
stage.value.user = null;
stage.value.isLogin = false;
},
},
});
export default loginUserSlice.reducer;
export const { loginAction, logoutAction } = loginUserSlice.actions;
export type { UserStoreInterface };

View File

@@ -1,59 +0,0 @@
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,
};
};

View File

@@ -1,33 +0,0 @@
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;
}
};