From 1e768b147f01db709514ec7cb6aa7ecf6aae24a7 Mon Sep 17 00:00:00 2001 From: wenhua <1084668738@qq.com> Date: Mon, 16 Oct 2023 14:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=A9=E5=8D=8E:=20=E6=B7=BB=E5=8A=A0authsto?= =?UTF-8?q?re=E5=B9=B6=E5=AF=B9=E6=95=B0=E6=8D=AE=E5=81=9A=E7=BB=88?= =?UTF-8?q?=E7=AB=AF=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/auth.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/stores/index.ts | 4 +++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/stores/auth.ts 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数据