Skip to content

Commit 1e039cb

Browse files
feat: ✨ Swiper 支持指定轮播项的文件类型 (#720)
Closes: #712
1 parent 314c2e8 commit 1e039cb

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

docs/component/swiper.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,13 @@ const isLoop = ref(false)
346346

347347
### SwiperList
348348

349-
轮播图项的列表配置,包括 图片或视频地址`value`、视频封面`poster` 等属性,支持扩展属性。
349+
轮播图项的列表配置,包括 图片或视频地址`value`、视频封面`poster` 、文件资源的类型`type`等属性,支持扩展属性。指定`type`后组件将不在内部判断文件类型,以`type`为准。
350+
| name | 说明 | 最低版本 |
351+
| --------- | ------------ | -------- |
352+
| value | 图片或视频地址 |- |
353+
| poster | 视频封面 |- |
354+
| type | 用于指定文件资源的类型,可选值`image``video` | $LOWEST_VERSION$ |
355+
350356

351357
### SwiperIndicatorProps
352358

src/uni_modules/wot-design-uni/components/wd-swiper/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ export type IndicatorPositionType = 'left' | 'top-left' | 'top' | 'top-right' |
2626
*/
2727
export type AdjustHeightType = 'first' | 'current' | 'highest' | 'none'
2828

29+
// 资源类型
30+
export type SwiperItemType = 'image' | 'video'
31+
2932
export interface SwiperList {
3033
[key: string]: any
3134
// 图片、视频等资源地址
3235
value?: string
3336
// 视频资源的封面
3437
poster?: string
38+
// 资源文件类型,可选值:'image' | 'video'
39+
type?: SwiperItemType
3540
}
3641

3742
export const swiperProps = {

src/uni_modules/wot-design-uni/components/wd-swiper/wd-swiper.vue

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
>
2222
<swiper-item v-for="(item, index) in list" :key="index" class="wd-swiper__item" @click="handleClick(index, item)">
2323
<image
24-
v-if="isImageUrl(isObj(item) ? item[valueKey] : item)"
24+
v-if="isImage(item)"
2525
:src="isObj(item) ? item[valueKey] : item"
2626
:class="`wd-swiper__image ${customImageClass} ${customItemClass} ${getCustomItemClass(navCurrent, index, list)}`"
2727
:style="{ height: addUnit(height) }"
2828
:mode="imageMode"
2929
/>
3030
<video
31-
v-else
31+
v-else-if="isVideo(item)"
3232
:id="`video-${index}-${uid}`"
3333
:style="{ height: addUnit(height) }"
3434
:src="isObj(item) ? item[valueKey] : item"
@@ -77,7 +77,7 @@ export default {
7777
<script lang="ts" setup>
7878
import wdSwiperNav from '../wd-swiper-nav/wd-swiper-nav.vue'
7979
import { computed, watch, ref, getCurrentInstance } from 'vue'
80-
import { addUnit, isObj, isImageUrl, isVideoUrl, uuid } from '../common/util'
80+
import { addUnit, isObj, isImageUrl, isVideoUrl, uuid, isDef } from '../common/util'
8181
import { swiperProps, type SwiperList } from './types'
8282
import type { SwiperNavProps } from '../wd-swiper-nav/types'
8383
@@ -122,6 +122,22 @@ const swiperIndicator = computed(() => {
122122
return swiperIndicator
123123
})
124124
125+
const getMediaType = (item: string | SwiperList, type: 'video' | 'image') => {
126+
if (isObj(item)) {
127+
return item.type ? item.type === type : type === 'video' ? isVideoUrl(item[props.valueKey]) : isImageUrl(item[props.valueKey])
128+
} else {
129+
return type === 'video' ? isVideoUrl(item) : isImageUrl(item)
130+
}
131+
}
132+
133+
const isVideo = (item: string | SwiperList) => {
134+
return getMediaType(item, 'video')
135+
}
136+
137+
const isImage = (item: string | SwiperList) => {
138+
return getMediaType(item, 'image')
139+
}
140+
125141
function go(index: number) {
126142
navCurrent.value = index
127143
}
@@ -205,12 +221,9 @@ function handleVideoChange(previous: number, current: number) {
205221
function handleStartVideoPaly(index: number) {
206222
if (props.autoplayVideo) {
207223
const currentItem = props.list[index]
208-
if (currentItem) {
209-
const url = isObj(currentItem) ? currentItem.url : currentItem
210-
if (isVideoUrl(url)) {
211-
const video = uni.createVideoContext(`video-${index}-${uid.value}`, proxy)
212-
video.play()
213-
}
224+
if (isDef(currentItem) && isVideo(currentItem)) {
225+
const video = uni.createVideoContext(`video-${index}-${uid.value}`, proxy)
226+
video.play()
214227
}
215228
}
216229
}
@@ -222,12 +235,9 @@ function handleStartVideoPaly(index: number) {
222235
function handleStopVideoPaly(index: number) {
223236
if (props.stopPreviousVideo) {
224237
const previousItem = props.list[index]
225-
if (previousItem) {
226-
const url = isObj(previousItem) ? previousItem.url : previousItem
227-
if (isVideoUrl(url)) {
228-
const video = uni.createVideoContext(`video-${index}-${uid.value}`, proxy)
229-
video.pause()
230-
}
238+
if (isDef(previousItem) && isVideo(previousItem)) {
239+
const video = uni.createVideoContext(`video-${index}-${uid.value}`, proxy)
240+
video.pause()
231241
}
232242
} else if (props.stopAutoplayWhenVideoPlay) {
233243
handleVideoPause()

0 commit comments

Comments
 (0)