import React, { useEffect, useRef, useState } from 'react'; import { fetchAIModels, getSystemSettings, updateLoginCredentials, updateSystemSettings, verifySession, } from '../services/api'; import { SystemSettings } from '../types'; import { Save, Sparkles, Settings as SettingsIcon, Eye, EyeOff, RefreshCw, Database, ChevronDown, Check, LockKeyhole, UserRound, ShieldCheck } from 'lucide-react'; const DEFAULT_AI_API_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1'; const LOG_LEVELS = [ { value: 'debug', label: 'Debug' }, { value: 'info', label: 'Info' }, { value: 'warn', label: 'Warn' }, { value: 'error', label: 'Error' }, ]; const SETTINGS_SAVE_OMIT_KEYS = new Set([ 'smtp_server', 'smtp_port', 'smtp_user', 'smtp_password', 'smtp_from', 'smtp_from_name', 'smtp_from_address', 'registration_enabled', 'show_default_login_info', 'login_captcha_enabled', 'item_sync_enabled', 'item_sync_interval', 'item_sync_max_pages', 'default_reply', ]); const Settings: React.FC = () => { const [settings, setSettings] = useState(null); const [loading, setLoading] = useState(false); const [loadError, setLoadError] = useState(''); const [saving, setSaving] = useState(false); const [aiModels, setAiModels] = useState([]); const [modelsLoading, setModelsLoading] = useState(false); const [modelError, setModelError] = useState(''); const [modelDropdownOpen, setModelDropdownOpen] = useState(false); const modelPickerRef = useRef(null); // Password visibility states const [showApiKey, setShowApiKey] = useState(false); const [showCaptchaSecret, setShowCaptchaSecret] = useState(false); const [showCurrentPassword, setShowCurrentPassword] = useState(false); const [showNewPassword, setShowNewPassword] = useState(false); const [credentialsSaving, setCredentialsSaving] = useState(false); const [credentialsMessage, setCredentialsMessage] = useState<{type: 'success' | 'error'; text: string} | null>(null); const [credentials, setCredentials] = useState({ new_username: '', current_password: '', new_password: '', confirm_password: '', }); useEffect(() => { loadSettings(); verifySession().then(session => { if (session.username) { setCredentials(current => ({...current, new_username: session.username || ''})); } }).catch(() => undefined); }, []); useEffect(() => { const handlePointerDown = (event: MouseEvent) => { if (!modelPickerRef.current?.contains(event.target as Node)) { setModelDropdownOpen(false); } }; document.addEventListener('mousedown', handlePointerDown); return () => document.removeEventListener('mousedown', handlePointerDown); }, []); const loadAIModels = async (source?: SystemSettings | null, openAfterLoad = false) => { const current = source || settings; const baseUrl = current?.ai_api_url || current?.ai_base_url || DEFAULT_AI_API_URL; setModelsLoading(true); setModelError(''); try { const models = await fetchAIModels(baseUrl, current?.ai_api_key || ''); setAiModels(models); setModelDropdownOpen(openAfterLoad && models.length > 0); if (!current?.ai_model && models.length > 0) { setSettings(prev => prev ? { ...prev, ai_model: models[0] } : prev); } } catch (e) { setAiModels([]); setModelDropdownOpen(false); setModelError((e as Error).message || '读取模型失败'); } finally { setModelsLoading(false); } }; const loadSettings = () => { setLoading(true); setLoadError(''); getSystemSettings() .then(data => { setSettings(data); loadAIModels(data); }) .catch(error => { setSettings(null); setLoadError((error as Error).message || '加载配置失败'); }) .finally(() => setLoading(false)); }; const handleSave = async () => { if(!settings) return; setSaving(true); try { const persistable = Object.fromEntries( Object.entries(settings).filter(([key, value]) => !SETTINGS_SAVE_OMIT_KEYS.has(key) && value !== undefined && value !== null ) ) as Partial; await updateSystemSettings(persistable); alert('系统配置已保存'); } catch (e) { alert('保存失败:' + (e as Error).message); } finally { setSaving(false); } }; const handleCredentialsSave = async (event: React.FormEvent) => { event.preventDefault(); setCredentialsMessage(null); const username = credentials.new_username.trim(); if (username.length < 3) { setCredentialsMessage({type: 'error', text: '用户名至少需要 3 个字符'}); return; } if (!credentials.current_password) { setCredentialsMessage({type: 'error', text: '请输入当前密码确认身份'}); return; } if (credentials.new_password && credentials.new_password.length < 8) { setCredentialsMessage({type: 'error', text: '新密码至少需要 8 个字符'}); return; } if (credentials.new_password !== credentials.confirm_password) { setCredentialsMessage({type: 'error', text: '两次输入的新密码不一致'}); return; } setCredentialsSaving(true); try { const result = await updateLoginCredentials({ current_password: credentials.current_password, new_username: username, new_password: credentials.new_password || undefined, }); if (!result.success) { setCredentialsMessage({type: 'error', text: result.message || '登录凭据更新失败'}); return; } setCredentialsMessage({type: 'success', text: result.message || '登录凭据已更新'}); window.setTimeout(() => window.location.reload(), 1400); } catch (error) { setCredentialsMessage({type: 'error', text: (error as Error).message || '登录凭据更新失败'}); } finally { setCredentialsSaving(false); } }; if (!settings) { return (
{loadError || (loading ? '加载配置中...' : '暂无配置')}
); } const currentModel = settings.ai_model || ''; const visibleAIModels = aiModels; return (
{/* Header */}

系统设置

配置全局自动化规则与系统参数

{/* Left Column */}
{/* Basic Settings */}

基础设置

等级越低输出越详细,Debug 适合排查问题

JSON 适合接入集中式日志系统,保存后需重启服务生效

setSettings({ ...settings, renewal_log_retention_days: parseInt(e.target.value) || 0 })} className="w-full ios-input px-4 py-3 rounded-xl" min="0" max="365" />

