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>
7878import wdSwiperNav from ' ../wd-swiper-nav/wd-swiper-nav.vue'
7979import { 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'
8181import { swiperProps , type SwiperList } from ' ./types'
8282import 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+
125141function go(index : number ) {
126142 navCurrent .value = index
127143}
@@ -205,12 +221,9 @@ function handleVideoChange(previous: number, current: number) {
205221function 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) {
222235function 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