This commit is contained in:
mtvpls
2026-02-02 12:44:39 +08:00
parent e2fe2058e9
commit 68b1332df1
10 changed files with 2089 additions and 47 deletions

View File

@@ -0,0 +1,200 @@
/* eslint-disable no-console */
import { NextRequest, NextResponse } from 'next/server';
import { randomUUID } from 'crypto';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { db } from '@/lib/db';
export const runtime = 'nodejs';
// GET - 获取用户的所有歌单
export async function GET(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const playlists = await db.getUserMusicPlaylists(authInfo.username);
return NextResponse.json({ playlists });
} catch (error) {
console.error('GET /api/music/playlists error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// POST - 创建新歌单
export async function POST(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const body = await request.json();
const { name, description } = body;
if (!name || typeof name !== 'string' || name.trim().length === 0) {
return NextResponse.json(
{ error: '歌单名称不能为空' },
{ status: 400 }
);
}
const playlistId = randomUUID();
await db.createMusicPlaylist(authInfo.username, {
id: playlistId,
name: name.trim(),
description: description?.trim(),
});
const playlist = await db.getMusicPlaylist(playlistId);
return NextResponse.json({ playlist });
} catch (error) {
console.error('POST /api/music/playlists error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// PUT - 更新歌单信息
export async function PUT(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const body = await request.json();
const { playlistId, name, description, cover } = body;
if (!playlistId) {
return NextResponse.json(
{ error: '歌单ID不能为空' },
{ status: 400 }
);
}
// 检查歌单是否存在且属于当前用户
const playlist = await db.getMusicPlaylist(playlistId);
if (!playlist) {
return NextResponse.json({ error: '歌单不存在' }, { status: 404 });
}
if (playlist.username !== authInfo.username) {
return NextResponse.json({ error: '无权限操作此歌单' }, { status: 403 });
}
const updates: any = {};
if (name !== undefined) updates.name = name.trim();
if (description !== undefined) updates.description = description?.trim();
if (cover !== undefined) updates.cover = cover;
await db.updateMusicPlaylist(playlistId, updates);
const updatedPlaylist = await db.getMusicPlaylist(playlistId);
return NextResponse.json({ playlist: updatedPlaylist });
} catch (error) {
console.error('PUT /api/music/playlists error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// DELETE - 删除歌单
export async function DELETE(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const { searchParams } = new URL(request.url);
const playlistId = searchParams.get('playlistId');
if (!playlistId) {
return NextResponse.json(
{ error: '歌单ID不能为空' },
{ status: 400 }
);
}
// 检查歌单是否存在且属于当前用户
const playlist = await db.getMusicPlaylist(playlistId);
if (!playlist) {
return NextResponse.json({ error: '歌单不存在' }, { status: 404 });
}
if (playlist.username !== authInfo.username) {
return NextResponse.json({ error: '无权限操作此歌单' }, { status: 403 });
}
await db.deleteMusicPlaylist(playlistId);
return NextResponse.json({ success: true });
} catch (error) {
console.error('DELETE /api/music/playlists error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,187 @@
/* eslint-disable no-console */
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { db } from '@/lib/db';
export const runtime = 'nodejs';
// GET - 获取歌单中的所有歌曲
export async function GET(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const { searchParams } = new URL(request.url);
const playlistId = searchParams.get('playlistId');
if (!playlistId) {
return NextResponse.json(
{ error: '歌单ID不能为空' },
{ status: 400 }
);
}
// 检查歌单是否存在且属于当前用户
const playlist = await db.getMusicPlaylist(playlistId);
if (!playlist) {
return NextResponse.json({ error: '歌单不存在' }, { status: 404 });
}
if (playlist.username !== authInfo.username) {
return NextResponse.json({ error: '无权限访问此歌单' }, { status: 403 });
}
const songs = await db.getPlaylistSongs(playlistId);
return NextResponse.json({ songs });
} catch (error) {
console.error('GET /api/music/playlists/songs error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// POST - 添加歌曲到歌单
export async function POST(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const body = await request.json();
const { playlistId, song } = body;
if (!playlistId) {
return NextResponse.json(
{ error: '歌单ID不能为空' },
{ status: 400 }
);
}
if (!song || !song.platform || !song.id || !song.name || !song.artist) {
return NextResponse.json(
{ error: '歌曲信息不完整' },
{ status: 400 }
);
}
// 检查歌单是否存在且属于当前用户
const playlist = await db.getMusicPlaylist(playlistId);
if (!playlist) {
return NextResponse.json({ error: '歌单不存在' }, { status: 404 });
}
if (playlist.username !== authInfo.username) {
return NextResponse.json({ error: '无权限操作此歌单' }, { status: 403 });
}
// 检查歌曲是否已在歌单中
const exists = await db.isSongInPlaylist(playlistId, song.platform, song.id);
if (exists) {
return NextResponse.json(
{ error: '歌曲已在歌单中' },
{ status: 400 }
);
}
await db.addSongToPlaylist(playlistId, {
platform: song.platform,
id: song.id,
name: song.name,
artist: song.artist,
album: song.album,
pic: song.pic,
duration: song.duration || 0,
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('POST /api/music/playlists/songs error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// DELETE - 从歌单中移除歌曲
export async function DELETE(request: NextRequest) {
try {
// 从 cookie 获取用户信息
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 检查用户状态
if (authInfo.username !== process.env.USERNAME) {
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2) {
return NextResponse.json({ error: '用户不存在' }, { status: 401 });
}
if (userInfoV2.banned) {
return NextResponse.json({ error: '用户已被封禁' }, { status: 401 });
}
}
const { searchParams } = new URL(request.url);
const playlistId = searchParams.get('playlistId');
const platform = searchParams.get('platform');
const songId = searchParams.get('songId');
if (!playlistId || !platform || !songId) {
return NextResponse.json(
{ error: '参数不完整' },
{ status: 400 }
);
}
// 检查歌单是否存在且属于当前用户
const playlist = await db.getMusicPlaylist(playlistId);
if (!playlist) {
return NextResponse.json({ error: '歌单不存在' }, { status: 404 });
}
if (playlist.username !== authInfo.username) {
return NextResponse.json({ error: '无权限操作此歌单' }, { status: 403 });
}
await db.removeSongFromPlaylist(playlistId, platform, songId);
return NextResponse.json({ success: true });
} catch (error) {
console.error('DELETE /api/music/playlists/songs error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -81,44 +81,95 @@ export async function POST(request: NextRequest) {
}
const body = await request.json();
const { key, record }: { key: string; record: MusicPlayRecord } = body;
if (!key || !record) {
return NextResponse.json(
{ error: 'Missing key or record' },
{ status: 400 }
);
// 检查是否是批量添加
if (Array.isArray(body.records)) {
// 批量添加
const records: Array<{ platform: string; id: string; record: MusicPlayRecord }> = [];
for (const item of body.records) {
const { key, record } = item;
if (!key || !record) {
return NextResponse.json(
{ error: 'Missing key or record in batch item' },
{ status: 400 }
);
}
// 验证音乐播放记录数据
if (!record.platform || !record.id || !record.name || !record.artist) {
return NextResponse.json(
{ error: 'Invalid record data in batch item' },
{ status: 400 }
);
}
// 从key中解析platform和id
const [platform, id] = key.split('+');
if (!platform || !id) {
return NextResponse.json(
{ error: 'Invalid key format in batch item' },
{ status: 400 }
);
}
records.push({ platform, id, record });
// 缓存歌曲信息到服务器内存
setCachedSong(platform, id, {
id: record.id,
name: record.name,
artist: record.artist,
album: record.album,
pic: record.pic,
});
}
// 批量保存到数据库
await db.batchSaveMusicPlayRecords(authInfo.username, records);
return NextResponse.json({ success: true, count: records.length }, { status: 200 });
} else {
// 单个添加(保持向后兼容)
const { key, record }: { key: string; record: MusicPlayRecord } = body;
if (!key || !record) {
return NextResponse.json(
{ error: 'Missing key or record' },
{ status: 400 }
);
}
// 验证音乐播放记录数据
if (!record.platform || !record.id || !record.name || !record.artist) {
return NextResponse.json(
{ error: 'Invalid record data' },
{ status: 400 }
);
}
// 从key中解析platform和id
const [platform, id] = key.split('+');
if (!platform || !id) {
return NextResponse.json(
{ error: 'Invalid key format' },
{ status: 400 }
);
}
await db.saveMusicPlayRecord(authInfo.username, platform, id, record);
// 缓存歌曲信息到服务器内存
setCachedSong(platform, id, {
id: record.id,
name: record.name,
artist: record.artist,
album: record.album,
pic: record.pic,
});
return NextResponse.json({ success: true }, { status: 200 });
}
// 验证音乐播放记录数据
if (!record.platform || !record.id || !record.name || !record.artist) {
return NextResponse.json(
{ error: 'Invalid record data' },
{ status: 400 }
);
}
// 从key中解析platform和id
const [platform, id] = key.split('+');
if (!platform || !id) {
return NextResponse.json(
{ error: 'Invalid key format' },
{ status: 400 }
);
}
await db.saveMusicPlayRecord(authInfo.username, platform, id, record);
// 缓存歌曲信息到服务器内存
setCachedSong(platform, id, {
id: record.id,
name: record.name,
artist: record.artist,
album: record.album,
pic: record.pic,
});
return NextResponse.json({ success: true }, { status: 200 });
} catch (err) {
console.error('保存音乐播放记录失败', err);
return NextResponse.json(