diff --git a/src/app/api/douban-related/route.ts b/src/app/api/douban-related/route.ts new file mode 100644 index 0000000..fb961ec --- /dev/null +++ b/src/app/api/douban-related/route.ts @@ -0,0 +1,38 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export const dynamic = 'force-dynamic'; + +export async function GET(req: NextRequest) { + const { searchParams } = new URL(req.url); + const id = (searchParams.get('id') || '').trim(); + const type = (searchParams.get('type') || 'movie').trim(); + if (!id) return NextResponse.json({ code: 0, msg: '缺少豆瓣ID' }); + + const ua = + 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1'; + const kind = type === 'tv' ? 'tv' : 'movie'; + + try { + const r = await fetch( + `https://m.douban.com/rexxar/api/v2/${kind}/${id}/recommendations?count=10&start=0`, + { + headers: { 'User-Agent': ua, Referer: `https://m.douban.com/${kind}/subject/${id}/`, Accept: 'application/json' }, + signal: AbortSignal.timeout(15000), + } + ); + if (!r.ok) return NextResponse.json({ code: 0, msg: '请求失败' }); + const arr: any = await r.json(); + if (!Array.isArray(arr)) return NextResponse.json({ code: 0, msg: '无数据' }); + const list = arr.slice(0, 10).map((x: any) => ({ + id: String(x.id || ''), + title: x.title || '', + poster: x?.pic?.large || x?.pic?.normal || '', + rating: Number(x?.rating?.value || 0), + subtitle: x.card_subtitle || '', + url: x.url || '', + })); + return NextResponse.json({ code: 1, msg: 'ok', list }); + } catch (e: any) { + return NextResponse.json({ code: 0, msg: e?.message || 'error' }); + } +} diff --git a/src/components/VideoMetaExtra.tsx b/src/components/VideoMetaExtra.tsx index 777b193..2adc2e0 100644 --- a/src/components/VideoMetaExtra.tsx +++ b/src/components/VideoMetaExtra.tsx @@ -24,6 +24,7 @@ export default function VideoMetaExtra({ doubanId, area, lang, + isTv, }: { title: string; actor?: string; @@ -34,10 +35,12 @@ export default function VideoMetaExtra({ doubanId?: number; area?: string; lang?: string; + isTv?: boolean; }) { const [comments, setComments] = useState([]); const [loading, setLoading] = useState(false); const [dbDetail, setDbDetail] = useState(null); + const [related, setRelated] = useState([]); useEffect(() => { if (!doubanId) return; @@ -59,6 +62,13 @@ export default function VideoMetaExtra({ if (alive && d?.code === 1) setDbDetail(d); }) .catch(() => {}); + // 相关推荐 + fetch(`/api/douban-related?id=${encodeURIComponent(String(doubanId))}&type=${isTv ? 'tv' : 'movie'}`) + .then((r) => r.json()) + .then((d) => { + if (alive && d?.code === 1) setRelated(d.list || []); + }) + .catch(() => {}); return () => { alive = false; }; @@ -185,6 +195,34 @@ export default function VideoMetaExtra({ ))} + {related.length > 0 && ( +
+
相关推荐
+
+ {related.map((r: any) => ( + + {r.poster ? ( + {r.title} + ) : null} +
{r.title}
+ {r.rating > 0 && ( +
{r.rating.toFixed(1)}
+ )} +
+ ))} +
+
+ )} ); } diff --git a/src/middleware.ts b/src/middleware.ts index ef2934f..1034f3b 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -172,6 +172,7 @@ function shouldSkipAuth(pathname: string): boolean { '/api/license-config', '/api/douban-comments', '/api/douban-detail', + '/api/douban-related', ]; return skipPaths.some((path) => pathname.startsWith(path)); @@ -180,6 +181,6 @@ function shouldSkipAuth(pathname: string): boolean { // 配置middleware匹配规则 export const config = { matcher: [ - '/((?!_next/static|_next/image|favicon.ico|login|warning|hls.min.js|mpegts.min.js|api/login|api/register|api/logout|api/cron|api/server-config|api/search|api/detail|api/image-proxy|api/license-config|api/douban-comments|api/douban-detail).*)', + '/((?!_next/static|_next/image|favicon.ico|login|warning|hls.min.js|mpegts.min.js|api/login|api/register|api/logout|api/cron|api/server-config|api/search|api/detail|api/image-proxy|api/license-config|api/douban-comments|api/douban-detail|api/douban-related).*)', ], };