30 lines
1.2 KiB
TypeScript
30 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { isSuperAdmin } from '@/lib/department-permission'
|
|
import { getPublicTrafficStats, normalizeTrafficRange } from '@/lib/public-traffic-stats'
|
|
|
|
export const runtime = 'nodejs'
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const userId = (session?.user as any)?.id
|
|
|
|
if (!session || !userId || !(await isSuperAdmin(userId))) {
|
|
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const range = normalizeTrafficRange(searchParams.get('start'), searchParams.get('end'))
|
|
const forceRefresh = searchParams.get('refresh') === '1' || searchParams.get('forceRefresh') === '1'
|
|
const stats = await getPublicTrafficStats(range, { forceRefresh })
|
|
|
|
return NextResponse.json(stats)
|
|
} catch (error) {
|
|
console.error('[admin/public-traffic] GET error:', error)
|
|
return NextResponse.json({ error: '获取公网流量统计失败' }, { status: 500 })
|
|
}
|
|
}
|