'use client'; import { useEffect, useMemo, useState } from 'react'; import { FileText, Plus, RefreshCw, Search, Download, Upload, SlidersHorizontal, Layers, CheckCircle2, X, } from 'lucide-react'; import { toast } from 'react-hot-toast'; import { retroConfirm } from '@/lib/dialog'; import ConfigurationTable from '@/components/admin/configurations/ConfigurationTable'; import ConfigurationModal from '@/components/admin/configurations/ConfigurationModal'; import ConfigurationDetailDrawer from '@/components/admin/configurations/ConfigurationDetailDrawer'; import ConfigurationExportModal from '@/components/admin/configurations/ConfigurationExportModal'; import ConfigurationImportModal from '@/components/admin/configurations/ConfigurationImportModal'; import { DynamicConfiguration } from '@/types'; import { configService } from '@/lib/services'; function normalizeConfig(item: Partial): DynamicConfiguration { let val = ''; if (item.config_value !== null && item.config_value !== undefined) { if (typeof item.config_value === 'string') { val = item.config_value; } else { try { val = JSON.stringify(item.config_value, null, 2); } catch { val = String(item.config_value); } } } return { id: item.id || 0, config_key: String(item.config_key || ''), config_value: val, description: String(item.description || ''), type: String(item.type || ''), created_at: item.created_at, updated_at: item.updated_at, }; } export default function AdminConfigurationsPage() { const [configs, setConfigs] = useState([]); const [loading, setLoading] = useState(true); const [keyword, setKeyword] = useState(''); const [selectedType, setSelectedType] = useState('all'); const [keyLookup, setKeyLookup] = useState(''); const [isModalOpen, setModalOpen] = useState(false); const [isDrawerOpen, setDrawerOpen] = useState(false); const [isExportModalOpen, setExportModalOpen] = useState(false); const [isImportModalOpen, setImportModalOpen] = useState(false); const [activeConfig, setActiveConfig] = useState(null); const fetchConfigs = async () => { setLoading(true); try { const res = await configService.getAll(); const list = res?.data || []; setConfigs(Array.isArray(list) ? list.map(normalizeConfig) : []); } catch (e: any) { toast.error('读取配置失败: ' + (e.response?.data?.message || e.message || '未知错误')); setConfigs([]); } finally { setLoading(false); } }; useEffect(() => { fetchConfigs(); }, []); const handleLookupByKey = async () => { const targetKey = keyLookup.trim(); if (!targetKey) { toast.error('请输入要查询的配置 Key'); return; } setLoading(true); try { const res = await configService.getByKey(targetKey); const item = res?.data; if (item) { setConfigs([normalizeConfig(item)]); toast.success(`找到配置 "${targetKey}"`); } else { toast.error(`未找到 Key 为 "${targetKey}" 的配置`); setConfigs([]); } } catch (e: any) { toast.error('查询失败: ' + (e.response?.data?.message || e.message || '未知错误')); } finally { setLoading(false); } }; const handleResetSearch = () => { setKeyword(''); setSelectedType('all'); setKeyLookup(''); fetchConfigs(); }; const filteredConfigs = useMemo(() => { const q = keyword.trim().toLowerCase(); return configs.filter((c) => { const configKey = String(c.config_key || '').toLowerCase(); const desc = String(c.description || '').toLowerCase(); const configVal = String(c.config_value || '').toLowerCase(); const configType = String(c.type || '').toLowerCase(); const matchKeyword = !q || configKey.includes(q) || desc.includes(q) || configVal.includes(q) || configType.includes(q); const valTrimmed = String(c.config_value || '').trim(); const isJson = valTrimmed.startsWith('{') || valTrimmed.startsWith('['); const computedType = (c.type || (isJson ? 'json' : 'string')).toLowerCase(); const matchType = selectedType === 'all' || (selectedType === 'json' && (computedType === 'json' || isJson)) || (selectedType === 'string' && (computedType === 'string' || computedType === 'text')) || (selectedType === 'boolean' && computedType === 'boolean') || (selectedType === 'number' && computedType === 'number'); return matchKeyword && matchType; }); }, [configs, keyword, selectedType]); const stats = useMemo(() => { const total = configs.length; let jsonCount = 0; let stringCount = 0; configs.forEach((c) => { const valTrimmed = String(c.config_value || '').trim(); const isJson = valTrimmed.startsWith('{') || valTrimmed.startsWith('['); if (isJson || (c.type && c.type.toLowerCase() === 'json')) { jsonCount++; } else { stringCount++; } }); return { total, jsonCount, stringCount }; }, [configs]); const handleOpenCreate = () => { setActiveConfig(null); setModalOpen(true); }; const handleOpenEdit = (config: DynamicConfiguration) => { setActiveConfig(config); setModalOpen(true); }; const handleOpenDetails = (config: DynamicConfiguration) => { setActiveConfig(config); setDrawerOpen(true); }; const handleDelete = async (config: DynamicConfiguration) => { const ok = await retroConfirm({ title: '[ CONFIRM // 永久删除配置项 ]', message: `警告:确定要永久删除配置项 "${config.config_key}" 吗?此操作不可撤销!`, type: 'danger', confirmText: '[ 确认删除 ]', }); if (!ok) return; try { await configService.delete(config.config_key); toast.success(`配置 "${config.config_key}" 已删除`); fetchConfigs(); } catch (e: any) { toast.error('删除失败: ' + (e.response?.data?.message || e.message || '未知错误')); } }; return (
{/* 顶部标头 */}
[ DYNAMIC-CONFIGS // 系统变量热加载 ]

动态系统配置与环境变量中枢

热更新全局汇率、通知网关秘钥、业务开关及限流规则,无需重启服务器即时生效。

{/* 顶部右侧主要操作 */}
{/* 2. 统计指标卡片 */}
[ 全局配置项总数 ]
{stats.total} 个
热加载系统环境
[ JSON 结构化变量 ]
{stats.jsonCount} 个
包含复杂对象/列表结构
[ 基础标量参数 ]
{stats.stringCount} 个
字符串 / 数值 / 开关
{/* 3. 搜索与筛选工具栏 */}
setKeyword(e.target.value)} />
{keyword && ( )} {keyword && ( {filteredConfigs.length} 项 )}
{/* 按 Key 直达查询 */}
[ KEY 直达查询 ]: setKeyLookup(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleLookupByKey()} className="flex-1 border-2 border-black bg-neutral-50 px-3 py-1.5 text-xs font-mono focus:bg-white outline-none shadow-[1px_1px_0px_#000] text-black w-full" /> {(keyword || selectedType !== 'all' || keyLookup) && ( )}
{/* 4. 配置数据表格 */} {/* 5. 新建 / 编辑弹窗 */} setModalOpen(false)} config={activeConfig} onSuccess={fetchConfigs} /> {/* 6. 详情查阅抽屉 */} setDrawerOpen(false)} config={activeConfig} onEdit={(cfg) => { setDrawerOpen(false); handleOpenEdit(cfg); }} onDelete={handleDelete} /> {/* 7. 导出配置弹窗 */} setExportModalOpen(false)} configs={configs} /> {/* 8. 导入配置弹窗 */} setImportModalOpen(false)} existingKeys={configs.map((c) => c.config_key)} onSuccess={fetchConfigs} />
); }