管理面板中增加聊天室配置
This commit is contained in:
98
src/app/api/admin/watch-room/route.ts
Normal file
98
src/app/api/admin/watch-room/route.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any,no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||
if (storageType === 'localstorage') {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: '不支持本地存储进行管理员配置',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const username = authInfo.username;
|
||||
|
||||
const {
|
||||
enabled,
|
||||
serverType,
|
||||
externalServerUrl,
|
||||
externalServerAuth,
|
||||
} = body as {
|
||||
enabled: boolean;
|
||||
serverType: 'internal' | 'external';
|
||||
externalServerUrl?: string;
|
||||
externalServerAuth?: string;
|
||||
};
|
||||
|
||||
// 参数校验
|
||||
if (
|
||||
typeof enabled !== 'boolean' ||
|
||||
(serverType !== 'internal' && serverType !== 'external')
|
||||
) {
|
||||
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 如果使用外部服务器,URL 必须提供
|
||||
if (serverType === 'external' && !externalServerUrl) {
|
||||
return NextResponse.json({ error: '外部服务器地址不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const adminConfig = await getConfig();
|
||||
|
||||
// 权限校验
|
||||
if (username !== process.env.USERNAME) {
|
||||
// 管理员
|
||||
const user = adminConfig.UserConfig.Users.find(
|
||||
(u) => u.username === username
|
||||
);
|
||||
if (!user || user.role !== 'admin' || user.banned) {
|
||||
return NextResponse.json({ error: '权限不足' }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
// 更新缓存中的观影室配置
|
||||
adminConfig.WatchRoomConfig = {
|
||||
enabled,
|
||||
serverType,
|
||||
externalServerUrl,
|
||||
externalServerAuth,
|
||||
};
|
||||
|
||||
// 写入数据库
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: true },
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': 'no-store', // 不缓存结果
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('更新观影室配置失败:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: '更新观影室配置失败',
|
||||
details: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user