Files

101 lines
5.8 KiB
TypeScript

import type { AIModelsResponse,OperationResponse,SystemSettings } from '../../../shared/api-contract/settings';
import { get,post,put,type RequestControlOptions } from '../../../shared/http/client';
export type * from '../../../shared/api-contract/settings';
/** 设置页面使用的会话状态传输契约,避免跨 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);