From 6492191a2d9eab8cd18f0718e2023b8b655676c2 Mon Sep 17 00:00:00 2001 From: shinya Date: Fri, 4 Jul 2025 01:23:22 +0800 Subject: [PATCH] fix: aggreaget match and keyword trim --- src/app/aggregate/page.tsx | 16 +++++++++------- src/app/api/search/route.ts | 4 ++-- src/app/detail/page.tsx | 2 +- src/app/play/page.tsx | 10 +++++----- src/app/search/page.tsx | 17 +++++++++++------ src/components/AggregateCard.tsx | 2 +- src/components/DemoCard.tsx | 4 +++- src/components/VideoCard.tsx | 2 +- src/lib/fetchVideoDetail.ts | 4 ++-- src/lib/refreshRecordAndFavorites.ts | 2 +- 10 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/app/aggregate/page.tsx b/src/app/aggregate/page.tsx index c8072c1..7a8cf1f 100644 --- a/src/app/aggregate/page.tsx +++ b/src/app/aggregate/page.tsx @@ -14,10 +14,10 @@ import PageLayout from '@/components/PageLayout'; function AggregatePageClient() { const searchParams = useSearchParams(); - const query = searchParams.get('q')?.trim() || ''; - const title = searchParams.get('title')?.trim() || ''; - const year = searchParams.get('year')?.trim() || ''; - const type = searchParams.get('type')?.trim() || ''; + const query = searchParams.get('q') || ''; + const title = searchParams.get('title') || ''; + const year = searchParams.get('year') || ''; + const type = searchParams.get('type') || ''; const [results, setResults] = useState([]); const [loading, setLoading] = useState(true); @@ -33,7 +33,9 @@ function AggregatePageClient() { const fetchData = async () => { try { - const res = await fetch(`/api/search?q=${encodeURIComponent(query)}`); + const res = await fetch( + `/api/search?q=${encodeURIComponent(query.trim())}` + ); if (!res.ok) { throw new Error('搜索失败'); } @@ -79,7 +81,7 @@ function AggregatePageClient() { setResults(Array.from(map.values()).flat()); } else if (map.size > 1) { // 存在多个匹配,跳转到搜索页 - router.push(`/search?q=${encodeURIComponent(query)}`); + router.push(`/search?q=${encodeURIComponent(query.trim())}`); } } catch (e) { setError(e instanceof Error ? e.message : '搜索失败'); @@ -189,7 +191,7 @@ function AggregatePageClient() { key={src.source} href={`/play?source=${src.source}&id=${ src.id - }&title=${encodeURIComponent(src.title)}${ + }&title=${encodeURIComponent(src.title.trim())}${ src.year ? `&year=${src.year}` : '' }&from=aggregate`} className='group relative flex items-center justify-center w-full h-14 bg-gray-500/80 hover:bg-green-500 dark:bg-gray-700/80 dark:hover:bg-green-600 rounded-lg transition-colors' diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index 53be883..0e948f5 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -83,7 +83,7 @@ async function searchFromApi( return { id: item.vod_id, - title: item.vod_name, + title: item.vod_name.trim().replace(/\s+/g, ' '), poster: item.vod_pic, episodes, source: apiSite.key, @@ -151,7 +151,7 @@ async function searchFromApi( return { id: item.vod_id, - title: item.vod_name, + title: item.vod_name.trim().replace(/\s+/g, ' '), poster: item.vod_pic, episodes, source: apiSite.key, diff --git a/src/app/detail/page.tsx b/src/app/detail/page.tsx index 713e7a4..17f98af 100644 --- a/src/app/detail/page.tsx +++ b/src/app/detail/page.tsx @@ -58,7 +58,7 @@ function DetailPageClient() { const detailData = await fetchVideoDetail({ source, id, - fallbackTitle, + fallbackTitle: fallbackTitle.trim(), fallbackYear, }); setDetail(detailData); diff --git a/src/app/play/page.tsx b/src/app/play/page.tsx index 5e71fb3..94768ca 100644 --- a/src/app/play/page.tsx +++ b/src/app/play/page.tsx @@ -274,7 +274,7 @@ function PlayPageClient() { const detailData = await fetchVideoDetail({ source: currentSource, id: currentId, - fallbackTitle: videoTitle, + fallbackTitle: videoTitle.trim(), fallbackYear: videoYear, }); @@ -531,7 +531,7 @@ function PlayPageClient() { try { const response = await fetch( - `/api/search?q=${encodeURIComponent(query)}` + `/api/search?q=${encodeURIComponent(query.trim())}` ); if (!response.ok) { throw new Error('搜索失败'); @@ -614,7 +614,7 @@ function PlayPageClient() { const newDetail = await fetchVideoDetail({ source: newSource, id: newId, - fallbackTitle: newTitle, + fallbackTitle: newTitle.trim(), fallbackYear: videoYear, }); @@ -1100,7 +1100,7 @@ function PlayPageClient() { onClick={() => { if (videoTitle) { window.location.href = `/aggregate?q=${encodeURIComponent( - videoTitle + videoTitle.trim() )}${videoYear ? `&year=${encodeURIComponent(videoYear)}` : ''}`; } else { window.location.href = '/'; @@ -1125,7 +1125,7 @@ function PlayPageClient() { // 返回选源页 if (videoTitle) { window.location.href = `/aggregate?q=${encodeURIComponent( - videoTitle + videoTitle.trim() )}${videoYear ? `&year=${encodeURIComponent(videoYear)}` : ''}`; } else { window.location.href = '/'; diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 6459f8e..e1d0df9 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -78,7 +78,7 @@ function SearchPageClient() { try { setIsLoading(true); const response = await fetch( - `/api/search?q=${encodeURIComponent(query)}` + `/api/search?q=${encodeURIComponent(query.trim())}` ); const data = await response.json(); setSearchResults(data.results); @@ -92,17 +92,20 @@ function SearchPageClient() { const handleSearch = (e: React.FormEvent) => { e.preventDefault(); - if (!searchQuery.trim()) return; + const trimmed = searchQuery.trim().replace(/\s+/g, ' '); + if (!trimmed) return; + // 回显搜索框 + setSearchQuery(trimmed); setIsLoading(true); setShowResults(true); - router.push(`/search?q=${encodeURIComponent(searchQuery)}`); + router.push(`/search?q=${encodeURIComponent(trimmed)}`); // 直接发请求 - fetchSearchResults(searchQuery); + fetchSearchResults(trimmed); // 保存到搜索历史 - addSearchHistory(searchQuery).then(async () => { + addSearchHistory(trimmed).then(async () => { const history = await getSearchHistory(); setSearchHistory(history); }); @@ -225,7 +228,9 @@ function SearchPageClient() {