import { $fetch } from 'ofetch' import { createError, defineEventHandler, getQuery } from 'h3' import { useRuntimeConfig } from '#imports' import { getTenantFromContext } from '../../utils/tenant' /** * 文章列表 * GET /api/article/list?navigationId=4273&page=1&limit=10 * 代理到 CMS: /cms/cms-article/page * * 参数说明: * - navigationId: 导航栏目ID(从 getSiteInfo 的 topNavs 获取) * - page / limit: 分页 * - keywords: 搜索关键词 */ export default defineEventHandler(async (event) => { const config = useRuntimeConfig() const ctx = getTenantFromContext(event, config) const query = getQuery(event) const modulesApiBase = config.public.modulesApiBase as string try { // 前端传 navigationId;上游 cms-article 实际按 categoryId 过滤(文章无 navigationId 字段,只有 categoryId)。 // 二者同值(文章分类 ID == 对应栏目 navigationId),透传 categoryId 以正确按栏目过滤; // 保留 navigationId 兼容旧租户。 const categoryId = query.navigationId ?? query.categoryId // 聚合场景:前端递归收集父栏目下所有子栏目的 navigationId,以逗号串传入。 // 上游 article 用 categoryIdsStr 字段接收(与现有 categoryIds Set 分支并存),有则走 IN 查询。 const categoryIds = query.categoryIds as string | undefined const res = await $fetch('/cms/cms-article/page', { baseURL: modulesApiBase, headers: { TenantId: ctx.tenantId }, query: { page: 1, limit: 10, ...query, navigationId: categoryIds ? undefined : query.navigationId, categoryId: categoryIds ? undefined : categoryId, categoryIdsStr: categoryIds || undefined, // C 端只展示已发布文章:status 0=已发布 1=草稿 2=下架(与早期假设相反,已于 2026-07-29 校正) // 放在最后以覆盖前端可能传入的 status,避免泄露草稿/下架内容 status: 0, // 同时排除软删除(deleted=1)的文章,兜底防止已删除内容出现在前台 deleted: 0 } }) return res } catch (error: any) { throw createError({ statusCode: error?.statusCode || error?.response?.status || 502, statusMessage: error?.statusMessage || 'Failed to fetch article list' }) } })