完成顶部导航条
> 添加左侧logo
> 添加右侧用户信息展示
> 修复一些登录注册的跳转问题
> 修复axios拦截器错误的头设置
> 修复authApi错误的接口路径
> 组织api文件结构
Change-Id: Ifaec7e9a78ad6862ce7d0ce76be5181185186edd
diff --git a/src/feature/user/userSlice.ts b/src/feature/user/userSlice.ts
index e69de29..d6200af 100644
--- a/src/feature/user/userSlice.ts
+++ b/src/feature/user/userSlice.ts
@@ -0,0 +1,75 @@
+// src/store/userSlice.ts
+import { createSlice, createAsyncThunk, type PayloadAction } from '@reduxjs/toolkit';
+import type { UserInfo } from '../../api/User/type';
+import UserAPi from '../../api/User/UserApi';
+
+// 定义用户信息的类型
+interface UserState {
+ username: string;
+ userid: string;
+ email: string;
+ status: 'idle' | 'loading' | 'succeeded' | 'failed';
+ error: string | null;
+}
+
+// 定义初始状态
+const initialState: UserState = {
+ username: '',
+ userid: '',
+ email: '',
+ status: 'idle',
+ error: null,
+};
+
+
+// 创建异步 action,用于获取用户信息
+export const getUserInfo = createAsyncThunk<
+ UserInfo,
+ void,
+ {rejectValue:string}
+>(
+ 'user/getUserInfo',
+ async (_, { rejectWithValue }) => {
+ const response = await UserAPi.getMe();
+ if (response.data.code == 0) {
+ console.log("xixi")
+ console.log(response)
+ return response.data.data;
+ } else {
+ console.log("buxixi")
+ console.log(response)
+ return rejectWithValue(response.data.message);
+ }
+ }
+);
+
+// 创建 userSlice
+const userSlice = createSlice({
+ name: 'user',
+ initialState,
+ reducers: {
+ // 可以在这里处理同步操作,如修改用户名等
+ setUser: (state, action: PayloadAction<string>) => {
+ state.username = action.payload;
+ },
+ },
+ extraReducers: (builder) => {
+ builder
+ .addCase(getUserInfo.pending, (state) => {
+ state.status = 'loading';
+ })
+ .addCase(getUserInfo.fulfilled, (state, action: PayloadAction<UserInfo>) => {
+ state.status = 'succeeded';
+ state.username = action.payload.username;
+ state.userid = action.payload.userid;
+ })
+ .addCase(getUserInfo.rejected, (state, action) => {
+ state.status = 'failed';
+ state.error = action.error.message ?? 'Unknown error';
+ });
+ },
+});
+
+// 导出 actions 和 reducer
+export const { setUser } = userSlice.actions;
+export default userSlice.reducer;