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