增加播放记录批量删除功能

This commit is contained in:
mtvpls
2026-06-01 17:52:16 +08:00
parent 2f1a763367
commit 0201891efa
8 changed files with 2721 additions and 828 deletions
+40 -5
View File
@@ -29,14 +29,18 @@ export async function GET(request: NextRequest) {
// 检查播放记录迁移标识,没有迁移标识时执行迁移
if (!userInfoV2.playrecord_migrated) {
console.log(`用户 ${authInfo.username} 播放记录未迁移,开始执行迁移...`);
console.log(
`用户 ${authInfo.username} 播放记录未迁移,开始执行迁移...`
);
await db.migratePlayRecords(authInfo.username);
}
} else {
// 站长也需要执行迁移(站长可能不在数据库中,直接尝试迁移)
const userInfoV2 = await db.getUserInfoV2(authInfo.username);
if (!userInfoV2 || !userInfoV2.playrecord_migrated) {
console.log(`站长 ${authInfo.username} 播放记录未迁移,开始执行迁移...`);
console.log(
`站长 ${authInfo.username} 播放记录未迁移,开始执行迁移...`
);
await db.migratePlayRecords(authInfo.username);
}
}
@@ -106,9 +110,11 @@ export async function POST(request: NextRequest) {
await db.savePlayRecord(authInfo.username, source, id, finalRecord);
// 异步清理旧的播放记录(不阻塞响应)
(db as any).storage.cleanupOldPlayRecords(authInfo.username).catch((err: Error) => {
console.error('异步清理播放记录失败:', err);
});
(db as any).storage
.cleanupOldPlayRecords(authInfo.username)
.catch((err: Error) => {
console.error('异步清理播放记录失败:', err);
});
return NextResponse.json({ success: true }, { status: 200 });
} catch (err) {
@@ -142,6 +148,23 @@ export async function DELETE(request: NextRequest) {
const username = authInfo.username;
const { searchParams } = new URL(request.url);
const key = searchParams.get('key');
let keys: string[] | undefined;
try {
const text = await request.text();
if (text) {
const body = JSON.parse(text);
if (Array.isArray(body?.keys)) {
keys = Array.from(
new Set(
body.keys.filter((item: unknown) => typeof item === 'string')
)
);
}
}
} catch {
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 });
}
if (key) {
// 如果提供了 key,删除单条播放记录
@@ -154,6 +177,18 @@ export async function DELETE(request: NextRequest) {
}
await db.deletePlayRecord(username, source, id);
} else if (keys && keys.length > 0) {
for (const item of keys) {
const [source, id] = item.split('+');
if (!source || !id) {
return NextResponse.json(
{ error: 'Invalid key format' },
{ status: 400 }
);
}
}
await db.deletePlayRecords(username, keys);
} else {
// 未提供 key,则清空全部播放记录
// 目前 DbManager 没有对应方法,这里直接遍历删除