import React, { useState, useEffect } from 'react'; import { Form, Input, Switch, Button, Card, Space, message, Modal, Tag, Divider, Descriptions, Alert, Row, Col, Typography, Badge, Tooltip, Avatar, ColorPicker, InputNumber, } from 'antd'; import { SettingOutlined, GlobalOutlined, BgColorsOutlined, InfoCircleOutlined, ExclamationCircleOutlined, LockOutlined, DatabaseOutlined, CloudServerOutlined, MailOutlined, PhoneOutlined, EnvironmentOutlined, FileTextOutlined, SafetyCertificateOutlined, ReloadOutlined, SaveOutlined, EyeOutlined, MenuFoldOutlined, MenuUnfoldOutlined, } from '@ant-design/icons'; import axios from 'axios'; import { useConfig } from '../hooks/useConfig'; const { Title, Text, Paragraph } = Typography; /** * 系统设置页面 * 3个Tab:基本设置、外观设置、关于系统 */ const SystemSettings = () => { const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [settings, setSettings] = useState({}); const [activeMenu, setActiveMenu] = useState('general'); const [systemInfo, setSystemInfo] = useState(null); const [form] = Form.useForm(); const [isMobile, setIsMobile] = useState(window.innerWidth < 768); const { reloadConfig } = useConfig(); useEffect(() => { fetchSettings(); fetchSystemInfo(); const handleResize = () => { setIsMobile(window.innerWidth < 768); }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); /** 获取所有系统设置 */ const fetchSettings = async () => { setLoading(true); try { const response = await axios.get('/api/system-settings'); setSettings(response.data); const formValues = {}; Object.entries(response.data).forEach(([key, data]) => { formValues[key] = data.value; }); form.setFieldsValue(formValues); } catch (error) { message.error('获取设置失败'); } finally { setLoading(false); } }; /** 获取系统运行信息 */ const fetchSystemInfo = async () => { try { const response = await axios.get('/api/system-settings/system/info'); setSystemInfo(response.data); } catch (error) { console.error('获取系统信息失败'); } }; /** 保存设置 */ const handleSaveSettings = async values => { setSaving(true); try { const updates = {}; Object.entries(values).forEach(([key, value]) => { if (settings[key] && settings[key].isEditable) { updates[key] = value; } }); await axios.put('/api/system-settings', { settings: updates }); message.success('设置保存成功'); fetchSettings(); await reloadConfig(); } catch (error) { message.error('保存设置失败'); } finally { setSaving(false); } }; /** 预览Logo */ const handlePreviewLogo = () => { const logoUrl = form.getFieldValue('site_logo'); if (!logoUrl) { message.warning('请先输入Logo URL'); return; } Modal.info({ title: 'Logo 预览', content: (
Logo预览 { e.target.style.display = 'none'; e.target.parentNode.innerHTML = '
图片加载失败,请检查URL是否正确
'; }} />
), okText: '关闭', width: 480, }); }; // 预设颜色 const presetColors = [ '#667eea', '#764ba2', '#f093fb', '#4facfe', '#43e97b', '#fa709a', '#fee140', '#00b4db', '#0083b0', '#fcb045', '#1890ff', '#52c41a', '#eb2f96', '#722ed1', '#13c2c2', '#fa8c16', ]; /** 颜色变更处理 */ const handleColorChange = (color, key) => { const hexColor = typeof color === 'string' ? color : color.toHexString(); form.setFieldValue(key, hexColor); const root = document.documentElement; if (key === 'primary_color') { root.style.setProperty('--primary-color', hexColor); root.style.setProperty('--primary-light', `${hexColor}20`); } else if (key === 'secondary_color') { root.style.setProperty('--secondary-color', hexColor); root.style.setProperty('--secondary-light', `${hexColor}20`); } }; /** 渲染基本设置Tab */ const renderGeneralSettings = () => { const maintenanceMode = form.getFieldValue('maintenance_mode'); return (
{/* 站点信息卡片 */}
站点信息
配置系统基本信息和站点标识
网站名称} name="site_name" style={{ marginBottom: 0 }} > 网站Logo URL} name="site_logo" style={{ marginBottom: 0 }} > 预览 } />
{/* 安全设置卡片 */}
安全设置
配置登录安全策略和维护模式
{/* 维护模式警告 */} {maintenanceMode && ( )} 空闲超时时间} name="idle_timeout" style={{ marginBottom: 0 }} > 最大登录尝试次数} name="max_login_attempts" style={{ marginBottom: 0 }} > 维护模式} name="maintenance_mode" valuePropName="checked" style={{ marginBottom: 0 }} >
{renderFixedFooter()}
); }; /** 渲染外观设置Tab */ const renderAppearanceSettings = () => { const primaryColor = form.getFieldValue('primary_color') || settings.primary_color?.value || '#667eea'; const secondaryColor = form.getFieldValue('secondary_color') || settings.secondary_color?.value || '#764ba2'; return (
主题颜色
自定义系统主题配色方案
主色调 用于按钮、链接等主要交互元素
handleColorChange(color, 'primary_color')} format="hex" showText presets={[{ label: '推荐配色', colors: presetColors }]} style={{ width: '100%' }} /> handleColorChange(e.target.value, 'primary_color')} placeholder="#667eea" style={{ borderRadius: 8 }} prefix={} />
次色调 用于渐变、悬停效果等辅助元素
handleColorChange(color, 'secondary_color')} format="hex" showText presets={[{ label: '推荐配色', colors: presetColors }]} style={{ width: '100%' }} /> handleColorChange(e.target.value, 'secondary_color')} placeholder="#764ba2" style={{ borderRadius: 8 }} prefix={} />
{/* 实时预览 */}
实时预览 标签样式
{renderFixedFooter()} ); }; /** 渲染关于系统Tab */ const renderAboutPage = () => { return (
机柜管理系统 版本 {settings.app_version?.value || '1.0.0'} | 运行正常} /> 专业的数据中心设备管理平台,提供机房、机柜、设备的全生命周期管理
{systemInfo && (
} size={32} /> 系统统计
{systemInfo.statistics?.devices || 0}
设备总数
{systemInfo.statistics?.racks || 0}
机柜总数
{systemInfo.statistics?.rooms || 0}
机房总数
{systemInfo.statistics?.users || 0}
用户总数
{systemInfo.system?.nodeVersion} {systemInfo.system?.platform} ({systemInfo.system?.arch}) {systemInfo.system?.pid} {systemInfo.system?.uptime ? `${Math.floor(systemInfo.system.uptime / 3600)}小时${Math.floor((systemInfo.system.uptime % 3600) / 60)}分钟` : '-'} {systemInfo.system?.memoryUsage ? `${(systemInfo.system.memoryUsage.heapUsed / 1024 / 1024).toFixed(2)} MB` : '-'} {systemInfo.timestamp ? new Date(systemInfo.timestamp).toLocaleString('zh-CN') : '-'}
)} {/* 公司信息编辑 */}
} size={32} /> 公司信息
} placeholder="请输入公司名称" /> } placeholder="请输入联系邮箱" /> } placeholder="请输入联系电话" /> } placeholder="请输入公司地址" />
); }; /** 底部固定操作栏 */ const renderFixedFooter = () => (
); /** 渲染当前Tab内容 */ const renderContent = () => { switch (activeMenu) { case 'general': return renderGeneralSettings(); case 'appearance': return renderAppearanceSettings(); case 'about': return renderAboutPage(); default: return renderGeneralSettings(); } }; const menuItems = [ { key: 'general', icon: , label: '基本设置', description: '站点信息、安全设置' }, { key: 'appearance', icon: , label: '外观设置', description: '主题颜色' }, { key: 'about', icon: , label: '关于系统', description: '系统信息、版本详情' }, ]; /** 桌面端顶部导航栏 */ const renderTopNav = () => (
{menuItems.map(item => (
setActiveMenu(item.key)} style={{ display: 'flex', alignItems: 'center', padding: '8px 16px', borderRadius: 20, cursor: 'pointer', transition: 'all 0.3s ease', backgroundColor: activeMenu === item.key ? 'rgba(255,255,255,0.25)' : 'transparent', color: '#fff', whiteSpace: 'nowrap', fontSize: 14, fontWeight: activeMenu === item.key ? 600 : 400, }} onMouseEnter={e => { if (activeMenu !== item.key) e.currentTarget.style.backgroundColor = 'rgba(255,255,255,0.1)'; }} onMouseLeave={e => { if (activeMenu !== item.key) e.currentTarget.style.backgroundColor = 'transparent'; }} > {item.icon} {item.label}
))}
); /** 移动端下拉导航 */ const renderMobileNav = () => ( ); return (
{/* 顶部固定导航栏 */}
系统设置 {!isMobile && renderTopNav()} {isMobile && renderMobileNav()}
{/* 主内容区 */}
{renderContent()}
); }; export default SystemSettings;