San3yuan | 4d0e803 | 2025-04-04 17:21:40 +0800 | [diff] [blame^] | 1 | import { createSlice } from '@reduxjs/toolkit'; |
| 2 | |
| 3 | interface UserState { |
| 4 | userId: number; |
| 5 | userName: string; |
| 6 | role: string; |
| 7 | isLogin: boolean; |
| 8 | uploadTraffic: number; |
| 9 | downloadTraffic: number; |
| 10 | downloadPoints: number; |
| 11 | avatar: string; |
| 12 | } |
| 13 | |
| 14 | const initialState: UserState = { |
| 15 | userId: 0, |
| 16 | userName: '', |
| 17 | role: '', |
| 18 | isLogin: false, |
| 19 | uploadTraffic: 0, |
| 20 | downloadTraffic: 0, |
| 21 | downloadPoints: 0, |
| 22 | avatar: '', |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | export const userSlice = createSlice({ |
| 27 | name: 'user', |
| 28 | initialState, |
| 29 | reducers: { |
| 30 | login: (state, action) => { |
| 31 | |
| 32 | state.userId = action.payload.userId; |
| 33 | state.userName = action.payload.userName; |
| 34 | state.role = action.payload.role; |
| 35 | state.isLogin = true; |
| 36 | state.uploadTraffic = action.payload.uploadTraffic; |
| 37 | state.downloadTraffic = action.payload.downloadTraffic; |
| 38 | state.downloadPoints = action.payload.downloadPoints; |
| 39 | state.avatar = action.payload.avatar; |
| 40 | |
| 41 | console.log('userId', state.userId); |
| 42 | }, |
| 43 | logout: (state) => { |
| 44 | state.userId = 0; |
| 45 | state.userName = ''; |
| 46 | state.role = ''; |
| 47 | state.isLogin = false; |
| 48 | state.uploadTraffic = 0; |
| 49 | state.downloadTraffic = 0; |
| 50 | state.downloadPoints = 0; |
| 51 | state.avatar = ''; |
| 52 | }, |
| 53 | updateTraffic: (state, action) => { |
| 54 | state.uploadTraffic = action.payload.uploadTraffic; |
| 55 | state.downloadTraffic = action.payload.downloadTraffic; |
| 56 | state.downloadPoints = action.payload.downloadPoints; |
| 57 | }, |
| 58 | updateAvatar: (state, action) => { |
| 59 | state.avatar = action.payload.avatar; |
| 60 | } |
| 61 | }, |
| 62 | |
| 63 | }); |
| 64 | |
| 65 | export const { login, logout, updateTraffic } = userSlice.actions; |
| 66 | export default userSlice.reducer; |