liveh5-nuxt/app/pages/liveRoom/index.client.vue
xingyy 0a7ab15d29 refactor(liveRoom): 优化直播室页面
- 移除静音提示按钮
- 添加诊断按钮配置
- 设置播放器容器大小
- 添加播放器 license配置
- 优化播放器初始化和播放逻辑
- 移除直播加载文本
2025-02-18 09:47:30 +08:00

257 lines
7.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import {ref, onMounted, onBeforeUnmount, watch} from 'vue'
import AliyunPlayer from 'aliyun-aliplayer'
import 'aliyun-aliplayer/build/skins/default/aliplayer-min.css'
import sideButton from '@/pages/liveRoom/components/SideButton/index.vue'
import broadcast from '@/pages/liveRoom/components/Broadcast/index.vue'
import {liveStore} from "@/stores/live/index.js"
import liveLoading from '@/components/liveLoading/index.vue'
import paymentResults from '@/pages/liveRoom/components/PaymentResults/index.vue'
import paymentInput from '@/pages/liveRoom/components/PaymentInput/index.vue'
import {goodStore} from "@/stores/goods/index.js"
import {message} from "~/components/x-message/useMessage.js"
import {showConfirmDialog} from 'vant';
import {artworkBuy} from "@/api/goods/index.js"
import {useI18n} from 'vue-i18n'
import gsap from 'gsap'
import {CountUp} from 'countup.js'
const { t } = useI18n()
const countUpRef = ref(null)
const nextPriceRef = ref(null)
const { auctionDetail} = goodStore();
const player = ref(null)
const {quoteStatus, show, playerId, show1, auctionData, getSocketData, getLiveLink, fullLive} = liveStore()
const pullLink = ref('')
definePageMeta({
i18n: 'login.title',
})
const handlePlayerError = (error) => {
console.error('播放器错误:', error)
player.value?.play()
}
const loading1=ref(false)
const initializePlayer = async () => {
try {
if (player.value) {
player.value.dispose()
}
// 判断是否是微信浏览器
const isWechat = /MicroMessenger/i.test(navigator.userAgent)
const playerConfig = {
id: playerId.value,
source: pullLink.value,
isLive: true,
preload: true,
autoplay: true, // 改为 true
muted: true, // 默认静音
diagnosisButtonVisible:false,
autoplayPolicy: {
fallbackToMute: true
},
width: '100%', //容器的大小
height: '100%', //容器的大小
skinLayout: false,
controlBarVisibility: 'never',
license: {
domain: "szjixun.cn",
key: "OProxmWaOZ2XVHXLtf4030126521c43429403194970aa8af9"
}
}
player.value = new AliyunPlayer(playerConfig, (playerInstance) => {
// 在微信环境下,需要用户手动触发播放
if (isWechat) {
console.log('当前是微信浏览器环境')
const startPlay = () => {
console.log('执行了startPlay')
playerInstance?.play()
document.removeEventListener('WeixinJSBridgeReady', startPlay)
document.removeEventListener('touchstart', startPlay)
}
document.addEventListener('WeixinJSBridgeReady', startPlay)
document.addEventListener('touchstart', startPlay)
}
loading1.value = true
playerInstance?.play()
})
player.value.on('playing', () => {
console.log('playing')
loading1.value = false
})
player.value.on('error', handlePlayerError)
} catch (error) {
showConfirmDialog({
message: t('live_room.error_mess'),
showCancelButton: true
}).then(() => {
location.reload()
}).catch(() => {
})
console.error('播放器初始化失败:', error)
}
}
// 添加取消静音方法
const unmutePlayer = () => {
if (player.value) {
player.value.muted = false
player.value.play()
}
}
onMounted(async () => {
pullLink.value = await getLiveLink()
if (auctionDetail.value.isLiving===1){
initializePlayer()
}
})
onBeforeUnmount(() => {
player.value?.dispose()
player.value = null
})
const createCountUp = (element, value, options = {}) => {
const defaultOptions = {
duration: 1.5,
separator: ',',
decimal: '.',
decimalPlaces: 0,
enableScrollSpy: true,
}
const countUp = new CountUp(element, Number(value), {...defaultOptions, ...options})
if (!countUp.error) {
countUp.start()
} else {
console.error(countUp.error)
}
return countUp
}
watch(() => fullLive.value, async (newVal) => {
if (!newVal) return
await getSocketData()
})
watchEffect(() => {
if (!fullLive.value) return
nextTick(()=>{
createCountUp(countUpRef.value, auctionData.value?.nowAuctionPrice?.nowPrice)
})
})
watchEffect(() => {
if (!fullLive.value) return
nextTick(()=>{
createCountUp(nextPriceRef.value, auctionData.value?.nowAuctionPrice?.nextPrice)
})
})
const goBuy = async () => {
const res = await artworkBuy({
auctionArtworkUuid: auctionData.value?.artwork?.uuid,
buyMoney: String(auctionData.value?.nowAuctionPrice?.nextPrice ?? 0)
})
if (res.status === 0) {
message.success(t('live_room.success_mess'))
}
}
const tipOpen = () => {
message.warning(t('live_room.warn_mess'))
}
</script>
<template>
<div class="relative h-full">
<div :id="playerId" class="w-full h-full"></div>
<liveLoading :loading="loading1"></liveLoading>
<transition name="fade">
<div v-if="fullLive">
<sideButton class="absolute top-196px right-0 z-999"></sideButton>
<div class="absolute left-1/2 transform -translate-x-1/2 flex flex-col items-center"
style="bottom:calc(var(--safe-area-inset-bottom) + 26px)">
<div class="text-16px text-#FFB25F font-600 flex">
<div class="mr-5px">{{ t('live_room.now_price') }}{{ auctionData?.nowAuctionPrice?.currency }}</div>
<div ref="countUpRef" class="min-w-50px"></div>
</div>
<div class="text-16px text-#fff font-600 flex">
<div class="mr-5px">{{ t('live_room.lower_price') }}{{ auctionData?.nowAuctionPrice?.currency }}</div>
<div ref="nextPriceRef" class="min-w-50px"></div>
</div>
<div v-if="quoteStatus" class="mt-10px mb-10px">
<van-button @click="goBuy" color="#FFB25F" class="w-344px !h-[40px]">
<div>{{
`${t('live_room.confirm')} ${auctionData?.nowAuctionPrice?.currency} ${auctionData?.nowAuctionPrice?.nextPrice ?? 0}`
}}
</div>
</van-button>
</div>
<div v-if="!quoteStatus" class="mt-10px mb-10px">
<van-button @click="tipOpen" color="#D6D6D8" class="w-344px !h-[40px]" >
<div class="text-#7D7D7F text-14px">{{ t('live_room.button') }}</div>
</van-button>
</div>
<broadcast></broadcast>
</div>
<paymentInput v-model:show="show"/>
<div>
</div>
<paymentResults v-model:show="show1" type="error"/>
<div v-if="auctionData?.wsType==='newArtwork'"
class="w-344px h-31px rounded-4px absolute top-9px bg-[#151824]/45 backdrop-blur-[10px] backdrop-saturate-[180%] left-1/2 transform translate-x--1/2 flex text-#fff text-14px items-center px-12px line-height-none">
<div class="mr-11px whitespace-nowrap">LOT{{ auctionData.artwork.index }}</div>
<div class="mr-10px truncate">{{ auctionData.artwork.name }}</div>
<div class="whitespace-nowrap">{{ t('live_room.start') }}</div>
</div>
</div>
</transition>
</div>
</template>
<style scoped lang="scss">
/* 定义过渡动画 */
.fade-enter-active {
transition: opacity 1s ease;
}
.fade-leave-active {
transition: opacity 0.2s ease;
}
/* 定义进入和离开的状态 */
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
.fade-enter-to,
.fade-leave-from {
opacity: 1;
}
.my-rolling-text {
--van-rolling-text-item-width: 10px;
--van-rolling-text-font-size: 16px;
--van-rolling-text-color: #FFB25F;
}
.my-rolling-text1 {
--van-rolling-text-item-width: 10px;
--van-rolling-text-font-size: 16px;
--van-rolling-text-color: #FFF;
}
:deep(.prism-license-watermark) {
display: none !important;
}
</style>