Some checks failed
CI Check / check (push) Has been cancelled
- 创建统一的Modal组件,采用Apple风格设计 - 实现毛玻璃效果、圆角、动画和细边框描边 - 修复弹窗打开时背景滚动问题 - 修复分类管理中重复卡片和数组字符串显示问题 - 优化选项解析逻辑,添加去重和过滤机制
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
/**
|
|
* 分类字段配置 API
|
|
*
|
|
* 支持查询「哪些字段可用于分类管理」以及启用/停用指定字段
|
|
* 这样管理字段无需回到模板编辑器逐一勾选。
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import {
|
|
getAvailableCategoryFields,
|
|
getManagedCategoryFields,
|
|
saveManagedCategoryFields,
|
|
} from '@/lib/category-fields'
|
|
|
|
export async function GET() {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: '权限不足' }, { status: 403 })
|
|
}
|
|
|
|
const [managedFields, availableFields] = await Promise.all([
|
|
getManagedCategoryFields(),
|
|
getAvailableCategoryFields(),
|
|
])
|
|
|
|
return NextResponse.json({
|
|
managedFields,
|
|
availableFields,
|
|
})
|
|
}
|
|
|
|
export async function PATCH(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: '权限不足' }, { status: 403 })
|
|
}
|
|
|
|
const { fieldKey, enabled } = await req.json()
|
|
if (typeof fieldKey !== 'string' || typeof enabled !== 'boolean') {
|
|
return NextResponse.json(
|
|
{ error: '参数错误' },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
const availableFields = await getAvailableCategoryFields()
|
|
if (!availableFields.find((field) => field.key === fieldKey)) {
|
|
return NextResponse.json(
|
|
{ error: '未找到该字段' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
|
|
const current = new Set(await getManagedCategoryFields())
|
|
if (enabled) {
|
|
current.add(fieldKey)
|
|
} else {
|
|
current.delete(fieldKey)
|
|
}
|
|
|
|
const updatedManagedFields = await saveManagedCategoryFields(Array.from(current))
|
|
|
|
await prisma.uploadField.updateMany({
|
|
where: {
|
|
key: fieldKey,
|
|
type: 'select',
|
|
},
|
|
data: {
|
|
addToCategory: enabled,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({
|
|
managedFields: updatedManagedFields,
|
|
})
|
|
} catch (error) {
|
|
console.error('Update category fields failed:', error)
|
|
return NextResponse.json(
|
|
{ error: '更新失败' },
|
|
{ status: 500 },
|
|
)
|
|
}
|
|
}
|
|
|