138 lines
3.5 KiB
TypeScript
138 lines
3.5 KiB
TypeScript
/**
|
||
* 系统配置API
|
||
* 优先 Prisma,失败时回退到 lib/db(避免 Prisma 连接池超时导致 500)
|
||
*/
|
||
|
||
import { NextRequest, NextResponse } from 'next/server'
|
||
import { prisma } from '@/lib/prisma'
|
||
import { getPrismaConfig, setPrismaConfig } from '@/lib/prisma-helpers'
|
||
import { query } from '@/lib/db'
|
||
|
||
/**
|
||
* GET - 获取配置
|
||
*/
|
||
export async function GET(request: NextRequest) {
|
||
const { searchParams } = new URL(request.url)
|
||
const key = searchParams.get('key')
|
||
|
||
try {
|
||
if (key) {
|
||
let config = await getPrismaConfig(key)
|
||
if (config == null) {
|
||
const rows = await query(
|
||
'SELECT config_value FROM system_config WHERE config_key = ?',
|
||
[key]
|
||
) as any[]
|
||
config = rows[0]?.config_value ?? null
|
||
}
|
||
return NextResponse.json({
|
||
success: true,
|
||
data: config
|
||
})
|
||
}
|
||
|
||
let configs: any[]
|
||
try {
|
||
configs = await prisma.system_config.findMany({
|
||
orderBy: { config_key: 'asc' }
|
||
})
|
||
} catch (e) {
|
||
const rows = await query(
|
||
'SELECT id, config_key, config_value, description, created_at, updated_at FROM system_config ORDER BY config_key ASC'
|
||
) as any[]
|
||
configs = rows || []
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
data: configs
|
||
})
|
||
} catch (error) {
|
||
console.error('[Config API] GET错误:', error)
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '获取配置失败'
|
||
}, { status: 500 })
|
||
}
|
||
}
|
||
|
||
/**
|
||
* POST - 创建/更新配置
|
||
*/
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
const body = await request.json()
|
||
const { key, value, description } = body
|
||
|
||
if (!key) {
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '配置键不能为空'
|
||
}, { status: 400 })
|
||
}
|
||
|
||
const valueStr = typeof value === 'string' ? value : JSON.stringify(value ?? null)
|
||
try {
|
||
await setPrismaConfig(key, value, description)
|
||
} catch (e) {
|
||
await query(
|
||
`INSERT INTO system_config (config_key, config_value, description, updated_at) VALUES (?, ?, ?, NOW())
|
||
ON DUPLICATE KEY UPDATE config_value = ?, description = ?, updated_at = NOW()`,
|
||
[key, valueStr, description || null, valueStr, description || null]
|
||
)
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: '配置保存成功'
|
||
})
|
||
} catch (error) {
|
||
console.error('[Config API] POST错误:', error)
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '保存配置失败'
|
||
}, { status: 500 })
|
||
}
|
||
}
|
||
|
||
/**
|
||
* DELETE - 删除配置
|
||
*/
|
||
export async function DELETE(request: NextRequest) {
|
||
try {
|
||
const { searchParams } = new URL(request.url)
|
||
const key = searchParams.get('key')
|
||
|
||
if (!key) {
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '配置键不能为空'
|
||
}, { status: 400 })
|
||
}
|
||
|
||
try {
|
||
await prisma.system_config.delete({
|
||
where: { config_key: key }
|
||
})
|
||
} catch (e: any) {
|
||
if (e.code === 'P2025') {
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '配置不存在'
|
||
}, { status: 404 })
|
||
}
|
||
await query('DELETE FROM system_config WHERE config_key = ?', [key])
|
||
}
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: '配置删除成功'
|
||
})
|
||
} catch (error: any) {
|
||
console.error('[Config API] DELETE错误:', error)
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: '删除配置失败'
|
||
}, { status: 500 })
|
||
}
|
||
}
|