添加行政区选择
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { View, Text, Image } from "@tarojs/components";
|
||||
import img from "@/config/images";
|
||||
import { useGlobalState } from "@/store/global";
|
||||
@@ -8,7 +8,10 @@ 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";
|
||||
import { CityPickerV2 as PopupPicker } from "@/components/Picker";
|
||||
|
||||
// 城市缓存 key
|
||||
const CITY_CACHE_KEY = "USER_SELECTED_CITY";
|
||||
|
||||
interface IProps {
|
||||
config?: {
|
||||
@@ -81,6 +84,7 @@ const HomeNavbar = (props: IProps) => {
|
||||
statusNavbarHeightInfo || {};
|
||||
|
||||
const [cityPopupVisible, setCityPopupVisible] = useState(false);
|
||||
const hasShownLocationDialog = useRef(false); // 防止重复弹窗
|
||||
|
||||
// 监听城市选择器状态变化,通知父组件
|
||||
useEffect(() => {
|
||||
@@ -88,13 +92,59 @@ const HomeNavbar = (props: IProps) => {
|
||||
}, [cityPopupVisible]);
|
||||
|
||||
const userInfo = useUserInfo();
|
||||
const province = (userInfo as any)?.province || "";
|
||||
const city = (userInfo as any)?.city || "";
|
||||
// const district = (userInfo as any)?.district || "";
|
||||
const locationProvince = (userInfo as any)?.province || ""; // 定位获取的省份
|
||||
// 只使用省份,不使用区域(city、district)
|
||||
|
||||
// 初始化城市:优先使用缓存的定位信息,其次使用当前定位
|
||||
useEffect(() => {
|
||||
updateArea(["中国", province, city]);
|
||||
}, [province, city]);
|
||||
// 1. 尝试从缓存中读取上次的定位信息
|
||||
const cachedCity = (Taro as any).getStorageSync(CITY_CACHE_KEY);
|
||||
|
||||
if (cachedCity && cachedCity.length === 2) {
|
||||
// 如果有缓存的定位信息,使用缓存
|
||||
console.log("使用缓存的定位城市:", cachedCity);
|
||||
updateArea(cachedCity);
|
||||
|
||||
// 如果当前定位省份已获取且与缓存不同,弹窗询问是否切换
|
||||
if (locationProvince && cachedCity[1] !== locationProvince && !hasShownLocationDialog.current) {
|
||||
hasShownLocationDialog.current = true;
|
||||
showLocationConfirmDialog(locationProvince, cachedCity);
|
||||
}
|
||||
} else if (locationProvince) {
|
||||
// 如果没有缓存但有定位信息,直接使用定位并保存到缓存
|
||||
console.log("没有缓存,使用当前定位省份:", locationProvince);
|
||||
const newArea: [string, string] = ["中国", locationProvince];
|
||||
updateArea(newArea);
|
||||
// 保存定位信息到缓存
|
||||
(Taro as any).setStorageSync(CITY_CACHE_KEY, newArea);
|
||||
}
|
||||
}, [locationProvince]);
|
||||
|
||||
// 显示定位确认弹窗
|
||||
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]);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// const currentAddress = city + district;
|
||||
|
||||
@@ -123,6 +173,7 @@ const HomeNavbar = (props: IProps) => {
|
||||
duration: 300,
|
||||
});
|
||||
}
|
||||
return; // 已经在列表页,只滚动到顶部,不需要跳转
|
||||
}
|
||||
(Taro as any).redirectTo({
|
||||
url: "/main_pages/index", // 列表页
|
||||
@@ -147,8 +198,8 @@ const HomeNavbar = (props: IProps) => {
|
||||
|
||||
const area_city = area.at(-1);
|
||||
|
||||
// 处理城市切换
|
||||
const handleCityChange = async (_newArea: any) => {
|
||||
// 处理城市切换(仅刷新数据,不保存缓存)
|
||||
const handleCityChangeWithoutCache = async () => {
|
||||
// 切换城市后,同时更新两个列表接口获取数据
|
||||
if (refreshBothLists) {
|
||||
await refreshBothLists();
|
||||
@@ -159,6 +210,15 @@ const HomeNavbar = (props: IProps) => {
|
||||
}
|
||||
};
|
||||
|
||||
// 处理城市切换(用户手动选择)
|
||||
const handleCityChange = async (_newArea: any) => {
|
||||
// 用户手动选择的城市不保存到缓存
|
||||
console.log("用户手动选择城市(不保存缓存):", _newArea);
|
||||
|
||||
// 切换城市后,同时更新两个列表接口获取数据
|
||||
await handleCityChangeWithoutCache();
|
||||
};
|
||||
|
||||
return (
|
||||
<View
|
||||
className="homeNavbar"
|
||||
|
||||
Reference in New Issue
Block a user