为多个组件和API添加用户登录状态检查,确保只有在用户登录后才能查询和修改收藏状态

This commit is contained in:
yuchenglong
2026-01-16 18:02:08 +08:00
parent b5fd763c7e
commit 887d205f65
8 changed files with 107 additions and 50 deletions

View File

@@ -15,6 +15,7 @@ import {
fetchSports,
fetchTodayMatches,
} from "@/lib/api";
import { storage } from "@/lib/storage";
import { League, Match, Sport } from "@/types/api";
import React, { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
@@ -316,19 +317,24 @@ export default function HomeScreen() {
deviceTimeZone
);
// 直接传递 match.id 查询是否收藏,并更新列表状态
const listWithFavStatus = await Promise.all(
list.map(async (m) => {
try {
// 查询比赛是否已被收藏
const favRes = await checkFavorite("match", m.id);
return { ...m, fav: favRes.isFavorite };
} catch (error) {
console.error(`Check favorite failed for match ${m.id}:`, error);
return m;
}
})
);
const token = await storage.getAccessToken();
let listWithFavStatus = list;
if (token) {
// 直接传递 match.id 查询是否收藏,并更新列表状态
listWithFavStatus = await Promise.all(
list.map(async (m) => {
try {
// 查询比赛是否已被收藏
const favRes = await checkFavorite("match", m.id);
return { ...m, fav: favRes.isFavorite };
} catch (error) {
console.error(`Check favorite failed for match ${m.id}:`, error);
return m;
}
})
);
}
// 将收藏的比赛置顶
const sortedList = [...listWithFavStatus].sort((a, b) => {