feat(webui): UI customization config store + endpoints (phase 1)
Backend foundation for the WebUI appearance + layout customization feature. - core/src/webui/ui-config.ts: config/ui.json store with TOTAL normalization (any input -> safe config), atomic write, lazy memoized cache, and the background-image lifecycle (magic-number sniff for PNG/JPEG/WebP). - server.ts: GET/POST /api/ui (bearer), GET /api/ui/public (unauth cosmetic subset for pre-login theming), POST/DELETE /api/ui/background (upload with Content-Length pre-check + 5MB cap + magic-number validation), and unauth GET /ui-asset/background (serves the uploaded bytes with sniffed mime + nosniff). SPA fallback excludes /ui-asset/. - webui: `ui` API-client namespace + mirror types (no UI wiring yet). Server-managed background fields (hasImage/imageMime/imageVersion) are taken from the trusted source per path — the file on load, the on-disk config on save — so a client save can never forge them, and a wallpaper survives a restart. Verified across 3 adversarial review rounds.
This commit is contained in:
5 files changed
+722
-3
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { AccountConnections, HookProcessInfo, LogEntry, LogLevel, QQInfo, SystemInfo, UpdateInfo } from '@/types';
|
||||
import type { AccountConnections, HookProcessInfo, LogEntry, LogLevel, QQInfo, SystemInfo, UiAppearance, UiConfig, UpdateInfo } from '@/types';
|
||||
import type { PasswordRule } from '@/components/pages/change-password-page';
|
||||
import { normalizeOneBotConfig } from '@/lib/onebot-config';
|
||||
import {
|
||||
@@ -43,6 +43,7 @@ class HttpApiClient implements ApiClient {
|
||||
readonly config: ApiClient['config'];
|
||||
readonly logs: ApiClient['logs'];
|
||||
readonly update: ApiClient['update'];
|
||||
readonly ui: ApiClient['ui'];
|
||||
|
||||
constructor(opts: CreateApiClientOptions = {}) {
|
||||
this.tokenStore = opts.tokenStore ?? localStorageTokenStore(DEFAULT_TOKEN_KEY);
|
||||
@@ -95,6 +96,46 @@ class HttpApiClient implements ApiClient {
|
||||
check: (force) =>
|
||||
this.getJson<UpdateInfo>(`/api/update/check${force ? '?force=true' : ''}`),
|
||||
};
|
||||
|
||||
this.ui = {
|
||||
get: () => this.getJson<{ config: UiConfig }>('/api/ui').then((d) => d.config),
|
||||
save: async (config) => {
|
||||
const data = await this.postJson<{ config: UiConfig }>('/api/ui', config);
|
||||
return data.config;
|
||||
},
|
||||
getPublic: async () => {
|
||||
// Pre-auth path: a plain fetch with no bearer. Used by the login page
|
||||
// to theme itself before the operator has signed in.
|
||||
const res = await fetch('/api/ui/public');
|
||||
if (!res.ok) throw new ApiError(res.status, '无法获取外观配置');
|
||||
const data = await readJson<{ appearance: UiAppearance }>(res);
|
||||
return data.appearance;
|
||||
},
|
||||
uploadBackground: async (file) => {
|
||||
// FormData must set its own multipart boundary, so this bypasses
|
||||
// request() (which would force application/json) and attaches the
|
||||
// bearer header directly — mirroring login()'s deliberate bypass.
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
const headers: Record<string, string> = {};
|
||||
if (this.currentToken) headers['Authorization'] = `Bearer ${this.currentToken}`;
|
||||
const res = await fetch('/api/ui/background', { method: 'POST', headers, body: form });
|
||||
if (res.status === 401) {
|
||||
this.setToken(null);
|
||||
this.onUnauthorized?.();
|
||||
}
|
||||
if (!res.ok) {
|
||||
const payload = await readJson<ErrorPayload>(res);
|
||||
throw new ApiError(res.status, extractErrorMessage(payload, '上传失败'), payload.code);
|
||||
}
|
||||
const data = await readJson<{ config: UiConfig }>(res);
|
||||
return data.config;
|
||||
},
|
||||
deleteBackground: async () => {
|
||||
const data = await this.fetchJson<{ config: UiConfig }>('/api/ui/background', { method: 'DELETE' });
|
||||
return data.config;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ---------- HTTP helpers ----------
|
||||
|
||||
@@ -6,6 +6,8 @@ import type {
|
||||
OneBotConfig,
|
||||
QQInfo,
|
||||
SystemInfo,
|
||||
UiAppearance,
|
||||
UiConfig,
|
||||
UpdateInfo,
|
||||
} from '@/types';
|
||||
import type { PasswordRule } from '@/components/pages/change-password-page';
|
||||
@@ -76,6 +78,20 @@ export interface ApiClient {
|
||||
check(force?: boolean): Promise<UpdateInfo>;
|
||||
};
|
||||
|
||||
// ---- WebUI customization (config/ui.json) ----
|
||||
ui: {
|
||||
/** Full config (appearance + layout). Bearer-gated. */
|
||||
get(): Promise<UiConfig>;
|
||||
/** Persist the full config; returns the normalized server view. */
|
||||
save(config: UiConfig): Promise<UiConfig>;
|
||||
/** Cosmetic appearance subset, usable pre-auth (login page theming). */
|
||||
getPublic(): Promise<UiAppearance>;
|
||||
/** Upload a background image (PNG/JPEG/WebP, ≤5MB). Returns updated config. */
|
||||
uploadBackground(file: File): Promise<UiConfig>;
|
||||
/** Remove the background image. Returns updated config. */
|
||||
deleteBackground(): Promise<UiConfig>;
|
||||
};
|
||||
|
||||
// ---- logs ----
|
||||
logs: {
|
||||
list(limit?: number): Promise<LogEntry[]>;
|
||||
|
||||
@@ -158,3 +158,66 @@ export interface LogEntry {
|
||||
message: string;
|
||||
line: string;
|
||||
}
|
||||
|
||||
// ─── WebUI customization config (config/ui.json) ───────────────────────────
|
||||
// Mirror of the server schema in core/src/webui/ui-config.ts. The contract is
|
||||
// the JSON shape served by `/api/ui`; this is the client-side view of it.
|
||||
|
||||
export type ThemeMode = 'light' | 'dark' | 'system';
|
||||
export type AccentMode = 'preset' | 'custom';
|
||||
export type AccentScope = 'sidebar' | 'global';
|
||||
export type DarkIntensity = 'soft' | 'black';
|
||||
export type SidebarStyle = 'follow' | 'panel' | 'accent';
|
||||
export type BackgroundType = 'none' | 'solid' | 'gradient' | 'image';
|
||||
export type Density = 'cozy' | 'compact';
|
||||
export type TimeFormat = '12h' | '24h';
|
||||
|
||||
export interface UiBackground {
|
||||
type: BackgroundType;
|
||||
color: string;
|
||||
gradient: string;
|
||||
imageOpacity: number;
|
||||
imageBlur: number;
|
||||
/** Server-managed: true when an image is on disk. Read-only to the client. */
|
||||
hasImage: boolean;
|
||||
imageMime: string;
|
||||
/** Server-managed cache-bust counter, bumped on each upload. */
|
||||
imageVersion: number;
|
||||
}
|
||||
|
||||
export interface UiAppearance {
|
||||
mode: ThemeMode;
|
||||
accentMode: AccentMode;
|
||||
accentPreset: string;
|
||||
accentCustom: string;
|
||||
accentScope: AccentScope;
|
||||
darkIntensity: DarkIntensity;
|
||||
sidebarStyle: SidebarStyle;
|
||||
background: UiBackground;
|
||||
fontSans: string;
|
||||
fontMono: string;
|
||||
uiScale: number;
|
||||
radius: number;
|
||||
density: Density;
|
||||
reduceMotion: boolean;
|
||||
highContrast: boolean;
|
||||
sidebarDefaultCollapsed: boolean;
|
||||
timeFormat: TimeFormat;
|
||||
pollInterval: number;
|
||||
}
|
||||
|
||||
export interface UiLayoutItem {
|
||||
id: string;
|
||||
visible: boolean;
|
||||
}
|
||||
|
||||
export interface UiLayout {
|
||||
overviewBlocks: UiLayoutItem[];
|
||||
navItems: UiLayoutItem[];
|
||||
}
|
||||
|
||||
export interface UiConfig {
|
||||
version: number;
|
||||
appearance: UiAppearance;
|
||||
layout: UiLayout;
|
||||
}
|
||||
Reference in new issue
Block a user