From f54c827b8be9175577da442e5d538f84844f7046 Mon Sep 17 00:00:00 2001
From: hejianhao <15708179461@qq.com>
Date: 星期二, 21 四月 2026 16:44:58 +0800
Subject: [PATCH] fix(站点搜索): 修复初始化加载期间显示错误推荐的问题
---
src/robotaxi/pages/site-search/index.tsx | 7 ++-
src/robotaxi/pages/site-search/hooks/use-site-search.ts | 69 ++++++++++++++++++++++------------
2 files changed, 50 insertions(+), 26 deletions(-)
diff --git a/src/robotaxi/pages/site-search/hooks/use-site-search.ts b/src/robotaxi/pages/site-search/hooks/use-site-search.ts
index 8f8b302..3032e46 100644
--- a/src/robotaxi/pages/site-search/hooks/use-site-search.ts
+++ b/src/robotaxi/pages/site-search/hooks/use-site-search.ts
@@ -33,6 +33,8 @@
const [isSearching, setIsSearching] = useState(false);
// 最近一次完成搜索的关键词
const [lastCompletedKeyword, setLastCompletedKeyword] = useState('');
+ // 初始化数据是否已完成加载
+ const [isInitLoaded, setIsInitLoaded] = useState(false);
const searchSeqRef = useRef(0);
const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -148,12 +150,17 @@
};
}, []);
- // 清空搜索结果列表
- const clearSearchResults = () => {
+ // 取消搜索输入防抖定时器
+ const cancelDebouncedSearch = () => {
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current);
debounceTimerRef.current = null;
}
+ };
+
+ // 清空搜索结果列表
+ const clearSearchResults = () => {
+ cancelDebouncedSearch();
// 推进请求序号,使清空前已发出的请求结果失效
searchSeqRef.current += 1;
setSiteList([]);
@@ -164,36 +171,48 @@
useEffect(() => {
return () => {
- if (debounceTimerRef.current) {
- clearTimeout(debounceTimerRef.current);
- }
+ cancelDebouncedSearch();
};
}, []);
// 初始化:拉取附近站点、历史记录、页面类型
useEffect(() => {
- const { pageType: type, latitude: lat, longitude: lon, electronicAreaId } = deps.router.params;
- if (lat && lon) {
- fetchNearbySites(Number(lat), Number(lon), electronicAreaId);
- }
- checkOtp(false).then(otp => {
- if (otp && lat && lon) {
- const payload = { lat: Number(lat), lon: Number(lon), electronicAreaId };
- const handleSuccess = (res: any) => {
- if (Array.isArray(res)) {
+ let isUnmounted = false;
+ const initPageData = async () => {
+ const { pageType: type, latitude: lat, longitude: lon, electronicAreaId } = deps.router.params;
+ const initTasks: Promise<unknown>[] = [];
+ if (lat && lon) {
+ initTasks.push(fetchNearbySites(Number(lat), Number(lon), electronicAreaId));
+ }
+ initTasks.push((async () => {
+ try {
+ const otp = await checkOtp(false);
+ if (!otp || !lat || !lon) {
+ return;
+ }
+ const payload = { lat: Number(lat), lon: Number(lon), electronicAreaId };
+ const res = type === 'start'
+ ? await getStartPoints(payload)
+ : type === 'end'
+ ? await getEndPoints(payload)
+ : [];
+ if (Array.isArray(res) && !isUnmounted) {
setHistorySite(res.map(normalizeSiteItem));
}
- };
- if (type === 'start') {
- getStartPoints(payload).then(handleSuccess);
- } else if (type === 'end') {
- getEndPoints(payload).then(handleSuccess);
- }
+ } catch { }
+ })());
+ if (type) {
+ setPageType(type);
}
- });
- if (type) {
- setPageType(type);
- }
+ await Promise.allSettled(initTasks);
+ if (!isUnmounted) {
+ setIsInitLoaded(true);
+ }
+ };
+ initPageData();
+ return () => {
+ isUnmounted = true;
+ };
}, []);
return {
@@ -204,8 +223,10 @@
nearbySiteList,
isSearching,
lastCompletedKeyword,
+ isInitLoaded,
searchSite,
debouncedSearch,
+ cancelDebouncedSearch,
clearSearchResults
};
}
diff --git a/src/robotaxi/pages/site-search/index.tsx b/src/robotaxi/pages/site-search/index.tsx
index 58507ca..f436584 100644
--- a/src/robotaxi/pages/site-search/index.tsx
+++ b/src/robotaxi/pages/site-search/index.tsx
@@ -39,8 +39,10 @@
nearbySiteList,
isSearching,
lastCompletedKeyword,
+ isInitLoaded,
searchSite,
debouncedSearch,
+ cancelDebouncedSearch,
clearSearchResults
} = useSiteSearch({
router,
@@ -238,8 +240,9 @@
<View className='search-input-box'>
<Input className='search-input' placeholder={pageType == 'start' ? '请输入上车点' : '请输入目的地'} value={searchValue}
onInput={(e) => changeSearchValue(e)}
- onBlur={() => searchSite()}
+ onBlur={cancelDebouncedSearch}
onConfirm={() => searchSite()}
+ confirmType='search'
autoFocus
/>
{
@@ -300,7 +303,7 @@
</View>
}
{
- canShowSearchFallback && (!siteList.length && (!searchValue ? !nearbySiteList.length : true) && !nonOperationalSite.length && (!searchValue ? !historySite.length : true)) &&
+ isInitLoaded && canShowSearchFallback && (!siteList.length && (!searchValue ? !nearbySiteList.length : true) && !nonOperationalSite.length && (!searchValue ? !historySite.length : true)) &&
<View className='history-card'>
<View className='card-header'>暂无搜索结果,为您推荐附近站点</View>
<View className='card-content list-content'>
--
Gitblit v1.7.1