0 表示不自动清理续期日志

{/* AI Configuration */}

AI 智能回复配置

setSettings({...settings, ai_api_url: e.target.value})} className="w-full ios-input px-4 py-3 rounded-xl text-sm" placeholder="https://api.openai.com/v1" />

无需补全 /chat/completions

setSettings({...settings, ai_api_key: e.target.value})} className="w-full ios-input px-4 py-3 pr-12 rounded-xl font-mono text-sm" placeholder="sk-..." />
aiModels.length > 0 && setModelDropdownOpen(true)} onChange={e => { setSettings({...settings, ai_model: e.target.value}); if (aiModels.length > 0) setModelDropdownOpen(true); }} onKeyDown={e => { if (e.key === 'Escape') setModelDropdownOpen(false); if (e.key === 'ArrowDown' && aiModels.length > 0) setModelDropdownOpen(true); }} className="w-full ios-input px-4 py-3 pr-10 rounded-xl" placeholder="从接口读取或手动输入模型名" /> {modelDropdownOpen && (
{visibleAIModels.length > 0 ? ( visibleAIModels.map(model => ( )) ) : (
没有匹配的模型
)}
)}
{modelError ? (

{modelError}

) : (

{aiModels.length > 0 ? `已从当前 API 地址读取到 ${aiModels.length} 个模型` : '模型列表从当前 API 地址读取,也可以手动输入模型名'}

)}
常见 AI 服务:
  • 阿里云通义千问: https://dashscope.aliyuncs.com/compatible-mode/v1
  • OpenAI: https://api.openai.com/v1
{/* Right Column */}

远程过滑块配置

setSettings({...settings, 'captcha.remote_service_url': event.target.value})} className="w-full ios-input px-4 py-3 rounded-xl text-sm" placeholder="https://example.com/internal/captcha/solve" />
setSettings({...settings, 'captcha.remote_secret_key': event.target.value})} className="w-full ios-input px-4 py-3 pr-12 rounded-xl font-mono text-sm" autoComplete="off" />

配置地址和秘钥后优先调用远程服务;只有网络不可用或超时才回退本机引擎,远程明确返回失败时不会重复触发本机验证。

登录凭据

setCredentials({...credentials, new_username: event.target.value})} className="w-full ios-input pl-11 pr-4 py-3 rounded-xl text-sm" autoComplete="username" />
setCredentials({...credentials, current_password: event.target.value})} className="w-full ios-input px-4 py-3 pr-12 rounded-xl text-sm" placeholder="用于确认当前身份" autoComplete="current-password" />
setCredentials({...credentials, new_password: event.target.value})} className="w-full ios-input px-4 py-3 pr-11 rounded-xl text-sm" placeholder="不修改则留空" autoComplete="new-password" />
setCredentials({...credentials, confirm_password: event.target.value})} className="w-full ios-input px-4 py-3 rounded-xl text-sm" placeholder="再次输入新密码" autoComplete="new-password" />
{credentialsMessage && (
{credentialsMessage.text}
)}
{/* SMTP 配置已移至「通知设置」页面 */}
{/* Save Button */}
); }; export default Settings;