living.vue 99 KB

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