blob: 607a6fd5be65fedbe1eb4dac046a465f93fd3fe6 [file] [log] [blame]
2230101462240ab2025-06-07 09:28:16 +08001import { createAsyncThunk, createSlice, type PayloadAction } from "@reduxjs/toolkit";
22301014bc4616f2025-06-03 16:59:44 +08002import type { AuthState } from "../../store/types";
22301014b1477f72025-06-07 22:54:40 +08003import type { LoginRequest } from "../../api/Auth/type";
4import AuthAPI from "../../api/Auth/AuthApi";
22301014bc4616f2025-06-03 16:59:44 +08005
22301014b1477f72025-06-07 22:54:40 +08006// 获取本地存储的 token
7const storedToken = localStorage.getItem('token');
22301014bc4616f2025-06-03 16:59:44 +08008
9const initialState: AuthState = {
22301014b1477f72025-06-07 22:54:40 +080010 token: storedToken || '',
22301014bc4616f2025-06-03 16:59:44 +080011 loading: false,
12 isAuth: false,
2230101462240ab2025-06-07 09:28:16 +080013 error: ''
22301014bc4616f2025-06-03 16:59:44 +080014}
15
2230101462240ab2025-06-07 09:28:16 +080016export const loginUser = createAsyncThunk<
17 {token: string},
18 LoginRequest,
19 { rejectValue: string }
20>(
21 'auth/login',
22 async (loginRequest: LoginRequest, { rejectWithValue }) => {
23 try {
22301014b1477f72025-06-07 22:54:40 +080024 const response = await AuthAPI.login(loginRequest);
2230101462240ab2025-06-07 09:28:16 +080025 if(response.data.code == 0) {
26 return {token: response.data.data};
27 }
28 else
29 return rejectWithValue(response.data.message);
30 } catch {
31 return rejectWithValue('登录失败');
32 }
33 }
34);
35
36export const refreshToken = createAsyncThunk<
37 {token: string},
38 string,
39 { rejectValue: string }
40>(
41
42 'auth/refresh',
43 async (oldToken: string, { rejectWithValue }) => {
44 try {
22301014b1477f72025-06-07 22:54:40 +080045 const response = await AuthAPI.refreshToken(oldToken);
46 console.log(response);
2230101462240ab2025-06-07 09:28:16 +080047 if(response.data.code == 0)
48 return {token: response.data.data};
49 else
50 return rejectWithValue(response.data.message);
51 } catch {
52 return rejectWithValue('刷新失败');
53 }
54 }
55);
56
22301014bc4616f2025-06-03 16:59:44 +080057const authSlice = createSlice({
58 name: 'auth',
59 initialState,
2230101462240ab2025-06-07 09:28:16 +080060 reducers: {
61 logout: (state) => {
62 state.token = '';
63 state.isAuth = false;
64 localStorage.clear()
65 },
66 },extraReducers: (builder) => {
67 // 处理登录的异步操作
68 builder
69 .addCase(loginUser.pending, (state) => {
70 state.loading = true;
71 })
72 .addCase(loginUser.fulfilled, (state, action: PayloadAction<{token: string}>) => {
73 state.loading = false;
74 state.token = action.payload.token;
75 state.isAuth = true;
76
77 localStorage.setItem('token', state.token);
78 })
79 .addCase(loginUser.rejected, (state, action) => {
80 state.loading = false;
81 state.error = action.payload ? action.payload : '' // 错误处理
82 })
83
84 // 处理刷新 token 的异步操作
85 .addCase(refreshToken.pending, (state) => {
86 state.loading = true;
87 })
88 .addCase(refreshToken.fulfilled, (state, action) => {
89 state.loading = false;
90 state.token = action.payload.token;
91 localStorage.setItem('token', state.token);
92 })
93 .addCase(refreshToken.rejected, (state, action) => {
94 state.loading = false;
95 state.error = action.payload ? action.payload : ''
96 });
97 },
98
22301014bc4616f2025-06-03 16:59:44 +080099});
22301014b1477f72025-06-07 22:54:40 +0800100
101export const { logout } = authSlice.actions;
22301014bc4616f2025-06-03 16:59:44 +0800102export default authSlice.reducer;