feat:开屏页视频播放优化

This commit is contained in:
2026-05-28 14:39:00 +08:00
parent 7a75f8b4bf
commit 864f9ce627
+138 -26
View File
@@ -5,27 +5,34 @@
class="bg"
:src="bgSrc"
autoplay
loop
muted
:controls="false"
:show-fullscreen-btn="false"
:show-play-btn="false"
:show-center-play-btn="false"
:enable-progress-gesture="false"
:show-loading="true"
playsinline
webkit-playsinline
object-fit="cover"
@error="onMediaError"
@loadedmetadata="onVideoMetadata"
@canplay="onVideoReady"
@play="onVideoReady"
@ended="onVideoEnded"
@click="onMediaClick"
/>
<image
v-else-if="bgSrc"
v-else-if="bgSrc && !isVideo"
class="bg"
:src="bgSrc"
mode="aspectFill"
@error="onMediaError"
@load="onImageLoaded"
@click="onMediaClick"
/>
<view class="count-down" :class="{ enabled: canEnter }" @click.stop="onEnterClick">
{{ canEnter ? '进入' : `跳过 ${countDown}s` }}
<view class="count-down" @click.stop="toHome">
{{ `跳过 ${countDown}s` }}
</view>
</view>
</template>
@@ -33,9 +40,32 @@
import { ref, onMounted, onUnmounted } from 'vue'
import * as IndexApi from '@/sheep/api/index/index'
const DEFAULT_IMAGE_SECONDS = 3
const MAX_VIDEO_SECONDS = 15
const bgSrc = ref('')
const jumpUrl = ref('')
const isVideo = ref(false)
const mediaReady = ref(false)
let downloadToken = 0
let readyFallbackTimer = null
let countDownTimer = null
let jumpTimer = null
const normalizeAppFileUrl = (path) => {
const p = String(path || '')
if (!p) return ''
if (/^https?:\/\//i.test(p)) return p
if (p.startsWith('file://')) return p
try {
if (typeof plus !== 'undefined' && plus?.io?.convertLocalFileSystemURL) {
const next = plus.io.convertLocalFileSystemURL(p)
return next || p
}
} catch (e) {}
if (p.startsWith('/')) return `file://${p}`
return p
}
const isVideoUrl = (url) => {
const u = String(url || '')
@@ -52,9 +82,32 @@ const fetchStartPage = async () => {
const media = String(data?.picUrl || '').trim()
const url = String(data?.url || '').trim()
if (!media) return false
bgSrc.value = media
jumpUrl.value = url
isVideo.value = isVideoUrl(media)
if (isVideo.value && typeof plus !== 'undefined') {
const token = (downloadToken += 1)
const downloadPromise = new Promise((resolve) => {
uni.downloadFile({
url: media,
success: (r) => resolve(r?.statusCode === 200 ? r.tempFilePath : ''),
fail: () => resolve(''),
})
})
downloadPromise.then((localPath) => {
const normalized = normalizeAppFileUrl(localPath)
if (!normalized) return
if (token !== downloadToken) return
if (mediaReady.value) return
bgSrc.value = normalized
})
const fastLocal = await Promise.race([
downloadPromise,
new Promise((resolve) => setTimeout(() => resolve(''), 1500)),
])
bgSrc.value = normalizeAppFileUrl(fastLocal) || media
return true
}
bgSrc.value = media
return true
} catch (e) {
return false
@@ -64,33 +117,100 @@ const fetchStartPage = async () => {
function onMediaError() {
bgSrc.value = ''
isVideo.value = false
mediaReady.value = false
startCountDown(DEFAULT_IMAGE_SECONDS)
}
const countDown = ref(3)
let timer = null
const canEnter = ref(false)
const countDown = ref(DEFAULT_IMAGE_SECONDS)
onMounted(() => {
fetchStartPage()
timer = setInterval(() => {
function clearTimers() {
if (readyFallbackTimer) clearTimeout(readyFallbackTimer)
readyFallbackTimer = null
if (countDownTimer) clearInterval(countDownTimer)
countDownTimer = null
if (jumpTimer) clearTimeout(jumpTimer)
jumpTimer = null
}
function startCountDown(seconds) {
const s = Math.max(1, Math.floor(Number(seconds) || 0))
countDown.value = s
if (countDownTimer) clearInterval(countDownTimer)
countDownTimer = setInterval(() => {
if (countDown.value <= 1) {
//点击进入
// countDown.value = 0
// canEnter.value = true
clearInterval(timer)
//直接进入
if (countDownTimer) clearInterval(countDownTimer)
countDownTimer = null
toHome()
return
}
countDown.value -= 1
}, 1000)
}
function scheduleJumpAfter(seconds) {
const ms = Math.max(0, Math.floor(Number(seconds) * 1000))
if (jumpTimer) clearTimeout(jumpTimer)
jumpTimer = setTimeout(() => {
toHome()
}, ms)
}
function onImageLoaded() {
if (mediaReady.value) return
mediaReady.value = true
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}
function onVideoReady() {
if (!mediaReady.value) {
mediaReady.value = true
}
if (!countDownTimer) {
startCountDown(MAX_VIDEO_SECONDS)
scheduleJumpAfter(MAX_VIDEO_SECONDS + 0.2)
}
}
function onVideoMetadata(e) {
if (!mediaReady.value) {
mediaReady.value = true
}
const duration = Number(e?.detail?.duration || e?.detail?.totalTime || 0)
const limit = duration > 0 ? Math.min(MAX_VIDEO_SECONDS, Math.ceil(duration)) : MAX_VIDEO_SECONDS
startCountDown(limit)
scheduleJumpAfter(limit + 0.2)
}
function onVideoEnded() {
toHome()
}
onMounted(() => {
fetchStartPage().then((ok) => {
if (!ok) {
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
return
}
readyFallbackTimer = setTimeout(() => {
if (countDownTimer) return
if (isVideo.value) {
onVideoReady()
return
}
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}, isVideo.value ? 2500 : 1200)
})
})
onUnmounted(() => {
if (timer) clearInterval(timer)
clearTimers()
})
function toHome() {
clearTimers()
uni.reLaunch({
url: '/pages/index/index',
})
@@ -110,11 +230,6 @@ function onMediaClick() {
return
}
}
function onEnterClick() {
if (!canEnter.value) return
toHome()
}
</script>
<style lang="scss" scoped>
.start-page {
@@ -141,9 +256,6 @@ function onEnterClick() {
line-height: 1;
color: #fff;
background: rgba(0, 0, 0, 0.4);
z-index: 10;
}
.count-down.enabled {
// background: rgba(54, 110, 238, 0.85);
z-index: 9999;
}
</style>