Some checks failed
CI Check / check (push) Has been cancelled
- 创建统一的Modal组件,采用Apple风格设计 - 实现毛玻璃效果、圆角、动画和细边框描边 - 修复弹窗打开时背景滚动问题 - 修复分类管理中重复卡片和数组字符串显示问题 - 优化选项解析逻辑,添加去重和过滤机制
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
/**
|
||
* 公共上传模板 API
|
||
*
|
||
* 获取启用的上传模板列表(不需要管理员权限)
|
||
* 包括预制模板和自定义模板,并做区分
|
||
* 用于上传页面选择模板
|
||
*/
|
||
|
||
import { NextResponse } from 'next/server'
|
||
import { prisma } from '@/lib/prisma'
|
||
import {
|
||
BUILTIN_TEMPLATE_KEYS,
|
||
BUILTIN_TEMPLATE_TYPES,
|
||
BUILTIN_TEMPLATE_NAMES,
|
||
BUILTIN_TEMPLATE_DESCRIPTIONS,
|
||
STANDARD_FIELDS,
|
||
BUILTIN_TEMPLATE_FIELDS,
|
||
createBuiltinTemplate,
|
||
} from '@/lib/upload-template-presets'
|
||
|
||
const publicSelect = {
|
||
id: true,
|
||
name: true,
|
||
description: true,
|
||
status: true,
|
||
materialType: true,
|
||
fields: {
|
||
where: {
|
||
// 只返回非只读字段,只读字段不需要用户填写
|
||
// 自定义模板中过滤掉 materialType 字段
|
||
isReadOnly: false,
|
||
key: { not: 'materialType' },
|
||
},
|
||
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,
|
||
addToCategory: true,
|
||
conditions: true,
|
||
},
|
||
},
|
||
} as const
|
||
|
||
export async function GET() {
|
||
try {
|
||
// 获取启用的预制模板列表
|
||
let enabledBuiltinTemplates: string[] = []
|
||
try {
|
||
// 使用原始 SQL 查询,避免字段不存在时的错误
|
||
const settings = await prisma.$queryRaw<Array<{ enabledBuiltinTemplates?: string | null }>>`
|
||
SELECT "enabledBuiltinTemplates" FROM "system_settings" LIMIT 1
|
||
`
|
||
if (settings[0]?.enabledBuiltinTemplates) {
|
||
const parsed = JSON.parse(settings[0].enabledBuiltinTemplates)
|
||
if (Array.isArray(parsed) && parsed.length > 0) {
|
||
enabledBuiltinTemplates = parsed
|
||
}
|
||
// 如果为空数组或null,表示全部启用,enabledBuiltinTemplates保持为空数组
|
||
}
|
||
// 如果字段不存在或为null,表示全部启用,enabledBuiltinTemplates保持为空数组
|
||
} catch (error) {
|
||
// 字段不存在或解析失败,表示全部启用
|
||
console.error('Failed to parse enabledBuiltinTemplates:', error)
|
||
}
|
||
|
||
// 获取所有启用的自定义模板
|
||
const customTemplates = await prisma.uploadTemplate.findMany({
|
||
where: {
|
||
status: 'active',
|
||
},
|
||
orderBy: { updatedAt: 'desc' },
|
||
select: publicSelect,
|
||
})
|
||
|
||
// 生成所有预制模板,只包含启用的
|
||
// 如果 enabledBuiltinTemplates 为空数组,表示全部启用
|
||
const builtinTemplates = BUILTIN_TEMPLATE_KEYS
|
||
.filter(key => enabledBuiltinTemplates.length === 0 || enabledBuiltinTemplates.includes(key))
|
||
.map(key => createBuiltinTemplate(key))
|
||
|
||
// 合并预制模板和自定义模板,预制模板在前
|
||
const allTemplates = [
|
||
...builtinTemplates.map(t => ({ ...t, isBuiltin: true })),
|
||
...customTemplates.map(t => ({ ...t, isBuiltin: false })),
|
||
]
|
||
|
||
return NextResponse.json(allTemplates)
|
||
} catch (error) {
|
||
console.error('Get active upload templates error:', error)
|
||
return NextResponse.json({ error: '获取上传模板失败' }, { status: 500 })
|
||
}
|
||
}
|
||
|