2
0

routes_maps.ts 4.5 KB

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