Files
katelyatv/src/app/douban/page.tsx
T
3970260761 e766391534
Docker Build & Test / build (push) Canceled after 0s
Docker Build & Test / test (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/amd64) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm/v7) (push) Canceled after 0s
Build & Push Docker image / build-and-push (linux/arm64) (push) Canceled after 0s
Build & Push Docker image / merge-images (push) Canceled after 0s
Release / release (push) Canceled after 0s
Release / docker (push) Canceled after 0s
fixed96: 新增/动漫页与/api/browse第三方分类浏览,修复动漫页首屏空列表
- 新增 src/app/anime + src/app/api/anime: 多源并行抓取、地区归一化、title+year去重、update_time降序、pg分页
- 新增 src/app/api/browse + src/lib/vodcat.ts: kind/cat|area/pg 统一第三方分类浏览接口
- douban 电影/剧集/综艺 选择器改走 /api/browse, 分类全量切第三方分类语义
- PageLayout/MobileBottomNav 增加 动漫 导航入口(源码实现)
- 修复 /anime reset=true 时 base 置空导致新数据被丢弃(接口200页面空)
- 版本 NEXT_PUBLIC_SITE_VERSION=fixed96; README/CHANGELOG 更新
2026-11-09 03:02:13 +08:00

383 lines
12 KiB
TypeScript

