为多个组件和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

@@ -1,6 +1,7 @@
import { ThemedText } from "@/components/themed-text";
import { IconSymbol } from "@/components/ui/icon-symbol";
import { addFavorite, checkFavorite, removeFavorite } from "@/lib/api";
import { storage } from "@/lib/storage";
import { LiveScoreMatch } from "@/types/api";
import { LinearGradient } from "expo-linear-gradient";
import { useRouter } from "expo-router";
@@ -46,6 +47,8 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) {
// 检查收藏状态
React.useEffect(() => {
const loadFavStatus = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("match", match.event_key.toString());
setIsFav(res.isFavorite);
@@ -59,6 +62,8 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) {
// 检查主队收藏状态
React.useEffect(() => {
const loadHomeFav = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("team", match.home_team_key.toString());
setIsHomeFav(res.isFavorite);
@@ -74,6 +79,8 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) {
// 检查客队收藏状态
React.useEffect(() => {
const loadAwayFav = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("team", match.away_team_key.toString());
setIsAwayFav(res.isFavorite);

View File

@@ -1,6 +1,7 @@
import { ThemedText } from "@/components/themed-text";
import { IconSymbol } from "@/components/ui/icon-symbol";
import { addFavorite, checkFavorite, removeFavorite } from "@/lib/api";
import { storage } from "@/lib/storage";
import { MatchDetailData } from "@/types/api";
import { LinearGradient } from "expo-linear-gradient";
import { useRouter } from "expo-router";
@@ -30,6 +31,8 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) {
// 检查比赛收藏状态
React.useEffect(() => {
const loadFavStatus = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("match", match.eventKey.toString());
setIsFav(res.isFavorite);
@@ -45,6 +48,8 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) {
// 检查主队收藏状态
React.useEffect(() => {
const loadHomeFav = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("team", match.homeTeamKey.toString());
setIsHomeFav(res.isFavorite);
@@ -60,6 +65,8 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) {
// 检查客队收藏状态
React.useEffect(() => {
const loadAwayFav = async () => {
const token = await storage.getAccessToken();
if (!token) return;
try {
const res = await checkFavorite("team", match.awayTeamKey.toString());
setIsAwayFav(res.isFavorite);