2
0

routes_theme.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. ThemeSettings,
  5. } from "../client/share/types.ts";
  6. import {
  7. ThemeMode,
  8. FontSize,
  9. CompactMode,
  10. } from "../client/share/types.ts";
  11. import type { Variables, WithAuth } from "./middlewares.ts";
  12. const log = {
  13. api: debug("api:sys"),
  14. };
  15. // 创建主题设置路由
  16. export function createThemeRoutes(withAuth: WithAuth) {
  17. const themeRoutes = new Hono<{ Variables: Variables }>();
  18. // 获取当前主题设置
  19. themeRoutes.get("/", withAuth, async (c) => {
  20. try {
  21. const apiClient = c.get('apiClient');
  22. const user = c.get('user');
  23. if (!user) {
  24. return c.json({ error: "未授权访问" }, 401);
  25. }
  26. // 获取用户的主题设置
  27. let themeSettings = await apiClient.database
  28. .table("theme_settings")
  29. .where("user_id", user.id)
  30. .first();
  31. // 如果用户没有主题设置,则创建默认设置
  32. if (!themeSettings) {
  33. const defaultSettings = {
  34. theme_mode: ThemeMode.LIGHT,
  35. primary_color: '#1890ff',
  36. font_size: FontSize.MEDIUM,
  37. is_compact: CompactMode.NORMAL
  38. };
  39. const [id] = await apiClient.database.table("theme_settings").insert({
  40. user_id: user.id,
  41. settings: defaultSettings,
  42. created_at: apiClient.database.fn.now(),
  43. updated_at: apiClient.database.fn.now(),
  44. });
  45. themeSettings = await apiClient.database
  46. .table("theme_settings")
  47. .where("id", id)
  48. .first();
  49. }
  50. return c.json({
  51. message: "获取主题设置成功",
  52. data: themeSettings?.settings,
  53. });
  54. } catch (error) {
  55. log.api("获取主题设置失败:", error);
  56. return c.json({ error: "获取主题设置失败" }, 500);
  57. }
  58. });
  59. // 更新主题设置
  60. themeRoutes.put("/", withAuth, async (c) => {
  61. try {
  62. const user = c.get('user');
  63. const apiClient = c.get('apiClient');
  64. if (!user) {
  65. return c.json({ error: "未授权访问" }, 401);
  66. }
  67. const themeData = (await c.req.json()) as Partial<ThemeSettings>;
  68. // 检查用户是否已有主题设置
  69. const existingTheme = await apiClient.database
  70. .table("theme_settings")
  71. .where("user_id", user.id)
  72. .first();
  73. if (existingTheme) {
  74. // 更新现有设置
  75. const currentSettings = existingTheme.settings || {};
  76. const updatedSettings = {
  77. ...currentSettings,
  78. ...themeData
  79. };
  80. await apiClient.database
  81. .table("theme_settings")
  82. .where("user_id", user.id)
  83. .update({
  84. settings: JSON.stringify(updatedSettings),
  85. updated_at: apiClient.database.fn.now(),
  86. });
  87. } else {
  88. // 创建新设置
  89. const defaultSettings = {
  90. theme_mode: ThemeMode.LIGHT,
  91. primary_color: '#1890ff',
  92. font_size: FontSize.MEDIUM,
  93. is_compact: CompactMode.NORMAL
  94. };
  95. const updatedSettings = {
  96. ...defaultSettings,
  97. ...themeData
  98. };
  99. await apiClient.database.table("theme_settings").insert({
  100. user_id: user.id,
  101. settings: updatedSettings,
  102. created_at: apiClient.database.fn.now(),
  103. updated_at: apiClient.database.fn.now(),
  104. });
  105. }
  106. // 获取更新后的主题设置
  107. const updatedTheme = await apiClient.database
  108. .table("theme_settings")
  109. .where("user_id", user.id)
  110. .first();
  111. return c.json({
  112. message: "主题设置更新成功",
  113. data: updatedTheme,
  114. });
  115. } catch (error) {
  116. log.api("更新主题设置失败:", error);
  117. return c.json({ error: "更新主题设置失败" }, 500);
  118. }
  119. });
  120. // 重置主题设置为默认值
  121. themeRoutes.post("/reset", withAuth, async (c) => {
  122. try {
  123. const user = c.get('user');
  124. const apiClient = c.get('apiClient');
  125. if (!user) {
  126. return c.json({ error: "未授权访问" }, 401);
  127. }
  128. // 默认主题设置
  129. const defaultSettings = {
  130. theme_mode: ThemeMode.LIGHT,
  131. primary_color: '#1890ff',
  132. font_size: FontSize.MEDIUM,
  133. is_compact: CompactMode.NORMAL
  134. };
  135. // 更新用户的主题设置
  136. await apiClient.database
  137. .table("theme_settings")
  138. .where("user_id", user.id)
  139. .update({
  140. settings: JSON.stringify(defaultSettings),
  141. updated_at: apiClient.database.fn.now(),
  142. });
  143. // 获取更新后的主题设置
  144. const updatedTheme = await apiClient.database
  145. .table("theme_settings")
  146. .where("user_id", user.id)
  147. .first();
  148. return c.json({
  149. message: "主题设置已重置为默认值",
  150. data: updatedTheme,
  151. });
  152. } catch (error) {
  153. log.api("重置主题设置失败:", error);
  154. return c.json({ error: "重置主题设置失败" }, 500);
  155. }
  156. });
  157. return themeRoutes;
  158. }