diff --git a/src/components/HomeNavbar/index.tsx b/src/components/HomeNavbar/index.tsx index bde384a..920a90f 100644 --- a/src/components/HomeNavbar/index.tsx +++ b/src/components/HomeNavbar/index.tsx @@ -15,6 +15,8 @@ import LocationConfirmDialog from "@/components/LocationConfirmDialog"; const CITY_CACHE_KEY = "USER_SELECTED_CITY"; // 定位弹窗关闭时间缓存 key(用户选择"继续浏览"时记录) const LOCATION_DIALOG_DISMISS_TIME_KEY = "LOCATION_DIALOG_DISMISS_TIME"; +// 城市切换时间缓存 key(用户手动切换城市时记录) +const CITY_CHANGE_TIME_KEY = "CITY_CHANGE_TIME"; // 2小时的毫秒数 const TWO_HOURS_MS = 2 * 60 * 60 * 1000; @@ -134,9 +136,26 @@ const HomeNavbar = (props: IProps) => { } }, [detectedLocation]); - // 检查是否在2小时内已选择"继续浏览" + // 检查是否在2小时内已选择"继续浏览"或切换过城市 const should_show_location_dialog = (): boolean => { try { + // 检查是否在2小时内切换过城市 + const city_change_time = (Taro as any).getStorageSync(CITY_CHANGE_TIME_KEY); + if (city_change_time) { + const current_time = Date.now(); + const time_diff = current_time - city_change_time; + + // 如果距离上次切换城市还在2小时内,不显示弹窗 + if (time_diff < TWO_HOURS_MS) { + console.log(`距离上次切换城市还不到2小时,剩余时间: ${Math.ceil((TWO_HOURS_MS - time_diff) / 1000 / 60)}分钟,不显示定位弹窗`); + return false; + } else { + // 超过2小时,清除过期记录 + (Taro as any).removeStorageSync(CITY_CHANGE_TIME_KEY); + } + } + + // 检查是否在2小时内已选择"继续浏览" const dismiss_time = (Taro as any).getStorageSync(LOCATION_DIALOG_DISMISS_TIME_KEY); if (!dismiss_time) { return true; // 没有记录,可以显示 @@ -304,12 +323,23 @@ const HomeNavbar = (props: IProps) => { // 处理城市切换(用户手动选择) const handleCityChange = async (_newArea: any) => { - // 用户手动选择的城市不保存到缓存(临时切换) - console.log("用户手动选择城市(不保存缓存):", _newArea); + // 用户手动选择的城市保存到缓存 + console.log("用户手动选择城市,更新缓存:", _newArea); // 先更新 area 状态(用于界面显示和接口参数) updateArea(_newArea); + // 保存城市到缓存 + try { + (Taro as any).setStorageSync(CITY_CACHE_KEY, _newArea); + // 记录切换时间,2小时内不再弹出定位弹窗 + const current_time = Date.now(); + (Taro as any).setStorageSync(CITY_CHANGE_TIME_KEY, current_time); + console.log("已保存城市到缓存并记录切换时间:", _newArea, current_time); + } catch (error) { + console.error("保存城市缓存失败:", error); + } + // 先调用列表接口(会使用更新后的 state.area) if (refreshBothLists) { await refreshBothLists();