53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
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(state);
|
|
|
|
console.log('正在请求登录用户信息')
|
|
})
|
|
.addCase(getAuthInfo.fulfilled, (state, {payload}) => {
|
|
console.log(state);
|
|
return payload
|
|
})
|
|
.addCase(getAuthInfo.rejected, (state, err) => {
|
|
console.log(state);
|
|
console.log(err, 'rejected')
|
|
})
|
|
}
|
|
})
|
|
|
|
export const { addAuthInfo } = authInfoSlice.actions
|
|
export default authInfoSlice.reducer |