From 82b6b6f80e59c7ff8bdb07d5a4f5942556888184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Sun, 7 Dec 2025 00:04:02 +0800 Subject: [PATCH] 1 --- src/components/HomeNavbar/index.tsx | 19 +++++++++++++--- src/store/listStore.ts | 35 +++++++++++++++-------------- types/list/types.ts | 6 ++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/components/HomeNavbar/index.tsx b/src/components/HomeNavbar/index.tsx index aef82a7..6e96650 100644 --- a/src/components/HomeNavbar/index.tsx +++ b/src/components/HomeNavbar/index.tsx @@ -243,11 +243,24 @@ const HomeNavbar = (props: IProps) => { // 处理城市切换(用户手动选择) const handleCityChange = async (_newArea: any) => { - // 用户手动选择的城市不保存到缓存 + // 用户手动选择的城市不保存到缓存(临时切换) console.log("用户手动选择城市(不保存缓存):", _newArea); - // 切换城市后,同时更新两个列表接口获取数据 - await handleCityChangeWithoutCache(); + // 先更新 area 状态(用于界面显示) + updateArea(_newArea); + + // 确保状态更新完成后再调用接口 + // 切换城市后,同时更新两个列表接口获取数据,传入新的城市信息 + const promises: Promise[] = []; + if (refreshBothLists) { + promises.push(refreshBothLists(_newArea)); + } + // 更新球局数量,直接传入新的城市信息,不依赖状态更新时序 + if (fetchGetGamesCount) { + promises.push(fetchGetGamesCount(_newArea)); + } + // 并行执行,提高性能 + await Promise.all(promises); }; return ( diff --git a/src/store/listStore.ts b/src/store/listStore.ts index de191e0..afe3095 100644 --- a/src/store/listStore.ts +++ b/src/store/listStore.ts @@ -185,7 +185,7 @@ export const useListStore = create()((set, get) => ({ gamesNum: 0, // 组装搜索数据 - getSearchParams: () => { + getSearchParams: (overrideArea?: [string, string]) => { const state = get(); const currentPageState = state.isSearchResult ? state.searchPageState : state.listPageState; const filterOptions = currentPageState?.filterOptions || {}; @@ -193,9 +193,12 @@ export const useListStore = create()((set, get) => ({ const distanceQuickFilter = currentPageState?.distanceQuickFilter || {}; const { distanceFilter, order, district } = distanceQuickFilter || {}; - // 从用户信息中获取 last_location_province 和 last_location_city + // 优先使用 overrideArea(切换城市时传入),其次使用 area 状态中的省份,最后使用用户信息中的 + const areaProvince = overrideArea?.at(1) || state.area?.at(1) || ""; const userInfo = useUser.getState().user as any; - const last_location_province = userInfo?.last_location_province || ""; + const userLastLocationProvince = userInfo?.last_location_province || ""; + // 优先使用切换后的城市,如果没有切换过则使用用户信息中的 + const last_location_province = areaProvince || userLastLocationProvince; const last_location_city = userInfo?.last_location_city || ""; // city 参数逻辑: @@ -221,16 +224,13 @@ export const useListStore = create()((set, get) => ({ ntrpMax: filterOptions?.ntrp?.[1], dateRange: dateRange, // 确保始终是两个值的数组 distanceFilter: distanceFilter, - last_location_province: last_location_province, // 使用 last_location_province + province: last_location_province, // 使用 province 替代 last_location_province }; - // 只在有值时添加 city 参数(使用 last_location_city) + // 只在有值时添加 city 参数 if (city) { - searchOption.last_location_city = city; - } else if (last_location_city) { - // 如果没有选择行政区,但有 last_location_city,则使用它 - searchOption.last_location_city = last_location_city; - } + searchOption.city = city; + } const params = { pageOption: currentPageState?.pageOption, @@ -337,13 +337,13 @@ export const useListStore = create()((set, get) => ({ }, // 同时更新两个列表接口(常规列表和智能排序列表) - refreshBothLists: async () => { + refreshBothLists: async (overrideArea?: [string, string]) => { const state = get(); const { getSearchParams, setListData } = state; const { getGamesList, getGamesIntegrateList } = await import("../services/listApi"); try { - const searchParams = getSearchParams() || {}; + const searchParams = getSearchParams(overrideArea) || {}; // 调用常规列表接口 const listParams = { @@ -408,11 +408,14 @@ export const useListStore = create()((set, get) => ({ }, // 获取球局数量 - fetchGetGamesCount: async () => { - const { getSearchParams } = get(); - const params = getSearchParams() || {}; + fetchGetGamesCount: async (overrideArea?: [string, string]) => { + const state = get(); + const { getSearchParams } = state; + const params = getSearchParams(overrideArea) || {}; + console.log("fetchGetGamesCount 参数:", { overrideArea, params: JSON.stringify(params) }); const resData = (await getGamesCount(params)) || {}; const gamesNum = resData?.data?.count || 0; + console.log("fetchGetGamesCount 结果:", gamesNum); set({ gamesNum }); }, @@ -695,9 +698,7 @@ export const useListStore = create()((set, get) => ({ }, updateArea(payload: [string, string]) { - const state = get(); set({ - ...state, area: payload, }) }, diff --git a/types/list/types.ts b/types/list/types.ts index bc1b8b1..69c19de 100644 --- a/types/list/types.ts +++ b/types/list/types.ts @@ -111,11 +111,11 @@ export interface ListActions { getSearchHistory: () => Promise; clearHistory: () => void; searchSuggestion: (val: string) => Promise; - getSearchParams: () => Record; + getSearchParams: (overrideArea?: [string, string]) => Record; loadMoreMatches: () => Promise; initialFilterSearch: (isSearchData?: boolean) => void; setListData: (payload: IPayload) => void; - fetchGetGamesCount: () => Promise; + fetchGetGamesCount: (overrideArea?: [string, string]) => Promise; getCurrentPageState: () => { currentPageState: any; currentPageKey: string }; updateCurrentPageState: (payload: Record) => void; updateDistanceQuickFilter: (payload: Record) => void; @@ -123,7 +123,7 @@ export interface ListActions { getCityQrCode: () => Promise; getDistricts: () => Promise; // 新增:获取行政区 updateArea: (payload: [string, string]) => void; - refreshBothLists: () => Promise; + refreshBothLists: (overrideArea?: [string, string]) => Promise; } export interface IPayload {