首页只调用必须的接口
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user