121 lines
3.4 KiB
TypeScript
121 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
const baseSelect = {
|
|
id: true,
|
|
name: true,
|
|
description: true,
|
|
status: true,
|
|
version: true,
|
|
fieldGroups: true,
|
|
displayConfig: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
fields: {
|
|
orderBy: { orderIndex: 'asc' },
|
|
select: {
|
|
id: true,
|
|
key: true,
|
|
label: true,
|
|
type: true,
|
|
required: true,
|
|
orderIndex: true,
|
|
layoutWidth: true,
|
|
validations: true,
|
|
optionsSource: true,
|
|
optionsStatic: true,
|
|
allowedExtensions: true,
|
|
visibleOnHomeFilter: true,
|
|
isSearchable: true,
|
|
isReadOnly: true,
|
|
conditions: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
},
|
|
} as const
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: '无权限' }, { status: 403 })
|
|
}
|
|
|
|
const templates = await prisma.uploadTemplate.findMany({
|
|
orderBy: { updatedAt: 'desc' },
|
|
select: baseSelect,
|
|
})
|
|
|
|
return NextResponse.json(templates)
|
|
} catch (error) {
|
|
console.error('List upload templates error:', error)
|
|
return NextResponse.json({ error: '获取上传模板失败' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: '无权限' }, { status: 403 })
|
|
}
|
|
|
|
const body = await req.json()
|
|
const {
|
|
name,
|
|
description,
|
|
status = 'draft',
|
|
fieldGroups,
|
|
displayConfig,
|
|
fields = [],
|
|
} = body || {}
|
|
|
|
if (!name) {
|
|
return NextResponse.json({ error: '模板名称不能为空' }, { status: 400 })
|
|
}
|
|
|
|
const formattedFields = Array.isArray(fields)
|
|
? fields.map((field: any, index: number) => ({
|
|
key: String(field.key || '').trim(),
|
|
label: String(field.label || '').trim() || `字段${index + 1}`,
|
|
type: String(field.type || 'text'),
|
|
required: !!field.required,
|
|
orderIndex: field.orderIndex ?? index,
|
|
layoutWidth: field.layoutWidth || 'full',
|
|
validations: field.validations ?? null,
|
|
optionsSource: field.optionsSource ?? null,
|
|
optionsStatic: field.optionsStatic ?? null,
|
|
allowedExtensions: field.allowedExtensions ?? null,
|
|
visibleOnHomeFilter: !!field.visibleOnHomeFilter,
|
|
isSearchable: !!field.isSearchable,
|
|
isReadOnly: !!field.isReadOnly,
|
|
conditions: field.conditions ?? null,
|
|
}))
|
|
: []
|
|
|
|
const template = await prisma.uploadTemplate.create({
|
|
data: {
|
|
name,
|
|
description: description ?? null,
|
|
status,
|
|
fieldGroups: fieldGroups ?? null,
|
|
displayConfig: displayConfig ?? null,
|
|
fields: formattedFields.length
|
|
? { createMany: { data: formattedFields } }
|
|
: undefined,
|
|
},
|
|
select: baseSelect,
|
|
})
|
|
|
|
return NextResponse.json(template, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Create upload template error:', error)
|
|
return NextResponse.json({ error: '创建上传模板失败' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
|