|
|
@@ -0,0 +1,36 @@
|
|
|
+import { defineEventHandler, EventHandlerRequest } from 'h3';
|
|
|
+import { DB } from '~~/server/db/DB';
|
|
|
+import { createErrorResponse, createSuccessResponse, IResponse } from '~~/server/utils/response';
|
|
|
+import { CommonPageResult } from '~~/server/db/CommonModel';
|
|
|
+import type { IArticle } from './[id]';
|
|
|
+import { IChannel } from '../channel/[id]';
|
|
|
+
|
|
|
+export default defineEventHandler<EventHandlerRequest, Promise<IResponse<CommonPageResult<IArticle>>>>(async (event) => {
|
|
|
+ try {
|
|
|
+ const query = getQuery(event);
|
|
|
+ const page = query.page as string;
|
|
|
+ const pageSize = query.pageSize as string;
|
|
|
+ const channelId = query.channelId as string;
|
|
|
+ if (!channelId)
|
|
|
+ return createErrorResponse('分类ID不能为空');
|
|
|
+
|
|
|
+ const childChannels = await DB.table('pr_cms_channel')
|
|
|
+ .where('parent_id', channelId)
|
|
|
+ .select('id')
|
|
|
+ .get() as IChannel[];
|
|
|
+
|
|
|
+ const articles = await DB.table('pr_cms_archives')
|
|
|
+ .whereIn('channel_id', [...childChannels.map(item => item.id), channelId])
|
|
|
+ .where('status', 'normal')
|
|
|
+ .orderBy('weigh', 'desc')
|
|
|
+ .orderBy('publishtime', 'desc')
|
|
|
+ .orderBy('createtime', 'desc')
|
|
|
+ .orderBy('id', 'asc')
|
|
|
+ .paginate(Number(page), Number(pageSize));
|
|
|
+ if (!articles)
|
|
|
+ return createErrorResponse('文章不存在');
|
|
|
+ return createSuccessResponse(articles);
|
|
|
+ } catch (error) {
|
|
|
+ return createErrorResponse(error);
|
|
|
+ }
|
|
|
+});
|