diff --git a/src/stores/auth.ts b/src/stores/auth.ts new file mode 100644 index 0000000..c224dcd --- /dev/null +++ b/src/stores/auth.ts @@ -0,0 +1,49 @@ +import { createSlice, createAsyncThunk } from '@reduxjs/toolkit' +import { getAuthInfoApi } from '../api/auth' + +export interface IAuthInfo { + permissions: Array, + roles: Array + user: object +} + +const initAuthInfo = new Object() as IAuthInfo + +export const getAuthInfo = createAsyncThunk('/auth/info', + async () => { + try { + const {code, data} = await getAuthInfoApi() + if(code === 1000) { + return data + } + } catch (err) { + return Promise.reject(err) + } + } +) + +export const authInfoSlice = createSlice({ + name: 'authinfo', + initialState: initAuthInfo, + reducers: { + addAuthInfo(state, {payload}) { + state = {...payload} + return state + } + }, + extraReducers(builder) { + builder + .addCase(getAuthInfo.pending, (state) => { + console.log('正在请求登录用户信息') + }) + .addCase(getAuthInfo.fulfilled, (state, {payload}) => { + return payload + }) + .addCase(getAuthInfo.rejected, (state, err) => { + console.log(err, 'rejected') + }) + } +}) + +export const { addAuthInfo } = authInfoSlice.actions +export default authInfoSlice.reducer \ No newline at end of file diff --git a/src/stores/index.ts b/src/stores/index.ts index 641137c..959cdc1 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -4,6 +4,7 @@ import storage from "redux-persist/lib/storage"; import cacheSlice from "./cache"; import sidebarSlice from "./sidebar"; import permissionSlice from "./permission"; +import authInfoSlice from "./auth" const persistConfig = { key: 'root', @@ -14,7 +15,8 @@ const persistConfig = { const reducers = combineReducers({ cache: cacheSlice, sidebar: sidebarSlice, - permission: permissionSlice + permission: permissionSlice, + auth: authInfoSlice }) // configureStore创建一个redux数据