327 lines
8.2 KiB
Vue
327 lines
8.2 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"
|
|
@timeupdate="onVideoTimeUpdate"
|
|
@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">{{ getSkipText() }}</cover-view>
|
|
</cover-view>
|
|
<view v-else class="count-down" @click.stop="toHome">
|
|
{{ getSkipText() }}
|
|
</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 = 9
|
|
|
|
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
|
|
let videoCountdownBootTimer = 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(0)
|
|
|
|
const getSkipText = () => {
|
|
const s = Number(countDown.value) || 0
|
|
return s > 0 ? `跳过 ${s}s` : '跳过'
|
|
}
|
|
|
|
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
|
|
if (videoCountdownBootTimer) clearTimeout(videoCountdownBootTimer)
|
|
videoCountdownBootTimer = 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) return
|
|
if (videoCountdownBootTimer) return
|
|
videoCountdownBootTimer = setTimeout(() => {
|
|
videoCountdownBootTimer = null
|
|
if (countDownTimer) return
|
|
startCountDown(MAX_VIDEO_SECONDS)
|
|
scheduleJumpAfter(MAX_VIDEO_SECONDS + 0.2)
|
|
}, 300)
|
|
}
|
|
|
|
function onVideoMetadata(e) {
|
|
if (videoCountdownBootTimer) {
|
|
clearTimeout(videoCountdownBootTimer)
|
|
videoCountdownBootTimer = null
|
|
}
|
|
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 onVideoTimeUpdate(e) {
|
|
const duration = Number(e?.detail?.duration || e?.detail?.totalTime || 0)
|
|
if (!Number.isFinite(duration) || duration <= 0) return
|
|
const limit = Math.min(MAX_VIDEO_SECONDS, Math.ceil(duration))
|
|
if (countDown.value === limit) return
|
|
if (countDownTimer && countDown.value < MAX_VIDEO_SECONDS) return
|
|
if (videoCountdownBootTimer) {
|
|
clearTimeout(videoCountdownBootTimer)
|
|
videoCountdownBootTimer = null
|
|
}
|
|
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;
|
|
height: 64rpx;
|
|
padding: 0 20rpx;
|
|
border-radius: 50rpx;
|
|
font-size: 28rpx;
|
|
line-height: 32rpx;
|
|
color: #fff;
|
|
background: rgba(0, 0, 0, 0.4);
|
|
z-index: 9999;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.count-down-text {
|
|
color: #fff;
|
|
font-size: 28rpx;
|
|
line-height: 32rpx;
|
|
}
|
|
</style>
|