新增首页API相关类型定义及实现,整合轮播图、新闻和通知数据的获取逻辑,优化数据结构以提升用户体验和代码可维护性。
This commit is contained in:
@@ -35,6 +35,7 @@ import { createAuthRoutes } from "./routes_auth.ts";
|
||||
import { createUserRoutes } from "./routes_users.ts";
|
||||
import { createMessagesRoutes } from "./routes_messages.ts";
|
||||
import { createMigrationsRoutes } from "./routes_migrations.ts";
|
||||
import { createHomeRoutes } from "./routes_home.ts";
|
||||
dayjs.extend(utc)
|
||||
// 初始化debug实例
|
||||
const log = {
|
||||
@@ -305,6 +306,7 @@ export default function({ apiClient, app, moduleDir }: ModuleParams) {
|
||||
api.route('/settings', createSystemSettingsRoutes(withAuth)) // 添加系统设置路由
|
||||
api.route('/messages', createMessagesRoutes(withAuth)) // 添加消息路由
|
||||
api.route('/migrations', createMigrationsRoutes(withAuth)) // 添加数据库迁移路由
|
||||
api.route('/home', createHomeRoutes(withAuth)) // 添加首页路由
|
||||
|
||||
// 注册API路由
|
||||
honoApp.route('/api', api)
|
||||
|
||||
@@ -73,6 +73,7 @@ const createKnowInfoTable: MigrationLiveDefinition = {
|
||||
table.string('category').comment('分类');
|
||||
table.string('cover_url').comment('封面图片URL');
|
||||
table.integer('audit_status').defaultTo(AuditStatus.PENDING).comment('审核状态');
|
||||
table.integer('sort_order').defaultTo(0).comment('排序权重');
|
||||
table.integer('is_deleted').defaultTo(0).comment('是否被删除 (0否 1是)');
|
||||
table.timestamps(true, true);
|
||||
|
||||
@@ -82,6 +83,7 @@ const createKnowInfoTable: MigrationLiveDefinition = {
|
||||
table.index('author');
|
||||
table.index('category');
|
||||
table.index('audit_status');
|
||||
table.index('sort_order');
|
||||
table.index('is_deleted');
|
||||
});
|
||||
},
|
||||
|
||||
117
server/routes_home.ts
Normal file
117
server/routes_home.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { Hono } from 'hono'
|
||||
import type { Variables } from './app.tsx'
|
||||
import type { WithAuth } from './app.tsx'
|
||||
import { AuditStatus } from '../client/share/types.ts'
|
||||
|
||||
export function createHomeRoutes(withAuth: WithAuth) {
|
||||
const homeRoutes = new Hono<{ Variables: Variables }>()
|
||||
|
||||
// 获取轮播图数据
|
||||
homeRoutes.get('/banners', async (c) => {
|
||||
try {
|
||||
const apiClient = c.get('apiClient')
|
||||
|
||||
const banners = await apiClient.database.table('know_info')
|
||||
.where('is_deleted', 0)
|
||||
.where('audit_status', AuditStatus.APPROVED) // 使用审核状态替代启用状态
|
||||
.where('category', 'banner') // 轮播图类型
|
||||
.orderBy('created_at', 'asc') // 使用创建时间排序
|
||||
.select('id', 'title', 'cover_url as image', 'content as link')
|
||||
|
||||
return c.json({
|
||||
message: '获取轮播图成功',
|
||||
data: banners
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取轮播图失败:', error)
|
||||
return c.json({ error: '获取轮播图失败' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
// 获取新闻列表
|
||||
homeRoutes.get('/news', async (c) => {
|
||||
try {
|
||||
const apiClient = c.get('apiClient')
|
||||
|
||||
const page = Number(c.req.query('page')) || 1
|
||||
const pageSize = Number(c.req.query('pageSize')) || 10
|
||||
const category = c.req.query('category')
|
||||
|
||||
const query = apiClient.database.table('know_info')
|
||||
.where('is_deleted', 0)
|
||||
.where('audit_status', AuditStatus.APPROVED) // 使用审核状态替代发布状态
|
||||
.where('category', 'news') // 新闻类型
|
||||
.orderBy('created_at', 'desc') // 使用创建时间替代发布时间
|
||||
.limit(pageSize)
|
||||
.offset((page - 1) * pageSize)
|
||||
|
||||
if (category) query.where('sub_category', category)
|
||||
|
||||
const countQuery = query.clone()
|
||||
const news = await query
|
||||
|
||||
// 获取总数用于分页
|
||||
const total = await countQuery.count()
|
||||
const totalCount = Number(total)
|
||||
const totalPages = Math.ceil(totalCount / pageSize)
|
||||
|
||||
return c.json({
|
||||
message: '获取新闻成功',
|
||||
data: news,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
current: page,
|
||||
pageSize,
|
||||
totalPages
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取新闻失败:', error)
|
||||
return c.json({ error: '获取新闻失败' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
// 获取通知列表
|
||||
homeRoutes.get('/notices', async (c) => {
|
||||
try {
|
||||
const apiClient = c.get('apiClient')
|
||||
|
||||
const page = Number(c.req.query('page')) || 1
|
||||
const pageSize = Number(c.req.query('pageSize')) || 10
|
||||
|
||||
const notices = await apiClient.database.table('know_info')
|
||||
.where('is_deleted', 0)
|
||||
.where('status', 1) // 1表示已发布
|
||||
.where('category', 'notice') // 通知类型
|
||||
.orderBy('created_at', 'desc')
|
||||
.limit(pageSize)
|
||||
.offset((page - 1) * pageSize)
|
||||
.select('id', 'title', 'content', 'created_at')
|
||||
|
||||
const total = await apiClient.database.table('know_info')
|
||||
.where('is_deleted', 0)
|
||||
.where('status', 1)
|
||||
.where('category', 'notice')
|
||||
.count()
|
||||
|
||||
const totalCount = Number(total)
|
||||
const totalPages = Math.ceil(totalCount / pageSize)
|
||||
|
||||
return c.json({
|
||||
message: '获取通知成功',
|
||||
data: notices,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
current: page,
|
||||
pageSize,
|
||||
totalPages
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取通知失败:', error)
|
||||
return c.json({ error: '获取通知失败' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
return homeRoutes
|
||||
}
|
||||
Reference in New Issue
Block a user