import type { Article, Product, CaseItem, ApiEnvelope, PageResult } from '~/types' /** * CMS 数据请求 * 封装文章、产品、案例等数据的获取逻辑 * 代理接口统一在 server/api/ 下 */ export function useCms() { /** * 文章列表 * @param params.navigationId 导航栏目ID(从 getSiteInfo 的 topNavs 获取) * @param params.page 页码 * @param params.limit 每页条数 * @param params.keywords 搜索关键词 */ async function fetchArticles(params?: { navigationId?: number categoryId?: number page?: number limit?: number keywords?: string }) { const res = await $fetch> | PageResult
>( '/api/article/list', { query: params } ) const envelope = res as ApiEnvelope> if (envelope?.data) { return envelope.data } return res as PageResult
} /** 文章详情 */ async function fetchArticleDetail(id: string | number) { const res = await $fetch | Article>('/api/article/detail', { query: { id } }) const envelope = res as ApiEnvelope
return envelope?.data || (res as Article) } /** * 根据导航模型获取文章列表 * model=article 的导航直接用 navigationId 查询 */ async function fetchArticlesByNav(navigationId: number, page = 1, limit = 10) { return fetchArticles({ navigationId, page, limit }) } /** 产品列表 */ async function fetchProducts(params?: { categoryId?: number navigationId?: number page?: number limit?: number keywords?: string }) { const res = await $fetch> | PageResult>( '/api/product/list', { query: params } ) const envelope = res as ApiEnvelope> if (envelope?.data) { return envelope.data } return res as PageResult } /** 产品详情 */ async function fetchProductDetail(id: string | number) { const res = await $fetch | Product>('/api/product/detail', { query: { id } }) const envelope = res as ApiEnvelope return envelope?.data || (res as Product) } /** 案例列表 */ async function fetchCases(params?: { categoryId?: number navigationId?: number page?: number limit?: number }) { const res = await $fetch> | PageResult>( '/api/case/list', { query: params } ) const envelope = res as ApiEnvelope> if (envelope?.data) { return envelope.data } return res as PageResult } /** 案例详情 */ async function fetchCaseDetail(id: string | number) { const res = await $fetch | CaseItem>('/api/case/detail', { query: { id } }) const envelope = res as ApiEnvelope return envelope?.data || (res as CaseItem) } /** 提交表单/留言 */ async function submitForm(data: Record) { const res = await $fetch('/api/form/submit', { method: 'POST', body: data }) return res } return { fetchArticles, fetchArticleDetail, fetchArticlesByNav, fetchProducts, fetchProductDetail, fetchCases, fetchCaseDetail, submitForm } }