routes_maps.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. FileLibrary,
  5. FileCategory,
  6. KnowInfo,
  7. ThemeSettings,
  8. } from "./asset/share/types.ts";
  9. import {
  10. EnableStatus,
  11. DeleteStatus,
  12. ThemeMode,
  13. FontSize,
  14. CompactMode,
  15. } from "./asset/share/types.ts";
  16. import type { Variables, WithAuth } from "./app.tsx";
  17. const log = {
  18. api: debug("api:sys"),
  19. };
  20. // 创建地图数据路由
  21. export function createMapRoutes(withAuth: WithAuth) {
  22. const mapRoutes = new Hono<{ Variables: Variables }>();
  23. // 获取地图标记点数据
  24. mapRoutes.get("/markers", withAuth, async (c) => {
  25. try {
  26. const apiClient = c.get('apiClient');
  27. // 从登录历史表中查询有经纬度的登录记录
  28. const locations = await apiClient.database
  29. .table('login_history')
  30. .select(
  31. 'id',
  32. 'user_id',
  33. 'location_name',
  34. 'longitude',
  35. 'latitude',
  36. 'login_time',
  37. 'ip_address'
  38. )
  39. .whereNotNull('longitude')
  40. .whereNotNull('latitude')
  41. .orderBy('login_time', 'desc')
  42. .limit(100); // 限制返回最近100条记录
  43. // 获取相关用户信息
  44. const userIds = [...new Set(locations.map(loc => loc.user_id))];
  45. const users = await apiClient.database
  46. .table('users')
  47. .select('id', 'username', 'nickname')
  48. .whereIn('id', userIds);
  49. // 构建用户信息映射
  50. const userMap = new Map(users.map(user => [user.id, user]));
  51. // 转换为地图标记点数据格式
  52. const markers = locations.map(location => ({
  53. id: location.id,
  54. name: location.location_name || '未知地点',
  55. longitude: location.longitude,
  56. latitude: location.latitude,
  57. loginTime: location.login_time,
  58. ipAddress: location.ip_address,
  59. user: userMap.get(location.user_id)
  60. }));
  61. return c.json({
  62. message: "获取登录位置数据成功",
  63. data: markers,
  64. });
  65. } catch (error) {
  66. log.api("获取登录位置数据失败:", error);
  67. return c.json({ error: "获取登录位置数据失败" }, 500);
  68. }
  69. });
  70. // 获取登录位置详情数据
  71. mapRoutes.get("/location/:id", withAuth, async (c) => {
  72. try {
  73. const id = Number(c.req.param("id"));
  74. if (!id || isNaN(id)) {
  75. return c.json({ error: "无效的登录记录ID" }, 400);
  76. }
  77. const apiClient = c.get('apiClient');
  78. // 查询登录记录详情
  79. const location = await apiClient.database
  80. .table('login_history')
  81. .where('id', id)
  82. .first();
  83. if (!location) {
  84. return c.json({ error: "登录记录不存在" }, 404);
  85. }
  86. // 获取用户信息
  87. const [user] = await apiClient.database
  88. .table('users')
  89. .select('id', 'username', 'nickname')
  90. .where('id', location.user_id);
  91. return c.json({
  92. message: "获取登录位置详情成功",
  93. data: {
  94. ...location,
  95. user
  96. },
  97. });
  98. } catch (error) {
  99. log.api("获取登录位置详情失败:", error);
  100. return c.json({ error: "获取登录位置详情失败" }, 500);
  101. }
  102. });
  103. // 更新登录位置信息
  104. mapRoutes.put("/location/:id", withAuth, async (c) => {
  105. try {
  106. const id = Number(c.req.param("id"));
  107. if (!id || isNaN(id)) {
  108. return c.json({ error: "无效的登录记录ID" }, 400);
  109. }
  110. const apiClient = c.get('apiClient');
  111. const data = await c.req.json();
  112. // 验证经纬度
  113. if (!data.longitude || !data.latitude) {
  114. return c.json({ error: "经度和纬度不能为空" }, 400);
  115. }
  116. // 检查登录记录是否存在
  117. const location = await apiClient.database
  118. .table('login_history')
  119. .where('id', id)
  120. .first();
  121. if (!location) {
  122. return c.json({ error: "登录记录不存在" }, 404);
  123. }
  124. // 更新位置信息
  125. await apiClient.database
  126. .table('login_history')
  127. .where('id', id)
  128. .update({
  129. longitude: data.longitude,
  130. latitude: data.latitude,
  131. location_name: data.location_name
  132. });
  133. // 获取更新后的登录记录
  134. const updatedLocation = await apiClient.database
  135. .table('login_history')
  136. .where('id', id)
  137. .first();
  138. return c.json({
  139. message: "登录位置信息更新成功",
  140. data: updatedLocation,
  141. });
  142. } catch (error) {
  143. log.api("更新登录位置信息失败:", error);
  144. return c.json({ error: "更新登录位置信息失败" }, 500);
  145. }
  146. });
  147. return mapRoutes;
  148. }