首页只调用必须的接口

This commit is contained in:
张成
2025-12-08 13:32:12 +08:00
parent 175e5814e3
commit a8dca0dd71
10 changed files with 245 additions and 92 deletions

View File

@@ -13,6 +13,7 @@ import { useDictionaryStore } from "@/store/dictionaryStore";
import { saveImage, navigateTo } from "@/utils";
export interface ListPageContentProps {
isActive?: boolean; // 是否处于激活状态(当前显示的页面)
onNavStateChange?: (state: {
isShowInputCustomerNavBar?: boolean;
isDistanceFilterVisible?: boolean;
@@ -26,6 +27,7 @@ export interface ListPageContentProps {
}
const ListPageContent: React.FC<ListPageContentProps> = ({
isActive = true,
onNavStateChange,
onScrollToTop: _onScrollToTop,
scrollToTopTrigger,
@@ -53,6 +55,7 @@ const ListPageContent: React.FC<ListPageContentProps> = ({
initialFilterSearch,
loadMoreMatches,
fetchGetGamesCount,
refreshBothLists,
updateDistanceQuickFilter,
getCities,
getCityQrCode,
@@ -86,6 +89,9 @@ const ListPageContent: React.FC<ListPageContentProps> = ({
const [showSearchBar, setShowSearchBar] = useState(true);
const [scrollTop, setScrollTop] = useState(0);
const [refreshing, setRefreshing] = useState(false);
// 记录上一次加载数据时的城市,用于检测城市变化
const lastLoadedAreaRef = useRef<[string, string] | null>(null);
const prevIsActiveRef = useRef(isActive);
// 处理距离筛选显示/隐藏
const handleDistanceFilterVisibleChange = useCallback(
@@ -200,11 +206,80 @@ const ListPageContent: React.FC<ListPageContentProps> = ({
getCityQrCode();
getDistricts(); // 新增:获取行政区列表
getLocation().catch((error) => {
console.error('获取位置信息失败:', error);
});
// 只有当页面激活时才加载位置和列表数据
if (isActive) {
getLocation().catch((error) => {
console.error('获取位置信息失败:', error);
});
}
}, [isActive]);
}, []);
// 当页面从非激活状态切换为激活状态时,检查城市是否变化,如果变化则重新加载数据
useEffect(() => {
// 如果从非激活状态变为激活状态(切回列表页)
if (isActive && !prevIsActiveRef.current) {
const currentArea = area;
const lastArea = lastLoadedAreaRef.current;
// 检查城市是否发生变化(比较省份)
const currentProvince = currentArea?.[1] || "";
const lastProvince = lastArea?.[1] || "";
// 如果城市发生变化,或者地址存在但不一致,需要重新加载数据
// 注意:即使 lastArea 为空,只要 currentArea 存在,也应该加载数据
if (currentProvince && (currentProvince !== lastProvince || !lastArea)) {
console.log("切回列表页,检测到地址变化或不一致,重新加载数据:", {
lastArea,
currentArea,
lastProvince,
currentProvince,
});
// 地址发生变化或不一致,重新加载数据和球局数量
const promises: Promise<any>[] = [];
if (refreshBothLists) {
promises.push(refreshBothLists(currentArea));
}
if (fetchGetGamesCount) {
promises.push(fetchGetGamesCount(currentArea));
}
Promise.all(promises).then(() => {
// 数据加载完成后,更新记录的城市(记录为上一次在列表页加载数据时的城市)
if (currentArea) {
lastLoadedAreaRef.current = [...currentArea] as [string, string];
}
}).catch((error) => {
console.error("重新加载数据失败:", error);
});
}
}
// 如果是首次加载且列表页激活,记录当前城市(用于后续比较)
if (isActive && !lastLoadedAreaRef.current && area) {
lastLoadedAreaRef.current = [...area] as [string, string];
}
// 更新上一次的激活状态
prevIsActiveRef.current = isActive;
}, [isActive, area, refreshBothLists, fetchGetGamesCount]);
// 监听城市变化(在列表页激活状态下),当城市切换后立即更新记录
// 注意:这个 useEffect 用于处理在列表页激活状态下切换城市的情况
// 当用户在列表页切换城市时HomeNavbar 的 handleCityChange 已经会调用 refreshBothLists
// 这里只需要同步更新 lastLoadedAreaRef确保后续检测逻辑正确
useEffect(() => {
// 如果页面激活且城市发生变化(用户在列表页切换了城市)
if (isActive && area) {
const currentProvince = area[1] || "";
const lastProvince = lastLoadedAreaRef.current?.[1] || "";
// 如果城市发生变化,立即更新记录(因为 refreshBothLists 已经在 HomeNavbar 中调用)
if (currentProvince && currentProvince !== lastProvince) {
// 立即更新记录,确保地址显示和使用的地址一致
lastLoadedAreaRef.current = [...area] as [string, string];
}
}
}, [isActive, area]);
useEffect(() => {
if (pageOption?.page === 1 && matches?.length > 0) {
@@ -237,8 +312,13 @@ const ListPageContent: React.FC<ListPageContentProps> = ({
console.error("更新用户位置失败:", error);
}
}
fetchGetGamesCount();
getMatchesData();
// 传入当前的 area确保接口请求的地址与界面显示一致
await fetchGetGamesCount(area);
await getMatchesData();
// 初始数据加载完成后,记录当前城市
if (area && isActive) {
lastLoadedAreaRef.current = [...area] as [string, string];
}
return location;
};

View File

@@ -1,4 +1,5 @@
import { useState, useEffect } from "react";
import React from "react";
import { View, Text, Image, ScrollView } from "@tarojs/components";
import { EmptyState } from "@/components";
import SubscribeNotificationTip from "@/components/SubscribeNotificationTip";
@@ -25,7 +26,11 @@ interface MessageItem {
type MessageCategory = "comment" | "follow";
const MessagePageContent = () => {
interface MessagePageContentProps {
isActive?: boolean;
}
const MessagePageContent: React.FC<MessagePageContentProps> = ({ isActive = true }) => {
const { statusNavbarHeightInfo } = useGlobalState() || {};
const { totalHeight = 98 } = statusNavbarHeightInfo || {};
@@ -34,6 +39,7 @@ const MessagePageContent = () => {
const [loading, setLoading] = useState(false);
const [reachedBottom, setReachedBottom] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const [hasLoaded, setHasLoaded] = useState(false); // 记录是否已经加载过数据
// 从 store 获取红点信息
const reddotInfo = useReddotInfo();
@@ -58,10 +64,14 @@ const MessagePageContent = () => {
}
};
// 只有当页面激活且未加载过数据时才加载接口
useEffect(() => {
getNoticeList();
fetchReddotInfo();
}, []);
if (isActive && !hasLoaded) {
getNoticeList();
fetchReddotInfo();
setHasLoaded(true);
}
}, [isActive, hasLoaded]);
const filteredMessages = messageList;

View File

@@ -8,14 +8,19 @@ import ListContainer from "@/container/listContainer";
import { TennisMatch } from "@/../types/list/types";
import { NTRPTestEntryCard } from "@/components";
import { EvaluateScene } from "@/store/evaluateStore";
import { useUserInfo } from "@/store/userStore";
import { useUserInfo, useUserActions } from "@/store/userStore";
import { usePickerOption } from "@/store/pickerOptionsStore";
import { useGlobalState } from "@/store/global";
const MyselfPageContent: React.FC = () => {
interface MyselfPageContentProps {
isActive?: boolean;
}
const MyselfPageContent: React.FC<MyselfPageContentProps> = ({ isActive = true }) => {
const pickerOption = usePickerOption();
const { statusNavbarHeightInfo } = useGlobalState() || {};
const { totalHeight = 98 } = statusNavbarHeightInfo || {};
const { fetchUserInfo } = useUserActions();
const instance = (Taro as any).getCurrentInstance();
const user_id = instance.router?.params?.userid || "";
@@ -29,6 +34,7 @@ const MyselfPageContent: React.FC = () => {
const [active_tab, setActiveTab] = useState<"hosted" | "participated">(
"hosted"
);
const [hasLoaded, setHasLoaded] = useState(false); // 记录是否已经加载过数据
const [collapseProfile, setCollapseProfile] = useState(false);
@@ -37,6 +43,16 @@ const MyselfPageContent: React.FC = () => {
pickerOption.getProfessions();
}, []);
// 当页面激活时,确保用户信息已加载
useEffect(() => {
if (isActive) {
// 如果用户信息不存在或没有 id则加载用户信息
if (!user_info || !("id" in user_info) || !user_info.id) {
fetchUserInfo();
}
}
}, [isActive, user_info, fetchUserInfo]);
const { useDidShow } = Taro as any;
useDidShow(() => {
// 确保从编辑页面返回时刷新数据
@@ -92,11 +108,20 @@ const MyselfPageContent: React.FC = () => {
}
}, [active_tab, user_info, classifyGameRecords]);
// 只有当页面激活且未加载过数据时才加载接口
useEffect(() => {
if (!loading) {
if (isActive && !hasLoaded && !loading && user_info && "id" in user_info) {
load_game_data();
setHasLoaded(true);
}
}, [isActive, hasLoaded, loading, load_game_data, user_info]);
// 当 active_tab 切换时,如果页面已激活,重新加载数据
useEffect(() => {
if (isActive && hasLoaded && !loading && user_info && "id" in user_info) {
load_game_data();
}
}, [loading, load_game_data]);
}, [active_tab, isActive, hasLoaded, loading, load_game_data, user_info]);
const handle_follow = async () => {
try {

View File

@@ -230,6 +230,7 @@ const MainPage: React.FC = () => {
className={`tab-content ${currentTab === "list" ? "active" : ""}`}
>
<ListPageContent
isActive={currentTab === "list"}
onNavStateChange={handleListNavStateChange}
onScrollToTop={scrollToTop}
scrollToTopTrigger={listPageScrollToTopTrigger}
@@ -243,14 +244,14 @@ const MainPage: React.FC = () => {
<View
className={`tab-content ${currentTab === "message" ? "active" : ""}`}
>
<MessagePageContent />
<MessagePageContent isActive={currentTab === "message"} />
</View>
{/* 我的页内容 */}
<View
className={`tab-content ${currentTab === "personal" ? "active" : ""}`}
>
<MyselfPageContent />
<MyselfPageContent isActive={currentTab === "personal"} />
</View>
{/* 底部导航栏 */}