routes_home.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Hono } from 'hono'
  2. import type { Variables, WithAuth } from "./middlewares.ts";
  3. import { AuditStatus } from '../client/share/types.ts'
  4. export function createHomeRoutes(withAuth: WithAuth) {
  5. const homeRoutes = new Hono<{ Variables: Variables }>()
  6. // 获取轮播图数据
  7. homeRoutes.get('/banners', async (c) => {
  8. try {
  9. const apiClient = c.get('apiClient')
  10. const banners = await apiClient.database.table('know_info')
  11. .where('is_deleted', 0)
  12. .where('audit_status', AuditStatus.APPROVED) // 使用审核状态替代启用状态
  13. .where('category', 'banner') // 轮播图类型
  14. .orderBy('created_at', 'asc') // 使用创建时间排序
  15. .select('id', 'title', 'cover_url', 'content')
  16. return c.json({
  17. message: '获取轮播图成功',
  18. data: banners
  19. })
  20. } catch (error) {
  21. console.error('获取轮播图失败:', error)
  22. return c.json({ error: '获取轮播图失败' }, 500)
  23. }
  24. })
  25. // 获取新闻列表
  26. homeRoutes.get('/news', async (c) => {
  27. try {
  28. const apiClient = c.get('apiClient')
  29. const page = Number(c.req.query('page')) || 1
  30. const pageSize = Number(c.req.query('pageSize')) || 10
  31. const category = c.req.query('category')
  32. const query = apiClient.database.table('know_info')
  33. .where('is_deleted', 0)
  34. .where('audit_status', AuditStatus.APPROVED) // 使用审核状态替代发布状态
  35. .where('category', 'news') // 新闻类型
  36. .orderBy('created_at', 'desc') // 使用创建时间替代发布时间
  37. .limit(pageSize)
  38. .offset((page - 1) * pageSize)
  39. if (category) query.where('sub_category', category)
  40. const countQuery = query.clone()
  41. const news = await query
  42. // 获取总数用于分页
  43. const total = await countQuery.count()
  44. const totalCount = Number(total)
  45. const totalPages = Math.ceil(totalCount / pageSize)
  46. return c.json({
  47. message: '获取新闻成功',
  48. data: news,
  49. pagination: {
  50. total: totalCount,
  51. current: page,
  52. pageSize,
  53. totalPages
  54. }
  55. })
  56. } catch (error) {
  57. console.error('获取新闻失败:', error)
  58. return c.json({ error: '获取新闻失败' }, 500)
  59. }
  60. })
  61. // 获取通知列表
  62. homeRoutes.get('/notices', async (c) => {
  63. try {
  64. const apiClient = c.get('apiClient')
  65. const page = Number(c.req.query('page')) || 1
  66. const pageSize = Number(c.req.query('pageSize')) || 10
  67. const notices = await apiClient.database.table('know_info')
  68. .where('is_deleted', 0)
  69. .where('status', 1) // 1表示已发布
  70. .where('category', 'notice') // 通知类型
  71. .orderBy('created_at', 'desc')
  72. .limit(pageSize)
  73. .offset((page - 1) * pageSize)
  74. .select('id', 'title', 'content', 'created_at')
  75. const total = await apiClient.database.table('know_info')
  76. .where('is_deleted', 0)
  77. .where('status', 1)
  78. .where('category', 'notice')
  79. .count()
  80. const totalCount = Number(total)
  81. const totalPages = Math.ceil(totalCount / pageSize)
  82. return c.json({
  83. message: '获取通知成功',
  84. data: notices,
  85. pagination: {
  86. total: totalCount,
  87. current: page,
  88. pageSize,
  89. totalPages
  90. }
  91. })
  92. } catch (error) {
  93. console.error('获取通知失败:', error)
  94. return c.json({ error: '获取通知失败' }, 500)
  95. }
  96. })
  97. return homeRoutes
  98. }