feat(home): 替换首页轮播图实现为广告系统驱动
- 移除原有的硬编码轮播图组件和相关样式 - 新增 getAdByCode 方法用于获取广告数据 - 实现解析广告数据的工具函数 parseSlides 和 parsePx - 集成 useAsyncData 获取 flash 广告数据 - 添加备用图片以确保加载失败时的显示 - 更新页面样式适配新的轮播组件结构
This commit is contained in:
@@ -8,15 +8,16 @@
|
|||||||
:autoplay-speed="autoplaySpeed"
|
:autoplay-speed="autoplaySpeed"
|
||||||
:dots="dotsEnabled"
|
:dots="dotsEnabled"
|
||||||
:effect="effect"
|
:effect="effect"
|
||||||
|
:style="{ height: heightCss }"
|
||||||
>
|
>
|
||||||
<div v-for="it in normalizedItems" :key="it.src" class="carousel-item">
|
<div v-for="it in normalizedItems" :key="it.src" class="carousel-item" :style="{ height: heightCss }">
|
||||||
<NuxtLink
|
<NuxtLink
|
||||||
v-if="it.href && !isExternal(it.href)"
|
v-if="it.href && !isExternal(it.href)"
|
||||||
:to="it.href"
|
:to="it.href"
|
||||||
class="carousel-link"
|
class="carousel-link"
|
||||||
aria-label="carousel-slide"
|
aria-label="carousel-slide"
|
||||||
>
|
>
|
||||||
<div class="carousel-bg" :style="{ backgroundImage: `url(${it.src})` }"></div>
|
<img class="carousel-img" :src="it.src" :alt="it.alt || 'slide'" loading="lazy" />
|
||||||
</NuxtLink>
|
</NuxtLink>
|
||||||
<a
|
<a
|
||||||
v-else-if="it.href"
|
v-else-if="it.href"
|
||||||
@@ -26,14 +27,14 @@
|
|||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
aria-label="carousel-slide"
|
aria-label="carousel-slide"
|
||||||
>
|
>
|
||||||
<div class="carousel-bg" :style="{ backgroundImage: `url(${it.src})` }"></div>
|
<img class="carousel-img" :src="it.src" :alt="it.alt || 'slide'" loading="lazy" />
|
||||||
</a>
|
</a>
|
||||||
<div v-else class="carousel-bg" :style="{ backgroundImage: `url(${it.src})` }"></div>
|
<img v-else class="carousel-img" :src="it.src" :alt="it.alt || 'slide'" loading="lazy" />
|
||||||
</div>
|
</div>
|
||||||
</a-carousel>
|
</a-carousel>
|
||||||
|
|
||||||
<template #fallback>
|
<template #fallback>
|
||||||
<div class="carousel-bg" :style="{ height: heightCss, backgroundImage: `url(${fallbackSrc})` }"></div>
|
<img class="carousel-img" :style="{ height: heightCss }" :src="fallbackSrc" alt="slide" />
|
||||||
</template>
|
</template>
|
||||||
</ClientOnly>
|
</ClientOnly>
|
||||||
</div>
|
</div>
|
||||||
@@ -44,6 +45,7 @@
|
|||||||
type CarouselItem = {
|
type CarouselItem = {
|
||||||
src: string
|
src: string
|
||||||
href?: string
|
href?: string
|
||||||
|
alt?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(
|
const props = withDefaults(
|
||||||
@@ -77,8 +79,6 @@ function isExternal(href: string) {
|
|||||||
<style scoped>
|
<style scoped>
|
||||||
.carousel-panel {
|
.carousel-panel {
|
||||||
background: #fff;
|
background: #fff;
|
||||||
border: 1px solid rgba(0, 0, 0, 0.06);
|
|
||||||
border-radius: 10px;
|
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
|
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
|
||||||
}
|
}
|
||||||
@@ -96,11 +96,11 @@ function isExternal(href: string) {
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.carousel-bg {
|
.carousel-img {
|
||||||
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background-size: cover;
|
object-fit: cover;
|
||||||
background-position: center;
|
display: block;
|
||||||
background-repeat: no-repeat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.carousel :deep(.slick-list),
|
.carousel :deep(.slick-list),
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="showBrandbar" class="brandbar">
|
<div v-if="showBrandbar" class="brandbar">
|
||||||
<div class="mx-auto grid max-w-screen-xl grid-cols-12 items-center gap-6 px-4 py-8">
|
<div class="mx-auto grid max-w-screen-xl grid-cols-12 items-center gap-6 px-4 py-4">
|
||||||
<NuxtLink to="/" class="col-span-12 flex items-center gap-4 md:col-span-6">
|
<NuxtLink to="/" class="col-span-12 flex items-center gap-4 md:col-span-6">
|
||||||
<img class="brand-logo" :src="`https://oss.wsdns.cn/20260127/989e5cf82b0847ed9168023baf68f4a9.png`" :alt="siteName" />
|
<img class="brand-logo" :src="`https://oss.wsdns.cn/20260127/989e5cf82b0847ed9168023baf68f4a9.png`" :alt="siteName" />
|
||||||
</NuxtLink>
|
</NuxtLink>
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
<template>
|
<template>
|
||||||
<main class="home">
|
<main class="home">
|
||||||
<Carousel class="px-4 py-8" :items="flashSlides" :height="flashHeight" />
|
<Carousel arrows class="mx-auto" :items="flashSlides" :height="flashHeight">
|
||||||
|
<template #prevArrow>
|
||||||
<section class="banner">
|
<div class="custom-slick-arrow" style="left: 10px; z-index: 1">
|
||||||
<div class="mx-auto max-w-screen-xl px-4 py-10">
|
<left-circle-outlined />
|
||||||
<div class="banner-title">以合规经营与品质服务为核心</div>
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</template>
|
||||||
|
<template #nextArrow>
|
||||||
|
<div class="custom-slick-arrow" style="right: 10px">
|
||||||
|
<right-circle-outlined />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</Carousel>
|
||||||
|
|
||||||
<section class="mx-auto max-w-screen-xl px-4 py-10">
|
<!-- <section class="banner">-->
|
||||||
|
<!-- <div class="mx-auto max-w-screen-xl px-4 py-6">-->
|
||||||
|
<!-- <div class="banner-title">以合规经营与品质服务为核心</div>-->
|
||||||
|
<!-- </div>-->
|
||||||
|
<!-- </section>-->
|
||||||
|
|
||||||
|
<section class="mx-auto max-w-screen-xl px-4 py-6">
|
||||||
<div class="section-title">
|
<div class="section-title">
|
||||||
<div class="section-title-main">资讯与公告</div>
|
<div class="section-title-main">资讯与公告</div>
|
||||||
<div class="section-title-sub">NEWS & UPDATES</div>
|
<div class="section-title-sub">NEWS & UPDATES</div>
|
||||||
@@ -63,8 +74,8 @@
|
|||||||
import {
|
import {
|
||||||
AppstoreOutlined,
|
AppstoreOutlined,
|
||||||
FileTextOutlined,
|
FileTextOutlined,
|
||||||
IdcardOutlined,
|
LeftCircleOutlined,
|
||||||
PhoneOutlined,
|
RightCircleOutlined,
|
||||||
SafetyCertificateOutlined,
|
SafetyCertificateOutlined,
|
||||||
ShopOutlined
|
ShopOutlined
|
||||||
} from '@ant-design/icons-vue'
|
} from '@ant-design/icons-vue'
|
||||||
@@ -88,64 +99,29 @@ function parsePx(value?: string) {
|
|||||||
return Number.isFinite(n) ? n : undefined
|
return Number.isFinite(n) ? n : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseSlides(ad?: CmsAd | null) {
|
type FlashImage = {
|
||||||
const raw = ad?.imageList
|
url?: string
|
||||||
const fallbackHref = ad?.path
|
path?: string
|
||||||
|
title?: string
|
||||||
function pickString(obj: Record<string, unknown>, keys: string[]) {
|
|
||||||
for (const k of keys) {
|
|
||||||
const v = obj[k]
|
|
||||||
if (typeof v === 'string' && v.trim()) return v
|
|
||||||
}
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
function asArray(val: unknown): unknown[] {
|
|
||||||
if (!val) return []
|
|
||||||
if (Array.isArray(val)) return val
|
|
||||||
if (typeof val === 'string') {
|
|
||||||
const text = val.trim()
|
|
||||||
if (!text) return []
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(text)
|
|
||||||
return Array.isArray(parsed) ? parsed : [parsed]
|
|
||||||
} catch {
|
|
||||||
return text.split(/[,;\n]+/g).map((s) => s.trim()).filter(Boolean)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof val === 'object') {
|
|
||||||
const obj = val as Record<string, unknown>
|
|
||||||
if (Array.isArray(obj.list)) return obj.list
|
|
||||||
if (Array.isArray(obj.imageList)) return obj.imageList
|
|
||||||
if (Array.isArray(obj.images)) return obj.images
|
|
||||||
return [obj]
|
|
||||||
}
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
return asArray(raw)
|
|
||||||
.map((it) => {
|
|
||||||
if (typeof it === 'string') return { src: it, href: fallbackHref }
|
|
||||||
if (it && typeof it === 'object') {
|
|
||||||
const obj = it as Record<string, unknown>
|
|
||||||
const src = pickString(obj, ['src', 'url', 'image', 'imageUrl', 'img', 'imgUrl', 'pic', 'picUrl'])
|
|
||||||
const href = pickString(obj, ['href', 'link', 'path', 'to']) ?? fallbackHref
|
|
||||||
if (!src) return null
|
|
||||||
return { src, href }
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
})
|
|
||||||
.filter(Boolean) as Array<{ src: string; href?: string }>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: flashAd } = await useAsyncData('cms-ad-flash', () =>
|
const { data: flashAd } = await useAsyncData<CmsAd | null>('cms-ad-flash', () =>
|
||||||
getAdByCode('flash').catch(() => null)
|
getAdByCode('flash').catch(() => null)
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log(flashAd.value?.imageList,'flashAdflashAdflashAd');
|
|
||||||
|
|
||||||
const flashSlides = computed(() => {
|
const flashSlides = computed(() => {
|
||||||
const slides = parseSlides(flashAd.value)
|
const list = (flashAd.value?.imageList || []) as FlashImage[]
|
||||||
|
const slides = list
|
||||||
|
.map((it) => {
|
||||||
|
if (!it?.url) return null
|
||||||
|
return {
|
||||||
|
src: it.url,
|
||||||
|
href: it.path || undefined,
|
||||||
|
alt: it.title || undefined
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean) as Array<{ src: string; href?: string; alt?: string }>
|
||||||
|
|
||||||
return slides.length
|
return slides.length
|
||||||
? slides
|
? slides
|
||||||
: [{ src: 'https://file-cloud.yst.com.cn/photo/website/2024/11/28/5a5cc07336224e54a84561c80899bcac.jpg' }]
|
: [{ src: 'https://file-cloud.yst.com.cn/photo/website/2024/11/28/5a5cc07336224e54a84561c80899bcac.jpg' }]
|
||||||
@@ -444,4 +420,33 @@ function scrollToCompany() {
|
|||||||
color: rgba(0, 0, 0, 0.55);
|
color: rgba(0, 0, 0, 0.55);
|
||||||
line-height: 1.6;
|
line-height: 1.6;
|
||||||
}
|
}
|
||||||
|
:deep(.slick-slide) {
|
||||||
|
text-align: center;
|
||||||
|
height: 160px;
|
||||||
|
line-height: 160px;
|
||||||
|
background: #364d79;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.slick-arrow.custom-slick-arrow) {
|
||||||
|
width: 25px;
|
||||||
|
height: 25px;
|
||||||
|
font-size: 25px;
|
||||||
|
color: #fff;
|
||||||
|
background-color: rgba(31, 45, 61, 0.11);
|
||||||
|
transition: ease all 0.3s;
|
||||||
|
opacity: 0.3;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
:deep(.slick-arrow.custom-slick-arrow:before) {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
:deep(.slick-arrow.custom-slick-arrow:hover) {
|
||||||
|
color: #fff;
|
||||||
|
opacity: 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.slick-slide h3) {
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user