import { Hono } from "hono"; import debug from "debug"; import type { ThemeSettings, } from "../client/share/types.ts"; import { ThemeMode, FontSize, CompactMode, } from "../client/share/types.ts"; import type { Variables, WithAuth } from "./middlewares.ts"; const log = { api: debug("api:sys"), }; // 创建主题设置路由 export function createThemeRoutes(withAuth: WithAuth) { const themeRoutes = new Hono<{ Variables: Variables }>(); // 获取当前主题设置 themeRoutes.get("/", withAuth, async (c) => { try { const apiClient = c.get('apiClient'); const user = c.get('user'); if (!user) { return c.json({ error: "未授权访问" }, 401); } // 获取用户的主题设置 let themeSettings = await apiClient.database .table("theme_settings") .where("user_id", user.id) .first(); // 如果用户没有主题设置,则创建默认设置 if (!themeSettings) { const defaultSettings = { theme_mode: ThemeMode.LIGHT, primary_color: '#1890ff', font_size: FontSize.MEDIUM, is_compact: CompactMode.NORMAL }; const [id] = await apiClient.database.table("theme_settings").insert({ user_id: user.id, settings: defaultSettings, created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now(), }); themeSettings = await apiClient.database .table("theme_settings") .where("id", id) .first(); } return c.json({ message: "获取主题设置成功", data: themeSettings?.settings, }); } catch (error) { log.api("获取主题设置失败:", error); return c.json({ error: "获取主题设置失败" }, 500); } }); // 更新主题设置 themeRoutes.put("/", withAuth, async (c) => { try { const user = c.get('user'); const apiClient = c.get('apiClient'); if (!user) { return c.json({ error: "未授权访问" }, 401); } const themeData = (await c.req.json()) as Partial; // 检查用户是否已有主题设置 const existingTheme = await apiClient.database .table("theme_settings") .where("user_id", user.id) .first(); if (existingTheme) { // 更新现有设置 const currentSettings = existingTheme.settings || {}; const updatedSettings = { ...currentSettings, ...themeData }; await apiClient.database .table("theme_settings") .where("user_id", user.id) .update({ settings: JSON.stringify(updatedSettings), updated_at: apiClient.database.fn.now(), }); } else { // 创建新设置 const defaultSettings = { theme_mode: ThemeMode.LIGHT, primary_color: '#1890ff', font_size: FontSize.MEDIUM, is_compact: CompactMode.NORMAL }; const updatedSettings = { ...defaultSettings, ...themeData }; await apiClient.database.table("theme_settings").insert({ user_id: user.id, settings: updatedSettings, created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now(), }); } // 获取更新后的主题设置 const updatedTheme = await apiClient.database .table("theme_settings") .where("user_id", user.id) .first(); return c.json({ message: "主题设置更新成功", data: updatedTheme, }); } catch (error) { log.api("更新主题设置失败:", error); return c.json({ error: "更新主题设置失败" }, 500); } }); // 重置主题设置为默认值 themeRoutes.post("/reset", withAuth, async (c) => { try { const user = c.get('user'); const apiClient = c.get('apiClient'); if (!user) { return c.json({ error: "未授权访问" }, 401); } // 默认主题设置 const defaultSettings = { theme_mode: ThemeMode.LIGHT, primary_color: '#1890ff', font_size: FontSize.MEDIUM, is_compact: CompactMode.NORMAL }; // 更新用户的主题设置 await apiClient.database .table("theme_settings") .where("user_id", user.id) .update({ settings: JSON.stringify(defaultSettings), updated_at: apiClient.database.fn.now(), }); // 获取更新后的主题设置 const updatedTheme = await apiClient.database .table("theme_settings") .where("user_id", user.id) .first(); return c.json({ message: "主题设置已重置为默认值", data: updatedTheme, }); } catch (error) { log.api("重置主题设置失败:", error); return c.json({ error: "重置主题设置失败" }, 500); } }); return themeRoutes; }