阶段七:完成 React Feature 化与前置重构收口
This commit is contained in:
1 parent
375e84d663
commit
1a228dc35c
305 files changed
+14179
-5431
No files matched your search
@@ -1,2 +1,99 @@
|
||||
/** Settings feature 使用的系统配置与身份 API。 */
|
||||
export { fetchAIModels, getSystemSettings, updateLoginCredentials, updateSystemSettings, verifySession } from '../../../services/api';
|
||||
import { get, post, put, type RequestControlOptions } from '../../../shared/http/client';
|
||||
import type { AIModelsResponse, OperationResponse, SystemSettings } from '../../../shared/api-contract';
|
||||
|
||||
/** 设置页面使用的会话状态传输契约,避免跨 feature 依赖。 */
|
||||
export interface SettingsSessionStatusResponse {
|
||||
/** 当前浏览器会话是否已认证。 */
|
||||
authenticated: boolean;
|
||||
/** 服务是否已经完成首次管理员初始化。 */
|
||||
initialized?: boolean;
|
||||
/** 当前用户的数据库标识。 */
|
||||
user_id?: number;
|
||||
/** 当前用户的登录名称。 */
|
||||
username?: string;
|
||||
/** 当前用户是否具有管理员权限。 */
|
||||
is_admin?: boolean;
|
||||
}
|
||||
|
||||
/** 敏感系统设置的显式三态变更命令。 */
|
||||
export type SensitiveSettingChange = {
|
||||
/** 敏感值的保存策略。 */
|
||||
action: 'retain' | 'replace' | 'clear';
|
||||
/** replace 策略需要保存的新值,只存在于提交请求中。 */
|
||||
value?: string;
|
||||
};
|
||||
|
||||
/** 系统设置更新请求,将普通配置与敏感命令分开传输。 */
|
||||
export type SystemSettingsUpdate = {
|
||||
/** 非敏感配置字段。 */
|
||||
values?: Record<string, unknown>;
|
||||
/** 由服务端解释的敏感值变更命令。 */
|
||||
secrets?: Record<string, SensitiveSettingChange>;
|
||||
};
|
||||
|
||||
/** SENSITIVE_SYSTEM_SETTING_KEYS 标识不得写入普通 values 的秘密键。 */
|
||||
const SENSITIVE_SYSTEM_SETTING_KEYS = new Set(['ai_api_key', 'smtp_password', 'qq_reply_secret_key', 'captcha.remote_secret_key']);
|
||||
|
||||
/** 将历史设置草稿转换为服务端要求的普通值与敏感命令结构。 */
|
||||
const normalizeSystemSettingsUpdate = (settings: Partial<SystemSettings> | SystemSettingsUpdate): Record<string, unknown> => {
|
||||
if ('values' in settings || 'secrets' in settings) return settings;
|
||||
// values 保存普通设置,永不容纳秘密文本。
|
||||
const values: Record<string, unknown> = {};
|
||||
// secrets 保存服务端需要独立处理的敏感设置命令。
|
||||
const secrets: Record<string, SensitiveSettingChange> = {};
|
||||
for (const /* key、value 是当前待分流的设置键和值。 */ [key, value] of Object.entries(settings)) {
|
||||
if (value === undefined || value === null) continue;
|
||||
if (SENSITIVE_SYSTEM_SETTING_KEYS.has(key)) {
|
||||
secrets[key] = value === '' ? { action: 'clear' } : { action: 'replace', value: String(value) };
|
||||
continue;
|
||||
}
|
||||
values[key] = value;
|
||||
}
|
||||
return Object.keys(secrets).length > 0 ? { values, secrets } : values;
|
||||
};
|
||||
|
||||
/** 将服务端可兼容的字符串和数值设置归一为 UI 可消费的只读契约。 */
|
||||
const normalizeSettings = (settings: Record<string, unknown>): SystemSettings => {
|
||||
// result 是不修改原始响应的设置副本。
|
||||
const result: Record<string, unknown> = { ...settings };
|
||||
// sensitiveKey 是当前需要转为布尔状态的敏感键。
|
||||
for (const /* sensitiveKey 是当前需要读取配置状态的敏感设置键。 */ sensitiveKey of SENSITIVE_SYSTEM_SETTING_KEYS) {
|
||||
// configuredKey 是敏感键对应的配置状态字段。
|
||||
const configuredKey = `${sensitiveKey}_configured`;
|
||||
if (configuredKey in result) result[configuredKey] = result[configuredKey] === true || result[configuredKey] === 'true';
|
||||
}
|
||||
if ('renewal_log_retention_days' in result) {
|
||||
// days 是完成数值转换后的日志保留天数。
|
||||
const days = Number(result.renewal_log_retention_days);
|
||||
result.renewal_log_retention_days = Number.isFinite(days) ? days : 10;
|
||||
}
|
||||
return result as SystemSettings;
|
||||
};
|
||||
|
||||
/** 修改当前管理员的密码。 */
|
||||
export const changePassword = async (currentPassword: string, newPassword: string): Promise<OperationResponse> => post('/api/v1/session/password', { current_password: currentPassword, new_password: newPassword });
|
||||
|
||||
/** 修改当前管理员的登录名称与可选密码。 */
|
||||
export const updateLoginCredentials = async (data: { /** 当前密码用于服务端重新验证身份。 */ current_password: string; /** 新的管理员名称。 */ new_username: string; /** 可选的新密码。 */ new_password?: string }, options?: RequestControlOptions): Promise<OperationResponse> => put('/api/v1/session/credentials', data, options);
|
||||
|
||||
/** 获取系统设置;仅保留敏感值是否已配置的状态,不接收敏感明文。 */
|
||||
export const getSystemSettings = async (options?: RequestControlOptions): Promise<SystemSettings> => {
|
||||
// response 是服务端可能包裹在 data 中的设置响应。
|
||||
const response = await get<{ /** settings 是当前系统配置对象。 */ data?: Record<string, unknown>; /** settings 是未包裹的兼容设置对象。 */ settings?: Record<string, unknown> } & Record<string, unknown>>('/api/v1/settings/system', undefined, options);
|
||||
// settings 是去除 transport 包裹后的配置对象。
|
||||
const settings = response.data || response.settings || response;
|
||||
return normalizeSettings(settings);
|
||||
};
|
||||
|
||||
/** 保存普通系统配置和敏感设置命令。 */
|
||||
export const updateSystemSettings = async (settings: Partial<SystemSettings> | SystemSettingsUpdate, options?: RequestControlOptions): Promise<OperationResponse> => put('/api/v1/settings/system', normalizeSystemSettingsUpdate(settings), options);
|
||||
|
||||
/** 向服务端请求指定人工智能服务的可用模型列表。 */
|
||||
export const fetchAIModels = async (baseURL: string, apiKey = '', options?: RequestControlOptions): Promise<string[]> => {
|
||||
// response 是模型发现接口的具名响应。
|
||||
const response = await post<AIModelsResponse>('/api/v1/settings/ai-models', { base_url: baseURL, api_key: apiKey }, options);
|
||||
return Array.isArray(response.models) ? response.models : [];
|
||||
};
|
||||
|
||||
/** 在保存登录凭据前读取当前会话状态。 */
|
||||
export const verifySession = async (options?: RequestControlOptions): Promise<SettingsSessionStatusResponse> => get('/api/v1/session', undefined, options);
|
||||
@@ -1,7 +1,7 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { OperationResponse, SystemSettings } from '../../../types';
|
||||
import type { OperationResponse, SystemSettings } from '../../../shared/api-contract';
|
||||
import { fetchAIModels, getSystemSettings, updateLoginCredentials, updateSystemSettings, verifySession } from './api';
|
||||
import { useSettings } from './hooks';
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import type React from 'react';
|
||||
import type { SystemSettings } from '../../../types';
|
||||
import type { SystemSettings } from '../../../shared/api-contract';
|
||||
import { fetchAIModels, getSystemSettings, updateLoginCredentials, updateSystemSettings, verifySession } from './api';
|
||||
import { DEFAULT_AI_API_URL } from './constants';
|
||||
import { buildPersistableSettings, createCredentials, createCredentialsMessage, isCurrentSettingsRequest, isSettingsAbortError, settingsErrorMessage, validateCredentials } from './state';
|
||||
|
||||
@@ -0,0 +1,416 @@
|
||||
import React from 'react';
|
||||
import {
|
||||
Save, Sparkles, Settings as SettingsIcon,
|
||||
Eye, EyeOff, RefreshCw, Database, ChevronDown, Check,
|
||||
LockKeyhole, UserRound, ShieldCheck
|
||||
} from 'lucide-react';
|
||||
import { DEFAULT_AI_API_URL, LOG_LEVELS } from '../constants';
|
||||
import { useSettings } from '../hooks';
|
||||
|
||||
// Settings 展示系统配置、AI 模型和登录凭据编辑页面。
|
||||
const Settings: React.FC = () => {
|
||||
// featureState 是 Settings Hook 提供的状态与动作集合。
|
||||
const {
|
||||
settings, loading, loadError, saving, saveError, aiModels, modelsLoading, modelError, modelDropdownOpen,
|
||||
showApiKey, showCaptchaSecret, showCurrentPassword, showNewPassword, credentialsSaving, credentialsMessage,
|
||||
credentials, modelPickerRef, loadSettings, loadAIModels, handleSave, handleCredentialsSave,
|
||||
setSettings, setModelDropdownOpen, setShowApiKey, setShowCaptchaSecret, setShowCurrentPassword,
|
||||
setShowNewPassword, setCredentials,
|
||||
} = useSettings();
|
||||
|
||||
if (!settings) {
|
||||
return (
|
||||
<div className="p-8 text-center text-gray-400 space-y-3">
|
||||
{loadError || (loading ? '加载配置中...' : '暂无配置')}
|
||||
{!loading && loadError && (
|
||||
<div>
|
||||
<button type="button" className="ios-btn-primary px-4 py-2 rounded-xl" onClick={loadSettings}>重新加载</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// currentModel 是当前配置中的模型名称。
|
||||
const currentModel = settings.ai_model || '';
|
||||
// visibleAIModels 是模型下拉框当前展示的候选列表。
|
||||
const visibleAIModels = aiModels;
|
||||
|
||||
return (
|
||||
<div className="max-w-6xl mx-auto space-y-8 animate-fade-in pb-24">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="w-12 h-12 bg-gray-100 rounded-2xl flex items-center justify-center">
|
||||
<SettingsIcon className="w-6 h-6 text-gray-600" />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-3xl font-extrabold text-gray-900">系统设置</h2>
|
||||
<p className="text-gray-500 mt-1 text-sm font-medium">配置全局自动化规则与系统参数</p>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={loadSettings}
|
||||
className="px-4 py-2 bg-gray-100 hover:bg-gray-200 rounded-xl font-bold text-gray-700 flex items-center gap-2 transition-colors"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
刷新
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{saveError && (
|
||||
<div className="flex items-center justify-between gap-3 rounded-xl border border-red-100 bg-red-50 px-4 py-3 text-sm text-red-700">
|
||||
<span>{saveError}</span>
|
||||
<button type="button" className="font-bold underline" onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => void handleSave}>重试保存</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
|
||||
{/* Left Column */}
|
||||
<div className="space-y-8">
|
||||
{/* Basic Settings */}
|
||||
<section className="space-y-4">
|
||||
<h3 className="text-lg font-extrabold text-gray-800 flex items-center gap-2">
|
||||
<div className="p-1.5 rounded-lg bg-gray-100 text-gray-600">
|
||||
<Database className="w-4 h-4" />
|
||||
</div>
|
||||
基础设置
|
||||
</h3>
|
||||
|
||||
<div className="ios-card rounded-xl p-6 bg-white space-y-4">
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">日志输出等级</label>
|
||||
<select
|
||||
value={settings.log_level || 'info'}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setSettings({ ...settings, log_level: event.target.value })}
|
||||
className="w-full ios-input px-4 py-3 rounded-xl"
|
||||
>
|
||||
{LOG_LEVELS.map(/* 当前回调处理集合中的单个元素。 */ level => (
|
||||
<option key={level.value} value={level.value}>{level.label}</option>
|
||||
))}
|
||||
</select>
|
||||
<p className="text-xs text-gray-500">等级越低输出越详细,Debug 适合排查问题</p>
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">日志输出格式</label>
|
||||
<select
|
||||
value={settings.log_format || 'text'}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setSettings({ ...settings, log_format: event.target.value })}
|
||||
className="w-full ios-input px-4 py-3 rounded-xl"
|
||||
>
|
||||
<option value="text">Text</option>
|
||||
<option value="json">JSON</option>
|
||||
</select>
|
||||
<p className="text-xs text-gray-500">JSON 适合接入集中式日志系统,保存后需重启服务生效</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">续期日志保留天数</label>
|
||||
<input
|
||||
type="number"
|
||||
value={settings.renewal_log_retention_days ?? 10}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ (e) => 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"
|
||||
/>
|
||||
<p className="text-xs text-gray-500">0 表示不自动清理续期日志</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* AI Configuration */}
|
||||
<section className="space-y-4">
|
||||
<h3 className="text-lg font-extrabold text-gray-800 flex items-center gap-2">
|
||||
<div className="p-1.5 rounded-lg bg-brand text-white">
|
||||
<Sparkles className="w-4 h-4" />
|
||||
</div>
|
||||
AI 智能回复配置
|
||||
</h3>
|
||||
|
||||
<div className="ios-card rounded-xl p-6 bg-white space-y-6">
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">API 地址</label>
|
||||
<input
|
||||
type="text"
|
||||
value={settings.ai_api_url || DEFAULT_AI_API_URL}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ e => 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"
|
||||
/>
|
||||
<p className="text-xs text-gray-500">无需补全 /chat/completions</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">API Key</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showApiKey ? 'text' : 'password'}
|
||||
value={settings.ai_api_key || ''}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ e => 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={settings.ai_api_key_configured ? '已配置,如需替换请输入新密钥' : 'sk-...'}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => setShowApiKey(!showApiKey)}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600 transition-colors"
|
||||
>
|
||||
{showApiKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<label className="block text-sm font-bold text-gray-800">模型</label>
|
||||
<div ref={modelPickerRef} className="relative flex flex-col sm:flex-row gap-2">
|
||||
<div className="relative flex-1">
|
||||
<input
|
||||
value={currentModel}
|
||||
onFocus={/* 当前回调处理用户交互或异步状态变化。 */ () => 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="从接口读取或手动输入模型名"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => aiModels.length > 0 && setModelDropdownOpen(/* 当前回调处理用户交互或异步状态变化。 */ open => !open)}
|
||||
disabled={aiModels.length === 0}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600 disabled:opacity-30"
|
||||
aria-label="展开模型列表"
|
||||
>
|
||||
<ChevronDown className={`w-4 h-4 transition-transform ${modelDropdownOpen ? 'rotate-180' : ''}`} />
|
||||
</button>
|
||||
{modelDropdownOpen && (
|
||||
<div className="absolute left-0 right-0 top-[calc(100%+6px)] z-40 max-h-64 overflow-y-auto rounded-xl border border-gray-200 bg-white shadow-xl shadow-gray-200/70 py-1">
|
||||
{visibleAIModels.length > 0 ? (
|
||||
visibleAIModels.map(/* 当前回调处理集合中的单个元素。 */ model => (
|
||||
<button
|
||||
key={model}
|
||||
type="button"
|
||||
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => {
|
||||
setSettings({...settings, ai_model: model});
|
||||
setModelDropdownOpen(false);
|
||||
}}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-gray-700 hover:bg-blue-50 hover:text-brand flex items-center justify-between gap-3"
|
||||
>
|
||||
<span className="truncate">{model}</span>
|
||||
{model === currentModel && <Check className="w-4 h-4 shrink-0 text-brand" />}
|
||||
</button>
|
||||
))
|
||||
) : (
|
||||
<div className="px-4 py-3 text-sm text-gray-400">没有匹配的模型</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => loadAIModels(undefined, true)}
|
||||
disabled={modelsLoading}
|
||||
className="px-4 py-3 rounded-xl bg-gray-100 text-gray-700 hover:bg-gray-200 disabled:opacity-60 font-bold flex items-center justify-center gap-2 whitespace-nowrap"
|
||||
>
|
||||
<RefreshCw className={`w-4 h-4 ${modelsLoading ? 'animate-spin' : ''}`} />
|
||||
读取模型
|
||||
</button>
|
||||
</div>
|
||||
{modelError ? (
|
||||
<p className="text-xs text-red-500">{modelError}</p>
|
||||
) : (
|
||||
<p className="text-xs text-gray-500">
|
||||
{aiModels.length > 0 ? `已从当前 API 地址读取到 ${aiModels.length} 个模型` : '模型列表从当前 API 地址读取,也可以手动输入模型名'}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="p-3 bg-blue-50 rounded-xl text-xs text-blue-700">
|
||||
<strong>常见 AI 服务:</strong>
|
||||
<ul className="list-disc list-inside mt-1 space-y-0.5">
|
||||
<li>阿里云通义千问: https://dashscope.aliyuncs.com/compatible-mode/v1</li>
|
||||
<li>OpenAI: https://api.openai.com/v1</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
{/* Right Column */}
|
||||
<div className="space-y-8">
|
||||
<section className="space-y-4">
|
||||
<h3 className="text-lg font-extrabold text-gray-800 flex items-center gap-2">
|
||||
<div className="p-1.5 rounded-lg bg-amber-500 text-white">
|
||||
<ShieldCheck className="w-4 h-4" />
|
||||
</div>
|
||||
远程过滑块配置
|
||||
</h3>
|
||||
|
||||
<div className="ios-card rounded-xl p-6 bg-white space-y-5">
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">服务地址</label>
|
||||
<input
|
||||
type="url"
|
||||
value={settings['captcha.remote_service_url'] || ''}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => 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"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">服务秘钥</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showCaptchaSecret ? 'text' : 'password'}
|
||||
value={settings['captcha.remote_secret_key'] || ''}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => 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"
|
||||
placeholder={settings['captcha.remote_secret_key_configured'] ? '已配置,如需替换请输入新密钥' : '请输入服务秘钥'}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => setShowCaptchaSecret(!showCaptchaSecret)}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600"
|
||||
title={showCaptchaSecret ? '隐藏秘钥' : '显示秘钥'}
|
||||
>
|
||||
{showCaptchaSecret ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label className="flex items-start gap-3 rounded-xl bg-amber-50 p-4 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={String(settings['captcha.remote_pass_cookies'] || '').toLowerCase() === 'true'}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setSettings({...settings, 'captcha.remote_pass_cookies': event.target.checked})}
|
||||
className="mt-0.5 w-4 h-4 rounded border-gray-300"
|
||||
/>
|
||||
<span>
|
||||
<span className="block text-sm font-bold text-amber-900">允许向远程服务传递账号 Cookie</span>
|
||||
<span className="block mt-1 text-xs text-amber-700">默认关闭。仅在信任远程服务且需要由其自动重取过期验证链接时开启。</span>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
<p className="text-xs text-gray-500">
|
||||
配置地址和秘钥后优先调用远程服务;只有网络不可用或超时才回退本机引擎,远程明确返回失败时不会重复触发本机验证。
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="space-y-4">
|
||||
<h3 className="text-lg font-extrabold text-gray-800 flex items-center gap-2">
|
||||
<div className="p-1.5 rounded-lg bg-gray-900 text-white">
|
||||
<LockKeyhole className="w-4 h-4" />
|
||||
</div>
|
||||
登录凭据
|
||||
</h3>
|
||||
|
||||
<form onSubmit={handleCredentialsSave} className="ios-card rounded-xl p-6 bg-white space-y-5">
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">登录用户名</label>
|
||||
<div className="relative">
|
||||
<UserRound className="absolute left-4 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
value={credentials.new_username}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setCredentials({...credentials, new_username: event.target.value})}
|
||||
className="w-full ios-input pl-11 pr-4 py-3 rounded-xl text-sm"
|
||||
autoComplete="username"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">当前密码</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showCurrentPassword ? 'text' : 'password'}
|
||||
value={credentials.current_password}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => 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"
|
||||
/>
|
||||
<button type="button" onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => setShowCurrentPassword(!showCurrentPassword)} className="absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600" title={showCurrentPassword ? '隐藏密码' : '显示密码'}>
|
||||
{showCurrentPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">新密码</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showNewPassword ? 'text' : 'password'}
|
||||
value={credentials.new_password}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => 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"
|
||||
/>
|
||||
<button type="button" onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => setShowNewPassword(!showNewPassword)} className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600" title={showNewPassword ? '隐藏密码' : '显示密码'}>
|
||||
{showNewPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">确认新密码</label>
|
||||
<input
|
||||
type={showNewPassword ? 'text' : 'password'}
|
||||
value={credentials.confirm_password}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setCredentials({...credentials, confirm_password: event.target.value})}
|
||||
className="w-full ios-input px-4 py-3 rounded-xl text-sm"
|
||||
placeholder="再次输入新密码"
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{credentialsMessage && (
|
||||
<div className={`flex items-start gap-2 rounded-xl px-3 py-2.5 text-sm font-medium ${credentialsMessage.type === 'success' ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'}`}>
|
||||
<ShieldCheck className="w-4 h-4 mt-0.5 flex-shrink-0" />
|
||||
<span>{credentialsMessage.text}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={credentialsSaving || !credentials.new_username || !credentials.current_password}
|
||||
className="w-full bg-gray-900 hover:bg-black text-white px-5 py-3 rounded-xl font-bold text-sm flex items-center justify-center gap-2 transition-colors disabled:opacity-40"
|
||||
>
|
||||
<LockKeyhole className="w-4 h-4" />
|
||||
{credentialsSaving ? '正在更新...' : '更新登录凭据'}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
{/* SMTP 配置已移至「通知设置」页面 */}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Save Button */}
|
||||
<div className="fixed bottom-10 right-10 z-30">
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={saving}
|
||||
className="ios-btn-primary px-10 py-5 rounded-xl text-lg shadow-2xl shadow-blue-200 flex items-center gap-3 transform hover:scale-105 active:scale-95 transition-all disabled:opacity-70"
|
||||
>
|
||||
<Save className="w-6 h-6" />
|
||||
{saving ? '保存中...' : '保存所有配置'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Settings;
|
||||
@@ -1,5 +1,5 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import type { SystemSettings } from '../../../types';
|
||||
import type { SystemSettings } from '../../../shared/api-contract';
|
||||
import { buildPersistableSettings, createCredentials, createCredentialsMessage, isCurrentSettingsRequest, validateCredentials } from './state';
|
||||
|
||||
// settingsFixture 是覆盖敏感字段过滤和系统配置保留字段的最小草稿。
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { SystemSettings } from '../../../types';
|
||||
import type { SystemSettings } from '../../../shared/api-contract';
|
||||
import { SETTINGS_SAVE_OMIT_KEYS } from './constants';
|
||||
import type { CredentialsForm, CredentialsMessage } from './types';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { SystemSettings } from '../../../types';
|
||||
import type { SystemSettings } from '../../../shared/api-contract';
|
||||
|
||||
/** 登录凭据编辑表单。 */
|
||||
export type CredentialsForm = {
|
||||
|
||||
Reference in new issue
Block a user