高级推荐

This commit is contained in:
mtvpls
2026-03-29 11:41:03 +08:00
parent 8acc50b198
commit 7547f78472
7 changed files with 397 additions and 4 deletions
@@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { listEnabledSourceScripts } from '@/lib/source-script';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const scripts = await listEnabledSourceScripts();
return NextResponse.json({
sources: scripts.map((item) => ({
key: item.key,
name: item.name,
description: item.description,
})),
});
} catch (error) {
return NextResponse.json(
{ error: '获取高级推荐脚本失败' },
{ status: 500 }
);
}
}
@@ -0,0 +1,66 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import {
executeSavedSourceScript,
normalizeScriptRecommendResults,
normalizeScriptSources,
} from '@/lib/source-script';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const sourceKey = searchParams.get('source');
const page = Number(searchParams.get('page') || '1');
if (!sourceKey) {
return NextResponse.json({ error: '缺少参数: source' }, { status: 400 });
}
try {
let sources = [{ id: 'default', name: '默认源' }];
try {
const sourcesExecution = await executeSavedSourceScript({
key: sourceKey,
hook: 'getSources',
payload: {},
});
sources = normalizeScriptSources(sourcesExecution.result);
} catch {
// 允许脚本未实现 getSources,继续使用默认源
}
const execution = await executeSavedSourceScript({
key: sourceKey,
hook: 'recommend',
payload: { page },
});
const results = normalizeScriptRecommendResults({
scriptKey: sourceKey,
scriptName: execution.meta?.name || sourceKey,
result: execution.result,
sources,
defaultSourceId: sources[0]?.id || 'default',
});
return NextResponse.json({
results,
page: Number(execution.result?.page || page),
pageCount: Number(execution.result?.pageCount || 1),
total: Number(execution.result?.total || results.length),
});
} catch (error) {
return NextResponse.json(
{ error: (error as Error).message || '获取高级推荐失败' },
{ status: 500 }
);
}
}