From bdfad48656acff102534b1b0800a154a32261b52 Mon Sep 17 00:00:00 2001 From: katelya Date: Fri, 5 Sep 2025 11:37:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=88=90=E4=BA=BA?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E8=BF=87=E6=BB=A4=E8=AE=BE=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=A4=84=E7=90=86=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=90=9C=E7=B4=A2API=E7=9A=84=E7=BC=93=E5=AD=98=E6=8E=A7?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/search/route.ts | 29 ++++++++++++++++++++++++--- src/app/api/user/settings/route.ts | 12 +++++++++++ src/app/search/page.tsx | 11 ++++++++-- src/components/AdultContentFilter.tsx | 12 +++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index ec4d4de..33a1931 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -1,7 +1,8 @@ import { NextResponse } from 'next/server'; -import { getCacheTime, getFilteredApiSites } from '@/lib/config'; +import { getAvailableApiSites,getCacheTime } from '@/lib/config'; import { addCorsHeaders, handleOptionsRequest } from '@/lib/cors'; +import { getStorage } from '@/lib/db'; import { searchFromApi } from '@/lib/downstream'; export const runtime = 'edge'; @@ -43,8 +44,30 @@ export async function GET(request: Request) { } try { - // 使用新的动态过滤方法,根据用户设置自动过滤成人内容源 - const availableSites = await getFilteredApiSites(userName); + // 检查是否明确要求包含成人内容(用于关闭过滤时的明确请求) + const includeAdult = searchParams.get('include_adult') === 'true'; + + // 获取用户的成人内容过滤设置 + let shouldFilterAdult = true; // 默认过滤 + if (userName) { + try { + const storage = getStorage(); + const userSettings = await storage.getUserSettings(userName); + // 如果用户设置存在且明确设为false,则不过滤;否则默认过滤 + shouldFilterAdult = userSettings?.filter_adult_content !== false; + } catch (error) { + // 出错时默认过滤成人内容 + shouldFilterAdult = true; + } + } + + // 根据用户设置和明确请求决定最终的过滤策略 + const finalShouldFilter = shouldFilterAdult || !includeAdult; + + // 使用动态过滤方法,但不依赖缓存,实时获取设置 + const availableSites = finalShouldFilter + ? await getAvailableApiSites(true) // 过滤成人内容 + : await getAvailableApiSites(false); // 不过滤成人内容 if (!availableSites || availableSites.length === 0) { const cacheTime = await getCacheTime(); diff --git a/src/app/api/user/settings/route.ts b/src/app/api/user/settings/route.ts index 33dffe3..f684444 100644 --- a/src/app/api/user/settings/route.ts +++ b/src/app/api/user/settings/route.ts @@ -34,6 +34,12 @@ export async function GET(_request: NextRequest) { auto_play: true, video_quality: 'auto' } + }, { + headers: { + 'Cache-Control': 'no-cache, no-store, must-revalidate', + 'Pragma': 'no-cache', + 'Expires': '0' + } }); } catch (error) { // eslint-disable-next-line no-console @@ -78,6 +84,12 @@ export async function PATCH(request: NextRequest) { return NextResponse.json({ success: true, message: '设置更新成功' + }, { + headers: { + 'Cache-Control': 'no-cache, no-store, must-revalidate', + 'Pragma': 'no-cache', + 'Expires': '0' + } }); } catch (error) { // eslint-disable-next-line no-console diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 281df4f..293c978 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -180,9 +180,16 @@ function SearchPageClient() { } // 简化的搜索请求 - 成人内容过滤现在在API层面自动处理 + // 添加时间戳参数避免缓存问题 + const timestamp = Date.now(); const response = await fetch( - `/api/search?q=${encodeURIComponent(query.trim())}`, - { headers } + `/api/search?q=${encodeURIComponent(query.trim())}&t=${timestamp}`, + { + headers: { + ...headers, + 'Cache-Control': 'no-cache, no-store, must-revalidate' + } + } ); const data = await response.json(); diff --git a/src/components/AdultContentFilter.tsx b/src/components/AdultContentFilter.tsx index afd2015..3fab0f8 100644 --- a/src/components/AdultContentFilter.tsx +++ b/src/components/AdultContentFilter.tsx @@ -68,6 +68,18 @@ const AdultContentFilter: React.FC = ({ if (response.ok) { const newState = !isEnabled; setIsEnabled(newState); + + // 强制刷新用户设置缓存 - 向搜索API发送一个空请求来刷新设置 + try { + await fetch('/api/search?q=_cache_refresh_', { + headers: { + 'Authorization': `Bearer ${userName}`, + }, + }); + } catch { + // 忽略刷新缓存的错误 + } + onUpdate?.(newState); } else { const errorData = await response.json();