routes_home.ts 3.5 KB

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