import { useEffect, useState } from "react";
import { View, Text, Image } from "@tarojs/components";
import img from "@/config/images";
import { useGlobalState } from "@/store/global";
import { useUserInfo } from "@/store/userStore";
import { useListState } from "@/store/listStore";
import { Input } from "@nutui/nutui-react-taro";
import Taro from "@tarojs/taro";
import "./index.scss";
import { getCurrentFullPath } from "@/utils";
import { CityPicker as PopupPicker } from "@/components/Picker";
interface IProps {
config?: {
showInput?: boolean;
inputLeftIcon?: string;
iconPath?: string;
leftIconClick?: () => void;
title?: string; // 显示标题(用于"我的"页面等)
showTitle?: boolean; // 是否显示标题模式
};
onCityPickerVisibleChange?: (visible: boolean) => void; // 城市选择器显示/隐藏回调
onScrollToTop?: () => void; // 滚动到顶部回调
}
function CityPicker(props) {
const { visible, setVisible, cities, area, setArea, onCityChange } = props;
console.log(cities, "cities");
const [value, setValue] = useState(area);
function onChange(value: any) {
console.log(value, "value");
setValue(value);
setArea(value);
// 切换城市时触发接口调用
if (onCityChange) {
onCityChange(value);
}
}
return (
visible && (
)
);
}
/**
* 首页专用导航栏组件
* 支持三种模式:
* 1. Logo + 城市选择 + 球局数量(默认模式)
* 2. 搜索输入框模式(showInput = true)
* 3. 标题模式(showTitle = true,用于"我的"页面等)
*/
const HomeNavbar = (props: IProps) => {
const { config, onCityPickerVisibleChange, onScrollToTop } = props;
const {
showInput = false,
inputLeftIcon,
leftIconClick,
title,
showTitle = false,
} = config || {};
const { getLocationLoading, statusNavbarHeightInfo } = useGlobalState();
const {
gamesNum,
searchValue,
cities,
area,
updateArea,
fetchGetGamesCount,
refreshBothLists,
} = useListState();
const { statusBarHeight = 0, navBarHeight = 44 } =
statusNavbarHeightInfo || {};
const [cityPopupVisible, setCityPopupVisible] = useState(false);
// 监听城市选择器状态变化,通知父组件
useEffect(() => {
onCityPickerVisibleChange?.(cityPopupVisible);
}, [cityPopupVisible]);
const userInfo = useUserInfo();
const province = (userInfo as any)?.province || "";
const city = (userInfo as any)?.city || "";
// const district = (userInfo as any)?.district || "";
useEffect(() => {
updateArea(["中国", province, city]);
}, [province, city]);
// const currentAddress = city + district;
const handleInputClick = () => {
const currentPagePath = getCurrentFullPath();
if (currentPagePath === "/game_pages/searchResult/index") {
(Taro as any).navigateBack();
} else {
(Taro as any).navigateTo({
url: "/game_pages/search/index",
});
}
};
// 点击logo
const handleLogoClick = () => {
// 如果当前在列表页,点击后页面回到顶部
if (getCurrentFullPath() === "/main_pages/index") {
// 使用父组件传递的滚动方法(适配 ScrollView)
if (onScrollToTop) {
onScrollToTop();
} else {
// 降级方案:使用页面滚动(兼容旧版本)
(Taro as any).pageScrollTo({
scrollTop: 0,
duration: 300,
});
}
}
(Taro as any).redirectTo({
url: "/main_pages/index", // 列表页
});
};
const handleInputLeftIconClick = () => {
if (leftIconClick) {
leftIconClick();
} else {
handleLogoClick();
}
};
const navbarStyle = {
height: `${navBarHeight}px`,
};
function handleToggleCity() {
setCityPopupVisible(true);
}
const area_city = area.at(-1);
// 处理城市切换
const handleCityChange = async (_newArea: any) => {
// 切换城市后,同时更新两个列表接口获取数据
if (refreshBothLists) {
await refreshBothLists();
}
// 更新球局数量
if (fetchGetGamesCount) {
await fetchGetGamesCount();
}
};
return (
{/* 标题模式(用于"我的"页面等) */}
{showTitle && (
{title || "我的"}
)}
{/* 首页logo 导航*/}
{!showTitle && (
{/* logo */}
{/* 位置 */}
{area_city}
{!getLocationLoading && area_city && (
)}
附近{gamesNum}场球局
)}
{/* 搜索导航 */}
{!showTitle && (
{/* logo */}
{/* 搜索框 */}
)}
{cityPopupVisible && !showTitle && (
)}
);
};
export default HomeNavbar;