diff --git a/app/admin/common/admin-panel-config.tsx b/app/admin/common/admin-panel-config.tsx index 2d4ab12..32f711c 100644 --- a/app/admin/common/admin-panel-config.tsx +++ b/app/admin/common/admin-panel-config.tsx @@ -419,7 +419,7 @@ export const defaultAdminPanelConfig: AdminPanelConfig = { autoSave: true, autoSaveDelay: 3000, validateOnChange: true, - validateOnSubmit: true, + validateOnSubmit: false, // 主题设置 theme: { diff --git a/app/admin/common/dynamic-admin-config-form.tsx b/app/admin/common/dynamic-admin-config-form.tsx new file mode 100644 index 0000000..501c63b --- /dev/null +++ b/app/admin/common/dynamic-admin-config-form.tsx @@ -0,0 +1,338 @@ +"use client"; + +import React, { useState } from "react"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { z } from "zod"; +import { + Settings, + Globe, + Palette, + Shield, + Users, + Database, + Mail, + FileText, + Server, + HardDrive, + Lock, + User, + ToggleLeft, + RefreshCw, + Download, + Upload, + Save, + CheckCircle, + AlertCircle +} from "lucide-react"; +import { SiteOpsConfigType } from "@/types/site-config"; +import { Button } from "@/components/ui/button"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { toast } from "sonner"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; +import { Switch } from "@/components/ui/switch"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; + +// 表单验证模式 +const configFormSchema = z.object({ + 'site.name': z.string().min(2, "网站名称至少需要2个字符").max(50, "网站名称不能超过50个字符"), + 'site.description': z.string().optional(), + 'site.keywords': z.string().optional(), + 'site.url': z.string() + .refine((url) => { + if (!url || url === "") return true; // 允许空值 + return url.startsWith("http://") || url.startsWith("https://"); + }, "无效的URL格式,必须以http://或https://开头") + .optional().or(z.literal("")), + 'site.logo': z.string().optional(), + 'site.color_style': z.enum(["light", "dark"]), + 'user.default_role': z.enum(["user", "vip", "admin"]), + 'user.register_invite_code': z.boolean(), + 'user.register_email_verification': z.boolean(), + 'switch.open_register': z.boolean(), + 'switch.open_comment': z.boolean(), +}); + +type ConfigFormValues = z.infer; + +// 配置管理表单组件 +function DynamicAdminConfigForm({ + data, + onSave, + onExport, + onImport, + loading = false +}: { + data?: SiteOpsConfigType; + onSave: (values: ConfigFormValues) => Promise; + onExport?: () => Promise; + onImport?: (file: File) => Promise; + loading?: boolean; +}) { + const [saveStatus, setSaveStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle'); + const [errorMessage, setErrorMessage] = useState(''); + + // 初始化表单 + const form = useForm({ + resolver: zodResolver(configFormSchema), + defaultValues: { + 'site.name': data?.site?.info?.name || "MMAP System", + 'site.description': "", + 'site.keywords': "", + 'site.url': "/", + 'site.logo': data?.site?.brand?.logo_url || "/images/logo.png", + 'site.color_style': (data?.site?.brand?.dark_mode_default ? 'dark' : 'light') as "light" | "dark", + 'user.default_role': 'user', + 'user.register_invite_code': data?.ops?.features?.invite_code_required ?? false, + 'user.register_email_verification': data?.ops?.features?.email_verification ?? false, + 'switch.open_register': data?.ops?.features?.registration_enabled ?? true, + 'switch.open_comment': true, + } + }); + + // 处理表单提交 + const onSubmit = async (values: ConfigFormValues) => { + setSaveStatus('loading'); + setErrorMessage(''); + + try { + await onSave(values); + setSaveStatus('success'); + toast.success("配置保存成功!"); + setTimeout(() => setSaveStatus('idle'), 3000); + } catch (error) { + setSaveStatus('error'); + setErrorMessage('保存失败,请重试'); + toast.error("配置保存失败,请重试"); + } + }; + + return ( +
+ {/* 头部 */} +
+
+
+

系统配置管理

+

管理站点配置、运营设置和系统参数

+
+
+ + + +
+
+
+ + {/* 主要内容 */} +
+
+
+ + + + 内容配置 + 用户管理 + 邮件配置 + 系统配置 + + + {/* 内容配置标签页 */} + + + + + + 站点信息 + +

网站基本信息和设置

+
+ + ( + + 网站名称 + + + + 显示在浏览器标题栏的网站名称 + + + )} + /> + ( + + 网站描述 + +