hookehuyr

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

重构认证流程以正确处理用户信息获取和存储
将签到列表获取改为响应式监听用户状态变化
/*
* @Date: 2025-03-20 21:11:31
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-03 12:55:25
* @LastEditTime: 2025-12-18 11:17:16
* @FilePath: /mlaj/src/contexts/auth.js
* @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能
*/
......@@ -78,26 +78,33 @@ export function provideAuth() {
} else {
const { code, data } = await getAuthInfoAPI();
if(code) {
// 查询用户是否授权, 从服务器获取用户信息并更新本地存储
if (data.openid_has) {
currentUser.value = { ...data.user, ...data.checkin }
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
// 如果接口返回了 user_info,先保存
if (data.user_info) {
localStorage.setItem('user_info', JSON.stringify(data.user_info))
}
// 查询用户是否授权或已登录
if (data.openid_has || data?.user?.id) {
// 重新设置认证头
const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
}
// 获取完整用户信息(包含打卡统计数据)
try {
const userRes = await getUserInfoAPI();
if (userRes.code) {
currentUser.value = { ...userRes.data.user, ...userRes.data.checkin }
} else {
currentUser.value = { ...data.user, ...data.checkin }
}
// 判断用户是否已经登录
if (data?.user?.id) {
// 已登录,更新用户状态
} catch (e) {
console.error('获取完整用户信息失败,使用基础信息:', e)
currentUser.value = { ...data.user, ...data.checkin }
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
// 重新设置认证头
const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
}
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
}
}
}
......
<!--
* @Date: 2025-03-20 19:55:21
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-12-18 11:04:24
* @LastEditTime: 2025-12-18 11:22:03
* @FilePath: /mlaj/src/views/HomePage.vue
* @Description: 美乐爱觉教育首页组件
*
......@@ -483,7 +483,7 @@
<script setup lang="jsx">
// 导入所需的Vue核心功能和组件
import { ref, onMounted, onUnmounted, defineComponent, h } from 'vue'
import { ref, onMounted, onUnmounted, defineComponent, h, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
// 导入布局和UI组件
......@@ -584,9 +584,11 @@ onMounted(async () => {
}
// 获取签到列表
if(currentUser.value) {
watch(() => currentUser.value, async (newVal) => {
if (newVal) {
const task = await getTaskListAPI()
if (task.code) {
checkInTypes.value = []
task.data.forEach(item => {
checkInTypes.value.push({
id: item.id,
......@@ -599,6 +601,7 @@ onMounted(async () => {
});
}
}
}, { immediate: true })
// 获取最新活动(外部接口)
await fetchExternalActivities()
......