优化自定义去广告接口

This commit is contained in:
mtvpls
2025-12-05 12:41:14 +08:00
parent 81588443f1
commit 32d2800e25
4 changed files with 162 additions and 136 deletions
+17 -4
View File
@@ -7,17 +7,30 @@ export const runtime = 'nodejs';
/**
* GET /api/ad-filter
* 获取自定义去广告代码配置(公开接口,无需认证)
* 返回代码内容和版本号,客户端通过版本号判断是否需要更新缓存
* 支持两种模式:
* - 不带参数:只返回版本号,用于检查更新
* - ?full=true:返回完整代码和版本号
*/
export async function GET() {
export async function GET(request: Request) {
try {
const config = await getConfig();
const { searchParams } = new URL(request.url);
const full = searchParams.get('full') === 'true';
// 返回自定义去广告代码和版本号
const version = config.SiteConfig?.CustomAdFilterVersion || 0;
if (full) {
// 返回完整代码和版本号
return NextResponse.json({
code: config.SiteConfig?.CustomAdFilterCode || '',
version: config.SiteConfig?.CustomAdFilterVersion || 0,
version,
});
} else {
// 只返回版本号
return NextResponse.json({
version,
});
}
} catch (error) {
console.error('获取去广告代码配置失败:', error);
return NextResponse.json(
+8 -5
View File
@@ -110,7 +110,8 @@ function HomeClient() {
}, []);
// 处理收藏数据更新的函数
const updateFavoriteItems = useCallback(async (allFavorites: Record<string, any>) => {
const updateFavoriteItems = useCallback(
async (allFavorites: Record<string, any>) => {
const allPlayRecords = await getAllPlayRecords();
// 根据保存时间排序(从近到远)
@@ -139,7 +140,9 @@ function HomeClient() {
} as FavoriteItem;
});
setFavoriteItems(sorted);
}, []);
},
[]
);
// 当切换到收藏夹时加载收藏数据(使用 ref 防止重复加载)
useEffect(() => {
@@ -414,9 +417,9 @@ function HomeClient() {
// 找到当前星期对应的番剧数据,并过滤掉没有图片的
const todayAnimes =
bangumiCalendarData.find(
(item) => item.weekday.en === currentWeekday
)?.items.filter((anime) => anime.images) || [];
bangumiCalendarData
.find((item) => item.weekday.en === currentWeekday)
?.items.filter((anime) => anime.images) || [];
return todayAnimes.map((anime, index) => (
<div
+18 -8
View File
@@ -130,18 +130,28 @@ function PlayPageClient() {
console.log('使用缓存的去广告代码');
}
// 异步从服务器获取最新版本号,检查是否需要更新
const response = await fetch('/api/ad-filter');
if (!response.ok) {
console.warn('获取去广告代码配置失败,使用缓存');
// 第一步:先只获取版本号,检查是否需要更新
const versionResponse = await fetch('/api/ad-filter');
if (!versionResponse.ok) {
console.warn('获取去广告代码版本失败,使用缓存');
return;
}
const { code, version } = await response.json();
const { version } = await versionResponse.json();
// 如果版本号不一致或没有缓存,更新缓存
// 如果版本号不一致或没有缓存,才获取完整代码
if (!cachedVersion || parseInt(cachedVersion) !== version) {
console.log('检测到去广告代码更新,更新本地缓存');
console.log('检测到去广告代码更新(版本 ' + version + '),获取最新代码');
// 第二步:获取完整代码
const fullResponse = await fetch('/api/ad-filter?full=true');
if (!fullResponse.ok) {
console.warn('获取完整去广告代码失败,使用缓存');
return;
}
const { code } = await fullResponse.json();
if (code) {
localStorage.setItem('custom_ad_filter_code_cache', code);
localStorage.setItem('custom_ad_filter_version_cache', version.toString());
@@ -152,7 +162,7 @@ function PlayPageClient() {
localStorage.removeItem('custom_ad_filter_version_cache');
}
} else {
console.log('去广告代码已是最新版本');
console.log('去广告代码已是最新版本(版本 ' + version + '');
}
} catch (error) {
console.error('获取去广告代码配置失败:', error);