温华: 添加authstore并对数据做终端缓存
This commit is contained in:
49
src/stores/auth.ts
Normal file
49
src/stores/auth.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
|
||||||
|
import { getAuthInfoApi } from '../api/auth'
|
||||||
|
|
||||||
|
export interface IAuthInfo {
|
||||||
|
permissions: Array<string>,
|
||||||
|
roles: Array<string>
|
||||||
|
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
|
||||||
@@ -4,6 +4,7 @@ import storage from "redux-persist/lib/storage";
|
|||||||
import cacheSlice from "./cache";
|
import cacheSlice from "./cache";
|
||||||
import sidebarSlice from "./sidebar";
|
import sidebarSlice from "./sidebar";
|
||||||
import permissionSlice from "./permission";
|
import permissionSlice from "./permission";
|
||||||
|
import authInfoSlice from "./auth"
|
||||||
|
|
||||||
const persistConfig = {
|
const persistConfig = {
|
||||||
key: 'root',
|
key: 'root',
|
||||||
@@ -14,7 +15,8 @@ const persistConfig = {
|
|||||||
const reducers = combineReducers({
|
const reducers = combineReducers({
|
||||||
cache: cacheSlice,
|
cache: cacheSlice,
|
||||||
sidebar: sidebarSlice,
|
sidebar: sidebarSlice,
|
||||||
permission: permissionSlice
|
permission: permissionSlice,
|
||||||
|
auth: authInfoSlice
|
||||||
})
|
})
|
||||||
|
|
||||||
// configureStore创建一个redux数据
|
// configureStore创建一个redux数据
|
||||||
|
|||||||
Reference in New Issue
Block a user