文件
xionggeshop/xgsc.gynhc.com_wx/pages/index/user/user-promo-banner.vue
2026-09-14 17:24:53 +08:00

159 行
3.3 KiB
Vue

<template>
<view v-if="displayList.length" class="user-promo-swiper">
<!-- 单图直接渲染,避免 swiper 嵌套导致真机有 src 但不显示 -->
<image
v-if="displayList.length === 1"
class="user-promo-swiper__img"
:src="currentImage"
mode="widthFix"
:lazy-load="false"
show-menu-by-longpress
@tap="onTap(0)"
@error="onImageError"
></image>
<swiper
v-else
class="user-promo-swiper__swiper"
:style="{ height: bannerHeight + 'rpx' }"
:indicator-dots="displayList.length > 1"
:circular="displayList.length > 1"
:autoplay="displayList.length > 1"
interval="5000"
duration="500"
>
<swiper-item
v-for="(item, index) in displayList"
:key="item.image || index"
@tap="onTap(index)"
>
<image
class="user-promo-swiper__slide"
:src="item.image"
mode="aspectFill"
:lazy-load="false"
></image>
</swiper-item>
</swiper>
</view>
</template>
<script>
import { mapGetters } from 'vuex';
import { IMG_URL } from '@/env.js';
function normalizeImageUrl(url, baseUrl = IMG_URL) {
if (!url || typeof url !== 'string') {
return '';
}
const value = url.trim();
if (/^https?:\/\//i.test(value)) {
return value;
}
if (value.startsWith('//')) {
return `https:${value}`;
}
const base = (baseUrl || '').replace(/\/$/, '');
return value.startsWith('/') ? `${base}${value}` : `${base}/${value}`;
}
function getDefaultList(baseUrl = IMG_URL) {
const base = `${baseUrl}/uploads/demo/xiongge`;
return [
{
image: `${base}/user-promo-banner-1.jpg`,
path: '/pages/index/index'
},
{
image: `${base}/user-promo-banner-2.jpg`,
path: '/pages/app/score/list'
}
];
}
export default {
data() {
return {
fallbackIndex: 0
};
},
computed: {
...mapGetters(['userTemplate']),
promoBannerModule() {
return (this.userTemplate || []).find(item => item.type === 'banner');
},
displayList() {
const list = this.promoBannerModule?.content?.list || [];
const valid = list
.filter(item => item && item.image)
.map(item => ({
...item,
image: normalizeImageUrl(item.image, this.$IMG_URL)
}))
.filter(item => item.image);
return valid.length ? valid : getDefaultList(this.$IMG_URL);
},
bannerHeight() {
const height = Number(this.promoBannerModule?.content?.height);
return height > 0 ? height : 163;
},
fallbackImages() {
return getDefaultList(this.$IMG_URL).map(item => item.image);
},
currentImage() {
const item = this.displayList[0];
if (!item) {
return '';
}
if (this.fallbackIndex === 0) {
return item.image;
}
return this.fallbackImages[this.fallbackIndex - 1] || item.image;
}
},
watch: {
displayList() {
this.fallbackIndex = 0;
}
},
methods: {
onTap(index) {
const item = this.displayList[index];
if (item && item.path) {
this.$tools.routerTo(item.path);
}
},
onImageError() {
if (this.fallbackIndex < this.fallbackImages.length) {
this.fallbackIndex += 1;
}
}
}
};
</script>
<style lang="scss">
.user-promo-swiper {
width: 100%;
background: #fff;
border-radius: $brand-card-radius;
overflow: hidden;
box-shadow: $brand-shadow;
}
.user-promo-swiper__img {
width: 100%;
display: block;
vertical-align: top;
}
.user-promo-swiper__swiper {
width: 100%;
}
.user-promo-swiper__slide {
width: 100%;
height: 100%;
display: block;
}
</style>