Files
admin/www/app/composables/useSiteApi.ts
T
2026-08-03 19:09:03 +08:00

86 lines
2.5 KiB
TypeScript

import type {
GlobalConfig,
NavConfig,
SectionBlock,
I18nEntry,
ProductsData,
NewsArticle,
NewsDetail,
NewsListConfig,
ProductListConfig,
AboutConfig,
SupportConfig,
ContactConfig,
PageSeo
} from '~/types/site';
/**
* 官网前台公开接口服务层
*
* 消费 dashboard 后端的 /api/site/* 只读接口(免鉴权)。
*/
export function useSiteApi() {
const { get } = useRequest();
/** 全局配置:站点信息 + 页脚 + 社交链接 + SEO 默认值 */
function getGlobal() {
return get<GlobalConfig>('/site/global');
}
/** 导航:可见入口 + 外观 */
function getNav() {
return get<NavConfig>('/site/nav');
}
/** Section 区块列表(按页面类型,仅可见) */
function getSections(pageType: string, productId?: number) {
const params: Record<string, unknown> = { page_type: pageType };
if (productId) params.product_id = productId;
return get<{ items: SectionBlock[]; total: number }>('/site/sections', params);
}
/** i18n 词条列表 */
function getI18n() {
return get<{ items: I18nEntry[]; total: number }>('/site/i18n');
}
/** 产品:可见系列及其下可见产品 */
function getProducts() {
return get<ProductsData>('/site/products');
}
/** 新闻列表(仅已发布) */
function getNews(skip = 0, limit = 9) {
return get<{ items: NewsArticle[]; total: number; skip: number; limit: number }>('/site/news', { skip, limit });
}
/** 新闻详情(按 slug,含推荐) */
function getNewsDetail(slug: string) {
return get<NewsDetail>(`/site/news/${encodeURIComponent(slug)}`);
}
/** 页面配置:product-list / news-list / about / support */
function getPageConfig<T>(type: 'product-list' | 'news-list' | 'about' | 'support') {
return get<T>('/site/page', { type });
}
/** 页面 SEO 配置 */
function getPageSeo(pageType: string, entityId?: number) {
const params: Record<string, unknown> = { page_type: pageType };
if (entityId) params.entity_id = entityId;
return get<PageSeo | null>('/site/page-seo', params);
}
/** 联系表单配置 + 字段 */
function getContact() {
return get<ContactConfig>('/site/contact');
}
/** 提交联系表单 */
function submitContact(formData: Record<string, string>) {
return useRequest().post<null>('/site/contact/submit', { form_data: formData });
}
return { getGlobal, getNav, getSections, getI18n, getProducts, getNews, getNewsDetail, getPageConfig, getPageSeo, getContact, submitContact };
}