140 lines
3.3 KiB
TypeScript
140 lines
3.3 KiB
TypeScript
/**
|
||
* 单个 API Key 管理接口
|
||
* PUT - 启用/禁用 API Key
|
||
* DELETE - 删除 API Key
|
||
*/
|
||
|
||
import { NextRequest, NextResponse } from 'next/server';
|
||
import { getServerSession } from 'next-auth';
|
||
import { authOptions } from '@/lib/auth';
|
||
import { isSuperAdmin } from '@/lib/department-permission';
|
||
import { enableApiKey, disableApiKey } from '@/lib/external-api';
|
||
import { prisma } from '@/lib/prisma';
|
||
|
||
/**
|
||
* PUT - 启用/禁用 API Key
|
||
*/
|
||
export async function PUT(
|
||
request: NextRequest,
|
||
{ params }: { params: { id: string } }
|
||
) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ success: false, error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
|
||
// 检查权限
|
||
const userId = (session.user as any).id;
|
||
if (!(await isSuperAdmin(userId))) {
|
||
return NextResponse.json(
|
||
{ success: false, error: '仅超级管理员可操作' },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
const { id } = params;
|
||
const body = await request.json();
|
||
const { isActive } = body;
|
||
|
||
if (typeof isActive !== 'boolean') {
|
||
return NextResponse.json(
|
||
{ success: false, error: '参数错误' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 验证 API Key 是否存在
|
||
const apiKey: any = await prisma.apiKey.findUnique({
|
||
where: { id },
|
||
select: { id: true },
|
||
});
|
||
|
||
if (!apiKey) {
|
||
return NextResponse.json(
|
||
{ success: false, error: 'API Key 不存在' },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
|
||
// 更新状态
|
||
if (isActive) {
|
||
await enableApiKey(id);
|
||
} else {
|
||
await disableApiKey(id);
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: isActive ? 'API Key 已启用' : 'API Key 已禁用',
|
||
});
|
||
} catch (error) {
|
||
console.error('[admin/api-keys/[id]] PUT error:', error);
|
||
return NextResponse.json(
|
||
{ success: false, error: '服务器错误' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* DELETE - 删除 API Key
|
||
*/
|
||
export async function DELETE(
|
||
request: NextRequest,
|
||
{ params }: { params: { id: string } }
|
||
) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ success: false, error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
|
||
// 检查权限
|
||
const userId = (session.user as any).id;
|
||
if (!(await isSuperAdmin(userId))) {
|
||
return NextResponse.json(
|
||
{ success: false, error: '仅超级管理员可操作' },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
const { id } = params;
|
||
|
||
// 验证 API Key 是否存在
|
||
const apiKey: any = await prisma.apiKey.findUnique({
|
||
where: { id },
|
||
select: { id: true },
|
||
});
|
||
|
||
if (!apiKey) {
|
||
return NextResponse.json(
|
||
{ success: false, error: 'API Key 不存在' },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
|
||
// 删除 API Key(级联删除相关日志)
|
||
await prisma.apiKey.delete({
|
||
where: { id },
|
||
});
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: 'API Key 已删除',
|
||
});
|
||
} catch (error) {
|
||
console.error('[admin/api-keys/[id]] DELETE error:', error);
|
||
return NextResponse.json(
|
||
{ success: false, error: '服务器错误' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|