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

373 lines
8.9 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"
/>
<view
v-if="(USE_STATIC_START_PAGE || bgSrc) && mediaReady && countDown > 0"
class="count-down"
@click.stop="toHome"
>
<text class="count-down-text">{{ getSkipText() }}</text>
</view>
<view v-if="!USE_STATIC_START_PAGE && isLoading" class="loading-mask">
<view class="loading-box">
<view class="loading-spinner"></view>
<view class="loading-text">加载中...</view>
</view>
</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)
const isLoading = 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() {
toHome()
}
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
isLoading.value = false
closeNativeSplashIfNeeded()
startCountDown(DEFAULT_IMAGE_SECONDS)
scheduleJumpAfter(DEFAULT_IMAGE_SECONDS + 0.2)
}
function onVideoReady() {
if (!mediaReady.value) {
mediaReady.value = true
isLoading.value = false
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
isLoading.value = false
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
}
isLoading.value = true
fetchStartPage().then((ok) => {
if (!ok) {
toHome()
return
}
readyFallbackTimer = setTimeout(() => {
if (mediaReady.value) return
toHome()
}, isVideo.value ? 3500 : 2000)
})
})
onUnmounted(() => {
clearTimers()
})
function toHome() {
clearTimers()
isLoading.value = false
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;
}
.loading-mask {
position: absolute;
inset: 0;
z-index: 9998;
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.1);
}
.loading-box {
padding: 22rpx 26rpx;
border-radius: 16rpx;
background: rgba(0, 0, 0, 0.45);
display: flex;
align-items: center;
gap: 16rpx;
}
.loading-spinner {
width: 32rpx;
height: 32rpx;
border-radius: 50%;
border: 4rpx solid rgba(255, 255, 255, 0.35);
border-top-color: rgba(255, 255, 255, 0.95);
animation: spin 0.85s linear infinite;
}
.loading-text {
font-size: 26rpx;
color: #fff;
line-height: 32rpx;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>