"use strict";const e=require("../common/vendor.js"),T=require("../api/list.js"),h=require("../api/live.js");require("../api/order.js");const g=require("../api/home.js"),a=require("../common/assets.js");var S="wss://live.test.ylrztop.com/ws/live-api/app/webSocket",c=!1;const I={data(){return{liveUserTotal:null,viewPageSize:10,viewPageNum:1,viewLoading:!1,viewNoMoreData:!1,scrollHeight:0,scrollTimer:null,socketInstance:null,reconnectCount:0,maxReconnectAttempts:3,isManualClose:!1,showRed:!0,redInfo:{},inputInfo:"",searchTimer:null,storeId:null,reconnectCount:0,maxReconnectAttempts:3,reconnectTimer:null,isCollect:!1,showPurchasePrompt:!1,purchasePromptTimer:null,prevOrderCount:0,videoUrl:null,showType:1,boxHeight:300,isFollow:!0,liveViewData:{},liveViewers:[],likeName:0,hlsPlayer:null,livingUrl:"",products:{},store:{},orderUser:{},userType:0,timestamp:"",liveId:null,codeimg:"",placeholderText:"说点什么...",isZoom:!1,userinfo:"",value:"",talkdisabled:!1,autoplay:!1,showadd:!1,talklist:[],scrollIntoView:"",bufferRate:0,playDuration:0,videoContext:"",thistime:e.index.$u.timeFormat(new Date,"yyyy-mm-dd hh:MM:ss"),upDown:!0,isLongPress:!1,timeout:null,showWelcomeMessage:!1,isSubmit:!1,messageContent:"",showziliao:!1,isScreen:!0,showAnswer:!1,Answerlistall:{},answerlist:{},answerfrist:0,checkboxValue:[],checkboxFormValue:"",allAnswerLists:[],showAnswerred:!1,answerbtn:!1,redallAnswerLists:[],redanswerAll:[],redanswerList:[],redanswertips:[],shopping:!1,scrollTop:0,old:{scrollTop:0},livedata:{}}},onLoad(t){this.userinfo=JSON.parse(e.index.getStorageSync("userInfo")),t.liveId&&(this.liveId=t.liveId),e.index.showShareMenu({withShareTicket:!0})},onShareAppMessage(){return{title:this.livedata.liveName,path:"/pages/home/index",imageUrl:this.products.image,success(t){console.log("分享成功",t)},fail(t){console.error("分享失败",t)}}},computed:{filteredViewers(){if(this.liveViewers)return this.liveViewers.slice(0,3)}},mounted(){this.getLiveMsg(),this.getliveViewData(),this.getliveOrder(),this.getliveUser(),this.initTime(),this.initSocket();var t=this;e.index.$on("initSocket",()=>{t.initSocket()}),e.index.$on("sendMsg",s=>{t.sendMsg(s)}),e.index.$on("closeWebSocket",()=>{t.closeWebSocket()}),this.getEWechatSdk(),this.getliving()},onReady:function(t){this.videoContext=e.index.createVideoContext("myVideo")},onHide(){clearInterval(this.intervalId)},onUnload(){this.closeWebSocket(),e.index.$off("refreshOrder"),clearInterval(this.intervalId),this.hlsPlayer&&this.hlsPlayer.destroy(),this.pingpangTimes&&clearInterval(this.pingpangTimes)},watch:{"orderUser.count":{handler(t,s){t!==this.prevOrderCount&&(this.prevOrderCount=t,this.showPurchaseMessage())},immediate:!0}},methods:{getList(){const t={page:1,page_size:10};e.index.showLoading({title:"处理中..."}),T.liveList(t).then(s=>{s.code==200?(this.list=s.rows,console.log("list>>",this.list)):e.index.showToast({title:s.msg,icon:"none"})}).catch(s=>{console.log("请求失败:",JSON.stringify(s))}).finally(()=>{e.index.hideLoading()})},openViews(){this.$nextTick(()=>{e.index.createSelectorQuery().in(this).select(".view-box").boundingClientRect(s=>{s&&(this.scrollHeight=s.height-80-120)}).exec()})},async getliveUser(t=!1){if(!(this.viewLoading||this.viewNoMoreData)){this.viewLoading=!0;try{const s=await h.watchUserList(this.liveId,this.viewPageSize,this.viewPageNum,!1);if(s.code==200){if(this.liveUserTotal=s.total,s.rows.length===0){this.viewNoMoreData=!0,this.viewPageNum>1&&e.index.showToast({title:"没有更多数据了",icon:"none"});return}t?this.liveViewers=[...this.liveViewers,...s.rows]:this.liveViewers=s.rows,this.viewPageNum++}else e.index.showToast({title:s.msg,icon:"none"})}catch(s){console.error("获取观众列表失败:",s),e.index.showToast({title:"获取数据失败",icon:"none"})}finally{this.viewLoading=!1}}},handleScrollToLower(){this.scrollTimer&&clearTimeout(this.scrollTimer),this.scrollTimer=setTimeout(()=>{this.getliveUser(!0)},1e3)},getLiveMsg(){if(!this.liveId)return;h.liveMsg(this.liveId,30,1).then(n=>{n.code==200?(this.talklist=[...n.rows],console.log("获取最近聊天记录>>",this.talklist),this.$nextTick(()=>{this.scrollIntoView=`list_${this.talklist.length-1}`,console.log("应该滚动到:",this.scrollIntoView)})):e.index.showToast({title:n.msg,icon:"none"})})},onRed(){var s;if(console.log("点了"),!this.liveId||(console.log("点了this.liveId",this.liveId),!((s=this.redInfo)!=null&&s.redId)))return;console.log("点了this.redInfo?.redId",this.redInfo.redId),console.log("点了this.userinfo.userId",this.userinfo.userId);let t={liveId:this.liveId,userId:this.userinfo.userId,redId:this.redInfo.redId};h.liveRed(t).then(n=>{n.code==200||e.index.showToast({title:n.msg,icon:"none"})},n=>{})},handleSearchInput(){clearTimeout(this.searchTimer),this.searchTimer=setTimeout(()=>{this.queryCollect()},500)},showPurchaseMessage(){this.purchasePromptTimer&&clearTimeout(this.purchasePromptTimer),this.showPurchasePrompt=!0,this.purchasePromptTimer=setTimeout(()=>{this.showPurchasePrompt=!1},2e3)},truncateString(t,s){return t.length>s?t.slice(0,s)+"...":t},goStore(){console.log("带过去storeId",this.storeId),e.index.navigateTo({url:"/pages_shop/store?liveId="+this.liveId+"&storeId="+this.storeId})},goOrderList(){e.index.navigateTo({url:"/pages_shop/order"})},initHlsPlayer(){if(e.Hls.isSupported()&&this.livingUrl){const t=document.getElementById("myVideo");t&&(this.hlsPlayer=new e.Hls,this.hlsPlayer.loadSource(this.livingUrl),this.hlsPlayer.attachMedia(t),this.hlsPlayer.on(e.Hls.Events.MANIFEST_PARSED,()=>{t.play()}))}},videoError(t){console.error("视频播放错误:",t.detail.errMsg),this.livingUrl&&setTimeout(()=>{this.videoContext.play()},2e3)},getliving(){if(!this.liveId)return;console.log("获取直播信息");const t={id:this.liveId};g.getlive(t).then(s=>{if(s.code!==200){e.index.showToast({title:s.msg,icon:"none",duration:2e3});return}if(this.livedata=s.data,this.showType=s.data.showType,this.storeId=s.storeId,this.queryCollect(),console.log("横屏1或竖屏2",this.showType),s.data.liveType===2)this.videoUrl=s.data.videoUrl,this.autoplay=!0,this.placeholderText="说点什么...",this.talkdisabled=!1;else if(s.data.liveType===1)this.livingUrl=s.data.flvHlsUrl,this.autoplay=!0,this.placeholderText="说点什么...",this.talkdisabled=!1;else{this.livingUrl="",this.autoplay=!1,this.placeholderText="直播未开始,暂时无法发言",this.talkdisabled=!0;return}this.$nextTick(()=>{this.livingUrl&&(this.videoContext||(this.videoContext=e.index.createVideoContext("myVideo",this)),s.data.liveType===1?this.videoContext.seek(s.data.nowDuration||0):this.videoContext.play())})}).catch(s=>{console.error("获取直播信息失败:",s),e.index.showToast({title:"获取直播信息失败",icon:"none"})})},maskString(t,s="*"){return t?String(t).split("").map((w,o)=>o===0?w:s).join(""):""},getPureDecimal(t,s=6){const n=Math.abs(t).toFixed(s).split(".")[1];return(n==null?void 0:n.replace(/0+$/,""))||""},goBack(){const t=getCurrentPages();this.closeWebSocket(),t.length>1?e.index.navigateBack():e.index.switchTab({url:"/pages/list/index"})},getliveViewData(){this.liveId&&h.getLiveViewData(this.liveId).then(t=>{t.code==200?this.liveViewData=t:e.index.showToast({title:t.msg,icon:"none"})},t=>{})},getliveOrder(){this.liveId&&h.liveOrderUser(this.liveId).then(t=>{t.code==200?(console.log("正在购买>>>>",t),this.orderUser=t):e.index.showToast({title:t.msg,icon:"none"})},t=>{})},getLiveinformation(){this.liveId&&h.getLiveInfo(this.liveId).then(t=>{t.code==200?(console.log("获取直播间信息接口>>>>",t),this.livingUrl=t.livingUrl):e.index.showToast({title:t.msg,icon:"none"})},t=>{})},onLike(){this.liveId&&h.liveDataLike(this.liveId).then(t=>{t.code==200?t.like?this.liveViewData.like++:e.index.showToast({title:t.msg,icon:"none"}):e.index.showToast({title:t.msg,icon:"none"})},t=>{})},goShop(t,s){e.index.navigateTo({url:"/pages_shop/goods?productId="+t+"&liveId="+this.liveId+"&goodsId="+s+"&storeId="+this.storeId})},queryCollect(){this.storeId&&h.store(this.storeId,this.inputInfo).then(t=>{t.code==200?(console.log("查询店铺>>",t),this.products=t.data.goodsList,this.store=t.data):e.index.showToast({title:t.msg,icon:"none"})},t=>{})},onStoreCollect(){this.storeId&&h.collectStore(this.storeId).then(t=>{t.code==200?(e.index.showToast({title:t.msg,icon:"none"}),this.store.isFavorite=!this.store.isFavorite):e.index.showToast({title:t.msg,icon:"none"})},t=>{})},onGoodsCollect(t){!t||t.length===0||!t.goodsId||h.collectGoods(t.goodsId).then(s=>{s.code==200?(e.index.showToast({title:s.msg,icon:"none"}),t.isFavorite=!t.isFavorite):e.index.showToast({title:s.msg,icon:"none"})},s=>{})},onFollow(){this.liveId&&h.follow(this.liveId).then(t=>{this.isFollow=!this.isFollow,e.index.showToast({title:t.msg,icon:"none"})},t=>{})},initTime(){const t=new Date;this.timestamp=t.getTime()},lowerChat:function(t){console.log(t)},scroll:function(t){console.log(t),this.old.scrollTop=t.detail.scrollTop},loadmore(){},scrollchat(){},noredanswer(){this.Answerlistall>0?this.showAnswer=!this.this.showAnswer:e.index.showToast({title:"暂无题目",icon:"none"})},redbagAnswer(){this.showAnswerred=!this.showAnswerred,this.answerbtn=!this.answerbtn},submitAnswers(){if(this.isSubmit)return;this.isSubmit=!0;const t={questionId:this.answerlist.id,answer:this.checkboxFormValue};g.submitAnswer(t).then(s=>{s.code==200&&e.index.showToast({title:s.msg,icon:"none"}),this.answerfrist{this.isSubmit=!1})},getAnswerlists(){if(!this.liveId)return;const t={liveId:this.liveId};g.getAnswerlist(t).then(s=>{s.code==200&&(s.data.length>0?(this.allAnswerLists=s.data,this.Answerlistall=s.data.length,this.allAnswerLists.length>0&&(this.answerlist=this.allAnswerLists[0],this.answerlist.content=JSON.parse(this.allAnswerLists[0].content)),this.showAnswer=!0):this.showAnswer=!1)})},openShop(){},openViews(){},close(){this.showadd=!this.showadd},closes(){this.showziliao=!this.showziliao},closest(){this.showAnswer=!this.showAnswer},closestred(){this.showAnswerred=!this.showAnswerred},closeanswer(){this.answerbtn=!this.answerbtn},closeShop(){this.shopping=!this.shopping},closeRed(){this.showRed=!this.showRed},handleTouchStart(){this.isZoom=!0},handleTouchEnd(){this.isZoom=!1},getEWechatSdk(){},closeWebSocket(){if(this.isManualClose=!0,clearInterval(this.pingpangTimes),clearTimeout(this.reconnectTimer),this.socketInstance){try{e.index.closeSocket(),console.log("WebSocket 已安全关闭")}catch(t){console.error("关闭 WebSocket 时出错:",t)}this.socketInstance=null}c=!1},reConnect(){var t=this;try{e.index.closeSocket()}catch{}setTimeout(function(){t.initSocket()},1e4)},startHeartbeat(){clearInterval(this.pingpangTimes),this.pingpangTimes=setInterval(()=>{if(!c)return;const t={cmd:"ping",timestamp:Date.now()};e.index.sendSocketMessage({data:JSON.stringify(t),success:()=>console.debug("心跳发送成功"),fail:s=>{console.error("心跳发送失败:",s),this.scheduleReconnect()}})},3e4)},initSocket(){if(c||this.reconnectCount>=this.maxReconnectAttempts||(this.closeWebSocket(),this.isManualClose=!1,!this.liveId))return;const t=e.CryptoJS.HmacSHA256(`${this.liveId}${this.userinfo.userId}${this.userType}${this.timestamp}`,this.timestamp.toString()).toString(e.CryptoJS.enc.Hex);try{this.socketInstance=e.index.connectSocket({url:`${S}?userId=${this.userinfo.userId}&liveId=${this.liveId}&userType=${this.userType}×tamp=${this.timestamp}&signature=${t}`,success:()=>{this.setupSocketListeners()},fail:s=>{console.error("WebSocket 连接失败:",s),this.scheduleReconnect()}})}catch(s){console.error("创建 WebSocket 异常:",s),this.scheduleReconnect()}},setupSocketListeners(){e.index.onSocketOpen(()=>{c=!0,this.reconnectCount=0,console.log("WebSocket 连接成功"),this.startHeartbeat()}),e.index.onSocketClose(()=>{c=!1,console.log("WebSocket 连接关闭"),this.isManualClose||this.scheduleReconnect()}),e.index.onSocketError(t=>{c=!1,console.error("WebSocket 错误:",t),this.isManualClose||this.scheduleReconnect()}),e.index.onSocketMessage(t=>{try{const s=JSON.parse(t.data);if(this.talklist.push(s.data),this.$nextTick(()=>{this.scrollIntoView=`list_${this.talklist.length-1}`}),s.data.cmd=="deleteId")e.index.$emit("deleteId");else if(s.data.cmd=="red")this.redInfo=JSON.parse(s.data.data),console.log(" 领红包1redata.data>>",s.data),console.log(" 领红包2redata.data.data>>",s.data.data),console.log(" 领红包3this.redInfo>>",this.redInfo),console.log(" 领红包4this.redInfo.redId>>",this.redInfo.redId);else if(s.data.cmd=="sendRedPacketQuestion"){const n=JSON.parse(s.data.data);this.redanswerAll=[...this.redanswerAll,...n],this.redanswerAll[1].randomAmount!==null&&(this.redanswertips=JSON.parse(this.redanswerAll[0].randomAmount)),console.log(this.redanswertips),console.log(this.redanswerAll)}else s.data.cmd=="entry"?(this.showWelcomeMessage=!0,e.index.$emit("entry",s.data)):s.data.cmd=="live_start"||s.data.cmd=="live_end"}catch(s){console.error("解析消息失败:",s)}})},scheduleReconnect(){if(this.isManualClose||this.reconnectCount>=this.maxReconnectAttempts)return;this.reconnectCount++;const t=Math.min(3e3*this.reconnectCount,3e4);console.log(`将在 ${t}ms 后重连 (尝试 ${this.reconnectCount}/${this.maxReconnectAttempts})`),this.reconnectTimer=setTimeout(()=>{this.initSocket()},t)},sendMsg(){if(!c){e.index.showToast({title:"连接未建立,请稍后再试",icon:"none"});return}if(!this.socketInstance){e.index.showToast({title:"连接异常",icon:"none"});return}const t={liveId:this.liveId,userId:this.userinfo.userId,userType:0,cmd:"sendMsg",msg:this.value,nickName:this.userinfo.nickName,avatar:this.userinfo.avatar};if(!this.value.trim()){e.index.showToast({title:"不能发送空消息",icon:"none"});return}this.socketInstance.send({data:JSON.stringify(t),success:()=>{console.log("发送成功"),this.value=""},fail:s=>{console.error("发送失败:",s),e.index.showToast({title:"发送失败,请重试",icon:"none"})}})}}};if(!Array){const t=e.resolveComponent("u-avatar"),s=e.resolveComponent("u-input"),n=e.resolveComponent("u-popup");(t+s+n)()}const k=()=>"../uni_modules/uview-plus/components/u-avatar/u-avatar.js",x=()=>"../uni_modules/uview-plus/components/u-input/u-input.js",y=()=>"../uni_modules/uview-plus/components/u-popup/u-popup.js";Math||(k+x+y)();function _(t,s,n,w,o,l){var v,m,p,f;return e.e({a:e.o((...i)=>l.goBack&&l.goBack(...i)),b:a._imports_0$2,c:e.p({src:o.livedata.liveImgUrl||t.$img.logo,size:"32"}),d:e.t(o.livedata.liveName?l.truncateString(o.livedata.liveName,5):"未命名"),e:e.t(o.liveViewData.follow||o.liveViewData.follow===0?o.liveViewData.follow:0),f:o.isFollow},o.isFollow?{g:e.o((...i)=>l.onFollow&&l.onFollow(...i))}:{h:e.o((...i)=>l.onFollow&&l.onFollow(...i))},{i:e.f(l.filteredViewers,(i,r,d)=>({a:r,b:i.avatar||t.$img.logo})),j:e.t(o.liveUserTotal||0),k:e.o(i=>o.showadd=!o.showadd),l:((v=o.redInfo)==null?void 0:v.redStatus)==1},((m=o.redInfo)==null?void 0:m.redStatus)==1?{m:e.o((...i)=>l.onRed&&l.onRed(...i)),n:a._imports_1$3}:{},{o:e.o((...i)=>l.onLike&&l.onLike(...i)),p:a._imports_2,q:e.t(o.liveViewData.like||0),r:e.o(i=>l.goStore()),s:a._imports_3,t:a._imports_4,v:e.n(o.showType==1?"siderow-group":"side-group"),w:o.showPurchasePrompt&&((p=o.orderUser)==null?void 0:p.count)},o.showPurchasePrompt&&((f=o.orderUser)!=null&&f.count)?{x:a._imports_5,y:e.t(o.orderUser.userName?l.maskString(o.orderUser.userName):""),z:e.t(o.orderUser.count||0)}:{},{A:o.livingUrl},o.livingUrl?{B:e.n(o.showType==1?"video_row":"videotop"),C:o.livingUrl,D:o.autoplay,E:e.o((...i)=>l.videoError&&l.videoError(...i))}:{},{F:o.videoUrl},o.videoUrl?{G:e.n(o.showType==1?"video_row":"videotop"),H:o.videoUrl,I:o.autoplay,J:e.o((...i)=>l.videoError&&l.videoError(...i))}:{},{K:e.f(o.talklist,(i,r,d)=>({a:e.t(i.msg),b:r,c:i.cmd=="announcement"})),L:e.t(o.messageContent),M:e.f(o.talklist,(i,r,d)=>({a:e.t(i.nickName),b:e.t(i.msg),c:r,d:`list_${r}`,e:i.cmd!="red"&&i.cmd!="out"&&i.cmd!="entry"})),N:o.showWelcomeMessage},o.showWelcomeMessage?{O:e.f(o.talklist,(i,r,d)=>({a:e.t(i.nickName),b:e.t(i.msg),c:r,d:i.cmd=="entry"||i.cmd=="out"})),P:e.t(o.messageContent)}:{},{Q:o.scrollIntoView,R:e.o(l.sendMsg),S:e.o(l.sendMsg),T:e.o(i=>o.value=i),U:e.p({placeholder:o.placeholderText,border:"none",customStyle:"font-size:24rpx;",shape:"circle",color:"#fff",placeholderStyle:"color:#e7e7e7",modelValue:o.value}),V:a._imports_6,W:e.o(i=>o.shopping=!o.shopping),X:a._imports_7,Y:e.o(i=>o.showziliao=!o.showziliao),Z:e.f(o.liveViewers,(i,r,d)=>({a:e.t(r+1),b:r===0?"#FF3B30":r===1?"#FF9500":r===2?"#FFCC00":"#8E8E93",c:r<3?"bold":"normal",d:"7175134e-3-"+d+",7175134e-2",e:e.p({src:i.avatar||t.$img.logo,size:"32"}),f:e.t(i.nickName||"未命名"),g:r})),aa:o.viewNoMoreData&&o.liveViewers.length>0},o.viewNoMoreData&&o.liveViewers.length>0?{}:{},{ab:o.scrollHeight+"px",ac:e.o((...i)=>l.handleScrollToLower&&l.handleScrollToLower(...i)),ad:e.p({src:o.livedata.liveImgUrl||t.$img.logo,size:"32"}),ae:e.t(o.livedata.liveName||"未命名"),af:e.o(l.close),ag:e.o(l.openViews),ah:e.p({show:o.showadd,round:"20rpx",bgColor:"#ffffff"}),ai:e.p({src:o.store.logoUrl,size:"36"}),aj:a._imports_1$2,ak:e.o([i=>o.inputInfo=i.detail.value,(...i)=>l.handleSearchInput&&l.handleSearchInput(...i)]),al:o.inputInfo,am:o.store.isFavorite},o.store.isFavorite?{an:e.o((...i)=>l.onStoreCollect&&l.onStoreCollect(...i)),ao:a._imports_12}:{ap:e.o((...i)=>l.onStoreCollect&&l.onStoreCollect(...i)),aq:a._imports_13},{ar:a._imports_11,as:e.o((...i)=>l.goOrderList&&l.goOrderList(...i)),at:e.f(o.products,(i,r,d)=>e.e({a:i.imgUrl,b:e.t(r+1),c:e.t(i.productName),d:e.t(i.sales),e:e.t(Math.trunc(i.price)),f:e.t(l.getPureDecimal(i.price)?l.getPureDecimal(i.price):"00"),g:i.isFavorite},i.isFavorite?{h:e.o(u=>l.onGoodsCollect(i),r),i:a._imports_12}:{j:e.o(u=>l.onGoodsCollect(i),r),k:a._imports_13},{l:i.status==1},i.status==1?{m:e.o(u=>l.goShop(i.productId,i.goodsId),r)}:i.status==0?{o:e.o(u=>l.goShop(i.productId),r)}:{},{n:i.status==0,p:r})),av:o.boxHeight+"px",aw:e.o(l.closeShop),ax:e.o(l.openShop),ay:e.p({show:o.shopping,round:"20rpx",bgColor:"#f3f5f9"})})}const b=e._export_sfc(I,[["render",_],["__scopeId","data-v-7175134e"]]);I.__runtimeHooks=2;wx.createPage(b);