import React, { useState } from 'react'; import { useQuery, useMutation } from '@tanstack/react-query'; import { Plus, Edit, Trash2, Search } from 'lucide-react'; import { format } from 'date-fns'; import { Input } from '@d8d/shared-ui-components/components/ui/input'; import { Textarea } from '@d8d/shared-ui-components/components/ui/textarea'; import { Button } from '@d8d/shared-ui-components/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@d8d/shared-ui-components/components/ui/card'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@d8d/shared-ui-components/components/ui/table'; import { Skeleton } from '@d8d/shared-ui-components/components/ui/skeleton'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@d8d/shared-ui-components/components/ui/dialog'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@d8d/shared-ui-components/components/ui/form'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { toast } from 'sonner'; import { DataTablePagination } from '@d8d/shared-ui-components/components/admin/DataTablePagination'; import { systemConfigClientManager } from '../api/systemConfigClient'; import { CreateSystemConfigSchema, UpdateSystemConfigSchema } from '@d8d/core-module-mt/system-config-module-mt/schemas'; import type { SystemConfigFormData, SystemConfigSearchParams, CreateSystemConfigRequest, UpdateSystemConfigRequest, SystemConfigResponse } from '../types'; type CreateRequest = CreateSystemConfigRequest; type UpdateRequest = UpdateSystemConfigRequest; const createFormSchema = CreateSystemConfigSchema; const updateFormSchema = UpdateSystemConfigSchema; export const SystemConfigManagement: React.FC = () => { const [searchParams, setSearchParams] = useState({ page: 1, limit: 10, search: '' }); const [isModalOpen, setIsModalOpen] = useState(false); const [editingSystemConfig, setEditingSystemConfig] = useState(null); const [isCreateForm, setIsCreateForm] = useState(true); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [systemConfigToDelete, setSystemConfigToDelete] = useState(null); // 表单实例 const createForm = useForm({ resolver: zodResolver(createFormSchema), defaultValues: { configKey: '', configValue: '', description: '' } }); const updateForm = useForm({ resolver: zodResolver(updateFormSchema), defaultValues: {} }); // 数据查询 const { data, isLoading, refetch } = useQuery({ queryKey: ['system-configs', searchParams], queryFn: async () => { const res = await systemConfigClientManager.get().index.$get({ query: { page: searchParams.page, pageSize: searchParams.limit, keyword: searchParams.search } }); if (res.status !== 200) throw new Error('获取系统配置列表失败'); return await res.json(); } }); // 创建系统配置 const createMutation = useMutation({ mutationFn: async (data: CreateRequest) => { const res = await systemConfigClientManager.get().index.$post({ json: data }); if (res.status !== 201) throw new Error('创建系统配置失败'); return await res.json(); }, onSuccess: () => { toast.success('系统配置创建成功'); setIsModalOpen(false); createForm.reset(); refetch(); }, onError: (error) => { toast.error(error instanceof Error ? error.message : '创建系统配置失败'); } }); // 更新系统配置 const updateMutation = useMutation({ mutationFn: async ({ id, data }: { id: number; data: UpdateRequest }) => { const res = await systemConfigClientManager.get()[':id']['$put']({ param: { id }, json: data }); if (res.status !== 200) throw new Error('更新系统配置失败'); return await res.json(); }, onSuccess: () => { toast.success('系统配置更新成功'); setIsModalOpen(false); setEditingSystemConfig(null); refetch(); }, onError: (error) => { toast.error(error instanceof Error ? error.message : '更新系统配置失败'); } }); // 删除系统配置 const deleteMutation = useMutation({ mutationFn: async (id: number) => { const res = await systemConfigClientManager.get()[':id']['$delete']({ param: { id } }); if (res.status !== 204) throw new Error('删除系统配置失败'); return await res.json(); }, onSuccess: () => { toast.success('系统配置删除成功'); setDeleteDialogOpen(false); setSystemConfigToDelete(null); refetch(); }, onError: (error) => { toast.error(error instanceof Error ? error.message : '删除系统配置失败'); } }); // 处理搜索 const handleSearch = (e: React.FormEvent) => { e.preventDefault(); setSearchParams((prev: SystemConfigSearchParams) => ({ ...prev, page: 1 })); refetch(); }; // 处理创建系统配置 const handleCreateSystemConfig = () => { setIsCreateForm(true); setEditingSystemConfig(null); createForm.reset(); setIsModalOpen(true); }; // 处理编辑系统配置 const handleEditSystemConfig = (systemConfig: SystemConfigResponse) => { setIsCreateForm(false); setEditingSystemConfig(systemConfig); updateForm.reset({ configKey: systemConfig.configKey || undefined, configValue: systemConfig.configValue || undefined, description: systemConfig.description || undefined }); setIsModalOpen(true); }; // 处理删除系统配置 const handleDeleteSystemConfig = (id: number) => { setSystemConfigToDelete(id); setDeleteDialogOpen(true); }; // 确认删除 const confirmDelete = () => { if (systemConfigToDelete) { deleteMutation.mutate(systemConfigToDelete); } }; // 处理创建表单提交 const handleCreateSubmit = async (data: SystemConfigFormData) => { try { await createMutation.mutateAsync(data); } catch (error) { throw error; } }; // 处理编辑表单提交 const handleUpdateSubmit = async (data: any) => { if (!editingSystemConfig) return; try { await updateMutation.mutateAsync({ id: editingSystemConfig.id, data }); } catch (error) { throw error; } }; return (

系统配置管理

系统配置列表 管理系统所有配置项,包括小程序配置、支付参数等
setSearchParams((prev: SystemConfigSearchParams) => ({ ...prev, search: e.target.value }))} className="pl-8" data-testid="search-input" />
ID 配置键 配置值 描述 创建时间 更新时间 操作 {isLoading ? ( Array.from({ length: 5 }).map((_, index) => (
)) ) : data?.data && data.data.length > 0 ? ( data.data.map((systemConfig) => ( {systemConfig.id} {systemConfig.configKey}
{systemConfig.configValue}
{systemConfig.description || '-'} {systemConfig.createdAt ? format(new Date(systemConfig.createdAt), 'yyyy-MM-dd HH:mm') : '-'} {systemConfig.updatedAt ? format(new Date(systemConfig.updatedAt), 'yyyy-MM-dd HH:mm') : '-'}
)) ) : (

暂无系统配置数据

)}
setSearchParams((prev: SystemConfigSearchParams) => ({ ...prev, page, limit }))} />
{/* 创建/编辑对话框 */} {isCreateForm ? '创建系统配置' : '编辑系统配置'} {isCreateForm ? '创建一个新的系统配置项' : '编辑现有系统配置信息'} {isCreateForm ? ( // 创建表单(独立渲染)
( 配置键 * 配置项的唯一标识,使用点分隔符命名,如:app.login.enabled )} /> ( 配置值 *