blob: 6a72540687b9e8cff0b9de307e86d6bc052f5ae7 [file] [log] [blame]
Xing Jinwenff16b1e2025-06-05 00:29:26 +08001export { authApi } from './auth'
2export { userApi } from './user'
3
4// 默认导出request实例,方便其他地方使用
5export { default as request } from './request'
6
7// ===========================================
8
9// src/store/modules/auth.js - 认证状态管理
10import { authApi } from '@/api/auth'
11import { ElMessage } from 'element-plus'
12
13const state = {
14 // 登录状态
15 isLoggedIn: false,
16 // 用户信息
17 userInfo: null,
18 // Token信息
19 token: null,
20 // 登录加载状态
21 loginLoading: false
22}
23
24const getters = {
25 // 是否已登录
26 isAuthenticated: state => state.isLoggedIn,
27 // 获取用户信息
28 userInfo: state => state.userInfo,
29 // 获取用户名
30 username: state => state.userInfo?.user?.username || '',
31 // 获取用户头像
32 avatar: state => state.userInfo?.user?.avatar || '',
33 // 获取用户组信息
34 userGroup: state => state.userInfo?.user?.group || null,
35 // 登录加载状态
36 loginLoading: state => state.loginLoading
37}
38
39const mutations = {
40 // 设置登录状态
41 SET_LOGIN_STATUS(state, status) {
42 state.isLoggedIn = status
43 },
44
45 // 设置用户信息
46 SET_USER_INFO(state, userInfo) {
47 state.userInfo = userInfo
48 },
49
50 // 设置Token
51 SET_TOKEN(state, token) {
52 state.token = token
53 },
54
55 // 设置登录加载状态
56 SET_LOGIN_LOADING(state, loading) {
57 state.loginLoading = loading
58 },
59
60 // 清除用户数据
61 CLEAR_USER_DATA(state) {
62 state.isLoggedIn = false
63 state.userInfo = null
64 state.token = null
65 state.loginLoading = false
66 }
67}
68
69const actions = {
70 // 用户登录
71 async login({ commit }, loginData) {
72 try {
73 commit('SET_LOGIN_LOADING', true)
74
75 const response = await authApi.login(loginData)
76
77 if (response) {
78 // 保存Token
79 const tokenInfo = response.token
80 if (tokenInfo && tokenInfo.tokenValue) {
81 localStorage.setItem('token', tokenInfo.tokenValue)
82 localStorage.setItem('tokenInfo', JSON.stringify(tokenInfo))
83 commit('SET_TOKEN', tokenInfo)
84 }
85
86 // 保存用户信息
87 localStorage.setItem('userInfo', JSON.stringify(response))
88 localStorage.setItem('isLoggedIn', 'true')
89
90 commit('SET_USER_INFO', response)
91 commit('SET_LOGIN_STATUS', true)
92
93 ElMessage.success('登录成功!')
94 return response
95 }
96 } catch (error) {
97 console.error('登录失败:', error)
98 throw error
99 } finally {
100 commit('SET_LOGIN_LOADING', false)
101 }
102 },
103
104 // 用户注册
105 async register({ commit }, registerData) {
106 try {
107 commit('SET_LOGIN_LOADING', true)
108
109 const response = await authApi.register(registerData)
110
111 if (response) {
112 // 注册成功后自动登录
113 const tokenInfo = response.token
114 if (tokenInfo && tokenInfo.tokenValue) {
115 localStorage.setItem('token', tokenInfo.tokenValue)
116 localStorage.setItem('tokenInfo', JSON.stringify(tokenInfo))
117 commit('SET_TOKEN', tokenInfo)
118 }
119
120 localStorage.setItem('userInfo', JSON.stringify(response))
121 localStorage.setItem('isLoggedIn', 'true')
122
123 commit('SET_USER_INFO', response)
124 commit('SET_LOGIN_STATUS', true)
125
126 ElMessage.success('注册成功!')
127 return response
128 }
129 } catch (error) {
130 console.error('注册失败:', error)
131 throw error
132 } finally {
133 commit('SET_LOGIN_LOADING', false)
134 }
135 },
136
137 // 用户登出
138 async logout({ commit }) {
139 try {
140 await authApi.logout()
141 } catch (error) {
142 console.error('登出请求失败:', error)
143 // 即使登出请求失败,也要清除本地数据
144 } finally {
145 // 清除本地存储
146 localStorage.removeItem('token')
147 localStorage.removeItem('tokenInfo')
148 localStorage.removeItem('userInfo')
149 localStorage.removeItem('isLoggedIn')
150
151 // 清除状态
152 commit('CLEAR_USER_DATA')
153
154 ElMessage.success('已退出登录')
155 }
156 },
157
158 // 检查登录状态
159 async checkLoginStatus({ commit }) {
160 try {
161 const response = await authApi.getStatus()
162
163 if (response && response.isLoggedIn && response.user) {
164 // 更新用户信息
165 localStorage.setItem('userInfo', JSON.stringify(response.user))
166 localStorage.setItem('isLoggedIn', 'true')
167
168 commit('SET_USER_INFO', response.user)
169 commit('SET_LOGIN_STATUS', true)
170
171 return true
172 } else {
173 // 登录状态无效,清除本地数据
174 commit('CLEAR_USER_DATA')
175 localStorage.removeItem('token')
176 localStorage.removeItem('tokenInfo')
177 localStorage.removeItem('userInfo')
178 localStorage.removeItem('isLoggedIn')
179
180 return false
181 }
182 } catch (error) {
183 console.error('检查登录状态失败:', error)
184 commit('CLEAR_USER_DATA')
185 return false
186 }
187 },
188
189 // 从本地存储恢复登录状态
190 restoreLoginState({ commit }) {
191 const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
192 const userInfo = localStorage.getItem('userInfo')
193 const tokenInfo = localStorage.getItem('tokenInfo')
194
195 if (isLoggedIn && userInfo) {
196 try {
197 const parsedUserInfo = JSON.parse(userInfo)
198 const parsedTokenInfo = tokenInfo ? JSON.parse(tokenInfo) : null
199
200 commit('SET_USER_INFO', parsedUserInfo)
201 commit('SET_TOKEN', parsedTokenInfo)
202 commit('SET_LOGIN_STATUS', true)
203
204 console.log('✅ 已从本地存储恢复登录状态')
205 } catch (error) {
206 console.error('❌ 恢复登录状态失败:', error)
207 commit('CLEAR_USER_DATA')
208 }
209 }
210 }
211}
212
213export default {
214 namespaced: true,
215 state,
216 getters,
217 mutations,
218 actions
219}