76 lines
2.1 KiB
TypeScript

/**
* API 调用日志接口
* GET - 获取调用日志列表
*/
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { isSuperAdmin, hasSystemAdminPermission } from '@/lib/department-permission';
import { getApiCallLogs } from '@/lib/external-api';
export async function GET(request: NextRequest) {
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;
const userRole = (session.user as any).role;
const isSuperAdminUser = await isSuperAdmin(userId);
const hasPermission = hasSystemAdminPermission(userRole, isSuperAdminUser);
if (!hasPermission) {
return NextResponse.json(
{ success: false, error: '无权限访问' },
{ status: 403 }
);
}
const { searchParams } = new URL(request.url);
const apiKeyId = searchParams.get('apiKeyId') || undefined;
const taskNo = searchParams.get('taskNo') || undefined;
const startDate = searchParams.get('startDate');
const endDate = searchParams.get('endDate');
const limit = parseInt(searchParams.get('limit') || '50');
const offset = parseInt(searchParams.get('offset') || '0');
const options: any = {
apiKeyId,
taskNo,
limit,
offset,
};
if (startDate) {
options.startDate = new Date(startDate);
}
if (endDate) {
options.endDate = new Date(endDate);
}
const result = await getApiCallLogs(options);
return NextResponse.json({
success: true,
data: {
logs: result.logs,
total: result.total,
limit,
offset,
},
});
} catch (error) {
console.error('[admin/api-logs] GET error:', error);
return NextResponse.json(
{ success: false, error: '服务器错误' },
{ status: 500 }
);
}
}