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: (

{
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 (
);
};
/** 渲染外观设置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 (
);
};
/** 渲染关于系统Tab */
const renderAboutPage = () => {
return (
机柜管理系统
版本 {settings.app_version?.value || '1.0.0'}
|
运行正常} />
专业的数据中心设备管理平台,提供机房、机柜、设备的全生命周期管理
{systemInfo && (
{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') : '-'}
)}
{/* 公司信息编辑 */}
);
};
/** 底部固定操作栏 */
const renderFixedFooter = () => (
} size="large"
style={{ borderRadius: 8, minWidth: 140, height: 44, fontSize: 15, fontWeight: 500 }}
>
保存设置
);
/** 渲染当前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()}
{/* 主内容区 */}
);
};
export default SystemSettings;