feat:接口联调
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
const ALL_LABEL = '全部'
|
||||
const LATEST_LABEL = '最新文章'
|
||||
const FALLBACK_IMAGE = '/images/lanhu/slices/home-group95-image21@2x.png'
|
||||
|
||||
function pickUrl(asset) {
|
||||
return asset?.url || asset?.fileUrl || asset?.filePath || ''
|
||||
}
|
||||
|
||||
function buildMediaMap(media = []) {
|
||||
return new Map(media.map((asset) => [asset.id, asset]))
|
||||
}
|
||||
|
||||
function toTags(value) {
|
||||
if (Array.isArray(value)) return value.filter(Boolean)
|
||||
return String(value || '')
|
||||
.split(/[,,、\s]+/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
function bySortAndTime(left, right) {
|
||||
const leftSort = Number.isFinite(Number(left.sortOrder)) ? Number(left.sortOrder) : 9999
|
||||
const rightSort = Number.isFinite(Number(right.sortOrder)) ? Number(right.sortOrder) : 9999
|
||||
if (leftSort !== rightSort) return leftSort - rightSort
|
||||
return String(right.updatedAt || right.publishedAt || '').localeCompare(
|
||||
String(left.updatedAt || left.publishedAt || '')
|
||||
)
|
||||
}
|
||||
|
||||
export function useOfficialContent() {
|
||||
const config = useRuntimeConfig()
|
||||
const serverApiBase = `${String(config.officialApiProxyTarget || 'http://localhost:8080').replace(/\/$/, '')}/api`
|
||||
const apiBase = import.meta.server ? serverApiBase : (config.public.officialApiBase || '/api')
|
||||
|
||||
function fetchPublicContent(type) {
|
||||
console.log(apiBase)
|
||||
return $fetch('/public/content', {
|
||||
baseURL: apiBase,
|
||||
query: { type }
|
||||
})
|
||||
}
|
||||
|
||||
function fetchHomepageSections() {
|
||||
return $fetch('/public/homepage/sections', { baseURL: apiBase })
|
||||
}
|
||||
|
||||
function fetchPublicSiteConfig() {
|
||||
return $fetch('/public/site-config', { baseURL: apiBase })
|
||||
}
|
||||
|
||||
function assetUrl(assetId, mediaMap, fallback = FALLBACK_IMAGE) {
|
||||
if (!assetId) return fallback
|
||||
return pickUrl(mediaMap.get(assetId)) || fallback
|
||||
}
|
||||
|
||||
function assetAlt(assetId, mediaMap, fallback = '') {
|
||||
if (!assetId) return fallback
|
||||
return mediaMap.get(assetId)?.alt || fallback
|
||||
}
|
||||
|
||||
function normalizeCase(item, mediaMap) {
|
||||
const metric = item.metricValue || ''
|
||||
const metricLabel = item.metricLabel || ''
|
||||
const clientName = item.isAnonymous && item.clientName ? `匿名客户:${item.clientName}` : item.clientName
|
||||
|
||||
return {
|
||||
...item,
|
||||
industry: item.industry || item.category || '未分类',
|
||||
client: clientName || '匿名客户',
|
||||
cover: item.coverUrl || assetUrl(item.coverAssetId, mediaMap),
|
||||
coverAlt: item.coverAlt || assetAlt(item.coverAssetId, mediaMap, item.title),
|
||||
summary: item.summary || item.description || '',
|
||||
description: item.description || item.summary || '',
|
||||
metric,
|
||||
metricLabel,
|
||||
metrics: [[metric, metricLabel], [item.secondary, '辅助成果']].filter((row) => row[0]),
|
||||
secondary: item.secondary || '',
|
||||
contentHtml: item.contentHtml || ''
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeArticle(item, mediaMap) {
|
||||
return {
|
||||
...item,
|
||||
category: item.category || '未分类',
|
||||
excerpt: item.summary || item.description || '',
|
||||
date: item.publishDate || item.publishedAt?.slice?.(0, 10) || '',
|
||||
readingTime: item.readingTime || '约 5 分钟',
|
||||
views: item.views || '0',
|
||||
cover: item.coverUrl || assetUrl(item.coverAssetId, mediaMap),
|
||||
coverAlt: item.coverAlt || assetAlt(item.coverAssetId, mediaMap, item.title),
|
||||
featured: Boolean(item.isFeatured),
|
||||
tags: toTags(item.tags),
|
||||
contentHtml: item.contentHtml || ''
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeWhitepaper(item, mediaMap) {
|
||||
const documentAsset = item.documentAssetId ? mediaMap.get(item.documentAssetId) : null
|
||||
|
||||
return {
|
||||
...item,
|
||||
category: item.category || '行业白皮书',
|
||||
excerpt: item.summary || item.description || '',
|
||||
cta: item.cta || '获取白皮书',
|
||||
cover: item.coverUrl || assetUrl(item.coverAssetId, mediaMap),
|
||||
documentUrl: item.documentUrl || pickUrl(documentAsset),
|
||||
documentName: item.documentName || documentAsset?.name || item.title,
|
||||
contentHtml: item.contentHtml || ''
|
||||
}
|
||||
}
|
||||
|
||||
function categoryList(items, field = 'category', extras = []) {
|
||||
const values = items.map((item) => item[field]).filter(Boolean)
|
||||
return [ALL_LABEL, ...extras, ...Array.from(new Set(values))]
|
||||
}
|
||||
|
||||
return {
|
||||
ALL_LABEL,
|
||||
LATEST_LABEL,
|
||||
bySortAndTime,
|
||||
buildMediaMap,
|
||||
fetchPublicContent,
|
||||
fetchHomepageSections,
|
||||
fetchPublicSiteConfig,
|
||||
normalizeCase,
|
||||
normalizeArticle,
|
||||
normalizeWhitepaper,
|
||||
categoryList
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user