- top-level view switch workbench|admin in App.tsx (no router; hash #turn anchors untouched) - new admin/console/AdminConsoleView with left group nav in the existing token/inline-style idiom, plus 8 self-fetching sections: overview, kb, features, settings, workflows, roles, users, docs - absorb the six AdminSettingsModal tabs into sections and delete the modal - new api/systemAdmin.ts + types for /api/v1/admin/system/* endpoints - playwright smoke spec web/e2e/admin-console.spec.ts (skip by default)
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { authorizedFetch } from './authHeaders';
|
|
import type {
|
|
SystemConfigValue,
|
|
SystemFeature,
|
|
SystemKbInfo,
|
|
SystemOverview,
|
|
SystemSetting,
|
|
} from '../types/systemAdmin';
|
|
|
|
const BASE = '/api/v1/admin/system';
|
|
|
|
async function readError(resp: Response, fallback: string): Promise<Error> {
|
|
let err: Error;
|
|
try {
|
|
const text = await resp.text();
|
|
err = text ? new Error(text.slice(0, 200)) : new Error(`${fallback} (HTTP ${resp.status})`);
|
|
} catch {
|
|
err = new Error(`${fallback} (HTTP ${resp.status})`);
|
|
}
|
|
(err as Error & { status?: number }).status = resp.status;
|
|
return err;
|
|
}
|
|
|
|
async function getJson<T>(path: string, fallback: string): Promise<T> {
|
|
const resp = await authorizedFetch(path);
|
|
if (!resp.ok) throw await readError(resp, fallback);
|
|
return resp.json() as Promise<T>;
|
|
}
|
|
|
|
export async function fetchSystemOverview(): Promise<SystemOverview> {
|
|
return getJson<SystemOverview>(`${BASE}/overview`, '获取系统总览失败');
|
|
}
|
|
|
|
export async function fetchSystemKb(): Promise<SystemKbInfo> {
|
|
return getJson<SystemKbInfo>(`${BASE}/kb`, '获取知识库连接信息失败');
|
|
}
|
|
|
|
export async function fetchSystemFeatures(): Promise<SystemFeature[]> {
|
|
const data = await getJson<{ features?: SystemFeature[] }>(`${BASE}/features`, '获取特性开关失败');
|
|
return data.features ?? [];
|
|
}
|
|
|
|
export async function fetchSystemSettings(): Promise<SystemSetting[]> {
|
|
const data = await getJson<{ settings?: SystemSetting[] }>(`${BASE}/settings`, '获取系统设置失败');
|
|
return data.settings ?? [];
|
|
}
|
|
|
|
export async function updateSystemSetting(
|
|
key: string,
|
|
value: SystemConfigValue,
|
|
): Promise<SystemSetting> {
|
|
const resp = await authorizedFetch(`${BASE}/settings/${encodeURIComponent(key)}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ value }),
|
|
});
|
|
if (!resp.ok) throw await readError(resp, '保存设置失败');
|
|
const data = (await resp.json()) as { setting?: SystemSetting } & SystemSetting;
|
|
return data.setting ?? data;
|
|
}
|