/* eslint-disable no-console,react-hooks/exhaustive-deps */
'use client';
import { useSearchParams } from 'next/navigation';
import { Suspense } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import DoubanCardSkeleton from '@/components/DoubanCardSkeleton';
import DoubanSelector from '@/components/DoubanSelector';
import PageLayout from '@/components/PageLayout';
import VideoCard from '@/components/VideoCard';
// 第三方采集站聚合条目(/api/browse 返回)
type BrowseItem = {
id: string;
title: string;
poster: string;
year: string;
area: string;
episodes: number;
source: string;
source_name: string;
};
function DoubanPageClient() {
const searchParams = useSearchParams();
const [browseData, setBrowseData] = useState<BrowseItem[]>([]);
const [loading, setLoading] = useState(false);
const [currentPage, setCurrentPage] = useState(0);
const [hasMore, setHasMore] = useState(true);
const [isLoadingMore, setIsLoadingMore] = useState(false);
const [selectorsReady, setSelectorsReady] = useState(false);
const observerRef = useRef<IntersectionObserver | null>(null);
const loadingRef = useRef<HTMLDivElement>(null);
const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const type = searchParams.get('type') || 'movie';
const catParam = searchParams.get('cat') || '';
// 选择器状态 - 完全独立,不依赖URL参数
const [primarySelection, setPrimarySelection] = useState<string>(() => {
return type === 'movie' ? '全部' : '';
});
const [secondarySelection, setSecondarySelection] = useState<string>(() => {
if (catParam) return catParam;
if (type === 'movie') return '全部';
if (type === 'tv') return 'tv';
if (type === 'show') return 'show';
return '全部';
});
// 初始化时标记选择器为准备好状态
useEffect(() => {
const timer = setTimeout(() => {
setSelectorsReady(true);
}, 50);
return () => clearTimeout(timer);
}, []);
// type变化时立即重置selectorsReady(最高优先级)
useEffect(() => {
setSelectorsReady(false);
setLoading(true); // 立即显示loading状态
}, [type, catParam]);
// 当type变化时重置选择器状态
useEffect(() => {
if (type === 'movie') {
setPrimarySelection('全部');
setSecondarySelection(catParam || '全部');
} else if (type === 'tv') {
setPrimarySelection('');
setSecondarySelection(catParam || 'tv');
} else if (type === 'show') {
setPrimarySelection('');
setSecondarySelection('show');
} else {
setPrimarySelection('');
setSecondarySelection('全部');
}
const timer = setTimeout(() => {
setSelectorsReady(true);
}, 50);
return () => clearTimeout(timer);
}, [type, catParam]);
// 生成骨架屏数据
const skeletonData = Array.from({ length: 25 }, (_, index) => index);
// 选择器 -> 第三方聚合接口参数映射
const getBrowseParams = useCallback((): { kind: string; cat: string; area: string } => {
if (type === 'movie') {
// 二级为地区,映射到 vod_area 关键词
const areaMap: Record<string, string> = {
全部: '',
华语: '中国|大陆|香港|台湾|澳门',
欧美: '美国|英国|法国|德国|加拿大|西班牙|意大利|荷兰|俄罗斯',
韩国: '韩国',
日本: '日本',
泰国: '泰国',
};
return {
kind: 'movie',
cat: primarySelection === '全部' || primarySelection === '热门' ? '' : primarySelection,
area: areaMap[secondarySelection] ?? '',
};
}
if (type === 'tv') {
// 二级为剧集地区/类型
const tvMap: Record<string, { kind: string; cat: string }> = {
tv: { kind: 'tv', cat: '' },
tv_domestic: { kind: 'tv', cat: '国产剧' },
tv_american: { kind: 'tv', cat: '欧美剧' },
tv_japanese: { kind: 'tv', cat: '日本剧' },
tv_korean: { kind: 'tv', cat: '韩国剧' },
tv_animation: { kind: 'anime', cat: '' },
tv_documentary: { kind: 'tv', cat: '纪录' },
};
const m = tvMap[secondarySelection] || { kind: 'tv', cat: '' };
return { kind: m.kind, cat: m.cat, area: '' };
}
// 综艺
const showMap: Record<string, string> = {
show: '',
show_domestic: '大陆|国内|港台',
show_foreign: '日韩|欧美|海外|国外',
};
return { kind: 'show', cat: showMap[secondarySelection] ?? '', area: '' };
}, [type, primarySelection, secondarySelection]);
const fetchPage = useCallback(
async (pg: number) => {
const { kind, cat, area } = getBrowseParams();
const params = new URLSearchParams({ kind, pg: String(pg) });
if (cat) params.set('cat', cat);
if (area) params.set('area', area);
const res = await fetch(`/api/browse?${params.toString()}`);
if (!res.ok) throw new Error(`浏览接口请求失败: ${res.status}`);
return (await res.json()) as {
list: BrowseItem[];
has_more: boolean;
};
},
[getBrowseParams]
);
// 防抖的数据加载函数
const loadInitialData = useCallback(async () => {
try {
setLoading(true);
const data = await fetchPage(1);
setBrowseData(data.list);
setHasMore(data.has_more && data.list.length > 0);
setLoading(false);
} catch (err) {
console.error(err);
setLoading(false);
}
}, [fetchPage]);
useEffect(() => {
if (!selectorsReady) {
return;
}
// 重置页面状态
setBrowseData([]);
setCurrentPage(0);
setHasMore(true);
setIsLoadingMore(false);
if (debounceTimeoutRef.current) {
clearTimeout(debounceTimeoutRef.current);
}
// 使用防抖机制加载数据,避免连续状态更新触发多次请求
debounceTimeoutRef.current = setTimeout(() => {
loadInitialData();
}, 100);
return () => {
if (debounceTimeoutRef.current) {
clearTimeout(debounceTimeoutRef.current);
}
};
}, [
selectorsReady,
type,
primarySelection,
secondarySelection,
loadInitialData,
]);
// 单独处理 currentPage 变化(加载更多)
useEffect(() => {
if (currentPage > 0) {
const fetchMoreData = async () => {
try {
setIsLoadingMore(true);
const data = await fetchPage(currentPage + 1);
setBrowseData((prev) => [...prev, ...data.list]);
setHasMore(data.has_more && data.list.length > 0);
} catch (err) {
console.error(err);
} finally {
setIsLoadingMore(false);
}
};
fetchMoreData();
}
}, [currentPage, type, primarySelection, secondarySelection]);
// 设置滚动监听
useEffect(() => {
if (!hasMore || isLoadingMore || loading) {
return;
}
if (!loadingRef.current) {
return;
}
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && hasMore && !isLoadingMore) {
setCurrentPage((prev) => prev + 1);
}
},
{ threshold: 0.1 }
);
observer.observe(loadingRef.current);
observerRef.current = observer;
return () => {
if (observerRef.current) {
observerRef.current.disconnect();
}
};
}, [hasMore, isLoadingMore, loading]);
// 处理选择器变化
const handlePrimaryChange = useCallback(
(value: string) => {
if (value !== primarySelection) {
setLoading(true);
setPrimarySelection(value);
}
},
[primarySelection]
);
const handleSecondaryChange = useCallback(
(value: string) => {
if (value !== secondarySelection) {
setLoading(true);
setSecondarySelection(value);
}
},
[secondarySelection]
);
const getPageTitle = () => {
if (type === 'tv' && catParam === 'tv_animation') return '动漫';
return type === 'movie' ? '电影' : type === 'tv' ? '电视剧' : '综艺';
};
const getActivePath = () => {
const params = new URLSearchParams();
if (type) params.set('type', type);
const queryString = params.toString();
const activePath = `/douban${queryString ? `?${queryString}` : ''}`;
return activePath;
};
return (
<PageLayout activePath={getActivePath()}>
<div className='px-4 sm:px-10 py-4 sm:py-8 overflow-visible'>
{/* 页面标题和选择器 */}
<div className='mb-6 sm:mb-8 space-y-4 sm:space-y-6'>
{/* 页面标题 */}
<div>
<h1 className='text-2xl sm:text-3xl font-bold text-gray-800 mb-1 sm:mb-2 dark:text-gray-200'>
{getPageTitle()}
</h1>
<p className='text-sm sm:text-base text-gray-600 dark:text-gray-400'>
全网资源站聚合,按分类实时更新
</p>
</div>
{/* 选择器组件 */}
<div className='bg-white/60 dark:bg-gray-800/40 rounded-2xl p-4 sm:p-6 border border-gray-200/30 dark:border-gray-700/30 backdrop-blur-sm'>
<DoubanSelector
type={type as 'movie' | 'tv' | 'show'}
primarySelection={primarySelection}
secondarySelection={secondarySelection}
onPrimaryChange={handlePrimaryChange}
onSecondaryChange={handleSecondaryChange}
/>
</div>
</div>
{/* 内容展示区域 */}
<div className='max-w-[95%] mx-auto mt-8 overflow-visible'>
{/* 内容网格 */}
<div className='grid grid-cols-3 gap-x-2 gap-y-12 px-0 sm:px-2 sm:grid-cols-[repeat(auto-fit,minmax(160px,1fr))] sm:gap-x-8 sm:gap-y-20'>
{loading || !selectorsReady
? // 显示骨架屏
skeletonData.map((index) => <DoubanCardSkeleton key={index} />)
: // 显示实际数据
browseData.map((item, index) => (
<div
key={`${item.source}-${item.id}-${index}`}
className='w-full'
>
<VideoCard
from='search'
id={item.id}
source={item.source}
source_name={item.source_name}
title={item.title}
poster={item.poster}
episodes={item.episodes}
year={item.year}
type={type === 'movie' ? 'movie' : ''} // 电影类型严格控制,tv 不控
/>
</div>
))}
</div>
{/* 加载更多指示器 */}
{hasMore && !loading && (
<div
ref={(el) => {
if (el && el.offsetParent !== null) {
(
loadingRef as React.MutableRefObject<HTMLDivElement | null>
).current = el;
}
}}
className='flex justify-center mt-12 py-8'
>
{isLoadingMore && (
<div className='flex items-center gap-2'>
<div className='animate-spin rounded-full h-6 w-6 border-b-2 border-green-500'></div>
<span className='text-gray-600'>加载中...</span>
</div>
)}
</div>
)}
{/* 没有更多数据提示 */}
{!hasMore && browseData.length > 0 && (
<div className='text-center text-gray-500 py-8'>已加载全部内容</div>
)}
{/* 空状态 */}
{!loading && browseData.length === 0 && (
<div className='text-center text-gray-500 py-8'>暂无相关内容</div>
)}
</div>
</div>
</PageLayout>
);
}
export default function DoubanPage() {
return (
<Suspense>
<DoubanPageClient />
</Suspense>
);
}