hookehuyr

fix(auth): 修复登录状态更新和认证头设置问题

在登录和用户状态更新时,确保正确设置认证头信息
修复注销时未清理user_info的问题
添加错误处理确保注销时清理本地状态
1 /* 1 /*
2 * @Date: 2025-03-20 21:11:31 2 * @Date: 2025-03-20 21:11:31
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-07-07 11:27:17 4 + * @LastEditTime: 2025-09-03 12:55:25
5 * @FilePath: /mlaj/src/contexts/auth.js 5 * @FilePath: /mlaj/src/contexts/auth.js
6 * @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能 6 * @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能
7 */ 7 */
...@@ -67,6 +67,11 @@ export function provideAuth() { ...@@ -67,6 +67,11 @@ export function provideAuth() {
67 if (code) { 67 if (code) {
68 currentUser.value = { ...data.user, ...data.checkin } 68 currentUser.value = { ...data.user, ...data.checkin }
69 localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) 69 localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
70 + // 重新设置认证头
71 + const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
72 + if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
73 + setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
74 + }
70 } else { 75 } else {
71 logout() 76 logout()
72 } 77 }
...@@ -77,12 +82,22 @@ export function provideAuth() { ...@@ -77,12 +82,22 @@ export function provideAuth() {
77 if (data.openid_has) { 82 if (data.openid_has) {
78 currentUser.value = { ...data.user, ...data.checkin } 83 currentUser.value = { ...data.user, ...data.checkin }
79 localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) 84 localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
85 + // 重新设置认证头
86 + const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
87 + if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
88 + setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
89 + }
80 } 90 }
81 // 判断用户是否已经登录 91 // 判断用户是否已经登录
82 if (data?.user?.id) { 92 if (data?.user?.id) {
83 // 已登录,更新用户状态 93 // 已登录,更新用户状态
84 currentUser.value = { ...data.user, ...data.checkin } 94 currentUser.value = { ...data.user, ...data.checkin }
85 localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) 95 localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
96 + // 重新设置认证头
97 + const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
98 + if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
99 + setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
100 + }
86 } 101 }
87 } 102 }
88 } 103 }
...@@ -122,12 +137,18 @@ export function provideAuth() { ...@@ -122,12 +137,18 @@ export function provideAuth() {
122 currentUser.value = null 137 currentUser.value = null
123 // 清理本地存储的用户信息和登录时间戳 138 // 清理本地存储的用户信息和登录时间戳
124 localStorage.removeItem('currentUser') 139 localStorage.removeItem('currentUser')
140 + localStorage.removeItem('user_info')
125 // localStorage.removeItem('loginTimestamp') 141 // localStorage.removeItem('loginTimestamp')
126 // 清除认证请求头 142 // 清除认证请求头
127 clearAuthHeaders() 143 clearAuthHeaders()
128 } 144 }
129 } catch (error) { 145 } catch (error) {
130 console.error('注销失败:', error) 146 console.error('注销失败:', error)
147 + // 即使API调用失败,也要清理本地状态
148 + currentUser.value = null
149 + localStorage.removeItem('currentUser')
150 + localStorage.removeItem('user_info')
151 + clearAuthHeaders()
131 } 152 }
132 } 153 }
133 154
......
...@@ -292,8 +292,8 @@ const handleSubmit = async () => { ...@@ -292,8 +292,8 @@ const handleSubmit = async () => {
292 292
293 const { code, data } = await getUserInfoAPI(); 293 const { code, data } = await getUserInfoAPI();
294 if (code) { 294 if (code) {
295 - // 登录成功,更新auth状态 295 + // 登录成功,更新auth状态,传递完整的用户信息包括打卡信息
296 - const success = login(data.user); 296 + const success = login({ ...data.user, ...data.checkin });
297 297
298 if (success) { 298 if (success) {
299 // 如果有重定向参数,登录成功后跳转到对应页面 299 // 如果有重定向参数,登录成功后跳转到对应页面
......
1 <!-- 1 <!--
2 * @Date: 2025-05-29 15:34:17 2 * @Date: 2025-05-29 15:34:17
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-08-26 15:16:55 4 + * @LastEditTime: 2025-08-26 15:23:38
5 * @FilePath: /mlaj/src/views/checkin/IndexCheckInPage.vue 5 * @FilePath: /mlaj/src/views/checkin/IndexCheckInPage.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -856,7 +856,6 @@ const formatData = (data) => { ...@@ -856,7 +856,6 @@ const formatData = (data) => {
856 /* 禁用未来日期的样式 */ 856 /* 禁用未来日期的样式 */
857 :deep(.calendar-disabled) { 857 :deep(.calendar-disabled) {
858 color: #c8c9cc !important; 858 color: #c8c9cc !important;
859 - background-color: #f7f8fa !important;
860 cursor: not-allowed !important; 859 cursor: not-allowed !important;
861 } 860 }
862 861
......