音乐播放记录多设备同步。

This commit is contained in:
mtvpls
2026-02-01 17:07:50 +08:00
parent 2ef04bc434
commit 4cb5bc27c6
8 changed files with 647 additions and 2 deletions

View File

@@ -0,0 +1,144 @@
/* eslint-disable no-console */
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { db } from '@/lib/db';
import { MusicPlayRecord } from '@/lib/db.client';
export const runtime = 'nodejs';
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 records = await db.getAllMusicPlayRecords(authInfo.username);
return NextResponse.json(records, { status: 200 });
} catch (err) {
console.error('获取音乐播放记录失败', err);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}
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 { 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);
return NextResponse.json({ success: true }, { status: 200 });
} catch (err) {
console.error('保存音乐播放记录失败', err);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}
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 key = searchParams.get('key');
if (key) {
// 删除单条记录
const [platform, id] = key.split('+');
if (!platform || !id) {
return NextResponse.json(
{ error: 'Invalid key format' },
{ status: 400 }
);
}
await db.deleteMusicPlayRecord(authInfo.username, platform, id);
} else {
// 清空所有记录
await db.clearAllMusicPlayRecords(authInfo.username);
}
return NextResponse.json({ success: true }, { status: 200 });
} catch (err) {
console.error('删除音乐播放记录失败', err);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}