hookehuyr

fix(auth): 修复用户认证流程并优化签到列表获取逻辑

重构认证流程以正确处理用户信息获取和存储
将签到列表获取改为响应式监听用户状态变化
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-09-03 12:55:25 4 + * @LastEditTime: 2025-12-18 11:17:16
5 * @FilePath: /mlaj/src/contexts/auth.js 5 * @FilePath: /mlaj/src/contexts/auth.js
6 * @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能 6 * @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能
7 */ 7 */
...@@ -78,26 +78,33 @@ export function provideAuth() { ...@@ -78,26 +78,33 @@ export function provideAuth() {
78 } else { 78 } else {
79 const { code, data } = await getAuthInfoAPI(); 79 const { code, data } = await getAuthInfoAPI();
80 if(code) { 80 if(code) {
81 - // 查询用户是否授权, 从服务器获取用户信息并更新本地存储 81 + // 如果接口返回了 user_info,先保存
82 - if (data.openid_has) { 82 + if (data.user_info) {
83 - currentUser.value = { ...data.user, ...data.checkin } 83 + localStorage.setItem('user_info', JSON.stringify(data.user_info))
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 - }
90 } 84 }
91 - // 判断用户是否已经登录 85 +
92 - if (data?.user?.id) { 86 + // 查询用户是否授权或已登录
93 - // 已登录,更新用户状态 87 + if (data.openid_has || data?.user?.id) {
94 - currentUser.value = { ...data.user, ...data.checkin }
95 - localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
96 // 重新设置认证头 88 // 重新设置认证头
97 const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}') 89 const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
98 if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) { 90 if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
99 setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN) 91 setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
100 } 92 }
93 +
94 + // 获取完整用户信息(包含打卡统计数据)
95 + try {
96 + const userRes = await getUserInfoAPI();
97 + if (userRes.code) {
98 + currentUser.value = { ...userRes.data.user, ...userRes.data.checkin }
99 + } else {
100 + currentUser.value = { ...data.user, ...data.checkin }
101 + }
102 + } catch (e) {
103 + console.error('获取完整用户信息失败,使用基础信息:', e)
104 + currentUser.value = { ...data.user, ...data.checkin }
105 + }
106 +
107 + localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
101 } 108 }
102 } 109 }
103 } 110 }
......
1 <!-- 1 <!--
2 * @Date: 2025-03-20 19:55:21 2 * @Date: 2025-03-20 19:55:21
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-12-18 11:04:24 4 + * @LastEditTime: 2025-12-18 11:22:03
5 * @FilePath: /mlaj/src/views/HomePage.vue 5 * @FilePath: /mlaj/src/views/HomePage.vue
6 * @Description: 美乐爱觉教育首页组件 6 * @Description: 美乐爱觉教育首页组件
7 * 7 *
...@@ -483,7 +483,7 @@ ...@@ -483,7 +483,7 @@
483 483
484 <script setup lang="jsx"> 484 <script setup lang="jsx">
485 // 导入所需的Vue核心功能和组件 485 // 导入所需的Vue核心功能和组件
486 -import { ref, onMounted, onUnmounted, defineComponent, h } from 'vue' 486 +import { ref, onMounted, onUnmounted, defineComponent, h, watch } from 'vue'
487 import { useRoute, useRouter } from 'vue-router' 487 import { useRoute, useRouter } from 'vue-router'
488 488
489 // 导入布局和UI组件 489 // 导入布局和UI组件
...@@ -584,21 +584,24 @@ onMounted(async () => { ...@@ -584,21 +584,24 @@ onMounted(async () => {
584 } 584 }
585 585
586 // 获取签到列表 586 // 获取签到列表
587 - if(currentUser.value) { 587 + watch(() => currentUser.value, async (newVal) => {
588 - const task = await getTaskListAPI() 588 + if (newVal) {
589 - if (task.code) { 589 + const task = await getTaskListAPI()
590 - task.data.forEach(item => { 590 + if (task.code) {
591 - checkInTypes.value.push({ 591 + checkInTypes.value = []
592 - id: item.id, 592 + task.data.forEach(item => {
593 - name: item.title, 593 + checkInTypes.value.push({
594 - task_type: item.task_type, 594 + id: item.id,
595 - is_gray: item.is_gray, 595 + name: item.title,
596 - is_finish: item.is_finish, 596 + task_type: item.task_type,
597 - checkin_subtask_id: item.checkin_subtask_id 597 + is_gray: item.is_gray,
598 - }) 598 + is_finish: item.is_finish,
599 - }); 599 + checkin_subtask_id: item.checkin_subtask_id
600 + })
601 + });
602 + }
600 } 603 }
601 - } 604 + }, { immediate: true })
602 605
603 // 获取最新活动(外部接口) 606 // 获取最新活动(外部接口)
604 await fetchExternalActivities() 607 await fetchExternalActivities()
......