Files
kangyuan-mobile/pages/index/startPage.vue
T

292 lines
7.1 KiB
Vue

<template>
<view class="start-page">
<image v-if="USE_STATIC_START_PAGE" class="bg" src="/static/startPage.jpeg" mode="aspectFill" />
<video
v-if="!USE_STATIC_START_PAGE && bgSrc && isVideo"
class="bg"
:src="bgSrc"
autoplay
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="!USE_STATIC_START_PAGE && bgSrc && !isVideo"
class="bg"
:src="bgSrc"
mode="aspectFill"
@error="onMediaError"
@load="onImageLoaded"
@click="onMediaClick"
/>
<cover-view v-if="!USE_STATIC_START_PAGE && bgSrc && isVideo" class="count-down" @click.stop="toHome">
<cover-view class="count-down-text">{{ `跳过 ${countDown}s` }}</cover-view>
</cover-view>
<view v-else class="count-down" @click.stop="toHome">
{{ `跳过 ${countDown}s` }}
</view>
</view>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as IndexApi from '@/sheep/api/index/index'
const USE_STATIC_START_PAGE = false
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 || '')
if (!u) return false
const clean = u.split('?')[0].split('#')[0]
return /\.(mp4|mov|m4v|webm|ogg)$/i.test(clean)
}
const fetchStartPage = async () => {
try {
const res = await IndexApi.getstartpage({})
if (res?.code !== 0) return false
const data = res?.data || {}
const media = String(data?.picUrl || '').trim()
const url = String(data?.url || '').trim()
if (!media) return false
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
}
}
function onMediaError() {
bgSrc.value = ''
isVideo.value = false
mediaReady.value = false
startCountDown(DEFAULT_IMAGE_SECONDS)
}
const countDown = ref(DEFAULT_IMAGE_SECONDS)
function closeNativeSplashIfNeeded() {
try {
if (typeof plus !== 'undefined' && plus?.navigator?.closeSplashscreen) {
plus.navigator.closeSplashscreen()
}
} catch (e) {}
}
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) {
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
closeNativeSplashIfNeeded()
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}
function onVideoReady() {
if (!mediaReady.value) {
mediaReady.value = true
closeNativeSplashIfNeeded()
}
if (!countDownTimer) {
startCountDown(MAX_VIDEO_SECONDS)
scheduleJumpAfter(MAX_VIDEO_SECONDS + 0.2)
}
}
function onVideoMetadata(e) {
if (!mediaReady.value) {
mediaReady.value = true
closeNativeSplashIfNeeded()
}
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(() => {
closeNativeSplashIfNeeded()
if (USE_STATIC_START_PAGE) {
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
return
}
fetchStartPage().then((ok) => {
if (!ok) {
closeNativeSplashIfNeeded()
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(() => {
clearTimers()
})
function toHome() {
clearTimers()
uni.reLaunch({
url: '/pages/index/index',
})
}
function onMediaClick() {
const url = String(jumpUrl.value || '').trim()
if (!url) return
if (/^https?:\/\//i.test(url)) {
uni.navigateTo({
url: `/pages/public/webview?url=${encodeURIComponent(url)}`,
})
return
}
if (url.startsWith('/pages/')) {
uni.navigateTo({ url })
return
}
}
</script>
<style lang="scss" scoped>
.start-page {
position: fixed;
inset: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
background: #fff;
}
.bg {
position: absolute;
inset: 0;
width: 100vw;
height: 100vh;
}
.count-down {
position: absolute;
top: calc(env(safe-area-inset-top) + 100rpx);
right: 60rpx;
padding: 18rpx 20rpx;
border-radius: 50rpx;
font-size: 28rpx;
line-height: 1;
color: #fff;
background: rgba(0, 0, 0, 0.4);
z-index: 9999;
}
.count-down-text {
color: #fff;
font-size: 28rpx;
line-height: 1;
}
</style>