添加程序选择
This commit is contained in:
@@ -9,6 +9,7 @@ import Taro from "@tarojs/taro";
|
||||
import "./index.scss";
|
||||
import { getCurrentFullPath } from "@/utils";
|
||||
import { CityPickerV2 as PopupPicker } from "@/components/Picker";
|
||||
import LocationConfirmDialog from "@/components/LocationConfirmDialog";
|
||||
|
||||
// 城市缓存 key
|
||||
const CITY_CACHE_KEY = "USER_SELECTED_CITY";
|
||||
@@ -70,7 +71,7 @@ const HomeNavbar = (props: IProps) => {
|
||||
title,
|
||||
showTitle = false,
|
||||
} = config || {};
|
||||
const { getLocationLoading, statusNavbarHeightInfo } = useGlobalState();
|
||||
const { getLocationLoading, statusNavbarHeightInfo, setShowGuideBar } = useGlobalState();
|
||||
const {
|
||||
gamesNum,
|
||||
searchValue,
|
||||
@@ -84,6 +85,11 @@ const HomeNavbar = (props: IProps) => {
|
||||
statusNavbarHeightInfo || {};
|
||||
|
||||
const [cityPopupVisible, setCityPopupVisible] = useState(false);
|
||||
const [locationDialogVisible, setLocationDialogVisible] = useState(false);
|
||||
const [locationDialogData, setLocationDialogData] = useState<{
|
||||
detectedProvince: string;
|
||||
cachedCity: [string, string];
|
||||
} | null>(null);
|
||||
const hasShownLocationDialog = useRef(false); // 防止重复弹窗
|
||||
|
||||
// 监听城市选择器状态变化,通知父组件
|
||||
@@ -122,28 +128,49 @@ const HomeNavbar = (props: IProps) => {
|
||||
|
||||
// 显示定位确认弹窗
|
||||
const showLocationConfirmDialog = (detectedProvince: string, cachedCity: [string, string]) => {
|
||||
(Taro as any).showModal({
|
||||
title: "提示",
|
||||
content: `检测到您当前位置在${detectedProvince},是否切换到${detectedProvince}?`,
|
||||
confirmText: "是",
|
||||
cancelText: "否",
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 用户选择"是",切换到当前定位城市
|
||||
const newArea: [string, string] = ["中国", detectedProvince];
|
||||
updateArea(newArea);
|
||||
// 更新缓存为新的定位信息(只有定位的才缓存)
|
||||
(Taro as any).setStorageSync(CITY_CACHE_KEY, newArea);
|
||||
console.log("切换到当前定位城市并更新缓存:", detectedProvince);
|
||||
|
||||
// 刷新数据
|
||||
handleCityChangeWithoutCache();
|
||||
} else {
|
||||
// 用户选择"否",保持缓存的定位城市
|
||||
console.log("保持缓存的定位城市:", cachedCity[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
console.log('[LocationDialog] 准备显示定位确认弹窗,隐藏 GuideBar');
|
||||
setLocationDialogData({ detectedProvince, cachedCity });
|
||||
setLocationDialogVisible(true);
|
||||
// 显示弹窗时隐藏 GuideBar
|
||||
setShowGuideBar(false);
|
||||
console.log('[LocationDialog] setShowGuideBar(false) 已调用');
|
||||
};
|
||||
|
||||
// 处理定位弹窗确认
|
||||
const handleLocationDialogConfirm = () => {
|
||||
if (!locationDialogData) return;
|
||||
|
||||
const { detectedProvince } = locationDialogData;
|
||||
// 用户选择"是",切换到当前定位城市
|
||||
const newArea: [string, string] = ["中国", detectedProvince];
|
||||
updateArea(newArea);
|
||||
// 更新缓存为新的定位信息(只有定位的才缓存)
|
||||
(Taro as any).setStorageSync(CITY_CACHE_KEY, newArea);
|
||||
console.log("切换到当前定位城市并更新缓存:", detectedProvince);
|
||||
|
||||
// 关闭弹窗
|
||||
setLocationDialogVisible(false);
|
||||
setLocationDialogData(null);
|
||||
// 关闭弹窗时显示 GuideBar
|
||||
setShowGuideBar(true);
|
||||
|
||||
// 刷新数据
|
||||
handleCityChangeWithoutCache();
|
||||
};
|
||||
|
||||
// 处理定位弹窗取消
|
||||
const handleLocationDialogCancel = () => {
|
||||
if (!locationDialogData) return;
|
||||
|
||||
const { cachedCity } = locationDialogData;
|
||||
// 用户选择"否",保持缓存的定位城市
|
||||
console.log("保持缓存的定位城市:", cachedCity[1]);
|
||||
|
||||
// 关闭弹窗
|
||||
setLocationDialogVisible(false);
|
||||
setLocationDialogData(null);
|
||||
// 关闭弹窗时显示 GuideBar
|
||||
setShowGuideBar(true);
|
||||
};
|
||||
|
||||
// const currentAddress = city + district;
|
||||
@@ -318,6 +345,16 @@ const HomeNavbar = (props: IProps) => {
|
||||
onCityChange={handleCityChange}
|
||||
/>
|
||||
)}
|
||||
{/* 定位确认弹窗 */}
|
||||
{locationDialogData && (
|
||||
<LocationConfirmDialog
|
||||
visible={locationDialogVisible}
|
||||
currentCity={locationDialogData.cachedCity[1]}
|
||||
detectedCity={locationDialogData.detectedProvince}
|
||||
onConfirm={handleLocationDialogConfirm}
|
||||
onCancel={handleLocationDialogCancel}
|
||||
/>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user