音乐增加歌手和专辑搜索
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { isMusicSource, lxGetJson, LxServerSong, normalizeLxSong, unwrapLxArray } from '@/lib/music-v2';
|
||||
import { badRequest, internalError } from '@/lib/music-v2-api';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get('id')?.trim() || '';
|
||||
const source = searchParams.get('source') || 'wy';
|
||||
|
||||
if (!id) return badRequest('缺少专辑 ID');
|
||||
if (!isMusicSource(source)) return badRequest('不支持的音源');
|
||||
|
||||
const payload = await lxGetJson<any>(`/api/music/albumSongs?id=${encodeURIComponent(id)}&source=${source}`, 'none');
|
||||
const list = unwrapLxArray<LxServerSong>(payload);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
list: list.map(normalizeLxSong),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return internalError('获取专辑歌曲失败', (error as Error).message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { isMusicSource, lxGetJson, LxServerSong, normalizeLxSong, unwrapLxArray } from '@/lib/music-v2';
|
||||
import { badRequest, internalError } from '@/lib/music-v2-api';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get('id')?.trim() || '';
|
||||
const source = searchParams.get('source') || 'wy';
|
||||
const order = searchParams.get('order') || 'hot';
|
||||
|
||||
if (!id) return badRequest('缺少歌手 ID');
|
||||
if (!isMusicSource(source)) return badRequest('不支持的音源');
|
||||
|
||||
const payload = await lxGetJson<any>(`/api/music/artistSongs?id=${encodeURIComponent(id)}&source=${source}&order=${encodeURIComponent(order)}`, 'none');
|
||||
const list = unwrapLxArray<LxServerSong>(payload);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
list: list.map(normalizeLxSong),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return internalError('获取歌手歌曲失败', (error as Error).message);
|
||||
}
|
||||
}
|
||||
@@ -10,23 +10,36 @@ export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const q = searchParams.get('q')?.trim() || '';
|
||||
const source = searchParams.get('source') || 'kw';
|
||||
const type = searchParams.get('type') || 'song';
|
||||
const page = Number(searchParams.get('page') || '1');
|
||||
const limit = Number(searchParams.get('limit') || '20');
|
||||
|
||||
if (!q) return badRequest('缺少搜索关键词');
|
||||
if (!isMusicSource(source)) return badRequest('不支持的音源');
|
||||
if (!['song', 'singer', 'album'].includes(type)) return badRequest('不支持的搜索类型');
|
||||
if ((type === 'singer' || type === 'album') && source !== 'wy' && source !== 'tx') {
|
||||
return badRequest('当前音源不支持歌手/专辑搜索');
|
||||
}
|
||||
|
||||
const list = await lxGetJson<LxServerSong[]>(`/api/music/search?name=${encodeURIComponent(q)}&source=${source}&page=${page}&limit=${limit}`, 'none');
|
||||
const list = await lxGetJson<any[]>(`/api/music/search?name=${encodeURIComponent(q)}&source=${source}&type=${type}&page=${page}&limit=${limit}`, 'none');
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
list: list.map(normalizeLxSong),
|
||||
page,
|
||||
limit,
|
||||
hasMore: Array.isArray(list) && list.length >= limit,
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
data: {
|
||||
list: type === 'song' ? (list as LxServerSong[]).map(normalizeLxSong) : list,
|
||||
type,
|
||||
page,
|
||||
limit,
|
||||
hasMore: Array.isArray(list) && list.length >= limit,
|
||||
},
|
||||
},
|
||||
});
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': 'public, max-age=3600, s-maxage=3600, stale-while-revalidate=3600',
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
return internalError('搜索歌曲失败', (error as Error).message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user