mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-11-13 22:11:42 +08:00
feat: 小程序端轮播图重写,接入 Banner 归一化公开接口
- uh-swiper 组件高内聚:内部请求 banners 公开列表,去掉前 5 条 上限,日期角标显示条目 date 快照,新增作者/日期信息浮层 - home.vue 移除配置 list 与审核模式劫持组装,点击分发改为 post→文章详情(postId)、custom→banner-detail 详情页 - 新增 banner-detail 详情页:公开详情接口 + mp-html 富文本 + 外链平台条件编译(非 App 端复制链接 / App 端访问按钮) - 类型与默认配置同步清理(IBannerConfig 移除 type/list) Co-Authored-By: AtomCode (deepseek-v4-flash) <noreply@atomgit.com>
This commit is contained in:
@@ -1,19 +1,33 @@
|
||||
<script lang="ts" setup>
|
||||
/**
|
||||
* 轮播组件(源自旧项目 components/e-swiper,新建复刻)
|
||||
* 支持:图片/视频轮播、今日首推日期指示器(useTop)、标题区域(useTitle)、底部小图指示器(useDot)
|
||||
* 数据高内聚:默认内部请求 plugin-uni-halo 公开 banners 接口(getBanners),支持外部 list 覆盖
|
||||
* 支持:图片轮播、日期角标(useTop,显示当前条目 date 快照)、标题浮层(useTitle)、
|
||||
* 作者/日期信息浮层(useUser)、底部小图指示器(useDot)
|
||||
*/
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { getBanners } from '@/api/uni-halo'
|
||||
import { checkAvatarUrl, checkThumbnailUrl } from '@/utils/url'
|
||||
import type { IBannerPublicItem } from '@/api/types/uni-halo'
|
||||
|
||||
export interface IBannerItem {
|
||||
id: string | number
|
||||
/** 条目标识(Banner 为 metadata.name;兼容旧数据) */
|
||||
id?: string | number
|
||||
/** Banner 条目 metadata.name(custom 详情页跳转用) */
|
||||
name?: string
|
||||
title?: string
|
||||
image?: string
|
||||
src?: string
|
||||
mp4?: string
|
||||
/** 来源:post=文章快照 / custom=自定义 */
|
||||
type?: string
|
||||
/** 文章 id(source=post 时跳转文章详情) */
|
||||
postId?: string
|
||||
content?: string
|
||||
url?: string
|
||||
/** 展示日期(ISO 快照) */
|
||||
date?: string
|
||||
authorName?: string
|
||||
authorAvatar?: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
@@ -21,12 +35,16 @@ const props = withDefaults(defineProps<{
|
||||
title?: string
|
||||
height?: string
|
||||
dotPosition?: string
|
||||
/** 日期角标(显示当前条目 date) */
|
||||
useTop?: boolean
|
||||
/** 底部小图指示器 */
|
||||
useDot?: boolean
|
||||
/** 标题浮层 */
|
||||
useTitle?: boolean
|
||||
/** 作者/日期信息浮层 */
|
||||
useUser?: boolean
|
||||
/** 轮播图数据列表 */
|
||||
list: IBannerItem[]
|
||||
/** 轮播数据列表(可选;不传时组件内部调公开 banners 接口拉取) */
|
||||
list?: IBannerItem[]
|
||||
/** 当前选中的项(指示器坐标位置) */
|
||||
current?: number
|
||||
/** 是否自动轮播 */
|
||||
@@ -54,26 +72,91 @@ const currentIndex = ref(props.current)
|
||||
/** 是否禁止用户 touch 操作 */
|
||||
const disableTouch = ref(false)
|
||||
|
||||
/** 日期(今日首推指示器用) */
|
||||
const date = ref({ year: '-', monthEn: '-', month: '-' })
|
||||
/* ---------------- 数据(高内聚:内部请求公开接口) ---------------- */
|
||||
const internalList = ref<IBannerItem[]>([])
|
||||
|
||||
/* ---------------- 计算属性 ---------------- */
|
||||
/** 仅渲染前 5 条 */
|
||||
const displayList = computed(() => props.list.slice(0, 5))
|
||||
/** 展示列表:外部传入(list)优先,否则使用内部拉取数据 */
|
||||
const displayItems = computed<IBannerItem[]>(() =>
|
||||
props.list && props.list.length > 0 ? props.list : internalList.value,
|
||||
)
|
||||
|
||||
const currentTitle = computed(() => props.list[currentIndex.value]?.title || '')
|
||||
|
||||
/* ---------------- 初始化 ---------------- */
|
||||
function initDate() {
|
||||
const now = new Date()
|
||||
const monthArray = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
||||
date.value.year = String(now.getFullYear())
|
||||
const month = now.getMonth() + 1
|
||||
date.value.month = month < 10 ? `0${month}` : String(month)
|
||||
date.value.monthEn = monthArray[now.getMonth()].toUpperCase()
|
||||
/** 公开 Banner 条目 → 轮播展示项 */
|
||||
function mapBanners(items: IBannerPublicItem[]): IBannerItem[] {
|
||||
return items.map(item => ({
|
||||
id: item.name,
|
||||
name: item.name,
|
||||
title: item.title || '',
|
||||
image: checkThumbnailUrl(item.cover),
|
||||
src: checkThumbnailUrl(item.cover),
|
||||
type: item.source,
|
||||
postId: item.postId,
|
||||
url: item.link,
|
||||
date: item.date,
|
||||
authorName: item.authorName,
|
||||
authorAvatar: item.authorAvatar ? checkAvatarUrl(item.authorAvatar) : '',
|
||||
}))
|
||||
}
|
||||
|
||||
initDate()
|
||||
onMounted(async () => {
|
||||
// 外部已传数据时不再重复请求
|
||||
if (props.list && props.list.length > 0) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const res = await getBanners()
|
||||
internalList.value = mapBanners(res.data || [])
|
||||
}
|
||||
catch (err) {
|
||||
console.error('获取轮播图失败', err)
|
||||
}
|
||||
})
|
||||
|
||||
// 列表变化(外部覆盖/接口返回)后索引越界时归零
|
||||
watch(displayItems, (val) => {
|
||||
if (currentIndex.value >= val.length) {
|
||||
currentIndex.value = 0
|
||||
}
|
||||
})
|
||||
|
||||
/* ---------------- 计算属性 ---------------- */
|
||||
const currentItem = computed<IBannerItem>(() =>
|
||||
displayItems.value[currentIndex.value] || {},
|
||||
)
|
||||
|
||||
/** 日期角标(useTop):当前条目 date 快照转换(年/月/日) */
|
||||
const dateParts = computed(() => {
|
||||
const dateStr = currentItem.value.date as string | undefined
|
||||
if (!dateStr) {
|
||||
return null
|
||||
}
|
||||
const d = new Date(dateStr)
|
||||
if (Number.isNaN(d.getTime())) {
|
||||
return null
|
||||
}
|
||||
const monthArray = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
|
||||
return {
|
||||
day: String(d.getDate()).padStart(2, '0'),
|
||||
month: String(d.getMonth() + 1).padStart(2, '0'),
|
||||
monthEn: monthArray[d.getMonth()],
|
||||
year: String(d.getFullYear()),
|
||||
}
|
||||
})
|
||||
|
||||
const currentTitle = computed(() => currentItem.value.title || props.title || '')
|
||||
|
||||
/** 作者日期展示(useUser 用) */
|
||||
const authorDateText = computed(() => {
|
||||
const dateStr = currentItem.value.date as string | undefined
|
||||
if (!dateStr) {
|
||||
return ''
|
||||
}
|
||||
const d = new Date(dateStr)
|
||||
if (Number.isNaN(d.getTime())) {
|
||||
return ''
|
||||
}
|
||||
const pad = (n: number) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
||||
})
|
||||
|
||||
/* ---------------- 交互 ---------------- */
|
||||
/** current 改变时会触发 change 事件,event.detail = {current, source} */
|
||||
@@ -100,8 +183,7 @@ function handleOnClick(item: IBannerItem) {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- 轮播图 -->
|
||||
<view class="uh-e-swiper">
|
||||
<view v-if="displayItems.length > 0" class="uh-e-swiper">
|
||||
<view class="swiper-box" :class="[dotPosition]">
|
||||
<swiper
|
||||
class="swiper"
|
||||
@@ -115,48 +197,27 @@ function handleOnClick(item: IBannerItem) {
|
||||
:disable-touch="disableTouch"
|
||||
@change="change"
|
||||
>
|
||||
<!-- 只需要前5条数据 -->
|
||||
<block v-for="(item, index) in list" :key="index">
|
||||
<swiper-item v-if="index <= 4" class="swiper-mfw-item">
|
||||
<!-- 如果有视频,则显示视频 -->
|
||||
<template v-if="item.mp4 && currentIndex === index">
|
||||
<video
|
||||
:id="`ImageVideo${index}`"
|
||||
:src="item.mp4"
|
||||
class="image-video h-full w-full"
|
||||
:loop="true"
|
||||
:muted="false"
|
||||
:autoplay="true"
|
||||
:controls="false"
|
||||
:show-fullscreen-btn="false"
|
||||
:show-play-btn="false"
|
||||
:enable-progress-gesture="false"
|
||||
:poster="item.image || item.src"
|
||||
/>
|
||||
</template>
|
||||
<!-- 否则显示图片 -->
|
||||
<image
|
||||
v-else
|
||||
:src="item.image || item.src"
|
||||
class="image h-full w-full"
|
||||
mode="aspectFill"
|
||||
@click.stop="handleOnClick(item)"
|
||||
/>
|
||||
</swiper-item>
|
||||
</block>
|
||||
<swiper-item v-for="(item, index) in displayItems" :key="index" class="swiper-mfw-item">
|
||||
<image
|
||||
:src="item.image || item.src"
|
||||
class="image h-full w-full"
|
||||
mode="aspectFill"
|
||||
@click.stop="handleOnClick(item)"
|
||||
/>
|
||||
</swiper-item>
|
||||
</swiper>
|
||||
|
||||
<!-- 指示器 [Top 今日首推] -->
|
||||
<view v-if="useTop" class="indicator-box indicator-top-box">
|
||||
<!-- 指示器 [Top 日期角标]:显示当前条目 date(年/月/日) -->
|
||||
<view v-if="useTop && dateParts" class="indicator-box indicator-top-box">
|
||||
<view class="top-date-hot">
|
||||
<view class="left-date-ri">
|
||||
<text class="date-ri-text">{{ date.month }}</text>
|
||||
<text class="date-ri-text">{{ dateParts.day }}</text>
|
||||
</view>
|
||||
<view class="center-date-nianyue">
|
||||
<view class="left-width-bgcolor" />
|
||||
<view class="right-date-nianyue">
|
||||
<text class="top-yue-usa">{{ date.monthEn }}</text>
|
||||
<text class="bottom-nian">{{ date.year }}</text>
|
||||
<text class="top-yue-usa">{{ dateParts.monthEn }}</text>
|
||||
<text class="bottom-nian">{{ dateParts.year }}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="right-hot-ttf">
|
||||
@@ -165,39 +226,32 @@ function handleOnClick(item: IBannerItem) {
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 指示器 标题区域 -->
|
||||
<!-- 指示器 标题区域 + 作者/日期信息(useUser) -->
|
||||
<view v-if="useTitle" class="indicator-top" :class="{ 'no-dot': !useDot }">
|
||||
<block v-for="(item, index) in list" :key="index">
|
||||
<view v-if="currentIndex === index" class="top-item" :class="currentIndex === index ? 'current' : 'no'">
|
||||
<!-- 如果存在视频,则显示"视频预览"提示 -->
|
||||
<view v-if="item.mp4" class="top-image-video">
|
||||
<view class="icons">
|
||||
<text class="video-icon">▶</text>
|
||||
</view>
|
||||
<text class="image-video-text">视频预览</text>
|
||||
</view>
|
||||
<!-- 标题 -->
|
||||
<view class="top-title">
|
||||
<text class="title-text text-overflow-2">{{ item.title }}</text>
|
||||
</view>
|
||||
<view v-if="useUser && (currentItem.authorName || authorDateText)" class="author-line">
|
||||
<view v-if="currentItem.authorAvatar" class="author-avatar">
|
||||
<image :src="currentItem.authorAvatar" class="h-full w-full" mode="aspectFill" />
|
||||
</view>
|
||||
</block>
|
||||
<text class="author-name">{{ currentItem.authorName }}</text>
|
||||
<text v-if="authorDateText" class="author-date">{{ authorDateText }}</text>
|
||||
</view>
|
||||
<view v-if="currentTitle" class="top-title">
|
||||
<text class="title-text text-overflow-2">{{ currentTitle }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 指示器 [左边图片列表+右边按钮] -->
|
||||
<!-- 指示器 [左边图片列表] -->
|
||||
<view v-if="useDot" class="indicator-bottom">
|
||||
<!-- 左边 -->
|
||||
<view class="bottom-left-imagelist">
|
||||
<block v-for="(item, index) in list" :key="index">
|
||||
<view
|
||||
v-if="Number(index) <= 4"
|
||||
class="bottom-item"
|
||||
:class="currentIndex === index ? 'current' : 'no'"
|
||||
@click="swiperIndTap(index)"
|
||||
>
|
||||
<image :src="item.image || item.src" class="image h-full w-full" mode="aspectFill" />
|
||||
</view>
|
||||
</block>
|
||||
<view
|
||||
v-for="(item, index) in displayItems"
|
||||
:key="index"
|
||||
class="bottom-item"
|
||||
:class="currentIndex === index ? 'current' : 'no'"
|
||||
@click="swiperIndTap(index)"
|
||||
>
|
||||
<image :src="item.image || item.src" class="image h-full w-full" mode="aspectFill" />
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -220,7 +274,7 @@ function handleOnClick(item: IBannerItem) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 今日首推指示器 */
|
||||
/* 日期角标(顶部) */
|
||||
.indicator-top-box {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
@@ -285,44 +339,50 @@ function handleOnClick(item: IBannerItem) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 标题区域 */
|
||||
/* 底部标题/作者浮层 */
|
||||
.indicator-top {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 60rpx;
|
||||
bottom: 0;
|
||||
z-index: 5;
|
||||
padding: 0 24rpx;
|
||||
padding: 48rpx 24rpx 20rpx;
|
||||
background: linear-gradient(to top, rgb(0 0 0 / 45%), transparent);
|
||||
|
||||
.top-item {
|
||||
.top-image-video {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
.author-line {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
margin-bottom: 8rpx;
|
||||
|
||||
.icons {
|
||||
.video-icon {
|
||||
font-size: 20rpx;
|
||||
color: #fff;
|
||||
}
|
||||
}
|
||||
|
||||
.image-video-text {
|
||||
font-size: 20rpx;
|
||||
color: #fff;
|
||||
}
|
||||
.author-avatar {
|
||||
width: 36rpx;
|
||||
height: 36rpx;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
border: 1rpx solid rgb(255 255 255 / 60%);
|
||||
}
|
||||
|
||||
.top-title {
|
||||
margin-top: 8rpx;
|
||||
.author-name {
|
||||
font-size: 22rpx;
|
||||
color: rgb(255 255 255 / 92%);
|
||||
text-shadow: 0 1rpx 4rpx rgb(0 0 0 / 40%);
|
||||
}
|
||||
|
||||
.title-text {
|
||||
display: block;
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
text-shadow: 0 2rpx 8rpx rgb(0 0 0 / 40%);
|
||||
}
|
||||
.author-date {
|
||||
font-size: 20rpx;
|
||||
color: rgb(255 255 255 / 70%);
|
||||
text-shadow: 0 1rpx 4rpx rgb(0 0 0 / 40%);
|
||||
}
|
||||
}
|
||||
|
||||
.top-title {
|
||||
.title-text {
|
||||
display: block;
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
text-shadow: 0 2rpx 8rpx rgb(0 0 0 / 40%);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -335,6 +395,8 @@ function handleOnClick(item: IBannerItem) {
|
||||
bottom: 16rpx;
|
||||
z-index: 5;
|
||||
padding: 0 24rpx;
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
|
||||
.bottom-left-imagelist {
|
||||
display: flex;
|
||||
|
||||
Reference in New Issue
Block a user