living.vue 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859
  1. <template>
  2. <view class="swiper-wrapper">
  3. <view class="container">
  4. <view class="content">
  5. <view style="position: fixed;top: 0;z-index: 5;" class="content-top">
  6. <view class="x-f">
  7. <image v-if="!scene" @click="goBack" class="w60 h64 mr26" src="/static/images/return3.png" />
  8. <view class="align-center"
  9. style="padding: 6rpx 8rpx;height: 64rpx;background: rgba(0,0,0,0.5);border-radius: 32rpx;">
  10. <u-avatar :src="liveItem.liveImgUrl||'/static/images/avatar.png'" :size="32"></u-avatar>
  11. <view class="colorf ml10 mr6">
  12. <view>{{liveItem.liveName?truncateString(liveItem.liveName,8):"未命名"}}</view>
  13. </view>
  14. </view>
  15. </view>
  16. <view v-if="Array.isArray(filteredViewers)" class=" align-center" @click="toggleViewerList"
  17. style="margin-top: 120rpx;">
  18. <view v-for="(item,viewerIndex) in filteredViewers" :key="viewerIndex">
  19. <image v-if="item.avatar" class="w52 h52 mr4" style="border-radius: 26rpx;"
  20. :src="item.avatar" />
  21. <view v-else class="w52 h52 mr4"
  22. :style="{backgroundColor: getUserRandomColor(item.userId), borderRadius:'50%'}">
  23. <text class="text-white text-xs">{{getNicknameInitial(item.nickName)}}</text>
  24. </view>
  25. </view>
  26. <view class="sum">{{liveUserTotal||0}}</view>
  27. </view>
  28. </view>
  29. <!-- 右边的side -->
  30. <view class="side-group">
  31. <view class="side-item" @click="onLike">
  32. <LikeButton :initialCount="100" :heartsPerClick="5" @like="onLike" />
  33. <view>{{liveViewData && liveViewData.like||0}}</view>
  34. </view>
  35. <view class="side-item">
  36. <button open-type="share" class="button button-reset">
  37. <image class="image" src="/static/images/weixin.png" mode="widthFix" />
  38. </button>
  39. <view>分享</view>
  40. </view>
  41. </view>
  42. <view class="activity-box">
  43. <view class="item-box" @click="onRed()" v-if="isShowRed">
  44. <view class="x-f">
  45. <view class="tip">领红包</view>
  46. <view class="item">
  47. <image class="w70 img" src="/static/images/redbag.png" mode="widthFix" />
  48. </view>
  49. </view>
  50. </view>
  51. <view class="item-box" @click="onLottery()" v-if="isShowLottery&&countdown">
  52. <view class="x-f">
  53. <view class="tip">抽奖</view>
  54. <view class="item">
  55. <image class="w60" src="/static/images/lottery.png" mode="widthFix" />
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="shop-prompt f30 x-f"
  61. v-if="showPurchasePrompt&&orderUser&&orderUser.count&&liveItem.status==2">
  62. <image class="w32 h32 mr8" src="/static/images/shopping.png" />
  63. <text>{{ orderUser.count || 0 }}人正在去购买</text>
  64. </view>
  65. <view class="videolist" v-if="liveItem.status==2">
  66. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  67. <!-- 直播 -->
  68. <live-player v-if="liveItem.livingUrl && liveItem.liveType == 1" :id="'myLivePlayer_' + liveId"
  69. :src="liveItem.livingUrl" autoplay mode="live" object-fit="contain" :muted="false"
  70. orientation="vertical" :enable-play-gesture="false" min-cache="1" max-cache="3"
  71. @statechange="onLiveStateChange($event, liveItem)" @error="onLiveError($event, liveItem)"
  72. class="item"></live-player>
  73. <!-- 录播 -->
  74. <video v-if="liveItem.videoUrl&&liveItem.liveType==2" :id="`myVideo_${liveId}`" :autoplay="true"
  75. class="item" :src="liveItem.videoUrl" :controls="false" object-fit="contain"
  76. :custom-cache="false" :enable-progress-gesture="false" vslide-gesture-in-fullscreen="false"
  77. :show-center-play-btn="false" :http-cache="false" loop @error="videoError"
  78. @timeupdate="onVideoTimeUpdate" @loadedmetadata="onVideoMetaLoaded" @pause="onVideoPause"
  79. @play="onVideoPlay" @waiting="onVideoWaiting" :enable-play-gesture="false"
  80. :play-strategy="1" @dblclick="preventDoubleClick" preload="auto"
  81. :enable-stash-buffer="false" :stash-initial-size="0" :stash-max-size="0" :stash-time="0"
  82. type="application/x-mpegURL">
  83. </video>
  84. <view v-if="liveItem.videoUrl&&liveItem.liveType==2" class="time">{{diffTotalTime}}
  85. </view>
  86. </view>
  87. </view>
  88. <view class="videolist" v-if="liveItem.status==3">
  89. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  90. <view class="end">直播已结束</view>
  91. </view>
  92. </view>
  93. <view class="videolist" v-if="liveItem.status==4">
  94. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  95. <!-- 直播回放 -->
  96. <video v-if="liveItem.videoUrl&&liveItem.liveType==3" :id="`myVideo_${liveId}`" class="item"
  97. :src="liveItem.videoUrl" :autoplay="true" :controls="true" object-fit="contain"
  98. :custom-cache="false" :enable-progress-gesture="liveItem.isSpeedAllowed"
  99. vslide-gesture-in-fullscreen="true" :show-center-play-btn="true" :http-cache="false" loop
  100. @error="videoError" @timeupdate="onVideoTimeUpdate" @loadedmetadata="onVideoMetaLoaded"
  101. @pause="onVideoPause" @play="onVideoPlay" :enable-play-gesture="true" preload="auto"
  102. @waiting="onVideoWaiting" type="application/x-mpegURL">
  103. </video>
  104. <view v-if="liveItem.videoUrl&&liveItem.liveType==3" class="lable">直播回放</view>
  105. </view>
  106. </view>
  107. <view class="trailer-box" v-if="liveItem.status==1">
  108. <video v-if="liveItem.previewUrl" :id="`myVideo_${liveId}`" class="item" :src="liveItem.previewUrl"
  109. :autoplay="true" :loop="false" :controls="false" object-fit="contain" :custom-cache="false"
  110. :enable-progress-gesture="false" vslide-gesture-in-fullscreen="false"
  111. :show-center-play-btn="false" :http-cache="false" @error="videoError"
  112. @loadedmetadata="onVideoMetaLoaded" @pause="onVideoPause" @play="onVideoPlay"
  113. :disable-progress="true" :enable-play-gesture="true" @waiting="onVideoWaiting" preload="auto"
  114. type="application/x-mpegURL">
  115. </video>
  116. <image v-if="liveItem.status==1&&!liveItem.previewUrl" class="img" src="/static/images/no_live.png">
  117. </image>
  118. <view class="countdown-item mt30 mb30" v-if="liveItem.status==1&&liveCountdown">
  119. 开播倒计时
  120. <view class="x-f">
  121. <view class="white">
  122. {{liveCountdown.hours||"00"}}
  123. </view>
  124. <view class="white">
  125. {{ liveCountdown.minutes||"00"}}
  126. </view>
  127. <view class="white">
  128. {{ liveCountdown.seconds||"00" }}
  129. </view>
  130. </view>
  131. </view>
  132. <view class="name"> {{liveItem.liveName}}</view>
  133. <view class="title" v-if="liveCountdown">暂未开播</view>
  134. <view class="button" v-if="!isAgreement" @click="handleAgreement">预约直播</view>
  135. <view class="button ash" v-if="isAgreement">已预约</view>
  136. <view class="title" v-if="!liveCountdown">主播还在来的路上</view>
  137. </view>
  138. <view class="trailer-box" v-if="!liveItem">
  139. <image class="img" src="/static/images/no_live.png">
  140. </image>
  141. <view class="title">暂无直播</view>
  142. </view>
  143. <!-- 底部聊天区域 -->
  144. <view
  145. :style="{position: 'fixed',width: '100%',bottom:isFocus ? keyboardHeight+'rpx' : '0' ,zIndex:999}">
  146. <view class="mt20" :style="{ height: isFocus ? '64rpx' : '30vh' }">
  147. <scroll-view v-if="Array.isArray(talklist)" enable-flex scroll-y="true"
  148. class="pl20 scrolly flex-1 column" style="width: calc(100% - 20rpx); height: 100%;"
  149. :scroll-top="scrollTop" ref="scrollView">
  150. <view class="list justify-start" v-for="(item,talkIndex) in (talklist||[])" :key="talkIndex"
  151. :id="`list_${talkIndex}`" v-show="item.cmd!='red'&&item.cmd!='out'&&item.cmd!='entry'">
  152. <view class="talk-list justify-start">
  153. <view class="fs30" style="max-width: 100%;">
  154. <text style="color: #FFDA73;">{{item.nickName||"未命名"}}:</text>
  155. <text class='colorf'>{{item.msg}}</text>
  156. </view>
  157. </view>
  158. </view>
  159. <view v-if="showWelcomeMessage" class="welcome-message" style="max-width: 100%;">
  160. <view class="list justify-start" v-show="inAndOut.cmd=='entry'||inAndOut.cmd=='out'">
  161. <view class="talk-list justify-start">
  162. <view class="fs30">
  163. <text style="color: #ff89d6;">{{inAndOut.nickName||"未命名"}} </text>
  164. <text class='colorf'>{{inAndOut.msg}}直播间</text>
  165. </view>
  166. </view>
  167. </view>
  168. </view>
  169. </scroll-view>
  170. </view>
  171. <!-- 底部输入框和操作按钮 -->
  172. <view class="justify-between p24">
  173. <view class="x-f justify-between"
  174. :style="{background:'#393939' ,padding: '10rpx 14rpx 10rpx 32rpx',boxSizing: 'border-box',borderRadius: '36rpx',width: isFocus ? '100%' : '580rpx'}">
  175. <input :placeholder="placeholderText" v-model="value" placeholder-style="color:#e7e7e7;"
  176. placeholder-class="placeholder-style" class="ml20 input-native" @focus="inputFocus"
  177. @blur="inputBlur"
  178. style="border: none; font-size: 32rpx; color: #ffffff; background: transparent; width: 70%;"
  179. cursor-spacing="100" :adjust-position="false" />
  180. <view class="send" @click="sendMsg()">发送</view>
  181. </view>
  182. <view class="justify-between mr15 align-center" v-if="!isFocus">
  183. <view class="icon-bg ml20" @tap="openCart()">
  184. <image src="/static/images/shopping.png" class="w58 h58" />
  185. </view>
  186. </view>
  187. </view>
  188. </view>
  189. </view>
  190. <view class="goods" v-if="isShowGoods">
  191. <view class="top">
  192. <view class="left">
  193. <image class="w30 h30 mr8" src="/static/images/signal.png" />讲解中
  194. </view>
  195. <image @click="isShowGoods=false" class="w40 h40 " src="/static/images/del_black.png" />
  196. </view>
  197. <image class="photo" :src="goodsCard.imgUrl" />
  198. <view class="item">
  199. <view class="price"><text class="red">¥{{goodsCard.price}} </text><text
  200. class="del">¥{{goodsCard.otPrice}}</text></view>
  201. <view class="title oneline-hide">{{goodsCard.productName}}</view>
  202. <view class="button" @click="goShop(goodsCard.productId,goodsCard.goodsId)">
  203. 立即抢购
  204. </view>
  205. </view>
  206. </view>
  207. <u-popup :show="isShowLotteryPop&&countdown" round='40rpx'>
  208. <view class="prize-box" style="border-radius: 40rpx;height: fit-content;">
  209. <image class="nav-img " src="/static/images/red_head.png" mode="widthFix" />
  210. <image class="bg-img " src="/static/images/red_bg.png" />
  211. <view class="prize-content">
  212. <view class=" mr20" style="display: flex;justify-content: flex-end;">
  213. <view
  214. style="width: 80rpx;height: 80rpx;display: flex; justify-content: center;align-items: center;"
  215. @click="isShowLotteryPop=false"><u-icon class="x-end" name="close" color="#fff"
  216. size="20"></u-icon>
  217. </view>
  218. </view>
  219. <view class="column align-center ">
  220. <image class="w446 h80" src="/static/images/red_title.png" />
  221. <view class="fs24 colorf x-f mt30 mb30">
  222. 开奖倒计时
  223. <view class="x-f">
  224. <view class="white-item">
  225. {{countdown.hours||"00"}}
  226. </view>
  227. <view class="white-item">
  228. {{ countdown.minutes||"00" }}
  229. </view>
  230. <view class="white-item">
  231. {{ countdown.seconds||"00" }}
  232. </view>
  233. </view>
  234. </view>
  235. <view class="item-group">
  236. <ThreeItemSwiper :products="lotteryProducts"></ThreeItemSwiper>
  237. </view>
  238. <view class="point-group" v-for="(item,index) in lotteryProducts" :key="index">
  239. </view>
  240. <view class="colorf fs28">
  241. 观看直播参与抽奖
  242. </view>
  243. <view class="button" @click="onClaim">参与抽奖</view>
  244. </view>
  245. </view>
  246. </view>
  247. </u-popup>
  248. <u-popup :show="!!integral.status" round='20rpx' mode="center" bgColor='#ffffff' zIndex='10076'>
  249. <view class="integral-box">
  250. <view class="top">
  251. <view class="title">观看视频领芳华币</view>
  252. <image class="photo" src="/static/images/integral_bg.png" mode="widthFix" />
  253. </view>
  254. <view class="item">
  255. <view class="title ">{{integral.msg}}</view>
  256. <view class="button" @click="integral.status=false">确认</view>
  257. </view>
  258. </view>
  259. </u-popup>
  260. <u-popup :show="isShowRedCard" round='20rpx' mode="center" bgColor='transparent' zIndex='10076'>
  261. <view class="red-card">
  262. <image class="img" src="/static/images/red_card.png" />
  263. <view class="red-content">
  264. <view class="title">{{redCard.msg}}</view>
  265. <view class="txt ">直播惊喜芳华币</view>
  266. <view class="button" @click="isShowRedCard=false">确认</view>
  267. </view>
  268. </view>
  269. </u-popup>
  270. <!-- 中奖和未中奖 -->
  271. <u-popup :show="isShowPrize&&havePrize" round='20rpx' mode="center" bgColor='#fff' zIndex='10076'>
  272. <view class="prize-card" v-if="isCurrentUserWon">
  273. <image class="nav-img " src="/static/images/red_head.png" mode="widthFix" />
  274. <view class="title">恭喜您 中奖啦!</view>
  275. <view class="prize-content" v-for="(item,index) in (prizeInfo||[])" :key="index">
  276. <view class="item">{{item.userName}}</view>
  277. <view class="item">{{item.userId}}</view>
  278. <view class="txt item">{{item.prizeLevel}}等奖</view>
  279. </view>
  280. <view class="tip">请截图联系客服领取奖品</view>
  281. <view class="button" @click="confirm">确认</view>
  282. </view>
  283. <view class="no-prize-card" v-else>
  284. <image class="img" src="/static/images/no-prize.png" mode="widthFix" />
  285. <view class="tip">很遗憾 您未中奖</view>
  286. <view class="button" @click="confirm">确认</view>
  287. </view>
  288. </u-popup>
  289. <!-- 中奖记录 -->
  290. <!-- winning -->
  291. <u-popup :show="winning" @close="closeWin" round='20rpx' bgColor='#f3f5f9' zIndex='10076'>
  292. <view class="winning_record">
  293. <image class="head_bg" src="/static/images/red_head.png" mode="widthFix"></image>
  294. <image class="bg" src="/static/images/red_bg.png"></image>
  295. <view class="winning_content">
  296. <view class="title">我的中奖记录</view>
  297. <view class="row mb40">
  298. <view class="head">时间</view>
  299. <view class="head">奖品</view>
  300. </view>
  301. <scroll-view enable-flex scroll-y v-if="prizeAll" style="height: 500rpx;">
  302. <view class="row fs30 mb20" v-for="(item,index) in prizeAll" :key="index">
  303. <view class="start">{{item.createTime}}</view>
  304. <view class="end">{{item.productName?truncateString(item.productName,8):'' }}</view>
  305. </view>
  306. </scroll-view>
  307. <view v-else class="f36 mt150" style="text-align: center;">
  308. 暂无中奖信息
  309. </view>
  310. </view>
  311. </view>
  312. </u-popup>
  313. <!-- 观众列表弹窗 -->
  314. <u-popup :show="showadd" @close="close" @open="openViews" round='20rpx' bgColor='#ffffff' zIndex='10077'>
  315. <view class="view-box">
  316. <view class="fs32" style="text-align: center;">在线观众</view>
  317. <scroll-view v-if="Array.isArray(liveViewers)" scroll-y class="scroll-content"
  318. :style="{height: scrollHeight + 'px'}" @scrolltolower="handleScrollToLower">
  319. <view class="fs28 x-f mb20 mt20" v-for="(item,index) in (liveViewers||[])" :key="index">
  320. <view
  321. :style="{color: index === 0 ? '#FF3B30' : index === 1 ? '#FF9500' : index === 2 ? '#FFCC00' : '#8E8E93',fontWeight: index < 3 ? 'bold' : 'normal',width: '50rpx'}"
  322. class="mr10">{{index+1}}</view>
  323. <u-avatar v-if="item.avatar" :src="item.avatar" :size="36"></u-avatar>
  324. <view v-else class="w72 h72"
  325. :style="{backgroundColor: getUserRandomColor(item.userId), borderRadius:'50%'}">
  326. <text class="text-white text-sm">{{getNicknameInitial(item.nickName)}}</text>
  327. </view>
  328. <text class="ml16 f30">{{item.nickName||"未命名"}}</text>
  329. </view>
  330. </scroll-view>
  331. </view>
  332. </u-popup>
  333. <!-- 更多弹窗 -->
  334. <u-popup :show="isMore" @close="closeMore" round='20rpx' bgColor='#f3f5f9' zIndex='10076'>
  335. <view class="more-block">
  336. <view class="item" @click="navgetTo('/pages_shopping/live/integral'),isMore=false">
  337. <image class="w48 h48" src="/static/images/order.png" />
  338. <view style="text-align: center;">芳华币</view>
  339. </view>
  340. <view class=" item"
  341. @click="navgetTo('/pages_shopping/live/storeOrderRefundList?liveId='+liveId),isMore=false">
  342. <image class="w48 h48" src="/static/images/after_sales.png" />
  343. <view style="text-align: center;">售后订单</view>
  344. </view>
  345. <view class=" item" @click="goMiniProgram(),isMore=false">
  346. <image class="w48 h48" src="/static/images/points.png" />
  347. <view style="text-align: center;">兑换好礼</view>
  348. </view>
  349. <view class="item" @click="getMyLottery(),isMore=false,winning=true">
  350. <image class="w48 h48" src="/static/images/health_sel.png" />
  351. <view style="text-align: center;">中奖记录</view>
  352. </view>
  353. </view>
  354. </u-popup>
  355. <!-- 商品弹窗 -->
  356. <!-- show="true" -->
  357. <u-popup :show="shopping" @close="closeShop" round='20rpx' bgColor='#f3f5f9' zIndex='10075'>
  358. <view class="shoppop">
  359. <view class="shoppop-top">
  360. <view class="search-input x-f">
  361. <image class="w24 mr16" src="/static/images/search.png" mode="widthFix" />
  362. <input placeholder="请搜索商品" v-model="inputInfo" @input="handleSearchInput" />
  363. </view>
  364. <view class=" search-top" @click="navgetTo('/pages_shopping/live/order')">
  365. <image class="w48 h48" src="/static/images/carts.png" />
  366. <view style="text-align: center;">订单</view>
  367. </view>
  368. <view class="search-top" @click="isMore=true,shopping=false">
  369. <image class="w48 h48" src="/static/images/search2.png" />
  370. <view style="text-align: center;">更多</view>
  371. </view>
  372. </view>
  373. <!-- <scroll-view enable-flex scroll-y class="shop-list" :style="{ height: boxHeight + 'px' }">
  374. <view class="list-item" v-for="(item,index) in products" :key="index">
  375. <view class="goods-img">
  376. <image class="img" :src="item.imgUrl" mode="widthFix" />
  377. <view class="goods-label">{{index+1}}</view>
  378. </view>
  379. <view class="goods-right">
  380. <view class="goods-title">{{item.productName}}</view>
  381. <view class="goods-people">{{item.sales}} 人已购</view>
  382. <view class="goods-shop">
  383. <text class="nummber"><text style="font-size: 20rpx;font-weight: 600;">¥</text><text
  384. style="font-size: 36rpx;font-weight: bold;">{{Math.trunc(item.price)}}</text>.{{getPureDecimal(item.price)?getPureDecimal(item.price):'00'}}</text>
  385. <view class="btn-group x-f">
  386. <view class="collect-btn">
  387. <image v-if="item.isFavorite" @click="onGoodsCollect(item)" class="w36 h36"
  388. style="vertical-align: middle;"
  389. src="/static/images/collect_select.png" />
  390. <image v-else @click="onGoodsCollect(item)" class="w36 h36"
  391. style="vertical-align: middle;" src="/static/images/collect.png" />
  392. </view>
  393. <view v-if="item.status==1" class="shop-btn"
  394. @click="goShop(item.productId,item.goodsId)">去购买 </view>
  395. <view v-else-if="item.status==0" class="shop-btn">
  396. 已下架</view>
  397. </view>
  398. </view>
  399. </view>
  400. </view>
  401. </scroll-view> -->
  402. <scroll-view enable-flex scroll-y class="shop-list" :style="{ height: boxHeight + 'px' }">
  403. <!-- 骨架屏 -->
  404. <view v-if="loadingProducts && products.length === 0" class="skeleton-list">
  405. <view v-for="n in 6" :key="n" class="skeleton-item">
  406. <view class="skeleton-img"></view>
  407. <view class="skeleton-content">
  408. <view class="skeleton-line short"></view>
  409. <view class="skeleton-line medium"></view>
  410. <view class="skeleton-line long"></view>
  411. </view>
  412. </view>
  413. </view>
  414. <!-- 实际商品列表 -->
  415. <template v-else>
  416. <view v-for="(item,index) in products" :key="index" class="list-item">
  417. <view class="goods-img">
  418. <image class="img" :src="item.imgUrl" mode="widthFix" />
  419. <view class="goods-label">{{index+1}}</view>
  420. </view>
  421. <view class="goods-right">
  422. <view class="goods-title">{{item.productName}}</view>
  423. <view class="goods-people">{{item.sales}} 人已购</view>
  424. <view class="goods-shop">
  425. <text class="nummber"><text
  426. style="font-size: 20rpx;font-weight: 600;">¥</text><text
  427. style="font-size: 36rpx;font-weight: bold;">{{Math.trunc(item.price)}}</text>.{{getPureDecimal(item.price)?getPureDecimal(item.price):'00'}}</text>
  428. <view class="btn-group x-f">
  429. <view class="collect-btn">
  430. <image v-if="item.isFavorite" @click="onGoodsCollect(item)"
  431. class="w36 h36" style="vertical-align: middle;"
  432. src="/static/images/collect_select.png" />
  433. <image v-else @click="onGoodsCollect(item)" class="w36 h36"
  434. style="vertical-align: middle;" src="/static/images/collect.png" />
  435. </view>
  436. <view v-if="item.status==1" class="shop-btn"
  437. @click="goShop(item.productId,item.goodsId)">去购买 </view>
  438. <view v-else-if="item.status==0" class="shop-btn">
  439. 已下架</view>
  440. </view>
  441. </view>
  442. </view>
  443. </view>
  444. </template>
  445. </scroll-view>
  446. </view>
  447. </u-popup>
  448. <!-- 优惠券弹窗 -->
  449. <view class="coupon-pop" v-if="isShowCoupon">
  450. <view class="coupon-block">
  451. <image class="bg" src="/static/images/coupon_bg.png" />
  452. <image class="nav" src="/static/images/coupon_top.png" />
  453. <image @click="isShowCoupon=false" class="w40 h40 close" style="z-index: 99;"
  454. src="/static/images/close1.png" />
  455. <view class="item">
  456. <view class="title">{{couponInfo.couponName}}</view>
  457. <view class="price">¥<text class="bold">{{couponInfo.couponPrice}}</text></view>
  458. <view class="txt">满{{couponInfo.useMinPrice}}元可用</view>
  459. <view class="txt" style="margin-top: 26rpx;">指定商品可用</view>
  460. <view class="txt">自领取起{{couponInfo.couponTime}}天内有效</view>
  461. <view class="button" @click="onCoupon()">
  462. 立即领券
  463. </view>
  464. </view>
  465. </view>
  466. </view>
  467. </view>
  468. </view>
  469. </template>
  470. <script>
  471. import LikeButton from '@/pages_course/components/like.vue'
  472. import ThreeItemSwiper from '@/pages_course/components/ThreeItemSwiper.vue'
  473. import CryptoJS from 'crypto-js'
  474. import {
  475. myLottery, // 我的中奖明细
  476. coupon, //领取优惠券
  477. liveLottery, // 抽奖查询
  478. claim, //抽奖
  479. liveRed, // 点击领红包
  480. liveDataLike, // 点赞
  481. collectStore, // 店铺收藏/取消收藏
  482. collectGoods, // 商品收藏/取消收藏
  483. watchUserList, //获取直播间用户(展示在线用户)
  484. liveMsg, //获取最近聊天记录
  485. // 小黄车
  486. liveStore, //店铺展示,
  487. liveOrderUser, //正在购买
  488. getLiveInfo, //获取直播间信息接口
  489. getLiveViewData, //直播间点赞、关注、在线人数数据
  490. currentActivities, //红包 卡片 抽奖
  491. getlive,
  492. subNotifyLive,
  493. internetTraffic, // 流量(缓冲百分比),
  494. liveInternetTraffic // 直播流量(缓冲百分比),
  495. } from '@/api/living.js'
  496. import {
  497. getUserInfo
  498. } from '@/api/user'
  499. import {
  500. generateRandomString
  501. } from "@/utils/common.js"
  502. import dayjs from 'dayjs';
  503. import {
  504. nextTick
  505. } from "vue"
  506. var isSocketOpen = false;
  507. var socket = null;
  508. export default {
  509. components: {
  510. ThreeItemSwiper,
  511. LikeButton
  512. },
  513. data() {
  514. return {
  515. uuId: "",
  516. totalTraffic: 0, // 总流量(字节)
  517. bitrate: 800, // 录播默认码率 0.16Mbps
  518. bitrateLive: 1600, // 直播默认码率 0.16Mbps
  519. //定时器
  520. trafficTimer: null,
  521. pingTimeoutTimer: null, // 心跳超时定时器
  522. heartBeatTimer: null, // 心跳定时器
  523. liveViewDataTimer: null,
  524. reconnectTimer: null, // 重连定时器
  525. scrollTimer: null, // 滚动防抖定时器
  526. searchTimer: null, //搜索
  527. purchasePromptTimer: null, //购买提示
  528. welcomeTimer: null, //进入离开直播间定时器
  529. redTimer: null,
  530. liveStartTimer: null,
  531. lotteryTimer: null,
  532. stayTime: 0,
  533. startTime: 0,
  534. scrollTop: 0,
  535. isOnload: false,
  536. isConnecting: false, // 是否正在连接中
  537. hasInitialized: false,
  538. liveViewersData: [],
  539. liveUserCalled: false, //调用过watchUserList没
  540. userRandomColors: Object.create(null), // 缓存用户ID -> 随机色的映射
  541. heartBeatRetryCount: 0, // 心跳发送重试次数(避免一次失败就重连)
  542. maxHeartBeatRetries: 2, // 心跳最大重试次数
  543. pingTimeout: 20000, // 心跳超时时间从10s延长到20s(应对网络波动)
  544. shownEntryUsers: new Set(), // 存储已显示过「进入提示」的用户ID
  545. socket: null, // WebSocket 实例
  546. isSocketOpen: false, // 连接是否已打开
  547. heartBeatInterval: 15000, // 心跳发送间隔(15秒)
  548. reconnectCount: 0, // 当前重连次数
  549. maxReconnectAttempts: 3, // 最大重连次数
  550. isManualClose: false, // 是否手动关闭(用于区分主动关闭和异常断开)
  551. templateId: 'fxYJu817lxHNKXl0hJypb-y-lLm8CNmGbcwpQmasCVg',
  552. isAgreement: false,
  553. wsNewUrl: 'wss://api.fhhx.runtzh.com/ws/app/webSocket',
  554. // wsNewUrl: 'ws://192.168.10.166:7114/ws/app/webSocket',
  555. qrFrom: null,
  556. scene: '',
  557. liveCountdown: {}, //直播倒计时
  558. countdown: {}, //抽奖倒计时
  559. liveViewData: {},
  560. keyboardHeight: 0,
  561. videoCurrentTime: 0, // 当前视频播放时间
  562. videoProgressKey: '', // 存储进度的key
  563. inAndOut: {},
  564. winning: false,
  565. isShowPrize: false,
  566. isShowCoupon: false,
  567. prizeInfo: [],
  568. havePrize: uni.getStorageSync('havePrize') || false, //是否参与抽奖
  569. countDownKey: 0,
  570. lotteryProducts: [],
  571. lotteryList: [],
  572. talklist: [],
  573. isShowLotteryPop: false,
  574. liveItem: {},
  575. isSending: false,
  576. isMore: false,
  577. value: '',
  578. placeholderText: '说点什么...',
  579. prizeAll: [],
  580. isShowLottery: false,
  581. isShowRedCard: false,
  582. redCard: null, //点击红包出现弹窗
  583. integral: {},
  584. lotteryInfo: {},
  585. goodsCard: {},
  586. couponInfo: {},
  587. redInfo: {},
  588. storeId: null,
  589. isFocus: false,
  590. shopping: false,
  591. inputInfo: '',
  592. showWelcomeMessage: false,
  593. scrollIntoView: '',
  594. isShowGoods: false,
  595. isShowRed: false,
  596. lastClickTime: 0,
  597. videoRetryCounts: Object.create(null), // 记录每个直播间的视频重试次数,格式: { liveId: 次数 }
  598. clickDelay: 300, // 300ms内只响应一次点击
  599. liveUserTotal: 0,
  600. viewPageSize: 10, // 每页数量
  601. viewPageNum: 1, // 当前页码
  602. viewLoading: false, // 是否正在加载
  603. scrollHeight: 0,
  604. showPurchasePrompt: false,
  605. prevOrderCount: 0, // 用于记录上一次的购买人数
  606. videoUrl: null,
  607. showType: 1, //横屏1 竖屏2
  608. boxHeight: 300, //小黄车高度
  609. liveViewers: [], //观众
  610. livingUrl: "",
  611. products: [],
  612. loadingProducts: false, // 商品加载状态
  613. orderUser: {}, //正在购买
  614. userType: 0,
  615. timestamp: '',
  616. showadd: false,
  617. liveId: null,
  618. userinfo: '', //用户信息
  619. userData: {},
  620. diffTotalTime: ''
  621. };
  622. },
  623. async onLoad(options) {
  624. if (options.liveId) {
  625. this.liveId = options.liveId;
  626. }
  627. // 扫码传来的参数
  628. if (options.scene) {
  629. this.scene = options.scene;
  630. const decodedScene = decodeURIComponent(this.scene);
  631. const params = {};
  632. decodedScene.split('&').forEach(item => {
  633. const [key, value] = item.split('=');
  634. params[key] = value;
  635. this.liveId = params.a;
  636. })
  637. if (params.b && params.c) {
  638. this.qrFrom = `&companyId=${params.b}&companyUserId=${params.c}`;
  639. }
  640. }
  641. if (options.companyId && options.companyUserId) {
  642. this.qrFrom = `&companyId=${options.companyId}&companyUserId=${options.companyUserId}`;
  643. }
  644. this.userinfo = uni.getStorageSync("userInfo")
  645. this.userData = uni.getStorageSync("userData")
  646. console.log("全部参数", options)
  647. try {
  648. const isLogin = await this.utils.isLogin();
  649. if (isLogin) {
  650. await this.getUserInfo();
  651. this.initTime();
  652. if (this.liveId) {
  653. // 先获取直播间信息
  654. await this.getliving(this.liveId);
  655. this.isOnload = true;
  656. // 然后获取聊天记录
  657. await this.getLiveMsg(this.liveId);
  658. await this.getliveViewData(this.liveItem);
  659. }
  660. }
  661. } catch (error) {
  662. console.error("初始化失败:", error);
  663. }
  664. //获取键盘高度
  665. uni.onKeyboardHeightChange(res => {
  666. this.keyboardHeight = res.height * 2;
  667. });
  668. },
  669. onPullDownRefresh() {
  670. this.getLiveMsg(this.liveId)
  671. this.getliveUser()
  672. setTimeout(() => {
  673. uni.stopPullDownRefresh()
  674. }, 1000)
  675. },
  676. async onShow() {
  677. if (this.liveId) {
  678. await this.getLiveMsg(this.liveItem);
  679. }
  680. if (!this.liveViewData) {
  681. this.getliveViewData(this.liveItem);
  682. }
  683. if (!this.userData) {
  684. await this.getUserInfo()
  685. }
  686. this.uuId = generateRandomString(16)
  687. const isLiveLogin = uni.getStorageSync('isLiveLogin')
  688. this.share = uni.getStorageSync('share')
  689. this.scene = uni.getStorageSync('scene')
  690. if (this.share && this.share.length > 0) {
  691. this.liveId = this.share.liveId
  692. this.qrFrom = `&companyId=${this.share.companyId}&companyUserId=${this.share.companyUserId}`;
  693. uni.removeStorageSync('share')
  694. }
  695. if (this.scene) {
  696. const decodedScene = decodeURIComponent(this.scene);
  697. const params = {};
  698. decodedScene.split('&').forEach(item => {
  699. const [key, value] = item.split('=');
  700. params[key] = value;
  701. this.liveId = params.a;
  702. })
  703. if (params.b && params.c) {
  704. this.qrFrom = `&companyId=${params.b}&companyUserId=${params.c}`;
  705. }
  706. uni.removeStorageSync('scene')
  707. }
  708. if (isLiveLogin) {
  709. if (this.liveId) {
  710. await this.getliving(this.liveId)
  711. this.getCurrentActivities()
  712. this.getliveOrder()
  713. this.initSocket()
  714. }
  715. this.hasInitialized = true
  716. uni.removeStorageSync('isLiveLogin');
  717. }
  718. // 恢复播放和连接
  719. await this.resumePageActivity()
  720. this.userinfo = JSON.parse(uni.getStorageSync("userInfo"))
  721. this.isAgreement = uni.getStorageSync('isAgreement')
  722. this.$nextTick(() => {
  723. this.setVideoProgress();
  724. });
  725. if (this.lookTimer) {
  726. clearInterval(this.lookTimer)
  727. this.lookTimer = null;
  728. this.stayTime = 0;
  729. this.startTime = 0;
  730. }
  731. if (this.trafficTimer) {
  732. clearInterval(this.trafficTimer)
  733. this.trafficTimer = null;
  734. this.startTime = 0;
  735. this.totalTraffic = 0;
  736. }
  737. this.startTimer()
  738. },
  739. //分享给好友
  740. onShareAppMessage() {
  741. return {
  742. title: '邀请你来观看直播:' + this.liveItem.liveName,
  743. path: '/pages_course/living?companyId=-2&companyUserId=' + this.userData.userId + '&liveId=' + this.liveId,
  744. imageUrl: '/static/images/logo.png',
  745. success(res) {
  746. console.log("分享成功", res);
  747. },
  748. fail(err) {
  749. console.error("分享失败", err);
  750. }
  751. };
  752. },
  753. computed: {
  754. filteredViewers() {
  755. const safeLiveViewers = Array.isArray(this.liveViewersData) ? this.liveViewersData : [];
  756. // 截取前3项
  757. return safeLiveViewers.slice(0, 3);
  758. },
  759. isCurrentUserWon() {
  760. if (!Array.isArray(this.prizeInfo) || !this.userData?.userId) {
  761. return false;
  762. }
  763. return this.prizeInfo.some(item => {
  764. return String(item.userId) === String(this.userData.userId);
  765. });
  766. }
  767. },
  768. onHide() {
  769. // 清除所有定时器
  770. this.clearAllTimers();
  771. this.stopHeartBeat()
  772. },
  773. onUnload() {
  774. this.stopHeartBeat()
  775. this.saveVideoProgress()
  776. if (this.liveItem) {
  777. this.pauseVideo()
  778. // 清除直播间的时间定时器
  779. if (this.liveItem.timeTimer) {
  780. clearInterval(this.liveItem.timeTimer);
  781. this.liveItem.timeTimer = null; // 清空定时器标识
  782. }
  783. }
  784. this.liveUserCalled = false
  785. this.loadUserColorsFromStorage();
  786. // 先关闭WebSocket
  787. this.closeWebSocket(true);
  788. // 清除所有定时器
  789. this.clearAllTimers();
  790. // 暂停视频
  791. const videoId = `myVideo_${this.liveId}`;
  792. const videoContext = uni.createVideoContext(videoId, this);
  793. if (videoContext) {
  794. videoContext.pause();
  795. }
  796. // 清理大数据
  797. this.talklist = [];
  798. this.liveViewersData = [];
  799. this.liveViewers = [];
  800. this.products = [];
  801. this.liveItem = [];
  802. },
  803. mounted() {
  804. this.getCurrentActivities();
  805. this.getliveOrder()
  806. },
  807. watch: {
  808. // 监听liveItem.status的变化
  809. 'liveItem.status': {
  810. handler(newStatus, oldStatus) {
  811. if (newStatus === undefined || oldStatus === undefined) return;
  812. if (newStatus === oldStatus) return;
  813. if (!this.liveId) {
  814. console.warn("liveId不存在,无法触发getliving请求");
  815. return;
  816. }
  817. // 状态变化时,调用getliving更新直播间数据
  818. // console.log(`liveItem.status从${oldStatus}变为${newStatus},触发getliving请求`);
  819. this.getliving(this.liveId);
  820. },
  821. deep: true,
  822. },
  823. // 监听orderUser.count的变化
  824. 'orderUser.count': {
  825. handler(newVal, oldVal) {
  826. if (newVal !== this.prevOrderCount) {
  827. this.prevOrderCount = newVal;
  828. this.showPurchaseMessage();
  829. }
  830. },
  831. immediate: true
  832. }
  833. },
  834. methods: {
  835. // 定期清理视频缓存
  836. startVideoCacheCleanup() {
  837. if (this.videoCleanupTimer) {
  838. clearInterval(this.videoCleanupTimer);
  839. }
  840. // 每30秒清理一次视频缓存
  841. this.videoCleanupTimer = setInterval(() => {
  842. this.cleanupVideoCache();
  843. }, 30000);
  844. },
  845. // 清理视频缓存
  846. cleanupVideoCache() {
  847. if (!this.liveItem || !this.liveId) return;
  848. // 只对录播视频进行清理
  849. if (this.liveItem.liveType === 2 && this.liveItem.videoUrl) {
  850. this.reloadVideoPlayer();
  851. }
  852. },
  853. // 重新加载视频播放器
  854. reloadVideoPlayer() {
  855. const videoId = `myVideo_${this.liveId}`;
  856. const videoContext = uni.createVideoContext(videoId, this);
  857. if (videoContext) {
  858. const currentTime = this.videoCurrentTime;
  859. // 暂停视频
  860. videoContext.pause();
  861. // 延迟重新加载
  862. setTimeout(() => {
  863. // 重新设置视频源,强制清理缓存
  864. this.$set(this.liveItem, 'videoUrl', this.liveItem.videoUrl + '&t=' + Date.now());
  865. // 恢复播放位置
  866. setTimeout(() => {
  867. if (videoContext.seek) {
  868. videoContext.seek(currentTime);
  869. }
  870. videoContext.play();
  871. }, 500);
  872. }, 100);
  873. }
  874. },
  875. // 启动内存监控
  876. startMemoryMonitoring() {
  877. if (this.memoryMonitorTimer) {
  878. clearInterval(this.memoryMonitorTimer);
  879. }
  880. this.memoryMonitorTimer = setInterval(() => {
  881. this.checkMemoryUsage();
  882. }, 15000); // 每15秒检查一次
  883. },
  884. // 检查内存使用情况
  885. checkMemoryUsage() {
  886. // 在微信小程序中,可以通过性能监控API检查内存
  887. if (wx && wx.getPerformance) {
  888. try {
  889. const performance = wx.getPerformance();
  890. const memory = performance.memory;
  891. if (memory && memory.usedJSHeapSize > 50 * 1024 * 1024) { // 超过50MB
  892. console.warn('内存使用过高,触发清理:', memory.usedJSHeapSize);
  893. this.forceMemoryCleanup();
  894. }
  895. } catch (e) {
  896. console.log('无法获取内存信息');
  897. }
  898. }
  899. },
  900. // 强制内存清理
  901. forceMemoryCleanup() {
  902. console.log('执行强制内存清理');
  903. // 1. 清理视频播放器
  904. this.cleanupVideoPlayer();
  905. // 2. 清理大数据
  906. this.clearBigData();
  907. // 3. 强制垃圾回收(在支持的环境下)
  908. this.triggerGarbageCollection();
  909. },
  910. // 清理视频播放器
  911. cleanupVideoPlayer() {
  912. const videoId = `myVideo_${this.liveId}`;
  913. const videoContext = uni.createVideoContext(videoId, this);
  914. if (videoContext) {
  915. // 停止播放
  916. videoContext.stop();
  917. // 重置视频源
  918. setTimeout(() => {
  919. this.$set(this.liveItem, 'videoUrl', '');
  920. setTimeout(() => {
  921. this.$set(this.liveItem, 'videoUrl', this.getFreshVideoUrl());
  922. this.playVideo();
  923. }, 500);
  924. }, 100);
  925. }
  926. },
  927. // 获取带时间戳的新视频URL
  928. getFreshVideoUrl() {
  929. if (!this.liveItem.originalVideoUrl) {
  930. this.liveItem.originalVideoUrl = this.liveItem.videoUrl;
  931. }
  932. // 添加时间戳参数,避免缓存
  933. const separator = this.liveItem.originalVideoUrl.includes('?') ? '&' : '?';
  934. return this.liveItem.originalVideoUrl + separator + 't=' + Date.now();
  935. },
  936. // 清理大数据
  937. clearBigData() {
  938. // 清理聊天记录,只保留最近50条
  939. if (this.talklist.length > 50) {
  940. this.talklist = this.talklist.slice(-50);
  941. }
  942. // 清理观众数据
  943. if (this.liveViewersData.length > 100) {
  944. this.liveViewersData = this.liveViewersData.slice(-100);
  945. }
  946. // 清理虚拟数据
  947. if (this.liveViewers.length > 100) {
  948. this.liveViewers = this.liveViewers.slice(-100);
  949. }
  950. // 清理商品数据
  951. if (this.products.length > 50) {
  952. this.products = this.products.slice(0, 50);
  953. }
  954. },
  955. // 触发垃圾回收(在支持的环境下)
  956. triggerGarbageCollection() {
  957. if (wx && wx.triggerGC) {
  958. wx.triggerGC();
  959. }
  960. },
  961. goMiniProgram() {
  962. // uni.navigateToMiniProgram({
  963. // appId: 'wx45cf09091aead547',
  964. // path: '/pages/home/index',
  965. // success: function(res) {
  966. // console.log('跳转成功', res);
  967. // },
  968. // fail: function(err) {
  969. // console.error('跳转失败', err);
  970. // }
  971. // });
  972. uni.showToast({
  973. title: '系统升级中,兑换请联系伴学助手!',
  974. icon: 'none'
  975. });
  976. },
  977. // 阻止双击事件
  978. preventDoubleClick(e) {
  979. e.preventDefault();
  980. e.stopPropagation();
  981. return false;
  982. },
  983. // 清理所有定时器
  984. clearAllTimers() {
  985. if (this.scrollTimer) {
  986. clearTimeout(this.scrollTimer);
  987. this.scrollTimer = null;
  988. }
  989. if (this.liveViewDataTimer) {
  990. clearInterval(this.liveViewDataTimer);
  991. this.liveViewDataTimer = null;
  992. }
  993. if (this.redTimer) {
  994. clearInterval(this.redTimer);
  995. this.redTimer = null;
  996. }
  997. if (this.liveStartTimer) {
  998. clearInterval(this.liveStartTimer);
  999. this.liveStartTimer = null;
  1000. }
  1001. if (this.lotteryTimer) {
  1002. clearInterval(this.lotteryTimer);
  1003. this.lotteryTimer = null;
  1004. }
  1005. if (this.welcomeTimer) {
  1006. clearInterval(this.welcomeTimer);
  1007. this.welcomeTimer = null;
  1008. }
  1009. if (this.trafficInterval != null) {
  1010. clearInterval(this.trafficInterval)
  1011. }
  1012. if (this.lookTimer) {
  1013. clearInterval(this.lookTimer)
  1014. this.lookTimer = null;
  1015. this.stayTime = 0;
  1016. this.startTime = 0;
  1017. }
  1018. if (this.trafficTimer) {
  1019. clearInterval(this.trafficTimer)
  1020. this.trafficTimer = null;
  1021. this.startTime = 0;
  1022. this.totalTraffic = 0;
  1023. }
  1024. if (this.intervalId) {
  1025. clearInterval(this.intervalId);
  1026. this.intervalId = null;
  1027. }
  1028. if (this.reconnectTimer) {
  1029. clearInterval(this.reconnectTimer);
  1030. this.reconnectTimer = null;
  1031. }
  1032. if (this.scrollTimer) {
  1033. clearTimeout(this.scrollTimer);
  1034. this.scrollTimer = null;
  1035. }
  1036. if (this.searchTimer) {
  1037. clearTimeout(this.searchTimer);
  1038. this.searchTimer = null;
  1039. }
  1040. if (this.purchasePromptTimer) {
  1041. clearTimeout(this.purchasePromptTimer);
  1042. this.purchasePromptTimer = null;
  1043. }
  1044. },
  1045. //直播计算流量
  1046. startTrafficCalculation(bitrate) {
  1047. if (this.trafficTimer) {
  1048. clearInterval(this.trafficTimer);
  1049. this.trafficTimer = null;
  1050. }
  1051. this.startTime = Date.now();
  1052. var that = this
  1053. this.trafficTimer = setInterval(() => {
  1054. that.calculateTraffic(bitrate);
  1055. }, 10000); // 每10秒计算一次
  1056. },
  1057. // 计算流量
  1058. calculateTraffic(bitrate) {
  1059. const currentTime = Date.now();
  1060. const duration = (currentTime - this.startTime) / 1000; // 持续时间(秒)
  1061. // 流量 = 码率 × 时间
  1062. // 码率单位: bps, 时间单位: 秒, 流量单位: 比特
  1063. const trafficBits = bitrate * duration;
  1064. // 转换为字节
  1065. this.totalTraffic = trafficBits / 8;
  1066. this.getLiveInternetTraffic()
  1067. },
  1068. startTimer() {
  1069. this.startTime = Date.now()
  1070. this.lookTimer = setInterval(() => {
  1071. this.stayTime = Math.floor((Date.now() - this.startTime) / 1000)
  1072. }, 1000)
  1073. },
  1074. //直播、录播缓冲
  1075. getLiveInternetTraffic() {
  1076. if (!this.liveId) return;
  1077. const currentTime = this.stayTime / this.liveItem.duration * 100;
  1078. const param = {
  1079. userId: this.userData.userId || '',
  1080. liveId: this.liveId || '',
  1081. uuId: dayjs().format('YYYYMMDD') + this.uuId,
  1082. internetTraffic: this.totalTraffic,
  1083. }
  1084. liveInternetTraffic(param)
  1085. },
  1086. // 回放、预告缓冲
  1087. getInternetTraffic() {
  1088. if (!this.liveId || !this.liveId || !this.userData.userId || !this.uuId) return;
  1089. const currentTime = this.stayTime / this.liveItem.duration * 100;
  1090. const param = {
  1091. videoType: this.liveItem.videoType,
  1092. videoId: this.liveItem.videoId,
  1093. userId: this.userData.userId,
  1094. liveId: this.liveId,
  1095. uuId: dayjs().format('YYYYMMDD') + this.uuId,
  1096. duration: this.liveItem.duration,
  1097. bufferRate: currentTime,
  1098. }
  1099. if (this.liveItem.status == 1) {
  1100. param.videoType = this.liveItem.previewVideoType || ''
  1101. param.videoId = this.liveItem.previewVideoId || ''
  1102. }
  1103. if (this.liveItem.liveType == 1) {
  1104. param.bufferRate = this.totalTraffic
  1105. }
  1106. internetTraffic(param)
  1107. },
  1108. scrollToBottom() {
  1109. if (this.scrollTimer) {
  1110. clearTimeout(this.scrollTimer);
  1111. }
  1112. this.scrollTimer = setTimeout(() => {
  1113. this.$nextTick(() => {
  1114. const query = uni.createSelectorQuery().in(this);
  1115. query.select('.scrolly').boundingClientRect(scrollRect => {
  1116. if (!scrollRect) return;
  1117. query.select('.scrolly').scrollOffset(scrollOffset => {
  1118. const newScrollTop = scrollOffset.scrollHeight - scrollRect
  1119. .height;
  1120. this.scrollTop = newScrollTop;
  1121. }).exec();
  1122. }).exec();
  1123. });
  1124. }, 0);
  1125. },
  1126. // 恢复页面活动
  1127. async resumePageActivity() {
  1128. if (this.liveItem) {
  1129. await this.getliving(this.liveId)
  1130. this.startTimeTimer(this.liveItem)
  1131. }
  1132. if (!this.isSocketAvailable()) {
  1133. this.initSocket()
  1134. }
  1135. },
  1136. // 获取用户专属随机色(缓存机制:同一用户始终用同一颜色)
  1137. getUserRandomColor(userId) {
  1138. if (!userId) {
  1139. return '#8978e2'; // 默认颜色
  1140. }
  1141. //如果缓存中已有该用户的颜色,直接返回
  1142. if (this.userRandomColors[userId]) {
  1143. return this.userRandomColors[userId];
  1144. }
  1145. //为新用户生成固定颜色(基于用户ID生成,不是完全随机)
  1146. const color = this.generateStableColor(userId);
  1147. this.userRandomColors[userId] = color;
  1148. // 存储到本地缓存,确保页面刷新后颜色不变
  1149. this.saveUserColorsToStorage();
  1150. return color;
  1151. }, // 基于用户ID生成稳定颜色(不是完全随机)
  1152. generateStableColor(userId) {
  1153. // 将用户ID转换为数字种子
  1154. let seed = 0;
  1155. for (let i = 0; i < userId.length; i++) {
  1156. seed = (seed * 31 + userId.charCodeAt(i)) % 1000000;
  1157. }
  1158. const colorPool = [
  1159. '#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
  1160. '#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9',
  1161. '#F8C471', '#82E0AA', '#F1948A', '#85C1E9', '#D7BDE2'
  1162. ];
  1163. return colorPool[seed % colorPool.length];
  1164. },
  1165. // 保存颜色映射到本地存储
  1166. saveUserColorsToStorage() {
  1167. try {
  1168. uni.setStorageSync('userRandomColors', this.userRandomColors);
  1169. } catch (e) {
  1170. console.warn('保存用户颜色缓存失败:', e);
  1171. }
  1172. },
  1173. // 从本地存储加载颜色映射
  1174. loadUserColorsFromStorage() {
  1175. try {
  1176. const cached = uni.getStorageSync('userRandomColors');
  1177. if (cached) {
  1178. this.userRandomColors = cached;
  1179. }
  1180. } catch (e) {
  1181. console.warn('加载用户颜色缓存失败:', e);
  1182. }
  1183. },
  1184. //头像名字
  1185. getNicknameInitial(nickName) {
  1186. if (!nickName || typeof nickName !== 'string') return '未';
  1187. if (/^[\u4e00-\u9fa5]/.test(nickName[0])) {
  1188. return nickName[0];
  1189. }
  1190. return nickName[0].toUpperCase();
  1191. },
  1192. async getUserInfo() {
  1193. await getUserInfo().then(
  1194. res => {
  1195. if (res.code == 200) {
  1196. this.userData = res.user;
  1197. } else {
  1198. uni.showToast({
  1199. icon: 'none',
  1200. title: "请求失败",
  1201. });
  1202. }
  1203. },
  1204. rej => {}
  1205. );
  1206. },
  1207. //订阅消息
  1208. handleAgreement() {
  1209. const templateId = this.templateId;
  1210. uni.requestSubscribeMessage({
  1211. tmplIds: [templateId],
  1212. success: (res) => {
  1213. // console.log("模板订阅状态", res[templateId]);
  1214. if (res[templateId] === "accept") {
  1215. uni.showToast({
  1216. title: '订阅成功,开播将提醒您',
  1217. icon: 'success'
  1218. });
  1219. this.callSendMessageApi()
  1220. } else if (res[templateId] === 'reject') {
  1221. uni.showToast({
  1222. title: '您已拒绝订阅,将无法收到提醒',
  1223. icon: 'none'
  1224. });
  1225. } else if (res[templateId] === 'ban') {
  1226. uni.showToast({
  1227. title: '您已关闭所有订阅权限,请在设置中开启',
  1228. icon: 'none'
  1229. });
  1230. }
  1231. },
  1232. fail: (err) => {
  1233. console.error('订阅消息失败', err);
  1234. uni.showToast({
  1235. title: '订阅失败,请重试',
  1236. icon: 'none'
  1237. });
  1238. }
  1239. });
  1240. },
  1241. async callSendMessageApi() {
  1242. if (!this.userData.userId) return;
  1243. const templateData = {
  1244. taskName: this.liveId,
  1245. userId: this.userData.userId,
  1246. templateId: this.templateId, // 模板ID
  1247. data: {
  1248. thing6: this.liveItem.liveName,
  1249. date7: this.liveItem.startTime,
  1250. }
  1251. };
  1252. subNotifyLive(templateData).then(res => {
  1253. if (res.code == 200) {
  1254. this.isAgreement = true;
  1255. uni.setStorageSync('isAgreement', true);
  1256. } else {
  1257. uni.showToast({
  1258. title: res.msg,
  1259. icon: 'none'
  1260. });
  1261. }
  1262. },
  1263. rej => {}
  1264. );
  1265. },
  1266. //发送心跳
  1267. sendHeartBeat() {
  1268. if (!this.isSocketAvailable()) return;
  1269. try {
  1270. const heartBeatMsg = JSON.stringify({
  1271. cmd: "heartbeat",
  1272. msg: "ping",
  1273. userId: this.userData.userId || '',
  1274. liveId: this.liveId
  1275. });
  1276. this.socket.send({
  1277. data: heartBeatMsg,
  1278. success: () => {
  1279. this.heartBeatRetryCount = 0; // 成功后重置重试次数
  1280. this.startPingTimeout(); // 启动超时检测
  1281. },
  1282. fail: (err) => {
  1283. console.error("心跳包发送失败:", err);
  1284. this.heartBeatRetryCount++;
  1285. // 心跳失败先重试,重试次数用完再重连
  1286. if (this.heartBeatRetryCount < this.maxHeartBeatRetries) {
  1287. setTimeout(() => this.sendHeartBeat(), 2000); // 2秒后重试
  1288. } else {
  1289. this.heartBeatRetryCount = 0;
  1290. this.handleReconnect(); // 重试用完才重连
  1291. }
  1292. }
  1293. });
  1294. } catch (err) {
  1295. console.error("心跳发送异常:", err);
  1296. this.heartBeatRetryCount++;
  1297. if (this.heartBeatRetryCount < this.maxHeartBeatRetries) {
  1298. setTimeout(() => this.sendHeartBeat(), 2000);
  1299. } else {
  1300. this.heartBeatRetryCount = 0;
  1301. this.handleReconnect();
  1302. }
  1303. }
  1304. },
  1305. // 启动心跳超时检测
  1306. startPingTimeout() {
  1307. // 清除之前的超时定时器
  1308. if (this.pingTimeoutTimer) {
  1309. clearTimeout(this.pingTimeoutTimer);
  1310. this.pingTimeoutTimer = null;
  1311. }
  1312. // 超时后触发重连
  1313. this.pingTimeoutTimer = setTimeout(() => {
  1314. // console.error("心跳响应超时,连接异常");
  1315. this.handleReconnect();
  1316. }, this.pingTimeout);
  1317. },
  1318. stopHeartBeat() {
  1319. if (this.heartBeatTimer) {
  1320. clearInterval(this.heartBeatTimer);
  1321. this.heartBeatTimer = null;
  1322. }
  1323. if (this.pingTimeoutTimer) {
  1324. clearTimeout(this.pingTimeoutTimer);
  1325. this.pingTimeoutTimer = null;
  1326. }
  1327. }, // 封装通用的连接状态校验方法
  1328. isSocketAvailable() {
  1329. // WebSocket readyState:0=CONNECTING, 1=OPEN, 2=CLOSING, 3=CLOSED
  1330. return this.socket && this.isSocketOpen && this.socket.readyState === 1;
  1331. },
  1332. // 处理重连逻辑
  1333. handleReconnect() {
  1334. this.resetReconnectState();
  1335. // uni.onNetworkStatusChange(res=>{
  1336. // if (res.networkType === 'wifi') {
  1337. // }
  1338. // })
  1339. this.stopHeartBeat();
  1340. if (this.reconnectCount < this.maxReconnectAttempts) {
  1341. this.reconnectCount++;
  1342. const delay = 1000;
  1343. console.log(`第${this.reconnectCount}次重连,延迟${delay}ms`);
  1344. this.reconnectTimer = setInterval(() => {
  1345. this.initSocket();
  1346. }, delay);
  1347. } else {
  1348. console.log(`停止重连(手动关闭/已达最大次数)`);
  1349. this.closeWebSocket(true);
  1350. }
  1351. },
  1352. // 重置重连状态
  1353. resetReconnectState() {
  1354. this.reconnectCount = 0;
  1355. if (this.reconnectTimer) {
  1356. clearInterval(this.reconnectTimer);
  1357. this.reconnectTimer = null;
  1358. }
  1359. },
  1360. //输入框
  1361. inputFocus() {
  1362. // this.chatHeight = this.keyboardHeight;
  1363. this.isFocus = true;
  1364. },
  1365. inputBlur() {
  1366. // this.chatHeight = 0;
  1367. this.isFocus = false
  1368. },
  1369. getTimeDifferenceInSeconds(createTimeStr) {
  1370. const createTime = new Date(createTimeStr.replace(/-/g, '/'));
  1371. const now = new Date();
  1372. const timeDiffMs = now - createTime;
  1373. const timeDiffSeconds = Math.floor(timeDiffMs / 1000);
  1374. return Math.max(0, timeDiffSeconds);
  1375. },
  1376. // 录播时间点
  1377. onVideoMetaLoaded(e) {
  1378. this.videoProgressKey = `videoProgress_${this.liveId}`;
  1379. this.setVideoProgress();
  1380. },
  1381. setVideoProgress() {
  1382. // 只有录播和回放需要设置进度
  1383. if (this.liveItem.liveType !== 2 && this.liveItem.liveType !== 3) {
  1384. return;
  1385. }
  1386. let currentTime = 0;
  1387. if (this.liveItem.liveType === 2) {
  1388. // 录播:计算当前时间与开始时间的差值,对视频总时长取模
  1389. const diff = this.getTimeDifferenceInSeconds(this.liveItem.startTime);
  1390. if (diff > this.liveItem.duration) {
  1391. const storedProgress = uni.getStorageSync(this.videoProgressKey);
  1392. currentTime = (storedProgress >= this.liveItem.duration) ? 0 : (storedProgress || 0);
  1393. } else {
  1394. currentTime = diff % this.liveItem.duration;
  1395. }
  1396. // currentTime = diff % this.liveItem.duration;
  1397. } else if (this.liveItem.liveType === 3) {
  1398. // 回放:从存储中获取进度
  1399. const storedProgress = uni.getStorageSync(this.videoProgressKey);
  1400. currentTime = storedProgress || 0;
  1401. }
  1402. const videoId = `myVideo_${this.liveId}`;
  1403. const videoContext = uni.createVideoContext(videoId, this);
  1404. if (videoContext) {
  1405. videoContext.seek(currentTime);
  1406. }
  1407. },
  1408. onVideoWaiting(e) {
  1409. // console.log('视频等待加载', e);
  1410. //流量
  1411. if (this.liveItem.liveType == 2) {
  1412. this.startTrafficCalculation(this.bitrate);
  1413. } else {
  1414. let that = this
  1415. if (this.trafficInterval) {
  1416. clearInterval(this.trafficInterval);
  1417. this.trafficInterval = null;
  1418. }
  1419. this.trafficInterval = setInterval(function() {
  1420. that.getInternetTraffic();
  1421. }, 10000);
  1422. }
  1423. },
  1424. // 视频时间更新
  1425. onVideoTimeUpdate(e) {
  1426. // 获取当前播放时间
  1427. this.videoCurrentTime = e.detail.currentTime;
  1428. // 每隔10秒保存一次进度(避免频繁存储)
  1429. if (Math.floor(this.videoCurrentTime) % 10 === 0) {
  1430. this.saveVideoProgress();
  1431. }
  1432. // this.setVideoProgress();
  1433. },
  1434. // 视频暂停
  1435. onVideoPause(e) {
  1436. if (this.liveItem.liveType === 2) {
  1437. const videoId = `myVideo_${this.liveId}`;
  1438. const videoContext = uni.createVideoContext(videoId, this);
  1439. setTimeout(() => {
  1440. videoContext.play();
  1441. }, 100);
  1442. }
  1443. // 暂停时保存进度
  1444. this.saveVideoProgress();
  1445. },
  1446. // 视频播放
  1447. onVideoPlay(e) {},
  1448. // 设置视频当前时间
  1449. // setVideoCurrentTime(time) {
  1450. // if (!time) return;
  1451. // const videoId = `myVideo_${this.liveId}`;
  1452. // const videoContext = uni.createVideoContext(videoId, this);
  1453. // if (videoContext) {
  1454. // videoContext.seek(time);
  1455. // console.log(`设置视频播放位置: ${time}秒`);
  1456. // }
  1457. // },
  1458. // 保存视频进度
  1459. saveVideoProgress() {
  1460. if (this.videoProgressKey) {
  1461. uni.setStorage({
  1462. key: this.videoProgressKey,
  1463. data: this.videoCurrentTime,
  1464. success: () => {},
  1465. fail: (err) => {
  1466. console.error('保存视频进度失败:', err);
  1467. }
  1468. });
  1469. }
  1470. },
  1471. // 点击红包
  1472. onRed() {
  1473. if (!this.liveId) return;
  1474. if (!this.redInfo?.redId) return;
  1475. if (this.redTimer) {
  1476. clearInterval(this.redTimer);
  1477. this.redTimer = null;
  1478. }
  1479. let data = {
  1480. liveId: this.liveId,
  1481. userId: this.userData.userId,
  1482. redId: this.redInfo.redId,
  1483. }
  1484. liveRed(data).then(res => {
  1485. this.isShowRed = false
  1486. this.redCard = res
  1487. this.isShowRedCard = true
  1488. },
  1489. rej => {}
  1490. );
  1491. },
  1492. //领取优惠券
  1493. onCoupon() {
  1494. if (!this.couponInfo.couponIssueId) return;
  1495. let data = {
  1496. goodsId: this.couponInfo.goodsId,
  1497. couponIssueId: this.couponInfo.couponIssueId,
  1498. liveId: this.liveId
  1499. };
  1500. coupon(data)
  1501. .then(res => {
  1502. this.isShowCoupon = false
  1503. if (res.code == 200) {
  1504. uni.showToast({
  1505. title: res.msg,
  1506. icon: 'none'
  1507. });
  1508. } else {
  1509. uni.showToast({
  1510. title: res.msg,
  1511. icon: 'none'
  1512. });
  1513. }
  1514. })
  1515. .catch(rej => {});
  1516. },
  1517. // 我的中奖名单
  1518. getMyLottery() {
  1519. this.winning = true
  1520. myLottery().then(res => {
  1521. if (res.code == 200) {
  1522. // console.log("我的中奖名单", res)
  1523. this.prizeAll = res.data.list || {};
  1524. } else {
  1525. }
  1526. })
  1527. .catch(rej => {});
  1528. },
  1529. // 抽奖
  1530. onLottery() {
  1531. if (!this.lotteryInfo) return;
  1532. let data = {
  1533. lotteryId: this.lotteryInfo.lotteryId
  1534. };
  1535. liveLottery(data)
  1536. .then(res => {
  1537. if (res.code == 200) {
  1538. const resData = res.data || {};
  1539. this.lotteryList = Array.isArray(resData) ? resData : [];
  1540. this.lotteryProducts = Array.isArray(resData.products) ? resData.products : [];
  1541. if (resData.duration) {
  1542. this.isShowLotteryPop = true;
  1543. }
  1544. } else {
  1545. uni.showToast({
  1546. title: res.msg,
  1547. icon: 'none'
  1548. });
  1549. this.lotteryList = [];
  1550. this.lotteryProducts = [];
  1551. }
  1552. })
  1553. .catch(rej => {
  1554. uni.showToast({
  1555. title: '获取抽奖信息失败',
  1556. icon: 'none'
  1557. });
  1558. // 失败时强制重置为数组
  1559. this.lotteryList = [];
  1560. this.lotteryProducts = [];
  1561. });
  1562. },
  1563. // 参与抽奖
  1564. onClaim() {
  1565. let data = {
  1566. liveId: this.liveId,
  1567. lotteryId: this.lotteryInfo.lotteryId,
  1568. }
  1569. claim(data).then(res => {
  1570. if (res.code == 200) {
  1571. uni.showToast({
  1572. title: res.msg,
  1573. icon: 'none'
  1574. });
  1575. this.isShowLotteryPop = false
  1576. this.havePrize = true
  1577. uni.setStorageSync('havePrize', this.havePrize);
  1578. } else {
  1579. uni.showToast({
  1580. title: res.msg,
  1581. icon: 'none'
  1582. });
  1583. }
  1584. },
  1585. rej => {}
  1586. );
  1587. },
  1588. confirm() {
  1589. this.isShowPrize = false;
  1590. this.havePrize = false;
  1591. uni.setStorageSync('havePrize', this.havePrize);
  1592. },
  1593. // 商品收藏
  1594. onGoodsCollect(item) {
  1595. if (!item || item.length === 0 || !item.goodsId) {
  1596. return;
  1597. }
  1598. collectGoods(item.goodsId).then(res => {
  1599. if (res.code == 200) {
  1600. uni.showToast({
  1601. title: res.msg,
  1602. icon: 'none'
  1603. });
  1604. item.isFavorite = !item.isFavorite
  1605. } else {
  1606. uni.showToast({
  1607. title: res.msg,
  1608. icon: 'none'
  1609. });
  1610. }
  1611. },
  1612. rej => {}
  1613. );
  1614. },
  1615. //正在购买
  1616. getliveOrder(item) {
  1617. if (!this.liveId) {
  1618. return;
  1619. }
  1620. liveOrderUser(this.liveId).then(res => {
  1621. if (res.code == 200) {
  1622. this.orderUser = res;
  1623. } else {
  1624. console.log('获取正在购买用户失败');
  1625. }
  1626. },
  1627. rej => {}
  1628. );
  1629. },
  1630. onLiveStateChange(e, liveItem) {
  1631. // 可以根据状态码处理不同的直播状态
  1632. const stateCode = e.detail.code;
  1633. if (e.detail.code == -2301 || e.detail.code == -2302) {
  1634. this.playVideo()
  1635. } else if (e.detail.code == 2004) {
  1636. this.calculateTimeDiff(this.liveItem)
  1637. this.startTrafficCalculation(this.bitrateLive);
  1638. }
  1639. // 2001: 已经连接服务器
  1640. // 2002: 已经连接服务器,开始拉流
  1641. // 2003: 网络接收到首个视频数据包(IDR)
  1642. // 2004: 视频播放开始
  1643. // 2005: 视频播放进度
  1644. // 2006: 视频播放结束
  1645. // 2007: 视频播放Loading
  1646. // 2008: 解码器启动
  1647. // -2301: 网络断连,且经多次重连抢救无效,更多重试请自行重启播放
  1648. // -2302: 获取加速拉流地址失败
  1649. }, // 直播错误事件
  1650. onLiveError(e, liveItem) {
  1651. this.videoError(e, liveItem);
  1652. console.log('错误');
  1653. },
  1654. // 红包 卡片 抽奖
  1655. getCurrentActivities() {
  1656. if (!this.liveId) return;
  1657. currentActivities(this.liveId).then(res => {
  1658. if (res.code === 200) {
  1659. // 提取数据(默认空数组/对象避免报错)
  1660. this.redInfo = (Array.isArray(res.red) ? res.red : [])[0] || {};
  1661. this.lotteryInfo = (Array.isArray(res.lottery) ? res.lottery : [])[0] || {};
  1662. this.goodsCard = res.goods || {};
  1663. this.isShowGoods = this.goodsCard && this.goodsCard.status == 1;
  1664. this.isShowRed = this.redInfo && this.redInfo.redStatus == 1;
  1665. this.isShowLottery = this.lotteryInfo && this.lotteryInfo.lotteryStatus == 1;
  1666. if (this.isShowRed) {
  1667. this.redTimer = setInterval(() => {
  1668. const redCountdown = this.handleTime(this.redInfo.updateTime, this.redInfo
  1669. .duration)
  1670. if (!redCountdown) {
  1671. this.isShowRed = false
  1672. clearInterval(this.redTimer)
  1673. }
  1674. }, 1000);
  1675. }
  1676. // 处理抽奖定时器
  1677. if (this.isShowLottery) {
  1678. this.lotteryTimer = setInterval(() => {
  1679. this.countdown = this.handleTime(this.lotteryInfo.updateTime, this
  1680. .lotteryInfo
  1681. .duration)
  1682. }, 1000);
  1683. }
  1684. } else {
  1685. uni.showToast({
  1686. title: res.msg,
  1687. icon: 'none'
  1688. });
  1689. }
  1690. }, rej => {});
  1691. },
  1692. // 计算当前时间与 liveItem.startTime 的差值,并更新 totalTime
  1693. calculateTimeDiff(item) {
  1694. if (!item.startTime) return;
  1695. // 提取对应场景的时间字符串(直播用startTime,抽奖用updateTime)
  1696. let timeStr = item.startTime;
  1697. // 转换时间格式(适配iOS,将 "-" 替换为 "/")
  1698. const time = new Date(timeStr.replace(/-/g, "/"));
  1699. if (isNaN(time.getTime())) {
  1700. return;
  1701. }
  1702. const now = new Date();
  1703. let diffMs = Math.max(0, now.getTime() - time.getTime())
  1704. // 转换为 时:分:秒(补零处理)
  1705. const totalSeconds = Math.floor(diffMs / 1000);
  1706. const hours = this.padZero(Math.floor(totalSeconds / 3600));
  1707. const minutes = this.padZero(Math.floor((totalSeconds % 3600) / 60));
  1708. const seconds = this.padZero(totalSeconds % 60);
  1709. this.diffTotalTime = `${hours}:${minutes}:${seconds}`;
  1710. // this.$set(this.liveItem, "diffMs", diffMs);
  1711. // this.$set(this.liveItem, "totalSeconds", totalSeconds);
  1712. },
  1713. padZero(num) {
  1714. return num < 10 ? `0${num}` : num;
  1715. },
  1716. // 启动当前直播间的时间差值定时器
  1717. startTimeTimer(item) {
  1718. if (!item) return;
  1719. // 立即计算一次(避免等待1秒才显示)
  1720. const totalTime = this.calculateTimeDiff(item);
  1721. item.timeTimer = setInterval(() => {
  1722. const totalTime = this.calculateTimeDiff(item);
  1723. }, 1000);
  1724. },
  1725. toggleViewerList() {
  1726. const now = Date.now();
  1727. if (now - this.lastClickTime > this.clickDelay) {
  1728. this.showadd = !this.showadd;
  1729. if (this.showadd) {
  1730. this.getliveUser(false); // 加载第一页观众
  1731. }
  1732. this.lastClickTime = now;
  1733. }
  1734. },
  1735. // 播放视频
  1736. playVideo() {
  1737. if (!this.liveItem) {
  1738. console.log('liveItem 为空,无法播放视频');
  1739. return;
  1740. }
  1741. try {
  1742. // 直播流使用live-player
  1743. if (this.liveItem.liveType === 1 && this.liveItem.livingUrl && this.liveItem.status == 2) {
  1744. const livePlayerId = `myLivePlayer_${this.liveId}`;
  1745. const livePlayerContext = uni.createLivePlayerContext(livePlayerId, this);
  1746. // console.log("直播")
  1747. if (livePlayerContext) {
  1748. livePlayerContext.play();
  1749. }
  1750. } else if (this.liveItem.status == 1 && this.liveItem.previewUrl) {
  1751. const videoId = `myVideo_${this.liveId}`;
  1752. const videoContext = uni.createVideoContext(videoId, this);
  1753. if (videoContext) {
  1754. videoContext.play();
  1755. }
  1756. } else if (this.liveItem.liveType === 2 && this.liveItem.videoUrl && this.liveItem.status == 2) {
  1757. const videoId = `myVideo_${this.liveId}`;
  1758. const videoContext = uni.createVideoContext(videoId, this);
  1759. // console.log("录播")
  1760. if (videoContext) {
  1761. videoContext.play();
  1762. }
  1763. } // 回放视频使用video
  1764. else if (this.liveItem.liveType === 3 && this.liveItem.videoUrl && this.liveItem.status == 4) {
  1765. const videoId = `myVideo_${this.liveId}`;
  1766. const videoContext = uni.createVideoContext(videoId, this);
  1767. // console.log("回放")
  1768. if (videoContext) {
  1769. videoContext.play();
  1770. }
  1771. }
  1772. } catch (error) {
  1773. console.error("播放视频失败:", error);
  1774. }
  1775. },
  1776. pauseVideo() {
  1777. if (!this.liveItem) return;
  1778. try {
  1779. // 直播流使用live-player
  1780. if (this.liveItem.status == 1) {
  1781. const videoId = `myVideo_${this.liveId}`;
  1782. const videoContext = uni.createVideoContext(videoId, this);
  1783. if (videoContext) {
  1784. videoContext.pause();
  1785. }
  1786. } else if (this.liveItem.status == 2) {
  1787. if (this.liveItem.liveType === 1) {
  1788. const livePlayerId = `myLivePlayer_${this.liveId}`;
  1789. const livePlayerContext = uni.createLivePlayerContext(livePlayerId, this);
  1790. if (livePlayerContext) {
  1791. livePlayerContext.pause();
  1792. }
  1793. } else if (this.liveItem.liveType === 2) {
  1794. const videoId = `myVideo_${this.liveId}`;
  1795. const videoContext = uni.createVideoContext(videoId, this);
  1796. if (videoContext) {
  1797. videoContext.pause();
  1798. }
  1799. }
  1800. }
  1801. } catch (error) {
  1802. console.error("暂停视频失败:", error);
  1803. }
  1804. },
  1805. // 视频错误处理
  1806. videoError(e, liveItem) {
  1807. if (!liveItem || !this.liveId) return;
  1808. // 初始化重试计数
  1809. if (this.videoRetryCounts[liveItem.liveId] === undefined) {
  1810. this.videoRetryCounts[liveItem.liveId] = 0;
  1811. }
  1812. // 限制重试次数
  1813. if (this.videoRetryCounts[liveItem.liveId] >= 3) {
  1814. console.error(`直播间 ${this.liveId} 视频加载失败,停止重试`);
  1815. // 显示错误提示
  1816. uni.showToast({
  1817. title: "视频加载失败,请检查网络",
  1818. icon: 'none',
  1819. duration: 2000
  1820. });
  1821. return;
  1822. }
  1823. this.videoRetryCounts[this.liveId]++;
  1824. // 延迟重试
  1825. setTimeout(() => {
  1826. if (this.liveId === this.liveId) {
  1827. console.log(`第${this.videoRetryCounts[this.liveId]}次重试播放视频`);
  1828. this.playVideo();
  1829. }
  1830. }, 2000);
  1831. },
  1832. openViews() {
  1833. // 计算scroll-view高度
  1834. this.$nextTick(() => {
  1835. const query = uni.createSelectorQuery().in(this);
  1836. query.select('.view-box').boundingClientRect(data => {
  1837. if (data) {
  1838. this.scrollHeight = data.height - 80; // 80是标题高度,120是底部高度
  1839. }
  1840. }).exec();
  1841. });
  1842. },
  1843. // 滚动到底部触发
  1844. handleScrollToLower() {
  1845. // 清除上一次未执行的定时器,避免重复请求
  1846. if (this.scrollTimer) {
  1847. clearTimeout(this.scrollTimer);
  1848. }
  1849. // 延迟0.5秒(500毫秒)执行接口请求
  1850. this.scrollTimer = setTimeout(() => {
  1851. this.getliveUser(true);
  1852. }, 1000);
  1853. },
  1854. async getLiveMsg(liveItem) {
  1855. if (!liveItem || !this.liveId) {
  1856. console.error('getLiveMsg 错误:无效的 liveItem');
  1857. return;
  1858. }
  1859. try {
  1860. const res = await liveMsg(this.liveId, 30, 1);
  1861. if (res.code == 200) {
  1862. const rows = Array.isArray(res.rows) ? res.rows : [];
  1863. const reversedTalkList = [...rows].reverse();
  1864. this.talklist = Array.isArray(reversedTalkList) ? reversedTalkList : [];
  1865. // 获取历史消息后也滚动到底部
  1866. this.$nextTick(() => {
  1867. this.scrollToBottom();
  1868. });
  1869. } else {
  1870. this.talklist = [];
  1871. }
  1872. } catch (error) {
  1873. this.talklist = [];
  1874. console.error("获取聊天记录失败:", error);
  1875. }
  1876. },
  1877. generateRandomChineseName() {
  1878. const prefixes = [
  1879. '幸福', '快乐', '安康', '吉祥', '如意', '平安', '健康', '长寿',
  1880. '福气', '美满', '和谐', '团圆', '富贵', '荣华', '宁静', '淡泊',
  1881. '知足', '常乐', '悠然', '自在', '夕阳', '晚霞', '金秋', '银发',
  1882. '老顽童', '开心', '笑口常开', '岁月静好', '云淡风轻', '海阔天空'
  1883. ];
  1884. const suffixes = [
  1885. '老人', '大爷', '大妈', '爷爷', '奶奶', '伯伯', '阿姨', '前辈',
  1886. '先生', '女士', '长者', '翁', '婆', '哥', '姐', '友',
  1887. '客', '迷', '粉丝', '达人', '爱好者', '之家', '之人', '之心',
  1888. '之情', '之乐', '之旅', '之梦', '之歌', '之韵'
  1889. ];
  1890. const specialNames = [
  1891. '老有所乐', '知足常乐', '笑看人生', '云淡风轻', '岁月如歌',
  1892. '夕阳红', '金色年华', '老当益壮', '鹤发童颜', '老骥伏枥',
  1893. '闲云野鹤', '淡泊明志', '宁静致远', '随遇而安', '顺其自然',
  1894. '老来俏', '开心果', '老宝贝', '不老松', '常青树'
  1895. ];
  1896. // 30%概率使用特殊网名,70%使用前缀+后缀组合
  1897. if (Math.random() < 0.3) {
  1898. return specialNames[Math.floor(Math.random() * specialNames.length)];
  1899. } else {
  1900. const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
  1901. const suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
  1902. return prefix + suffix;
  1903. }
  1904. },
  1905. // 获取直播间用户
  1906. async getliveUser(isLoadMore = false) {
  1907. this.viewLoading = true;
  1908. try {
  1909. const res = await watchUserList(this.liveId, this.viewPageSize, this.viewPageNum, false);
  1910. if (res.code === 200) {
  1911. const userRows = Array.isArray(res.rows) ? res.rows : [];
  1912. let array = userRows.map(item => ({
  1913. avatar: item.avatar || '',
  1914. userId: item.userId || '',
  1915. nickName: item.nickName || '未命名'
  1916. }));
  1917. // 虚拟数据
  1918. let virtualData = []
  1919. let virtualTotal = res.total * 2 + 50
  1920. this.liveUserTotal = virtualTotal
  1921. for (let i = 0; i < virtualTotal; i++) {
  1922. let data = {
  1923. avatar: '',
  1924. userId: '8565' + i,
  1925. nickName: this.generateRandomChineseName()
  1926. }
  1927. virtualData.push(data)
  1928. }
  1929. this.liveViewersData = [...array, ...virtualData]
  1930. const newRows = Array.isArray(res.rows) ? res.rows : [];
  1931. const currentViewers = Array.isArray(this.liveViewers) ? this.liveViewers : [];
  1932. let viewlist = isLoadMore ? [...currentViewers, ...newRows] : newRows;
  1933. this.liveViewers = [...viewlist, ...virtualData]
  1934. this.viewPageNum++;
  1935. }
  1936. } catch (error) {
  1937. console.error('获取观众列表失败:', error);
  1938. } finally {
  1939. this.viewLoading = false;
  1940. }
  1941. },
  1942. //小黄车搜索商品
  1943. handleSearchInput() {
  1944. // 使用防抖优化性能,避免频繁请求
  1945. clearTimeout(this.searchTimer);
  1946. this.searchTimer = setTimeout(() => {
  1947. this.queryCollect();
  1948. }, 500); // 500毫秒延迟
  1949. },
  1950. // 显示购买提示信息
  1951. showPurchaseMessage() {
  1952. // 清除之前的定时器
  1953. if (this.purchasePromptTimer) {
  1954. clearTimeout(this.purchasePromptTimer);
  1955. }
  1956. // 显示提示
  1957. this.showPurchasePrompt = true;
  1958. // 2秒后自动隐藏
  1959. this.purchasePromptTimer = setTimeout(() => {
  1960. this.showPurchasePrompt = false;
  1961. }, 2000);
  1962. },
  1963. //名字超过省略
  1964. truncateString(str, maxLength) {
  1965. if (typeof str !== 'string' || str.length <= maxLength) {
  1966. return str;
  1967. }
  1968. return str.slice(0, maxLength) + '...'; // 截断后加省略号
  1969. },
  1970. // 跳转页面
  1971. navgetTo(url) {
  1972. uni.navigateTo({
  1973. url: url
  1974. })
  1975. },
  1976. // 修改获取直播信息方法
  1977. async getliving(liveId) {
  1978. if (!liveId) return;
  1979. const param = {
  1980. id: liveId
  1981. };
  1982. try {
  1983. const res = await getlive(param);
  1984. if (res.code !== 200) {
  1985. uni.showToast({
  1986. title: res.msg,
  1987. icon: 'none'
  1988. });
  1989. return;
  1990. }
  1991. this.liveItem = Object.assign({}, this.liveItem, res.data);
  1992. // this.talklist = Array.isArray((res.data || {}).talklist) ? res.data.talklist : [];
  1993. this.startTimeTimer(this.liveItem);
  1994. // 清除旧定时器(如预告倒计时)
  1995. if (this.liveStartTimer) {
  1996. clearInterval(this.liveStartTimer);
  1997. this.liveStartTimer = null;
  1998. }
  1999. if (res.data.status == 1) {
  2000. // 直播预告
  2001. this.liveStartTimer = setInterval(async () => {
  2002. this.liveCountdown = this.handleTime(res.data.startTime, 0);
  2003. if (!this.liveCountdown) {
  2004. uni.removeStorageSync('isAgreement');
  2005. await this.getliving(this.liveId);
  2006. clearInterval(this.liveStartTimer);
  2007. }
  2008. }, 1000);
  2009. this.$set(this.liveItem, 'previewUrl', res.data.previewUrl);
  2010. this.$set(this.liveItem, 'livingUrl', ''); // 清空直播流
  2011. this.$set(this.liveItem, 'videoUrl', ''); // 清空回放视频
  2012. } else if (res.data.status == 2) {
  2013. if (res.data.liveType == 1) {
  2014. // 直播流
  2015. let cTime = Math.floor(Math.random() * 10000) + 1;
  2016. let livingUrl = res.data.flvHlsUrl + "&t=" + cTime;
  2017. console.log("地址在", this.liveItem.livingUrl)
  2018. this.$set(this.liveItem, 'livingUrl', livingUrl);
  2019. this.$set(this.liveItem, 'videoUrl', ''); // 清空回放视频
  2020. } else if (res.data.liveType === 2) {
  2021. // 回放视频 2录播 3直播回放
  2022. // let urlStr = 'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/10%E6%9C%8820%E6%97%A5%20%281%29.m3u8'
  2023. // this.$set(this.liveItem, 'videoUrl', urlStr);
  2024. this.$set(this.liveItem, 'videoUrl', res.data.videoUrl);
  2025. this.$set(this.liveItem, 'livingUrl', '');
  2026. }
  2027. } else if (res.data.status == 4 && res.data.liveType == 3) {
  2028. this.$set(this.liveItem, 'videoUrl', res.data.videoUrl);
  2029. this.$set(this.liveItem, 'livingUrl', '');
  2030. } else {
  2031. // 未开播
  2032. this.$set(this.liveItem, 'livingUrl', '');
  2033. this.$set(this.liveItem, 'videoUrl', '');
  2034. }
  2035. this.$set(this.liveItem, 'autoplay', res.data.liveType !== 0);
  2036. this.$set(this.liveItem, 'showType', res.data.showType);
  2037. this.storeId = res.storeId;
  2038. // 启动“30秒刷新直播间数据”的定时器
  2039. this.startLiveViewDataTimer();
  2040. this.playVideo();
  2041. } catch (err) {
  2042. console.error("获取直播信息失败:", err);
  2043. uni.showToast({
  2044. title: "获取直播信息失败",
  2045. icon: 'none'
  2046. });
  2047. }
  2048. },
  2049. getPureDecimal(num, precision = 6) {
  2050. const decimalPart = Math.abs(num).toFixed(precision).split('.')[1];
  2051. return decimalPart?.replace(/0+$/, '') || ''; // 移除末尾多余的0
  2052. },
  2053. goBack() {
  2054. // 暂停当前视频
  2055. if (this.liveItem) {
  2056. this.pauseVideo();
  2057. }
  2058. // 关闭WebSocket连接
  2059. this.closeWebSocket(true);
  2060. // 导航返回
  2061. const pages = getCurrentPages();
  2062. if (pages.length > 1) {
  2063. uni.navigateBack();
  2064. } else {
  2065. uni.reLaunch({
  2066. url: '/pages_course/livingList'
  2067. });
  2068. }
  2069. },
  2070. // 点赞
  2071. async onLike() {
  2072. if (!this.liveId) return;
  2073. try {
  2074. const res = await liveDataLike(this.liveId);
  2075. if (res?.like) {
  2076. this.liveViewData.like++; // 只更新当前直播间的点赞数
  2077. } else {
  2078. uni.showToast({
  2079. title: res.msg,
  2080. icon: 'none'
  2081. });
  2082. }
  2083. } catch (error) {
  2084. console.error('点赞失败:', error);
  2085. }
  2086. },
  2087. //直播间点赞、关注、在线人数数据
  2088. getliveViewData(liveItem) {
  2089. if (!liveItem || !this.liveId) return;
  2090. getLiveViewData(this.liveId).then(res => {
  2091. if (res.code == 200) {
  2092. // 强制响应式更新,确保数据实时显示
  2093. this.liveViewData = res;
  2094. }
  2095. }).catch(error => {
  2096. console.error("获取直播间数据失败:", error);
  2097. // 失败时兜底,避免显示异常
  2098. this.liveViewData = {
  2099. like: 0,
  2100. watchCount: 0
  2101. };
  2102. });
  2103. },
  2104. // 30秒刷新一下直播间点赞数
  2105. startLiveViewDataTimer() {
  2106. // 先清除旧定时器(防止重复创建)
  2107. if (this.liveViewDataTimer) {
  2108. clearInterval(this.liveViewDataTimer);
  2109. this.liveViewDataTimer = null;
  2110. }
  2111. // 启动新定时器,每3秒执行一次
  2112. this.liveViewDataTimer = setInterval(() => {
  2113. // 安全校验:确保liveItem和liveId存在(避免无效请求)
  2114. if (this.liveItem && this.liveId) {
  2115. this.getliveViewData(this.liveItem);
  2116. }
  2117. }, 30000);
  2118. },
  2119. // 去购买,跳商品详情
  2120. goShop(productId, goodsId) {
  2121. if (!this.liveId) return;
  2122. uni.navigateTo({
  2123. url: '/pages_shopping/live/goods?productId=' + productId + '&liveId=' + this.liveId +
  2124. '&goodsId=' +
  2125. goodsId + '&storeId=' + this.storeId
  2126. })
  2127. },
  2128. // 查询店铺
  2129. async queryCollect() {
  2130. this.loadingProducts = true;
  2131. if (!this.liveId) return;
  2132. if (this.inputInfo == null) this.inputInfo = ''
  2133. uni.showLoading({
  2134. title: '加载中'
  2135. });
  2136. try {
  2137. const res = await liveStore(this.liveId, this.inputInfo);
  2138. uni.hideLoading()
  2139. this.shopping = true
  2140. if (res.code === 200) {
  2141. // 数据绑定到当前 liveItem,避免全局污染
  2142. this.products = Array.isArray(res.data) ? res.data : [];
  2143. }
  2144. } catch (error) {
  2145. console.error('获取小黄车商品失败:', error);
  2146. } finally {
  2147. this.loadingProducts = false;
  2148. }
  2149. },
  2150. // 时间戳
  2151. initTime() {
  2152. const now = new Date();
  2153. this.timestamp = now.getTime();
  2154. },
  2155. openCart() {
  2156. this.queryCollect()
  2157. },
  2158. close() {
  2159. this.showadd = false
  2160. },
  2161. // 关闭小黄车
  2162. closeShop() {
  2163. this.shopping = false;
  2164. },
  2165. closeMore() {
  2166. this.isMore = false;
  2167. },
  2168. closeWin() {
  2169. this.winning = false;
  2170. },
  2171. // 关闭WebSocket连接(isManual:是否手动关闭)
  2172. closeWebSocket(isManual = true) {
  2173. if (!this.socket || !this.isSocketOpen) {
  2174. console.warn("WebSocket 任务不存在或未打开,无需关闭");
  2175. return;
  2176. }
  2177. // console.log("websocket 连接关闭");
  2178. this.isManualClose = isManual;
  2179. this.stopHeartBeat();
  2180. this.resetReconnectState();
  2181. try {
  2182. // 先保存引用,避免在关闭过程中被置为null
  2183. const socketToClose = this.socket;
  2184. this.socket = null;
  2185. this.isSocketOpen = false;
  2186. socketToClose.close({
  2187. code: 1000,
  2188. reason: isManual ? "主动关闭" : "异常关闭"
  2189. });
  2190. console.log("WebSocket连接已发起关闭");
  2191. } catch (err) {
  2192. console.error("关闭WebSocket失败:", err);
  2193. // 即使关闭失败,也要重置状态
  2194. this.socket = null;
  2195. this.isSocketOpen = false;
  2196. }
  2197. },
  2198. startHeartBeat() {
  2199. this.stopHeartBeat(); // 先停止现有心跳,防止重复
  2200. this.sendHeartBeat(); // 立即发送一次心跳
  2201. // 按间隔循环发送心跳
  2202. this.heartBeatTimer = setInterval(() => {
  2203. this.sendHeartBeat();
  2204. }, this.heartBeatInterval);
  2205. },
  2206. initSocket() {
  2207. if (this.isConnecting) {
  2208. return;
  2209. }
  2210. // 如果已经存在连接且状态为open,则退出
  2211. if (this.socket && this.socket.readyState === 1) {
  2212. return;
  2213. }
  2214. if (this.socket && (this.socket.readyState === 0 || this.socket.readyState === 1)) {
  2215. console.log('关闭现有WebSocket连接,创建新连接');
  2216. this.closeWebSocket(true);
  2217. }
  2218. // 校验必要参数
  2219. if (!this.liveId) {
  2220. console.error("缺失直播间ID,无法初始化WebSocket");
  2221. return;
  2222. }
  2223. if (!this.userData || !this.userData.userId) {
  2224. console.error("用户信息缺失,无法初始化WebSocket");
  2225. return;
  2226. }
  2227. // 清除之前的重连定时器
  2228. this.resetReconnectState();
  2229. const now = new Date();
  2230. this.timestamp = now.getTime(); // 生成签名
  2231. const signature = CryptoJS.HmacSHA256(
  2232. `${this.liveId}${this.userData.userId}${this.userType}${this.timestamp}`,
  2233. this.timestamp.toString()
  2234. ).toString(CryptoJS.enc.Hex);
  2235. try {
  2236. const baseWsUrl = 'wss://api.fhhx.runtzh.com/ws/app/webSocket';
  2237. // const baseWsUrl = 'ws://192.168.10.166:7114/ws/app/webSocket';
  2238. let wsUrl =
  2239. `${baseWsUrl}?userId=${this.userData.userId}&liveId=${this.liveId}&userType=${this.userType}&timestamp=${this.timestamp}&signature=${signature}`;
  2240. if (this.qrFrom) {
  2241. wsUrl += this.qrFrom;
  2242. }
  2243. const socketTask = uni.connectSocket({
  2244. url: wsUrl,
  2245. success: () => {
  2246. // console.log("Socket连接请求发送成功")
  2247. },
  2248. fail: (err) => {
  2249. console.error("Socket连接请求失败:", err);
  2250. this.handleReconnect();
  2251. }
  2252. });
  2253. // 连接打开事件
  2254. socketTask.onOpen((res) => {
  2255. // console.log("WebSocket连接已打开");
  2256. this.socket = socketTask;
  2257. this.isConnecting = false;
  2258. this.isSocketOpen = true;
  2259. this.reconnectCount = 0;
  2260. this.resetReconnectState();
  2261. this.heartBeatRetryCount = 0; // 重置心跳重试次数
  2262. this.shownEntryUsers.clear(); // 重连后重置进入提示记录
  2263. this.startHeartBeat();
  2264. });
  2265. // 消息接收事件
  2266. socketTask.onMessage((res) => {
  2267. try {
  2268. const data = JSON.parse(res.data);
  2269. // 处理服务端心跳响应
  2270. if (data.cmd === "heartBeatAck") {
  2271. this.stopHeartBeat(); // 清除当前超时检测
  2272. this.startHeartBeat(); // 重新启动心跳周期
  2273. return;
  2274. }
  2275. this.handleSocketMessage(res);
  2276. } catch (err) {
  2277. console.error("消息解析异常:", err);
  2278. }
  2279. });
  2280. // 连接错误事件
  2281. socketTask.onError((err) => {
  2282. console.error("WebSocket连接错误:", err);
  2283. this.isSocketOpen = false;
  2284. this.stopHeartBeat();
  2285. this.isConnecting = false;
  2286. this.handleReconnect(); // 错误直接触发重连
  2287. });
  2288. // 连接关闭事件
  2289. socketTask.onClose((res) => {
  2290. console.log("WebSocket连接关闭:", res);
  2291. this.isSocketOpen = false;
  2292. this.isConnecting = false;
  2293. this.stopHeartBeat(); // 清除心跳定时器
  2294. if (!this.isManualClose && this.reconnectCount < this.maxReconnectAttempts) {
  2295. this.handleReconnect();
  2296. } else {}
  2297. });
  2298. } catch (e) {
  2299. console.error("创建WebSocket异常:", e);
  2300. this.handleReconnect();
  2301. }
  2302. },
  2303. handleTime(time, duration) {
  2304. // 将time转换为毫秒级时间戳
  2305. let timeStamp;
  2306. // 如果time是数字且合理(毫秒级时间戳通常为13位左右的正数),视为有效时间戳
  2307. if (typeof time === 'number' && time > 0 && time < 9999999999999) {
  2308. timeStamp = time;
  2309. }
  2310. //如果是字符串,尝试解析为日期后转为时间戳
  2311. else if (typeof time === 'string' && time.trim() !== '') {
  2312. // 处理兼容性:将"2025-09-25 17:30:00"转为ISO格式"2025-09-25T17:30:00"
  2313. const isoTime = time.replace(' ', 'T');
  2314. const date = new Date(isoTime);
  2315. // 校验日期是否有效
  2316. if (!isNaN(date.getTime())) {
  2317. timeStamp = date.getTime();
  2318. } else {
  2319. console.error('无效的日期格式:', time);
  2320. return false; // 格式错误时返回false
  2321. }
  2322. } else {
  2323. console.error('time参数必须是有效的时间戳(数字)或日期字符串');
  2324. return false;
  2325. }
  2326. // 后续逻辑保持不变(基于转换后的timeStamp计算)
  2327. const targetTimestamp = timeStamp + duration * 60 * 1000;
  2328. const currentTimestamp = new Date().getTime();
  2329. const timeDiffMs = targetTimestamp - currentTimestamp;
  2330. if (timeDiffMs <= 0) {
  2331. return false;
  2332. }
  2333. const hours = Math.floor(timeDiffMs / (1000 * 60 * 60));
  2334. const minutes = Math.floor((timeDiffMs % (1000 * 60 * 60)) / (1000 * 60));
  2335. const seconds = Math.floor((timeDiffMs % (1000 * 60)) / 1000);
  2336. const formatNum = (num) => num.toString().padStart(2, "0");
  2337. return {
  2338. hours: formatNum(hours),
  2339. minutes: formatNum(minutes),
  2340. seconds: formatNum(seconds)
  2341. };
  2342. },
  2343. // 限制聊天消息数量,防止内存泄漏
  2344. addToTalkList(message) {
  2345. const MAX_TALK_ITEMS = 50;
  2346. const oldList = Array.isArray(this.talklist) ? this.talklist : [];
  2347. const newList = [...oldList, message];
  2348. if (newList.length > MAX_TALK_ITEMS) {
  2349. newList.splice(0, newList.length - MAX_TALK_ITEMS);
  2350. }
  2351. this.talklist = newList;
  2352. // 使用 setTimeout 替代 requestAnimationFrame
  2353. setTimeout(() => {
  2354. this.scrollToBottom();
  2355. }, 0);
  2356. },
  2357. // 处理Socket消息
  2358. async handleSocketMessage(message) {
  2359. let data = JSON.parse(message.data)
  2360. const socketMessage = data.data // 服务端返回的消息体
  2361. if (data.code == 200) {
  2362. const messageData = {
  2363. ...socketMessage,
  2364. cmd: socketMessage.cmd || '', // 确保cmd字段存在
  2365. ts: Date.now() // 时间戳
  2366. };
  2367. // 处理服务端返回的sendMsg消息,加入本地列表
  2368. if (socketMessage.cmd == 'sendMsg') {
  2369. if (!this.isSocketAvailable()) {
  2370. uni.showToast({
  2371. title: '连接已断开,正在重试...',
  2372. icon: 'none'
  2373. });
  2374. this.handleReconnect();
  2375. return;
  2376. }
  2377. this.addToTalkList(messageData);
  2378. } else if (socketMessage.cmd == 'red') {
  2379. const redData = socketMessage.data ? JSON.parse(socketMessage.data) : {};
  2380. this.redInfo = redData || {};
  2381. this.isShowRed = socketMessage.status === 1;
  2382. if (this.isShowRed) {
  2383. this.redTimer = setInterval(() => {
  2384. const redCountdown = this.handleTime(this.redInfo.updateTime, this.redInfo
  2385. .duration)
  2386. if (!redCountdown) {
  2387. this.isShowRed = false
  2388. clearInterval(this.redTimer)
  2389. }
  2390. }, 1000);
  2391. }
  2392. } else if (socketMessage.cmd == 'goods') {
  2393. const goodsData = socketMessage.data ? JSON.parse(socketMessage.data) : {};
  2394. this.goodsCard = goodsData || {};
  2395. this.isShowGoods = socketMessage.status == 1;
  2396. } else if (socketMessage.cmd == 'coupon') {
  2397. const couponData = socketMessage.data ? JSON.parse(socketMessage.data) : {};
  2398. this.couponInfo = couponData || {};
  2399. this.isShowCoupon = socketMessage.status === 1;
  2400. if (this.isShowCoupon) {}
  2401. } else if (socketMessage.cmd == 'lottery') {
  2402. const lotteryData = socketMessage.data ? JSON.parse(socketMessage.data) : {};
  2403. this.lotteryInfo = lotteryData || {};
  2404. this.isShowLottery = socketMessage.status === 1;
  2405. if (socketMessage.status != 1) {
  2406. this.isShowLotteryPop = false
  2407. }
  2408. // 清除已有定时器(无论状态是否为1,先清掉旧的)
  2409. clearTimeout(this.lotteryTimer);
  2410. if (this.isShowLottery) {
  2411. this.lotteryTimer = setInterval(() => {
  2412. this.countdown = this.handleTime(this.lotteryInfo.updateTime, this.lotteryInfo
  2413. .duration)
  2414. if (!this.countdown) {
  2415. console.log("倒计时", this.countdown)
  2416. this.isShowLottery = false;
  2417. this.isShowLotteryPop = false;
  2418. clearInterval(this.lotteryTimer)
  2419. }
  2420. }, 1000);
  2421. } else {
  2422. this.isShowLottery = false
  2423. }
  2424. } else if (socketMessage.cmd == 'entry') {
  2425. try {
  2426. if (!this.liveUserCalled) {
  2427. await this.getliveUser(false);
  2428. this.liveUserCalled = true;
  2429. }
  2430. const userIdToEntry = socketMessage.userId;
  2431. const existingIndex = this.liveViewersData.findIndex(item => item.userId ===
  2432. userIdToEntry);
  2433. if (existingIndex === -1) {
  2434. const liveViewers = {
  2435. userId: socketMessage.userId,
  2436. nickName: socketMessage.nickName,
  2437. avatar: socketMessage.avatar
  2438. }
  2439. this.liveViewersData.push(liveViewers);
  2440. this.liveUserTotal++;
  2441. }
  2442. // 解析用户ID(根据实际接口字段调整,此处假设data含userId)
  2443. const userData = JSON.parse(socketMessage.data || '{}');
  2444. const userId = userData.userId || socketMessage.userId; // 兼容不同字段
  2445. if (!userId) return; // 无用户ID不处理
  2446. // 仅新用户(未显示过)才触发提示
  2447. if (!this.shownEntryUsers.has(userId)) {
  2448. this.inAndOut = socketMessage;
  2449. this.showWelcomeMessage = true;
  2450. this.shownEntryUsers.add(userId); // 加入已显示集合
  2451. // 3秒后隐藏提示(可调整时长)
  2452. if (this.welcomeTimer) clearTimeout(this.welcomeTimer);
  2453. this.welcomeTimer = setTimeout(() => {
  2454. this.showWelcomeMessage = false;
  2455. }, 3000);
  2456. }
  2457. } catch (err) {
  2458. console.error("解析entry用户数据失败:", err);
  2459. }
  2460. } else if (socketMessage.cmd == 'out') {
  2461. if (this.liveUserTotal > 0) {
  2462. const userIdToRemove = socketMessage.userId;
  2463. const index = this.liveViewersData.findIndex(item => item.userId === userIdToRemove);
  2464. if (index !== -1) {
  2465. this.liveViewersData.splice(index, 1);
  2466. this.liveUserTotal--; // 根据userId删除对应的用户数据
  2467. }
  2468. }
  2469. this.inAndOut = socketMessage;
  2470. this.showWelcomeMessage = true;
  2471. // 3秒后隐藏提示
  2472. if (this.welcomeTimer) clearTimeout(this.welcomeTimer);
  2473. this.welcomeTimer = setTimeout(() => {
  2474. this.showWelcomeMessage = false;
  2475. }, 3000);
  2476. } else if (socketMessage.cmd == 'live_start' || socketMessage.cmd ==
  2477. 'live_end') {
  2478. // 开始直播,关闭直播
  2479. if (this.liveStartTimer) {
  2480. clearInterval(this.liveStartTimer);
  2481. this.liveStartTimer = null;
  2482. }
  2483. if (this.redTimer) {
  2484. clearInterval(this.redTimer);
  2485. this.redTimer = null;
  2486. }
  2487. // 请求最新直播间数据
  2488. await this.getliving(this.liveId);
  2489. } else if (socketMessage.cmd == 'Integral') {
  2490. this.integral = {
  2491. msg: socketMessage.msg,
  2492. status: true
  2493. }
  2494. } else if (socketMessage.cmd == 'LotteryDetail') {
  2495. try {
  2496. this.prizeInfo = Array.isArray(JSON.parse(socketMessage.data || '[]')) ?
  2497. JSON.parse(socketMessage.data || '[]') : [];
  2498. } catch (err) {
  2499. console.error('解析抽奖结果失败:', err);
  2500. this.prizeInfo = [];
  2501. }
  2502. this.isShowPrize = true;
  2503. this.isShowLottery = false;
  2504. this.isShowLotteryPop = false;
  2505. } else if (socketMessage.cmd == 'blockUser') {
  2506. uni.removeStorage({
  2507. key: 'AppToken',
  2508. success: () => {
  2509. uni.reLaunch({
  2510. url: '/pages/auth/login'
  2511. });
  2512. }
  2513. });
  2514. }
  2515. } else {
  2516. uni.showToast({
  2517. title: data.msg,
  2518. icon: 'none'
  2519. });
  2520. }
  2521. },
  2522. sendMsg(retries = 1) {
  2523. // 防止连续点击发送两次(800ms短时锁定)
  2524. if (this.isSending) return;
  2525. this.isSending = true;
  2526. setTimeout(() => {
  2527. this.isSending = false;
  2528. }, 800);
  2529. const text = (this.value || '').trim();
  2530. if (!text) {
  2531. uni.showToast({
  2532. title: "不能发送空消息",
  2533. icon: 'none'
  2534. });
  2535. return;
  2536. }
  2537. if (!this.isSocketAvailable()) {
  2538. if (retries > 0) {
  2539. uni.showToast({
  2540. title: `连接不稳定,正在重试(${retries}次)...`,
  2541. icon: 'none'
  2542. });
  2543. // 延迟500ms重试,重试次数减1
  2544. setTimeout(() => this.sendMsg(retries - 1), 500);
  2545. } else {
  2546. uni.showToast({
  2547. title: '连接已断开,发送失败',
  2548. icon: 'none'
  2549. });
  2550. this.value = text; // 恢复输入框内容
  2551. }
  2552. return;
  2553. }
  2554. const liveId = this.liveId;
  2555. this.value = ''; // 立即清空输入框
  2556. // 构造发送给服务端的消息数据
  2557. const data = {
  2558. liveId,
  2559. userId: this.userData.userId,
  2560. userType: 0,
  2561. cmd: "sendMsg",
  2562. msg: text,
  2563. nickName: this.userData.nickname || '未命名',
  2564. avatar: this.userData.avatar || '/static/images/avatar.png'
  2565. };
  2566. // 发送socket消息
  2567. try {
  2568. this.socket.send({
  2569. data: JSON.stringify(data),
  2570. success: () => {
  2571. this.value = '';
  2572. },
  2573. fail: (err) => {
  2574. console.error("消息发送失败:", err);
  2575. if (retries > 0) {
  2576. uni.showToast({
  2577. title: `发送失败,正在重试(${retries}次)`,
  2578. icon: 'none'
  2579. });
  2580. setTimeout(() => this.sendMsg(retries - 1), 500);
  2581. } else {
  2582. uni.showToast({
  2583. title: '发送失败,请稍后再试',
  2584. icon: 'none'
  2585. });
  2586. this.value = text; // 恢复输入框内容
  2587. }
  2588. }
  2589. });
  2590. } catch (err) {
  2591. console.error("发送消息异常:", err);
  2592. if (retries > 0) {
  2593. setTimeout(() => this.sendMsg(retries - 1), 500);
  2594. } else {
  2595. uni.showToast({
  2596. title: '发送失败,请稍后再试',
  2597. icon: 'none'
  2598. });
  2599. this.value = text;
  2600. }
  2601. }
  2602. }
  2603. }
  2604. };
  2605. </script>
  2606. <style scoped lang="scss">
  2607. .skeleton-item {
  2608. display: flex;
  2609. padding: 20rpx;
  2610. background: #fff;
  2611. margin-bottom: 16rpx;
  2612. border-radius: 16rpx;
  2613. }
  2614. .skeleton-img {
  2615. width: 200rpx;
  2616. height: 200rpx;
  2617. background: #f0f0f0;
  2618. border-radius: 8rpx;
  2619. margin-right: 24rpx;
  2620. animation: pulse 1.5s ease-in-out infinite;
  2621. }
  2622. .skeleton-content {
  2623. flex: 1;
  2624. }
  2625. .skeleton-line {
  2626. height: 20rpx;
  2627. background: #f0f0f0;
  2628. margin-bottom: 16rpx;
  2629. border-radius: 4rpx;
  2630. animation: pulse 1.5s ease-in-out infinite;
  2631. }
  2632. .skeleton-line.short {
  2633. width: 60%;
  2634. }
  2635. .skeleton-line.medium {
  2636. width: 80%;
  2637. }
  2638. .skeleton-line.long {
  2639. width: 95%;
  2640. }
  2641. @keyframes pulse {
  2642. 0% {
  2643. opacity: 1;
  2644. }
  2645. 50% {
  2646. opacity: 0.5;
  2647. }
  2648. 100% {
  2649. opacity: 1;
  2650. }
  2651. }
  2652. ::v-deep .u-icon--right {
  2653. justify-content: flex-end !important;
  2654. }
  2655. // 无头像用户文字样式
  2656. .text-white {
  2657. color: #ffffff;
  2658. }
  2659. .text-xs {
  2660. font-size: 18rpx;
  2661. }
  2662. .text-sm {
  2663. font-size: 24rpx;
  2664. }
  2665. .w52,
  2666. .w72 {
  2667. display: flex;
  2668. align-items: center;
  2669. justify-content: center;
  2670. }
  2671. .swiper-wrapper {
  2672. position: relative;
  2673. width: 100%;
  2674. height: 100vh;
  2675. overflow: hidden;
  2676. background-color: #000000;
  2677. .container {
  2678. width: 100%;
  2679. height: 100%;
  2680. position: relative;
  2681. transition: opacity 0.3s ease;
  2682. transform: translateZ(0);
  2683. will-change: opacity;
  2684. }
  2685. }
  2686. .welcome-message {
  2687. position: fixed;
  2688. width: 100%;
  2689. bottom: 38vh;
  2690. left: 50%;
  2691. transform: translateX(-50%);
  2692. color: white;
  2693. padding: 10px 20px;
  2694. border-radius: 20px;
  2695. z-index: 10;
  2696. transition: opacity 0.3s ease;
  2697. }
  2698. .send {
  2699. background-color: #fafafa;
  2700. border-radius: 28rpx;
  2701. padding: 14rpx 16rpx;
  2702. color: #181818;
  2703. font-weight: 500;
  2704. }
  2705. /* 减少重绘和重排 */
  2706. .talk-list,
  2707. .viewer-item,
  2708. .message-item {
  2709. will-change: transform;
  2710. contain: layout style paint;
  2711. backface-visibility: hidden;
  2712. transform: translateZ(0);
  2713. }
  2714. .talk-list {
  2715. will-change: transform; // 提示浏览器优化
  2716. contain: layout style paint; // 限制渲染范围
  2717. max-width: 100%;
  2718. border-radius: 30rpx;
  2719. background-color: rgba(33, 33, 33, 0.5);
  2720. padding: 10rpx 30rpx;
  2721. }
  2722. .content {
  2723. position: relative;
  2724. z-index: 2;
  2725. height: 100%;
  2726. width: 100%;
  2727. top: 0;
  2728. left: 0;
  2729. display: flex;
  2730. flex-direction: column;
  2731. justify-content: space-between;
  2732. .activity-box {
  2733. position: fixed;
  2734. top: 188rpx;
  2735. left: 30rpx;
  2736. z-index: 999999;
  2737. display: flex;
  2738. align-items: center;
  2739. .item-box {
  2740. -webkit-transform: translateZ(0);
  2741. /* 硬件加速 */
  2742. transform: translateZ(0);
  2743. border-radius: 16rpx;
  2744. background-color: rgba(77, 77, 77, 0.5);
  2745. width: 90rpx;
  2746. height: 100rpx;
  2747. margin-right: 20rpx;
  2748. position: relative;
  2749. z-index: 999;
  2750. .tip {
  2751. position: absolute;
  2752. width: 100%;
  2753. bottom: 0;
  2754. color: #fafcff;
  2755. font-size: 26rpx;
  2756. background-color: rgba(15, 15, 15, 0.8);
  2757. border-radius: 16rpx;
  2758. text-align: center;
  2759. }
  2760. .item {
  2761. margin: 0 auto;
  2762. padding: 10rpx 0;
  2763. .img {
  2764. height: 100%;
  2765. }
  2766. }
  2767. }
  2768. }
  2769. // 减少重绘
  2770. .side-group,
  2771. .content-top {
  2772. will-change: transform;
  2773. }
  2774. .side-group {
  2775. position: absolute;
  2776. top: 65%;
  2777. right: 30rpx;
  2778. z-index: 1000;
  2779. display: flex;
  2780. flex-direction: column;
  2781. align-items: center;
  2782. .side-item {
  2783. font-weight: 500;
  2784. font-size: 24rpx;
  2785. color: #FFFFFF;
  2786. margin-bottom: 32rpx;
  2787. text-align: center;
  2788. // 将 button 选择器改为类选择器
  2789. .button-reset::after {
  2790. border: none !important;
  2791. padding: 0 !important;
  2792. margin: 0 !important;
  2793. }
  2794. .button-reset {
  2795. background-color: transparent !important;
  2796. padding: 0 !important;
  2797. line-height: inherit !important;
  2798. margin: 0 !important;
  2799. width: auto !important;
  2800. font-weight: 500 !important;
  2801. border-radius: none !important;
  2802. }
  2803. .image {
  2804. width: 72rpx;
  2805. height: 72rpx;
  2806. }
  2807. }
  2808. }
  2809. .content-top {
  2810. width: 100%;
  2811. margin-top: 48rpx;
  2812. display: flex;
  2813. justify-content: space-between;
  2814. padding: 0 24rpx;
  2815. box-sizing: border-box;
  2816. .sum {
  2817. width: 80rpx;
  2818. height: 52rpx;
  2819. background: rgba(0, 0, 0, 0.5);
  2820. border-radius: 26rpx 26rpx 26rpx 26rpx;
  2821. font-size: 24rpx;
  2822. color: #FFFFFF;
  2823. text-align: center;
  2824. line-height: 52rpx;
  2825. }
  2826. }
  2827. .follow-btn {
  2828. padding: 8rpx 16rpx;
  2829. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  2830. border-radius: 26rpx;
  2831. font-weight: 500;
  2832. font-size: 26rpx;
  2833. color: #FFFFFF;
  2834. }
  2835. }
  2836. .trailer-box {
  2837. position: relative;
  2838. top: 15%;
  2839. display: flex;
  2840. flex-direction: column;
  2841. align-items: center;
  2842. color: #ffffff;
  2843. padding: 20rpx;
  2844. .button {
  2845. margin-top: 20rpx;
  2846. background: #6d7bd4;
  2847. color: #fff;
  2848. border-radius: 20rpx;
  2849. padding: 20rpx 30rpx;
  2850. position: relative;
  2851. z-index: 99999;
  2852. }
  2853. .ash {
  2854. background: #636363;
  2855. }
  2856. .countdown-item {
  2857. display: flex;
  2858. flex-direction: column;
  2859. align-items: center;
  2860. .white {
  2861. width: 60rpx;
  2862. height: 60rpx;
  2863. text-align: center;
  2864. overflow: hidden;
  2865. background: #FFFFFF;
  2866. border-radius: 8rpx;
  2867. margin: 30rpx 10rpx 0;
  2868. font-weight: 600;
  2869. font-size: 24rpx;
  2870. color: #000000;
  2871. line-height: 60rpx;
  2872. }
  2873. }
  2874. .item {
  2875. width: 100%;
  2876. height: 400rpx;
  2877. }
  2878. .name {
  2879. font-size: 34rpx;
  2880. }
  2881. .img {
  2882. margin-bottom: 40rpx;
  2883. width: 240rpx;
  2884. height: 240rpx;
  2885. }
  2886. .title {
  2887. margin-top: 30rpx;
  2888. font-size: 42rpx;
  2889. font-weight: 500;
  2890. }
  2891. }
  2892. .videolist {
  2893. position: relative;
  2894. height: 100vh;
  2895. width: 100%;
  2896. .video {
  2897. transform: translateZ(0);
  2898. backface-visibility: hidden;
  2899. perspective: 1000;
  2900. height: 100vh;
  2901. width: 100%;
  2902. // background-color: rgba(57, 57, 57, 0.4);
  2903. .item {
  2904. width: 100%;
  2905. height: 100%;
  2906. }
  2907. .time {
  2908. color: #ffffff;
  2909. font-size: 20rpx;
  2910. margin-left: 10rpx;
  2911. }
  2912. .end {
  2913. position: absolute;
  2914. top: 50%;
  2915. left: 50%;
  2916. transform: translate(-50%, -50%);
  2917. font-size: 36rpx;
  2918. color: #fff;
  2919. }
  2920. .lable {
  2921. position: absolute;
  2922. top: 50rpx;
  2923. right: 16rpx;
  2924. background-color: rgba(57, 57, 57, 0.6);
  2925. padding: 4rpx 10rpx;
  2926. color: #fff;
  2927. border-radius: 15rpx;
  2928. }
  2929. }
  2930. .video_row {
  2931. position: absolute;
  2932. top: 20%;
  2933. max-height: 450rpx;
  2934. z-index: 99;
  2935. }
  2936. }
  2937. .prize-card {
  2938. width: 504rpx;
  2939. padding: 40rpx 40rpx 30rpx;
  2940. box-sizing: border-box;
  2941. display: flex;
  2942. flex-direction: column;
  2943. border-radius: 20rpx;
  2944. align-items: center;
  2945. background: linear-gradient(180deg, #f7823f 0%, #ffd4be 27%, #ffffff 100%);
  2946. position: relative;
  2947. .nav-img {
  2948. width: 311rpx;
  2949. position: absolute;
  2950. top: -122rpx;
  2951. left: 50%;
  2952. transform: translateX(-50%);
  2953. }
  2954. .title {
  2955. color: #C32008;
  2956. font-size: 34rpx;
  2957. font-weight: 600;
  2958. margin: 20rpx 0 40rpx;
  2959. }
  2960. .prize-content {
  2961. width: 100%;
  2962. display: flex;
  2963. justify-content: space-between;
  2964. align-items: center;
  2965. font-size: 28rpx;
  2966. margin: 10rpx 0;
  2967. .txt {
  2968. font-weight: 600;
  2969. }
  2970. }
  2971. .tip {
  2972. font-size: 28rpx;
  2973. color: #414141;
  2974. margin: 40rpx 0;
  2975. }
  2976. .button {
  2977. width: 200rpx;
  2978. height: 70rpx;
  2979. line-height: 70rpx;
  2980. background: linear-gradient(180deg, #ff7c30 0%, #FF3A1E 100%);
  2981. border-radius: 28rpx;
  2982. font-weight: 500;
  2983. font-size: 32rpx;
  2984. color: #ffffff;
  2985. text-align: center;
  2986. }
  2987. }
  2988. .no-prize-card {
  2989. width: 504rpx;
  2990. padding: 40rpx 40rpx 30rpx;
  2991. box-sizing: border-box;
  2992. display: flex;
  2993. flex-direction: column;
  2994. border-radius: 20rpx;
  2995. align-items: center;
  2996. position: relative;
  2997. .img {
  2998. margin-top: 40rpx;
  2999. width: 300rpx;
  3000. height: 300rpx;
  3001. }
  3002. .tip {
  3003. font-size: 36rpx;
  3004. color: #414141;
  3005. margin: 40rpx 0;
  3006. }
  3007. .button {
  3008. width: 220rpx;
  3009. height: 80rpx;
  3010. line-height: 80rpx;
  3011. background: linear-gradient(180deg, #fdfbb8 0%, #b79243 100%);
  3012. border-radius: 28rpx;
  3013. font-weight: 500;
  3014. font-size: 32rpx;
  3015. color: #ffffff;
  3016. text-align: center;
  3017. }
  3018. }
  3019. .red-card {
  3020. width: 550rpx;
  3021. height: 636rpx;
  3022. position: relative;
  3023. .img {
  3024. position: absolute;
  3025. width: 100%;
  3026. height: 100%;
  3027. }
  3028. .red-content {
  3029. position: relative;
  3030. z-index: 5;
  3031. display: flex;
  3032. flex-direction: column;
  3033. align-items: center;
  3034. .title {
  3035. font-size: 36rpx;
  3036. color: #FF3A1E;
  3037. margin: 180rpx 0 90rpx;
  3038. }
  3039. .txt {
  3040. font-size: 28rpx;
  3041. color: #FFECC3;
  3042. margin: 80rpx 0 40rpx;
  3043. }
  3044. .button {
  3045. width: 392rpx;
  3046. height: 96rpx;
  3047. line-height: 96rpx;
  3048. background: linear-gradient(180deg, #FFF4D5 0%, #FFE5B1 100%);
  3049. border-radius: 48rpx 48rpx 48rpx 48rpx;
  3050. font-weight: 600;
  3051. font-size: 36rpx;
  3052. color: #C32008;
  3053. text-align: center;
  3054. }
  3055. }
  3056. }
  3057. .integral-box {
  3058. min-width: 400rpx;
  3059. max-width: 600rpx;
  3060. display: flex;
  3061. flex-direction: column;
  3062. align-items: center;
  3063. border-radius: 20rpx;
  3064. overflow: hidden;
  3065. .top {
  3066. width: 100%;
  3067. position: relative;
  3068. .title {
  3069. width: 100%;
  3070. font-weight: 600;
  3071. font-size: 40rpx;
  3072. color: #FFFFFF;
  3073. text-shadow: 0px 4px 8px rgba(255, 89, 2, 0.8);
  3074. position: absolute;
  3075. top: 50rpx;
  3076. text-align: center;
  3077. }
  3078. .photo {
  3079. width: 100%;
  3080. }
  3081. }
  3082. .item {
  3083. padding: 20rpx;
  3084. .title {
  3085. font-weight: 500;
  3086. font-size: 32rpx;
  3087. text-align: center;
  3088. }
  3089. .button {
  3090. font-size: 32rpx;
  3091. margin-top: 20rpx;
  3092. background: linear-gradient(270deg, #ff4702 0%, #fe6304 100%);
  3093. color: #fff;
  3094. text-align: center;
  3095. padding: 18rpx 60rpx;
  3096. border-radius: 10rpx;
  3097. font-weight: 500;
  3098. }
  3099. }
  3100. }
  3101. .goods {
  3102. position: fixed;
  3103. bottom: 140rpx;
  3104. right: 104rpx;
  3105. z-index: 5;
  3106. background-color: #fff;
  3107. border-radius: 20rpx;
  3108. overflow: hidden;
  3109. width: 280rpx;
  3110. padding: 4rpx;
  3111. .top {
  3112. position: absolute;
  3113. top: 4rpx;
  3114. display: flex;
  3115. justify-content: space-between;
  3116. align-items: center;
  3117. color: #fff;
  3118. width: 100%;
  3119. padding-right: 10rpx;
  3120. box-sizing: border-box;
  3121. .left {
  3122. background-color: rgba(0, 0, 0, 0.8);
  3123. padding: 12rpx;
  3124. font-size: 22rpx;
  3125. display: flex;
  3126. justify-content: space-between;
  3127. align-items: center;
  3128. border-radius: 10rpx;
  3129. }
  3130. }
  3131. .photo {
  3132. width: 100%;
  3133. height: 180rpx;
  3134. border-radius: 16rpx 16rpx 0 0;
  3135. overflow: hidden;
  3136. }
  3137. .item {
  3138. padding: 4rpx;
  3139. .price {
  3140. font-size: 30rpx;
  3141. .red {
  3142. color: #FF5701;
  3143. font-weight: 600;
  3144. margin-right: 10rpx;
  3145. }
  3146. .del {
  3147. color: #828282;
  3148. font-weight: 500;
  3149. font-size: 28rpx;
  3150. text-decoration: line-through
  3151. }
  3152. }
  3153. .title {
  3154. font-weight: 500;
  3155. font-size: 30rpx;
  3156. margin: 10rpx 0 12rpx;
  3157. }
  3158. .button {
  3159. background: linear-gradient(270deg, #ff4702 0%, #fe6304 100%);
  3160. color: #fff;
  3161. text-align: center;
  3162. padding: 16rpx 0;
  3163. border-radius: 10rpx;
  3164. font-weight: 500;
  3165. font-size: 30rpx;
  3166. }
  3167. }
  3168. }
  3169. .coupon-pop {
  3170. position: fixed;
  3171. bottom: 140rpx;
  3172. right: 100rpx;
  3173. z-index: 5;
  3174. border-radius: 20rpx;
  3175. width: 320rpx;
  3176. .coupon-block {
  3177. width: 100%;
  3178. position: relative;
  3179. .item {
  3180. width: 100%;
  3181. position: absolute;
  3182. top: 20rpx;
  3183. display: flex;
  3184. flex-direction: column;
  3185. align-items: center;
  3186. color: #fff;
  3187. .title {
  3188. font-weight: 500;
  3189. font-size: 30rpx;
  3190. margin: 16rpx 0 12rpx;
  3191. }
  3192. .price {
  3193. font-size: 40rpx;
  3194. .bold {
  3195. font-size: 56rpx;
  3196. font-weight: 600;
  3197. }
  3198. }
  3199. .txt {
  3200. font-weight: 500;
  3201. font-size: 30rpx;
  3202. margin: 5rpx 0;
  3203. }
  3204. .button {
  3205. background: linear-gradient(270deg, #fffce1 0%, #ffeaaf 100%);
  3206. color: #ff0004;
  3207. text-align: center;
  3208. padding: 16rpx 0;
  3209. border-radius: 40rpx;
  3210. font-weight: 500;
  3211. font-size: 30rpx;
  3212. width: 70%;
  3213. margin-top: 26rpx;
  3214. }
  3215. }
  3216. .bg {
  3217. height: 432rpx;
  3218. width: 100%;
  3219. }
  3220. .nav {
  3221. position: absolute;
  3222. height: 120rpx;
  3223. top: -120rpx;
  3224. left: 0;
  3225. width: 100%;
  3226. z-index: 6;
  3227. }
  3228. .close {
  3229. position: absolute;
  3230. right: 10rpx;
  3231. top: 10rpx;
  3232. }
  3233. }
  3234. }
  3235. .icon-bg {
  3236. background-color: rgba(57, 57, 57, 0.8);
  3237. border-radius: 50%;
  3238. width: 88rpx;
  3239. height: 88rpx;
  3240. display: flex;
  3241. justify-content: center;
  3242. align-items: center;
  3243. transition: transform 0.2s ease;
  3244. }
  3245. .list {
  3246. width: 80%;
  3247. margin-bottom: 20rpx;
  3248. animation: simpleFade .2s;
  3249. }
  3250. @keyframes simpleFade {
  3251. from {
  3252. opacity: 0;
  3253. }
  3254. to {
  3255. opacity: 1;
  3256. }
  3257. }
  3258. .shop-prompt {
  3259. position: absolute;
  3260. bottom: 750rpx;
  3261. left: 24rpx;
  3262. padding: 6rpx 20rpx;
  3263. background: rgba(230, 154, 34, 0.7);
  3264. border-radius: 24rpx;
  3265. z-index: 9;
  3266. font-weight: 500;
  3267. color: #FFFFFF;
  3268. transition: opacity 0.3s ease;
  3269. }
  3270. .view-box {
  3271. position: relative;
  3272. height: 40vh;
  3273. /* 设置弹出层高度为视窗高度的70% */
  3274. padding: 40rpx 0rpx;
  3275. box-sizing: border-box;
  3276. display: flex;
  3277. flex-direction: column;
  3278. .scroll-content {
  3279. flex: 1;
  3280. margin-top: 50rpx;
  3281. overflow-y: auto;
  3282. padding: 0 40rpx;
  3283. }
  3284. .bottom {
  3285. padding: 20rpx 40rpx;
  3286. position: absolute;
  3287. bottom: 0;
  3288. width: 100%;
  3289. box-shadow: 0rpx -4rpx 10rpx 0rpx rgba(195, 195, 195, 0.3);
  3290. background: #fff;
  3291. }
  3292. }
  3293. // 抽奖
  3294. .prize-box {
  3295. position: relative;
  3296. .nav-img {
  3297. width: 311rpx;
  3298. position: absolute;
  3299. top: -122rpx;
  3300. left: 50%;
  3301. transform: translateX(-50%);
  3302. }
  3303. .bg-img {
  3304. position: absolute;
  3305. top: 0;
  3306. left: 0;
  3307. width: 100%;
  3308. height: 100%;
  3309. z-index: 1;
  3310. }
  3311. .prize-content {
  3312. position: relative;
  3313. z-index: 2;
  3314. padding: 24rpx 0 68rpx;
  3315. .item {
  3316. flex: 1;
  3317. text-align: center;
  3318. }
  3319. .white-item {
  3320. width: 40rpx;
  3321. height: 40rpx;
  3322. text-align: center;
  3323. overflow: hidden;
  3324. background: #FFFFFF;
  3325. box-shadow: inset 0rpx 2rpx 8rpx 0rpx #FFEBB2;
  3326. border-radius: 8rpx;
  3327. margin: 4rpx;
  3328. font-weight: 600;
  3329. font-size: 24rpx;
  3330. color: #F85D22;
  3331. line-height: 40rpx;
  3332. }
  3333. .item-group {
  3334. width: 100%;
  3335. }
  3336. .point-group {
  3337. margin: 20rpx 0 50rpx;
  3338. display: flex;
  3339. gap: 6rpx;
  3340. .item {
  3341. width: 20rpx;
  3342. height: 8rpx;
  3343. background: rgba(255, 255, 255, 0.5);
  3344. border-radius: 4rpx 4rpx 4rpx 4rpx;
  3345. }
  3346. .selected {
  3347. background: #FFEB66;
  3348. }
  3349. }
  3350. .button {
  3351. margin-top: 30rpx;
  3352. width: 520rpx;
  3353. height: 88rpx;
  3354. line-height: 88rpx;
  3355. background: linear-gradient(180deg, #FFF4D6 0%, #FFEB66 100%);
  3356. box-shadow: 0rpx 10rpx 8rpx 4rpx rgba(246, 82, 25, 0.5);
  3357. border-radius: 44rpx;
  3358. font-weight: 500;
  3359. font-size: 36rpx;
  3360. color: #F4410B;
  3361. text-align: center;
  3362. }
  3363. }
  3364. }
  3365. .more-block {
  3366. border-radius: 20rpx 0 0 20rpx;
  3367. padding: 70rpx 30rpx;
  3368. display: flex;
  3369. justify-content: space-between;
  3370. .item {
  3371. text-align: center;
  3372. }
  3373. }
  3374. .winning_record {
  3375. position: relative;
  3376. height: 800rpx;
  3377. .head_bg {
  3378. position: absolute;
  3379. top: -125rpx;
  3380. left: 50%;
  3381. width: 311rpx;
  3382. transform: translateX(-50%);
  3383. }
  3384. .bg {
  3385. width: 100%;
  3386. height: 100%;
  3387. }
  3388. .winning_content {
  3389. position: absolute;
  3390. top: 50rpx;
  3391. color: #fff;
  3392. width: 100%;
  3393. padding: 0 36rpx;
  3394. box-sizing: border-box;
  3395. .title {
  3396. text-align: center;
  3397. font-size: 40rpx;
  3398. font-weight: 500;
  3399. margin-bottom: 40rpx;
  3400. }
  3401. .row {
  3402. display: flex;
  3403. justify-content: space-between;
  3404. .head {
  3405. flex: 1;
  3406. text-align: center;
  3407. }
  3408. .start {
  3409. flex: 1;
  3410. }
  3411. .end {
  3412. flex: 1;
  3413. text-align: end;
  3414. }
  3415. }
  3416. }
  3417. }
  3418. .shoppop {
  3419. padding: 22rpx 16rpx;
  3420. .shoppop-top {
  3421. display: flex;
  3422. justify-content: space-between;
  3423. align-items: center;
  3424. padding: 0 16rpx 22rpx;
  3425. .search-input {
  3426. width: 414rpx;
  3427. height: 76rpx;
  3428. background: #FFFFFF;
  3429. border-radius: 36rpx;
  3430. margin-left: 20rpx;
  3431. padding: 0 32rpx;
  3432. box-sizing: border-box;
  3433. font-size: 24rpx;
  3434. margin-right: 24rpx;
  3435. }
  3436. .search-top {
  3437. font-size: 20rpx;
  3438. color: #222222;
  3439. }
  3440. }
  3441. .shop-list {
  3442. overflow: hidden;
  3443. .list-item {
  3444. display: flex;
  3445. align-items: center;
  3446. padding: 20rpx 16rpx;
  3447. background: #FFFFFF;
  3448. border-radius: 16rpx;
  3449. margin-bottom: 16rpx;
  3450. .goods-img {
  3451. width: 200rpx;
  3452. height: 200rpx;
  3453. border-radius: 16rpx;
  3454. overflow: hidden;
  3455. position: relative;
  3456. margin-right: 24rpx;
  3457. .img {
  3458. width: 100%;
  3459. height: 100%;
  3460. }
  3461. .goods-label {
  3462. position: absolute;
  3463. top: 0;
  3464. width: 64rpx;
  3465. height: 40rpx;
  3466. background: rgba(0, 0, 0, 0.5);
  3467. border-radius: 16rpx 0rpx 16rpx 0rpx;
  3468. text-align: center;
  3469. font-weight: 500;
  3470. font-size: 28rpx;
  3471. color: #FFFFFF;
  3472. }
  3473. }
  3474. .goods-right {
  3475. flex: 1;
  3476. .goods-title {
  3477. font-weight: 500;
  3478. font-size: 30rpx;
  3479. color: #000000;
  3480. margin-bottom: 10rpx;
  3481. }
  3482. .goods-details {
  3483. font-size: 24rpx;
  3484. color: #999999;
  3485. margin: 10rpx 0 20rpx;
  3486. }
  3487. .goods-people {
  3488. font-size: 22rpx;
  3489. color: #E69A22;
  3490. height: 56rpx;
  3491. }
  3492. .goods-shop {
  3493. display: flex;
  3494. justify-content: space-between;
  3495. .nummber {
  3496. color: #FF5C03;
  3497. font-size: 22rpx;
  3498. font-weight: 500;
  3499. }
  3500. .btn-group {
  3501. text-align: center;
  3502. line-height: 56rpx;
  3503. .collect-btn {
  3504. width: 72rpx;
  3505. height: 100%;
  3506. background: #F5F7FA;
  3507. border-radius: 8rpx 0rpx 0rpx 8rpx;
  3508. }
  3509. .shop-btn {
  3510. width: 152rpx;
  3511. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  3512. border-radius: 0rpx 8rpx 8rpx 0rpx;
  3513. font-weight: 500;
  3514. font-size: 30rpx;
  3515. color: #FFFFFF;
  3516. }
  3517. }
  3518. }
  3519. }
  3520. }
  3521. }
  3522. }
  3523. </style>