living.vue 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  1. <template>
  2. <view class="swiper-wrapper">
  3. <swiper class="swiper-container" :current="currentSwiperIndex" :circular="false" vertical
  4. @change="onSwiperChange" :style="{height: '100vh'}" :duration="swiperDuration">
  5. <swiper-item v-for="(liveItem, index) in list" :key="liveItem.liveId">
  6. <view class="container" :class="{active: currentSwiperIndex === index}">
  7. <view class="blackbg"></view>
  8. <view class="content">
  9. <!-- 页面内容 -->
  10. <view style="position: fixed;top: 0;z-index: 5;" class="content-top">
  11. <view class="u-flex-y-center">
  12. <image @click="goBack" style="width: 60rpx;height: 64rpx;margin-right: 26rpx;"
  13. src="/static/images/live/return.png"></image>
  14. <view class="align-center"
  15. style="padding: 6rpx 4rpx;height: 64rpx;background: rgba(0,0,0,0.5);border-radius: 32rpx;">
  16. <u-avatar :src="liveItem.liveImgUrl||$img.logo" size="32"></u-avatar>
  17. <view class="colorf ml10 mr6">
  18. <view>{{liveItem.liveName?truncateString(liveItem.liveName,8):"未命名"}}</view>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="u-flex-end align-center" @click="toggleViewerList" style="margin-top: 100rpx;">
  23. <image class="w52 h52 mr4" v-for="(item,viewerIndex) in filteredViewers"
  24. :key="viewerIndex" style="border-radius: 26rpx;" :src="item.avatar||$img.logo">
  25. </image>
  26. <view class="sum">{{liveUserTotal||0}}</view>
  27. </view>
  28. </view>
  29. <!-- 右边的 -->
  30. <view :class=" liveItem.showType==1 ? 'siderow-group' : 'side-group'">
  31. <view class="side-item">
  32. <image class="image" @click="onLike(liveItem)" src="/static/images/live/like.png">
  33. </image>
  34. <view>{{liveItem.liveViewData?.like||0}}</view>
  35. </view>
  36. <!-- <view class="side-item">
  37. <image @click="goStore(liveItem)" src="/static/images/live/shop.png"></image>
  38. <view>店铺</view>
  39. </view> -->
  40. <view class="side-item">
  41. <button open-type="share" class="button">
  42. <image class="image" src="/static/images/live/share.png" mode="widthFix"></image>
  43. </button>
  44. <view>分享</view>
  45. </view>
  46. </view>
  47. <!-- 红包内容 -->
  48. <view class="hongbao-box" v-if="liveItem.redInfo?.redStatus==1&&isShowRed">
  49. <view class="u-flex-y-center">
  50. <view class="tip">领红包</view>
  51. <view class="item">
  52. <image @click="onRed(liveItem)" src="/static/images/redbag.png" mode="widthFix">
  53. </image>
  54. </view>
  55. </view>
  56. </view>
  57. <view class="shop-prompt u-flex-y-center" v-if="showPurchasePrompt&&orderUser?.count">
  58. <image class="w32 h32 mr8" src="/static/images/live/shopping.png"></image>
  59. <text> {{orderUser.userName ? maskString(orderUser.userName) : ''}} 等
  60. {{orderUser.count || 0}}
  61. 人正在去购买</text>
  62. </view>
  63. <view class="videolist" style="margin: auto 0">
  64. <view class="video" style="height:100vh">
  65. <!-- 视频组件 -->
  66. <!-- <live-player v-if="currentSwiperIndex === index && liveItem.livingUrl" mode="live"
  67. orientation="vertical" :id="'myVideo_' + liveItem.liveId" :src="liveItem.livingUrl"
  68. :autoplay="currentSwiperIndex === index"
  69. bindstatechange="onStatechange"></live-player> -->
  70. <!-- <video v-if="currentSwiperIndex === index && liveItem.livingUrl"
  71. :id="'myVideo_' + liveItem.liveId"
  72. :class="liveItem.showType == 1 ? 'video_row' : 'videotop'" :src="liveItem.livingUrl"
  73. :controls='false' object-fit='contain' :custom-cache="false"
  74. :enable-progress-gesture="false" vslide-gesture-in-fullscreen='true'
  75. :show-center-play-btn="false" :http-cache="false" @error="videoError">
  76. </video> -->
  77. <live-player v-if="currentSwiperIndex === index && liveItem.livingUrl"
  78. :id="'myLivePlayer_' + liveItem.liveId"
  79. :class="liveItem.showType == 1 ? 'video_row' : 'videotop'" :src="liveItem.livingUrl"
  80. mode="live" orientation="vertical" :autoplay="currentSwiperIndex === index"
  81. :controls="false" object-fit="contain" :enable-progress-gesture="false"
  82. :show-center-play-btn="false" @error="livePlayerError"
  83. @statechange="onLivePlayerStateChange"
  84. @fullscreenchange="onLiveFullScreenChange"></live-player>
  85. <video v-if="currentSwiperIndex === index && liveItem.videoUrl"
  86. :class="liveItem.showType == 1 ? 'video_row' : 'videotop'" :src="liveItem.videoUrl"
  87. :autoplay="currentSwiperIndex === index" :controls='false' object-fit='contain'
  88. :custom-cache="false" :enable-progress-gesture="false"
  89. vslide-gesture-in-fullscreen='true' :show-center-play-btn="false"
  90. :http-cache="false" loop @error="videoError">
  91. </video>
  92. <view v-if="liveItem.videoUrl" class="time">{{liveItem.totalTime}}</view>
  93. </view>
  94. </view>
  95. <!-- 底部聊天区域 -->
  96. <view class="pb40 mt90" style="position: fixed;bottom: 0;width: 100%;">
  97. <view class="w100 h300 mt20">
  98. <scroll-view enable-flex scroll-y="true" class="talk p20 scrolly flex-1 column"
  99. style="width: calc(100% - 40rpx);height: calc(100% - 40rpx);"
  100. :scroll-into-view="liveItem.scrollIntoView">
  101. <view>
  102. <view class="list justify-start" v-for="(item,talkIndex) in liveItem.talklist"
  103. :key="talkIndex" v-show="item.cmd=='announcement'">
  104. <view class="talk-list ml16 justify-start">
  105. <view class="fs24">
  106. <text class='fs24 colorf'>
  107. {{item.msg}}直播间{{liveItem.messageContent}}</text>
  108. </view>
  109. </view>
  110. </view>
  111. </view>
  112. <view class="list justify-start" v-for="(item,talkIndex) in liveItem.talklist"
  113. :key="talkIndex" :id="`list_${talkIndex}`"
  114. v-show="item.cmd!='red'&&item.cmd!='out'&&item.cmd!='entry'">
  115. <view class="talk-list ml16 justify-start">
  116. <view class="fs24">
  117. <text style="color: #FFDA73;">{{item.nickName}}:</text>
  118. <text class='fs24 colorf'>{{item.msg}}</text>
  119. </view>
  120. </view>
  121. </view>
  122. <view v-if="liveItem.showWelcomeMessage" class="welcome-message">
  123. <view class="list justify-start" v-for="(item,talkIndex) in liveItem.talklist"
  124. :key="talkIndex" v-show="item.cmd=='entry'||item.cmd=='out'">
  125. <view class="talk-list ml16 justify-start">
  126. <view class="fs24">
  127. <text style="color: #ff89d6;">{{item.nickName}} </text>
  128. <text class='fs24 colorf'>
  129. {{item.msg}}直播间{{liveItem.messageContent}}</text>
  130. </view>
  131. </view>
  132. </view>
  133. </view>
  134. </scroll-view>
  135. </view>
  136. <!-- 底部输入框和操作按钮 -->
  137. <view class="justify-between p24">
  138. <view class="u-flex-y-center"
  139. style="background:rgba(57, 57, 57, 0.8);padding:10rpx 14rpx 10rpx 32rpx;width: 580rpx;box-sizing:border-box;border-radius:36rpx;">
  140. <u-input :placeholder="liveItem.placeholderText" border="none"
  141. customStyle='font-size:24rpx;' v-model="liveItem.value" shape='circle'
  142. color='#fff' placeholderStyle='color:#e7e7e7' class="ml20">
  143. </u-input>
  144. <view class="send" @click="sendMsg(liveItem)">发送</view>
  145. </view>
  146. <view class="justify-between mr15 align-center">
  147. <view class="icon-bg ml20" @click="openCart(liveItem)">
  148. <image src="/static/images/shopping.png" class="w48 h48"></image>
  149. </view>
  150. </view>
  151. </view>
  152. </view>
  153. </view>
  154. <u-popup :show="liveItem.goodsCard?.isShow" @close="!liveItem.goodsCard?.isShow" round='20rpx'
  155. mode="center" bgColor='#ffffff'>
  156. <view class="goods">
  157. <view class="top">
  158. <view class="left">
  159. <image class="w30 h30 mr8" src="/static/images/signal.png"></image>讲解中
  160. </view>
  161. <image class="w30 h30" src="/static/images/close.png"
  162. @click="liveItem.goodsCard.isShow=false"></image>
  163. </view>
  164. <image class="photo" :src="liveItem.goodsCard?.imgUrl"></image>
  165. <view class="item">
  166. <view class="price"><text class="red">¥{{liveItem.goodsCard?.price}} </text><text
  167. class="del">¥{{liveItem.goodsCard?.otPrice}}</text></view>
  168. <view class="title oneline-hide">{{liveItem.goodsCard?.productName}}</view>
  169. <view class="button"
  170. @click="goShop(liveItem.goodsCard?.productId,liveItem.goodsCard?.goodsId)">立即抢购
  171. </view>
  172. </view>
  173. </view>
  174. </u-popup>
  175. <!-- 抽奖 -->
  176. <!-- <u-popup :show="true" @close="" round='40rpx' bgColor='#fff'>
  177. <view class=" p20 bgf" style="border-radius: 40rpx;height: fit-content;">
  178. <view class="u-flex-row-reverse u-flex">
  179. <u-icon name="close" size="18" @click=""></u-icon>
  180. </view>
  181. <view class="column align-center h400">
  182. <view class="fs36 " style="color: #000000;font-weight: bold;">答题获取红包/积分奖励</view>
  183. <view class="answerpop" @click="noredanswer">
  184. <view class="color6 w350 fs24">
  185. 观看直播参与抽奖
  186. </view>
  187. <view class="answera">参与抽奖</view>
  188. </view>
  189. </view>
  190. </view>
  191. </u-popup> -->
  192. <u-popup :show="liveItem.integral?.status" @close="!liveItem.integral?.status" round='20rpx'
  193. mode="center" bgColor='#ffffff'>
  194. <view class="integral-box">
  195. <view class="top">
  196. <view class="title">观看视频领芳华币</view>
  197. <image class="photo" src="/static/images/integral.png" mode="widthFix"></image>
  198. </view>
  199. <view class="item">
  200. <view class="title ">{{liveItem.integral?.msg}}</view>
  201. <view class="button" @click="liveItem.integral.status=flase">确认</view>
  202. </view>
  203. </view>
  204. </u-popup>
  205. <!-- 观众列表弹窗 -->
  206. <u-popup :show="showadd" @close="close" @open="openViews" round='20rpx' bgColor='#ffffff'>
  207. <view class="view-box">
  208. <view class="t-c fs30">在线观众</view>
  209. <scroll-view scroll-y class="scroll-content" :style="{height: scrollHeight + 'px'}"
  210. @scrolltolower="handleScrollToLower">
  211. <view class="fs24 u-flex-y-center mb20" v-for="(item,index) in liveViewers"
  212. :key="index">
  213. <view
  214. :style="{color: index === 0 ? '#FF3B30' : index === 1 ? '#FF9500' : index === 2 ? '#FFCC00' : '#8E8E93',fontWeight: index < 3 ? 'bold' : 'normal',width: '50rpx'}"
  215. class="mr10">{{index+1}}</view>
  216. <u-avatar :src="item.avatar||$img.logo" size="32"></u-avatar>
  217. <text class="ml16">{{item.nickName||"未命名"}}</text>
  218. </view>
  219. <view class="no-more" v-if="viewNoMoreData && liveViewers.length > 0">
  220. <text>没有更多了</text>
  221. </view>
  222. </scroll-view>
  223. </view>
  224. </u-popup>
  225. <!-- 商品弹窗 -->
  226. <u-popup :show="liveItem.shopping" @close="closeShop" @open="openShop" round='20rpx'
  227. bgColor='f3f5f9'>
  228. <view class="shoppop">
  229. <view class="shoppop-top">
  230. <u-avatar :src="store.logoUrl" size="36" class="ml16"></u-avatar>
  231. <view class="search-input u-flex-y-center">
  232. <image style="width: 24rpx;margin-right: 16rpx;" src="/static/images/search.png"
  233. mode="widthFix">
  234. </image>
  235. <input placeholder="请搜索商品" v-model="liveItem.inputInfo"
  236. @input="handleSearchInput" />
  237. </view>
  238. <view class="t-c search-top" style="margin-right: 48rpx;">
  239. <image @click="onStoreCollect(liveItem)" v-if="store.isFavorite"
  240. style="width:40rpx;height: 40rpx;" src="/static/images/collect_select.png">
  241. </image>
  242. <image v-else @click="onStoreCollect(liveItem)" style="width: 32rpx;height: 32rpx;"
  243. src="/static/images/collect.png"></image>
  244. <view>收藏</view>
  245. </view>
  246. <view class="t-c search-top" @click="goOrderList(liveItem)">
  247. <image style="width: 40rpx;height: 40rpx;" src="/static/images/order.png"></image>
  248. <view>订单</view>
  249. </view>
  250. </view>
  251. <scroll-view enable-flex scroll-y class="shop-list" :style="{ height: boxHeight + 'px' }">
  252. <view class="list-item" v-for="(item,index) in products" :key="index">
  253. <view class="goods-img">
  254. <image :src="item.imgUrl" mode="widthFix"></image>
  255. <view class="goods-label">{{index+1}}</view>
  256. </view>
  257. <view class="goods-right">
  258. <view class="goods-title">{{item.productName}}</view>
  259. <view class="goods-people">{{item.sales}} 人已购</view>
  260. <view class="goods-shop">
  261. <text class="nummber"><text
  262. style="font-size: 20rpx;font-weight: 600;">¥</text><text
  263. style="font-size: 36rpx;font-weight: bold;">{{Math.trunc(item.price)}}</text>.{{getPureDecimal(item.price)?getPureDecimal(item.price):'00'}}/日</text>
  264. <view class="btn-group u-flex-y-center">
  265. <view class="collect-btn">
  266. <image v-if="item.isFavorite" @click="onGoodsCollect(item)"
  267. style="width: 32rpx;height: 32rpx;"
  268. src="/static/images/collect_select.png">
  269. </image>
  270. <image v-else @click="onGoodsCollect(item)"
  271. style="width: 32rpx;height: 32rpx;"
  272. src="/static/images/collect.png">
  273. </image>
  274. </view>
  275. <view v-if="item.status==1" class="shop-btn"
  276. @click="goShop(item.productId,item.goodsId)">去购买 </view>
  277. <view v-else-if="item.status==0" class="shop-btn">
  278. 已下架</view>
  279. </view>
  280. </view>
  281. </view>
  282. </view>
  283. </scroll-view>
  284. </view>
  285. </u-popup>
  286. </view>
  287. </swiper-item>
  288. </swiper>
  289. </view>
  290. </template>
  291. <script>
  292. import Hls from 'hls.js';
  293. import CryptoJS from 'crypto-js'
  294. import {
  295. liveList
  296. } from '@/api/list'
  297. import {
  298. liveRed, // 点击领红包
  299. liveDataLike, // 点赞
  300. collectStore, // 店铺收藏/取消收藏
  301. collectGoods, // 商品收藏/取消收藏
  302. store, // 查询店铺
  303. // showGoods, //弹出商品卡片
  304. // follow, // 关注/取消关注
  305. watchUserList, //获取直播间用户(展示在线用户)
  306. liveMsg, //获取最近聊天记录
  307. // 小黄车
  308. liveStore, //店铺展示,
  309. // liveGoodsDetail, //商品详情,
  310. liveOrderUser, //正在购买
  311. getLiveInfo, //获取直播间信息接口
  312. getLiveViewData, //直播间点赞、关注、在线人数数据
  313. currentActivities //红包 卡片 抽奖
  314. } from '@/api/live'
  315. import {
  316. liveOrderList, // 订单列表
  317. } from '@/api/order'
  318. import parse from '/uni_modules/uview-plus/libs/config/props/parse';
  319. import {
  320. LiveWS
  321. } from '@/utils/liveWS.js'
  322. import {
  323. getlive,
  324. // gettextlist,
  325. getAnswerlist,
  326. submitAnswer
  327. } from '@/api/home'
  328. var wsUrl = "wss://live.test.ylrztop.com/ws/live-api/app/webSocket"; //余红奇
  329. // var wsUrl = "ws://192.168.10.166:7114/app/webSocket"; //余红奇
  330. // var wsUrl = "ws://192.168.10.125:7114/app/webSocket"; //涂小龙
  331. // var wsUrl = "ws://live.test.ylrztop.com/prod-api/app/webSocket"; //余红奇
  332. // var wsUrl = "ws://nd383294.natappfree.cc/app/webSocket"; //余红奇
  333. // var wsUrl = "ws://v56c9b8e.natappfree.cc/app/webSocket"; //余红奇
  334. // var wsUrl = "ws://192.168.10.170:7114/app/webSocket"; //陈果
  335. // var wsUrl = "ws://live.ylrzcloud.com/socket/app/webSocket";
  336. var pingpangTimes = null;
  337. var initTimes = null;
  338. var isSocketOpen = false;
  339. var socket = null;
  340. export default {
  341. data() {
  342. return {
  343. livePlayerInstances: {}, // 存储 live-player 组件实例(key: liveId, value: 实例)
  344. livePlayerStates: {}, // 存储每个直播间的直播状态(key: liveId, value: 状态码)
  345. liveRetryCounts: {}, // 直播重连次数(key: liveId, value: 次数)
  346. maxLiveRetry: 3, // 最大重连次数
  347. timer: null,
  348. isShowRed: false,
  349. // 新增:滑动节流相关变量
  350. lastSwiperChangeTime: 0, // 上次切换时间戳
  351. swiperChangeThrottle: 300, // 切换节流阈值(毫秒)
  352. lastClickTime: 0,
  353. clickDelay: 300, // 300ms内只响应一次点击
  354. connectingLiveId: null, // 记录正在连接的直播间ID
  355. swiperDuration: 0, // 初始化为0,首次加载无动画
  356. videoRetryCounts: {}, // 记录每个直播间的视频重试次数,格式: { liveId: 次数 }
  357. socketInstances: {}, // 改为对象存储多个socket实例
  358. currentSwiperIndex: 0, // 当前swiper索引
  359. list: [], // 直播间列表
  360. preloading: false, // 预加载状态
  361. liveUserTotal: null,
  362. viewPageSize: 10, // 每页数量
  363. viewPageNum: 1, // 当前页码
  364. viewLoading: false, // 是否正在加载
  365. viewNoMoreData: false, // 是否没有更多数据
  366. scrollHeight: 0,
  367. scrollTimer: null, // 滚动防抖定时器
  368. socketInstance: null, // 统一管理 socket 实例
  369. reconnectCount: 0,
  370. maxReconnectAttempts: 3,
  371. isManualClose: false, // 标记是否手动关闭
  372. showRed: true,
  373. // redInfo: {},
  374. // inputInfo: '',
  375. searchTimer: null,
  376. // storeId: null,
  377. reconnectTimer: null,
  378. isCollect: false,
  379. showPurchasePrompt: false,
  380. purchasePromptTimer: null,
  381. prevOrderCount: 0, // 用于记录上一次的购买人数
  382. videoUrl: null,
  383. showType: 1, //横屏1 竖屏2
  384. boxHeight: 300, //小黄车高度
  385. // isFollow: false,
  386. // liveViewData: {},
  387. liveViewers: [], //观众
  388. likeName: 0,
  389. hlsPlayer: null, // HLS播放器实例,
  390. livingUrl: "",
  391. products: {},
  392. store: {},
  393. orderUser: {}, //正在购买
  394. userType: 0,
  395. timestamp: '',
  396. liveId: null,
  397. // userId: uni.getStorageSync("userInfo.userId"),
  398. // isZoom: false, //点赞按钮控制是否放大
  399. userinfo: '', //用户信息
  400. // path: 'http://192.168.10.166/dev-api', //余红奇
  401. // path: 'http://v56c9b8e.natappfree.cc', //余红奇
  402. // path: 'live.test.ylrztop.com/prod-api', //余红奇
  403. // path: 'http://192.168.10.170/dev-api', //陈果
  404. // value: '',
  405. talkdisabled: false, //输入框是否禁用
  406. autoplay: false, //视频自动播放
  407. showadd: false,
  408. // talklist: [],
  409. scrollIntoView: '',
  410. videoContext: '',
  411. // thistime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
  412. // showWelcomeMessage: false,
  413. // isSubmit: false,
  414. // messageContent: "",
  415. // showziliao: false,
  416. // isScreen: true,
  417. // shopping: false, //小黄车弹窗
  418. // scrollTop: 0, //弹幕
  419. // old: {
  420. // scrollTop: 0
  421. // },
  422. livedata: {}, //直播间点赞、关注、在线人数数据
  423. };
  424. },
  425. onLoad(options) {
  426. this.initTime();
  427. if (options.liveId) {
  428. this.liveId = options.liveId; // 仅当 liveId 变化时更新
  429. }
  430. this.userinfo = JSON.parse(uni.getStorageSync("userInfo"))
  431. // 初始化直播间列表
  432. this.initLiveList(options);
  433. this.getliveUser(false); // 调用获取在线用户接口
  434. const platform = uni.getWindowInfo().platform;
  435. // 初始化直播间列表
  436. if (['mp-weixin', 'mp-alipay', 'mp-baidu', 'mp-toutiao'].includes(platform)) {
  437. // 确保 API 存在再调用
  438. if (uni.showShareMenu) {
  439. uni.showShareMenu({
  440. withShareTicket: true, // 可选参数,根据需求配置
  441. success: () => {
  442. console.log('分享菜单显示成功');
  443. },
  444. fail: (err) => {
  445. console.error('分享菜单显示失败:', err);
  446. }
  447. });
  448. } else {
  449. console.warn('当前平台不支持 uni.showShareMenu');
  450. }
  451. } else {
  452. // console.log('当前平台无需显示分享菜单');
  453. }
  454. this.getCurrentActivities(options.liveId);
  455. },
  456. onReady() {},
  457. onShareAppMessage() {
  458. return {
  459. // title: this.product.title,
  460. title: this.livedata.liveName,
  461. path: '/pages/home/living',
  462. imageUrl: this.products.image,
  463. success(res) {
  464. console.log("分享成功", res);
  465. },
  466. fail(err) {
  467. console.error("分享失败", err);
  468. }
  469. };
  470. },
  471. computed: {
  472. filteredViewers() {
  473. if (!this.liveViewers) {
  474. return
  475. } else {
  476. return this.liveViewers.slice(0, 3);
  477. }
  478. }
  479. },
  480. onHide() {
  481. const currentLive = this.list[this.currentSwiperIndex];
  482. if (currentLive) {
  483. // 暂停当前直播/回放
  484. if (currentLive.livingUrl) {
  485. this.pauseLive(currentLive);
  486. } else if (currentLive.videoUrl) {
  487. this.pauseVideo(currentLive);
  488. }
  489. this.clearTimeTimer(currentLive);
  490. }
  491. this.closeAllWebSockets();
  492. },
  493. onUnload() {
  494. // 1. 销毁所有 live-player 实例
  495. Object.values(this.livePlayerInstances).forEach(player => {
  496. try {
  497. player.stop(); // 停止直播
  498. } catch (e) {
  499. console.error('销毁 live-player 失败', e);
  500. }
  501. });
  502. this.livePlayerInstances = {};
  503. this.livePlayerStates = {};
  504. this.liveRetryCounts = {};
  505. // 2. 保留原有清理逻辑(WebSocket、定时器、HLS等)
  506. this.list.forEach(liveItem => {
  507. this.clearTimeTimer(liveItem);
  508. if (liveItem.videoUrl) {
  509. const videoContext = uni.createVideoContext(`myVideo_${liveItem.liveId}`, this);
  510. videoContext?.pause();
  511. }
  512. });
  513. this.closeAllWebSockets();
  514. this.removeAllEventListeners();
  515. this.clearAllTimers();
  516. if (this.hlsPlayer) {
  517. this.hlsPlayer.destroy();
  518. this.hlsPlayer = null;
  519. }
  520. },
  521. watch: {
  522. // 监听orderUser.count的变化
  523. 'orderUser.count': {
  524. handler(newVal, oldVal) {
  525. if (newVal !== this.prevOrderCount) {
  526. this.prevOrderCount = newVal;
  527. this.showPurchaseMessage();
  528. }
  529. },
  530. immediate: true
  531. }
  532. },
  533. methods: {
  534. // 1. 直播播放器状态变化回调(核心)
  535. onLivePlayerStateChange(e) {
  536. const liveId = this.getLiveIdFromPlayerId(e.currentTarget.id); // 从ID中解析liveId
  537. const state = e.detail.code; // 状态码:0-初始,1-连接中,2-播放中,3-暂停,4-播放结束,5-错误,6-缓冲中
  538. this.livePlayerStates[liveId] = state; // 记录当前状态
  539. const liveItem = this.list.find(item => item.liveId === liveId);
  540. if (!liveItem) return;
  541. // 根据状态码处理逻辑
  542. switch (state) {
  543. case 1: // 连接中
  544. console.log(`直播间${liveId}:正在连接直播流`);
  545. break;
  546. case 2: // 播放中
  547. console.log(`直播间${liveId}:直播已开始`);
  548. this.liveRetryCounts[liveId] = 0; // 播放成功,重置重连次数
  549. break;
  550. case 5: // 错误状态(需重连)
  551. console.error(`直播间${liveId}:直播播放错误,状态码=${state}`);
  552. this.handleLivePlayerError(liveItem); // 触发错误处理(重连)
  553. break;
  554. case 6: // 缓冲中
  555. uni.showToast({
  556. title: '直播缓冲中...',
  557. icon: 'none',
  558. duration: 1000
  559. });
  560. break;
  561. default:
  562. break;
  563. }
  564. },
  565. // 2. 直播错误回调(补充 statechange 未覆盖的错误场景)
  566. livePlayerError(e) {
  567. const liveId = this.getLiveIdFromPlayerId(e.currentTarget.id);
  568. const liveItem = this.list.find(item => item.liveId === liveId);
  569. if (liveItem) {
  570. this.handleLivePlayerError(liveItem);
  571. }
  572. },
  573. // 3. 直播错误统一处理(重连逻辑)
  574. handleLivePlayerError(liveItem) {
  575. const liveId = liveItem.liveId;
  576. // 初始化重连次数
  577. if (this.liveRetryCounts[liveId] === undefined) {
  578. this.liveRetryCounts[liveId] = 0;
  579. }
  580. // 超过最大重连次数,停止重试
  581. if (this.liveRetryCounts[liveId] >= this.maxLiveRetry) {
  582. uni.showToast({
  583. title: `直播连接失败,请检查网络(已重试${this.maxLiveRetry}次)`,
  584. icon: 'none',
  585. duration: 3000
  586. });
  587. return;
  588. }
  589. // 重连计数+1,延迟重试(指数退避:1s→2s→4s)
  590. this.liveRetryCounts[liveId]++;
  591. const delay = 1000 * Math.pow(2, this.liveRetryCounts[liveId] - 1);
  592. setTimeout(() => {
  593. console.log(`直播间${liveId}:第${this.liveRetryCounts[liveId]}次重连`);
  594. this.playLive(liveItem); // 触发重连播放
  595. }, delay);
  596. },
  597. // 4. 播放直播(获取 live-player 实例并控制播放)
  598. playLive(liveItem) {
  599. if (!liveItem || !liveItem.livingUrl) return;
  600. const playerId = `myLivePlayer_${liveItem.liveId}`;
  601. // 获取 live-player 实例(兼容小程序异步获取)
  602. const getPlayerInstance = () => {
  603. return new Promise((resolve) => {
  604. const player = uni.createLivePlayerContext(playerId, this);
  605. if (player) {
  606. this.livePlayerInstances[liveItem.liveId] = player; // 缓存实例
  607. resolve(player);
  608. } else {
  609. // 实例获取失败,100ms后重试(最多3次)
  610. let retry = 0;
  611. const retryGet = setInterval(() => {
  612. const retryPlayer = uni.createLivePlayerContext(playerId, this);
  613. if (retryPlayer || retry >= 3) {
  614. clearInterval(retryGet);
  615. resolve(retryPlayer);
  616. }
  617. retry++;
  618. }, 100);
  619. }
  620. });
  621. };
  622. // 控制播放
  623. getPlayerInstance().then(player => {
  624. if (player) {
  625. player.play({
  626. success: () => {
  627. console.log(`直播间${liveItem.liveId}:直播播放指令已发送`);
  628. },
  629. fail: (err) => {
  630. console.error(`直播间${liveItem.liveId}:播放指令失败`, err);
  631. }
  632. });
  633. }
  634. });
  635. },
  636. // 5. 暂停直播(切换swiper时调用)
  637. pauseLive(liveItem) {
  638. if (!liveItem) return;
  639. const player = this.livePlayerInstances[liveItem.liveId];
  640. if (player) {
  641. player.pause({
  642. success: () => {
  643. console.log(`直播间${liveItem.liveId}:直播已暂停`);
  644. },
  645. fail: (err) => {
  646. console.error(`直播间${liveItem.liveId}:暂停失败`, err);
  647. }
  648. });
  649. }
  650. },
  651. // 6. 从 playerId 中解析 liveId(辅助方法)
  652. getLiveIdFromPlayerId(playerId) {
  653. // playerId 格式:myLivePlayer_123456 → 提取 123456
  654. return playerId.replace('myLivePlayer_', '');
  655. },
  656. // 7. 全屏变化处理(可选,补全体验)
  657. onLiveFullScreenChange(e) {
  658. const isFullScreen = e.detail.fullScreen;
  659. const liveId = this.getLiveIdFromPlayerId(e.currentTarget.id);
  660. console.log(`直播间${liveId}:${isFullScreen ? '进入' : '退出'}全屏`);
  661. // 可根据全屏状态调整页面样式(如隐藏底部输入框)
  662. this.isLiveFullScreen = isFullScreen;
  663. },
  664. // 红包 卡片 抽奖
  665. getCurrentActivities(liveItem) {
  666. if (!liveItem.liveId) return;
  667. // const res = currentActivities(liveId);
  668. currentActivities(liveItem.liveId).then(res => {
  669. if (res.code == 200) {
  670. console.log("红包 卡片 抽奖res>>>", res)
  671. // 商品卡片
  672. this.$set(liveItem, 'goodsCard', res.goods);
  673. // 红包
  674. this.$set(liveItem, 'redInfo', res.red[0]);
  675. this.isShowRed = true
  676. // 抽奖
  677. this.$set(liveItem, 'lottery', res.lottery);
  678. } else {
  679. uni.showToast({
  680. title: res.msg,
  681. icon: 'none'
  682. });
  683. }
  684. },
  685. rej => {}
  686. );
  687. },
  688. // 计算当前时间与 liveItem.startTime 的差值,并更新 totalTime
  689. calculateTimeDiff(liveItem) {
  690. // 1. 校验 startTime 格式(防止无效时间)
  691. if (!liveItem.startTime) {
  692. liveItem.totalTime = '00小时00分钟00秒';
  693. return;
  694. }
  695. const iosCompatibleTime = liveItem.startTime
  696. .replace(/-/g, '/') // 把所有 "-" 替换为 "/"
  697. .replace(' ', ' '); // 保留空格(也可替换为 "T",即 "yyyy-MM-ddTHH:mm:ss")
  698. // 2. 转换时间格式(字符串转 Date 对象)
  699. const startTime = new Date(liveItem.startTime);
  700. const now = new Date();
  701. // 4. 容错处理:防止时间解析失败(极端情况)
  702. if (isNaN(startTime.getTime())) {
  703. console.warn(`iOS 时间解析失败,原始时间:${liveItem.startTime},转换后:${iosCompatibleTime}`);
  704. liveItem.totalTime = '00小时00分钟00秒';
  705. return;
  706. }
  707. // 3. 计算毫秒级差值(确保差值非负,避免未来时间出现负数)
  708. const diffMs = Math.max(0, now - startTime);
  709. // 4. 转换为 小时:分钟:秒(补零确保两位数)
  710. const totalSeconds = Math.floor(diffMs / 1000);
  711. const hours = this.padZero(Math.floor(totalSeconds / 3600));
  712. const minutes = this.padZero(Math.floor((totalSeconds % 3600) / 60));
  713. const seconds = this.padZero(totalSeconds % 60);
  714. // 5. 更新当前直播间的 totalTime(响应式更新)
  715. this.$set(liveItem, 'totalTime', `${hours}:${minutes}:${seconds}`);
  716. },
  717. // 辅助方法:数字补零(如 9 → 09)
  718. padZero(num) {
  719. return num < 10 ? `0${num}` : num;
  720. },
  721. // 启动当前直播间的时间差值定时器
  722. startTimeTimer(liveItem) {
  723. // 先清除当前直播间的旧定时器(避免重复)
  724. if (liveItem.timeTimer) {
  725. clearInterval(liveItem.timeTimer);
  726. }
  727. // 1. 立即计算一次(避免等待1秒才显示)
  728. this.calculateTimeDiff(liveItem);
  729. // 2. 每秒更新一次(实时刷新)
  730. liveItem.timeTimer = setInterval(() => {
  731. this.calculateTimeDiff(liveItem);
  732. }, 1000);
  733. },
  734. // 清除指定直播间的时间定时器
  735. clearTimeTimer(liveItem) {
  736. if (liveItem.timeTimer) {
  737. clearInterval(liveItem.timeTimer);
  738. liveItem.timeTimer = null; // 清空定时器标识
  739. }
  740. },
  741. toggleViewerList() {
  742. const now = Date.now()
  743. if (now - this.lastClickTime > this.clickDelay) {
  744. this.showadd = !this.showadd
  745. this.lastClickTime = now
  746. }
  747. },
  748. // 重置在线用户列表的分页参数
  749. resetUserListParams() {
  750. this.viewPageNum = 1; // 重置页码为第一页
  751. this.viewNoMoreData = false; // 重置“没有更多数据”状态
  752. this.liveViewers = []; // 清空现有列表
  753. },
  754. closeAllWebSockets() {
  755. this.isManualClose = true;
  756. // 关闭所有连接
  757. Object.keys(this.socketInstances).forEach(liveId => {
  758. try {
  759. if (this.socketInstances[liveId] && this.socketInstances[liveId].instance) {
  760. this.socketInstances[liveId].instance.close({
  761. code: 1000,
  762. reason: '页面关闭'
  763. });
  764. console.log(`直播间 ${liveId} WebSocket 已关闭`);
  765. }
  766. } catch (e) {
  767. console.error(`关闭直播间 ${liveId} WebSocket 时出错:`, e);
  768. }
  769. });
  770. this.socketInstances = {};
  771. isSocketOpen = false;
  772. }, // 移除所有全局事件监听器
  773. removeAllEventListeners() {
  774. uni.$off('initSocket');
  775. uni.$off('sendMsg');
  776. uni.$off('closeWebSocket');
  777. uni.$off('refreshOrder');
  778. // 可以根据实际情况添加其他需要移除的事件
  779. }, // 清理所有定时器
  780. clearAllTimers() {
  781. if (this.intervalId) {
  782. clearInterval(this.intervalId);
  783. this.intervalId = null;
  784. }
  785. if (this.pingpangTimes) {
  786. clearInterval(this.pingpangTimes);
  787. this.pingpangTimes = null;
  788. }
  789. if (this.reconnectTimer) {
  790. clearInterval(this.reconnectTimer);
  791. this.reconnectTimer = null;
  792. }
  793. if (this.scrollTimer) {
  794. clearTimeout(this.scrollTimer);
  795. this.scrollTimer = null;
  796. }
  797. if (this.searchTimer) {
  798. clearTimeout(this.searchTimer);
  799. this.searchTimer = null;
  800. }
  801. if (this.purchasePromptTimer) {
  802. clearTimeout(this.purchasePromptTimer);
  803. this.purchasePromptTimer = null;
  804. }
  805. },
  806. // 初始化当前直播间数据
  807. async initCurrentLiveData() {
  808. const currentLive = this.list[this.currentSwiperIndex];
  809. if (!currentLive) return;
  810. // 设置加载状态
  811. currentLive.loading = true;
  812. try {
  813. // 先初始化WebSocket连接
  814. await this.initSocket(currentLive);
  815. // 然后并行加载其他数据
  816. await Promise.allSettled([
  817. this.getLiveMsg(currentLive),
  818. this.getCurrentActivities(currentLive),
  819. this.getliveViewData(currentLive),
  820. // this.getShowGoods(currentLive),
  821. this.getliving(currentLive)
  822. ]);
  823. currentLive.loaded = true;
  824. // 初始化完成后,启动当前直播间的时间差值定时器
  825. this.startTimeTimer(currentLive);
  826. } catch (error) {
  827. console.error("初始化直播间数据失败:", error);
  828. } finally {
  829. currentLive.loading = false;
  830. }
  831. },
  832. // 初始化直播间列表
  833. async initLiveList(options) {
  834. try {
  835. await this.getList(); // 先加载直播间列表
  836. if (this.list.length === 0) {
  837. console.error("直播间列表为空,无法初始化");
  838. return;
  839. }
  840. // 设置初始直播间索引(原逻辑保留)
  841. if (options.liveId) {
  842. const index = this.list.findIndex(item => item.liveId == options.liveId);
  843. this.currentSwiperIndex = index !== -1 ? index : 0;
  844. } else {
  845. this.currentSwiperIndex = 0;
  846. }
  847. // 延迟设置动画时间(原逻辑保留)
  848. setTimeout(() => {
  849. this.swiperDuration = 300;
  850. }, 100);
  851. // 获取当前直播间的liveItem
  852. const currentLive = this.list[this.currentSwiperIndex];
  853. if (currentLive) {
  854. this.liveId = currentLive.liveId;
  855. // 重置用户列表参数
  856. this.resetUserListParams();
  857. // 预加载相邻直播间(不初始化WebSocket)
  858. this.preloadNearbyLives(false);
  859. // 初始化当前直播间数据(包含WebSocket连接)
  860. await this.initCurrentLiveData();
  861. // 获取在线用户
  862. this.getliveUser(false);
  863. // 延迟播放视频
  864. setTimeout(() => {
  865. this.playVideo(currentLive);
  866. }, 500);
  867. }
  868. } catch (error) {
  869. console.error("初始化失败:", error);
  870. }
  871. },
  872. // Swiper切换事件
  873. async onSwiperChange(e) {
  874. const now = Date.now();
  875. const newIndex = e.detail.current;
  876. const oldIndex = this.currentSwiperIndex;
  877. // 相同索引或节流期间,不处理
  878. if (newIndex === oldIndex || now - this.lastSwiperChangeTime < this.swiperChangeThrottle) {
  879. return;
  880. }
  881. this.lastSwiperChangeTime = now;
  882. // 1. 暂停旧直播间(直播用 pauseLive,回放用 pauseVideo)
  883. const oldLive = this.list[oldIndex];
  884. if (oldLive) {
  885. if (oldLive.livingUrl) {
  886. this.pauseLive(oldLive); // 暂停旧直播
  887. // 清理旧直播状态
  888. delete this.livePlayerInstances[oldLive.liveId];
  889. delete this.livePlayerStates[oldLive.liveId];
  890. delete this.liveRetryCounts[oldLive.liveId];
  891. } else if (oldLive.videoUrl) {
  892. this.pauseVideo(oldLive); // 暂停旧回放
  893. }
  894. this.clearTimeTimer(oldLive); // 清除旧直播间定时器
  895. // 关闭旧直播间WebSocket(原有逻辑保留)
  896. if (this.socketInstances[oldLive.liveId]) {
  897. this.closeWebSocket(oldLive.liveId);
  898. }
  899. }
  900. // 2. 处理新直播间
  901. this.currentSwiperIndex = newIndex;
  902. const newLive = this.list[newIndex];
  903. if (!newLive) return;
  904. this.liveId = newLive.liveId;
  905. this.resetUserListParams();
  906. await this.initCurrentLiveData(); // 初始化当前直播间数据
  907. this.getliveUser(false);
  908. // 3. 播放新直播间(直播用 playLive,回放用 playVideo)
  909. setTimeout(() => {
  910. if (newLive.livingUrl) {
  911. this.playLive(newLive); // 播放新直播
  912. } else if (newLive.videoUrl) {
  913. this.playVideo(newLive); // 播放新回放
  914. }
  915. }, 100);
  916. },
  917. // 获取直播间列表
  918. async getList() {
  919. const data = {
  920. page: 1,
  921. page_size: 20
  922. };
  923. try {
  924. const res = await liveList(data);
  925. if (res.code === 200) {
  926. this.list = res.rows.map(item => ({
  927. ...item,
  928. talklist: [],
  929. loading: false,
  930. loaded: false,
  931. shopping: false,
  932. inputInfo: '',
  933. showWelcomeMessage: false,
  934. redInfo: {},
  935. goodsCard: {},
  936. totalTime: '00小时00分钟00秒', // 初始化差值为0
  937. timeTimer: null, // 存储当前直播间的定时器,用于后续清理
  938. // 预处理:统一将 startTime 转换为 iOS 兼容格式(避免重复转换)
  939. startTime: item.startTime ? item.startTime.replace(/-/g, '/') : ''
  940. // 其他初始化字段...
  941. }));
  942. console.log("直播间列表加载成功,数量:", this.list.length); // 确认数量是否 > 0
  943. } else {
  944. uni.showToast({
  945. title: res.msg,
  946. icon: "none"
  947. });
  948. this.list = []; // 接口失败时清空列表,避免后续错误
  949. }
  950. } catch (error) {
  951. console.error("获取直播间列表失败:", error);
  952. this.list = []; // 异常时清空列表
  953. }
  954. },
  955. // 播放视频
  956. playVideo(liveItem) {
  957. if (!liveItem) return;
  958. try {
  959. const videoId = `myVideo_${liveItem.liveId}`;
  960. const videoContext = uni.createVideoContext(videoId, this);
  961. if (videoContext) {
  962. videoContext.pause(() => {
  963. // 暂停完成后再播放
  964. videoContext.play();
  965. });
  966. }
  967. } catch (error) {
  968. console.error("播放视频失败:", error);
  969. }
  970. },
  971. pauseVideo(liveItem) {
  972. if (!liveItem) return;
  973. try {
  974. const videoId = `myVideo_${liveItem.liveId}`;
  975. const videoContext = uni.createVideoContext(videoId, this);
  976. if (videoContext) {
  977. videoContext.pause();
  978. }
  979. } catch (error) {
  980. console.error("暂停视频失败:", error);
  981. }
  982. },
  983. // 预加载相邻直播间
  984. async preloadNearbyLives(includeWebSocket = false) {
  985. this.preloading = true;
  986. const currentIndex = this.currentSwiperIndex;
  987. const preloadIndexes = [];
  988. // 只预加载数据,不初始化WebSocket(强制关闭WebSocket预加载)
  989. includeWebSocket = false; // 关键:强制不预加载WebSocket
  990. if (currentIndex > 0) preloadIndexes.push(currentIndex - 1);
  991. if (currentIndex < this.list.length - 1) preloadIndexes.push(currentIndex + 1);
  992. for (const index of preloadIndexes) {
  993. const liveItem = this.list[index];
  994. if (!liveItem || liveItem.loaded || liveItem.loading) continue;
  995. liveItem.loading = true;
  996. try {
  997. // 只加载基础数据,不初始化WebSocket
  998. await Promise.all([
  999. this.getliving(liveItem),
  1000. this.getLiveMsg(liveItem),
  1001. this.getCurrentActivities(liveItem),
  1002. this.getliveViewData(liveItem),
  1003. // this.getShowGoods(liveItem)
  1004. ]);
  1005. liveItem.loaded = true;
  1006. } catch (error) {
  1007. console.error(`预加载直播间 ${index} 失败:`, error);
  1008. liveItem.loaded = false;
  1009. } finally {
  1010. liveItem.loading = false;
  1011. }
  1012. }
  1013. this.preloading = false;
  1014. },
  1015. openViews() {
  1016. // 计算scroll-view高度
  1017. this.$nextTick(() => {
  1018. const query = uni.createSelectorQuery().in(this);
  1019. query.select('.view-box').boundingClientRect(data => {
  1020. if (data) {
  1021. // 减去标题和底部固定区域的高度
  1022. this.scrollHeight = data.height - 80 - 120; // 80是标题高度,120是底部高度
  1023. }
  1024. }).exec();
  1025. });
  1026. },
  1027. // 重置直播间数据
  1028. resetLiveData() {
  1029. // 保留不需要重置的数据(如用户信息等)
  1030. const keepData = {
  1031. userinfo: this.userinfo,
  1032. list: this.list,
  1033. currentSwiperIndex: this.currentSwiperIndex
  1034. };
  1035. // 重置其他直播间特定数据
  1036. this.liveUserTotal = null;
  1037. // this.liveViewData = {};
  1038. this.liveViewers = [];
  1039. this.livingUrl = "";
  1040. this.videoUrl = null;
  1041. this.products = {};
  1042. this.store = {};
  1043. this.orderUser = {};
  1044. this.talklist = [];
  1045. this.livedata = {};
  1046. // this.isFollow = false;
  1047. this.shopping = false;
  1048. this.inputInfo = ''
  1049. // 恢复需要保留的数据
  1050. Object.assign(this, keepData);
  1051. },
  1052. // 获取直播间用户
  1053. async getliveUser(isLoadMore = false) {
  1054. // 强制使用当前 liveId,避免使用旧值
  1055. const currentLiveId = this.liveId;
  1056. if (!currentLiveId) return;
  1057. // if (this.viewLoading || this.viewNoMoreData) return;
  1058. this.viewLoading = true;
  1059. try {
  1060. const res = await watchUserList(currentLiveId, this.viewPageSize, this.viewPageNum, false);
  1061. if (res.code === 200) {
  1062. // 若请求期间已切换直播间,忽略旧数据
  1063. if (this.liveId !== currentLiveId) return;
  1064. this.liveUserTotal = res.total;
  1065. if (res.rows.length === 0) {
  1066. this.viewNoMoreData = true;
  1067. return;
  1068. }
  1069. this.liveViewers = isLoadMore ? [...this.liveViewers, ...res.rows] : res
  1070. .rows; // 首次加载/切换直播间时重置数据
  1071. this.viewPageNum++;
  1072. // console.log("在线观众", this.liveViewers)
  1073. }
  1074. } catch (error) {
  1075. console.error('获取观众列表失败:', error);
  1076. } finally {
  1077. this.viewLoading = false;
  1078. }
  1079. },
  1080. // 滚动到底部触发
  1081. handleScrollToLower() {
  1082. // 清除上一次未执行的定时器,避免重复请求
  1083. if (this.scrollTimer) {
  1084. clearTimeout(this.scrollTimer);
  1085. }
  1086. // 延迟0.5秒(500毫秒)执行接口请求
  1087. this.scrollTimer = setTimeout(() => {
  1088. this.getliveUser(true); // 加载更多数据
  1089. }, 1000); // 延迟时间:500毫秒
  1090. },
  1091. async getLiveMsg(liveItem) {
  1092. // 强化校验:确保 liveItem 存在且包含 liveId
  1093. if (!liveItem || !liveItem.liveId) {
  1094. console.error('getLiveMsg 错误:无效的 liveItem', {
  1095. liveItem: liveItem,
  1096. currentIndex: this.currentSwiperIndex,
  1097. listLength: this.list.length
  1098. });
  1099. return; // 直接返回,不执行后续逻辑
  1100. }
  1101. try {
  1102. const res = await liveMsg(liveItem.liveId, 30, 1);
  1103. if (res.code == 200) {
  1104. // 确保使用响应式更新
  1105. const reversedTalkList = [...res.rows].reverse();
  1106. // 确保使用响应式更新(赋值反转后的数组)
  1107. this.$set(liveItem, 'talklist', reversedTalkList);
  1108. this.$nextTick(() => {
  1109. // 滚动到最新消息(反转后最新消息在数组最后一位,索引为 reversedTalkList.length - 1)
  1110. this.$set(liveItem, 'scrollIntoView', `list_${reversedTalkList.length - 1}`);
  1111. });
  1112. }
  1113. } catch (error) {
  1114. console.error("获取聊天记录失败:", error);
  1115. }
  1116. },
  1117. // 点击红包
  1118. onRed(item) {
  1119. // console.log("点了this.liveId", item)
  1120. if (!item.liveId) return;
  1121. if (!item.redInfo?.redId) return;
  1122. let data = {
  1123. liveId: item.liveId,
  1124. userId: this.userinfo.userId,
  1125. redId: item.redInfo.redId,
  1126. }
  1127. liveRed(data).then(res => {
  1128. this.isShowRed = false
  1129. if (res.code == 200) {
  1130. console.log(this.isShowRed)
  1131. console.log("点了this.liveId", liveItem.redInfo)
  1132. uni.showToast({
  1133. title: res.msg,
  1134. icon: 'none'
  1135. });
  1136. } else {
  1137. uni.showToast({
  1138. title: res.msg,
  1139. icon: 'none'
  1140. });
  1141. }
  1142. },
  1143. rej => {}
  1144. );
  1145. },
  1146. handleSearchInput() {
  1147. // 使用防抖优化性能,避免频繁请求
  1148. clearTimeout(this.searchTimer);
  1149. this.searchTimer = setTimeout(() => {
  1150. this.queryCollect();
  1151. }, 500); // 500毫秒延迟
  1152. },
  1153. // 显示购买提示信息
  1154. showPurchaseMessage() {
  1155. // 清除之前的定时器
  1156. if (this.purchasePromptTimer) {
  1157. clearTimeout(this.purchasePromptTimer);
  1158. }
  1159. // 显示提示
  1160. this.showPurchasePrompt = true;
  1161. // 2秒后自动隐藏
  1162. this.purchasePromptTimer = setTimeout(() => {
  1163. this.showPurchasePrompt = false;
  1164. }, 2000);
  1165. },
  1166. truncateString(str, maxLength) {
  1167. return str.length > maxLength ? str.slice(0, maxLength) + '...' : str;
  1168. },
  1169. goStore(item) {
  1170. console.log("带过去storeId", item)
  1171. if (!item.storeId) {
  1172. uni.showToast({
  1173. title: "当前直播间暂未开通店铺",
  1174. icon: 'none'
  1175. });
  1176. return;
  1177. }
  1178. uni.navigateTo({
  1179. url: '/pages_shop/store?liveId=' + item.liveId + "&storeId=" + item.storeId
  1180. })
  1181. },
  1182. // 去订单列表
  1183. goOrderList() {
  1184. uni.navigateTo({
  1185. url: "/pages_shop/order"
  1186. })
  1187. },
  1188. // 初始化HLS播放器
  1189. initHlsPlayer() {
  1190. if (Hls.isSupported() && this.livingUrl) {
  1191. const video = document.getElementById('myVideo');
  1192. if (video) {
  1193. this.hlsPlayer = new Hls();
  1194. this.hlsPlayer.loadSource(this.livingUrl);
  1195. this.hlsPlayer.attachMedia(video);
  1196. this.hlsPlayer.on(Hls.Events.MANIFEST_PARSED, () => {
  1197. video.play();
  1198. });
  1199. }
  1200. }
  1201. },
  1202. // 视频错误处理
  1203. videoError(e, liveItem) {
  1204. if (!liveItem || liveItem.liveId !== this.liveId) return;
  1205. // 初始化重试计数
  1206. if (this.videoRetryCounts[liveItem.liveId] === undefined) {
  1207. this.videoRetryCounts[liveItem.liveId] = 0;
  1208. }
  1209. // 限制重试次数
  1210. if (this.videoRetryCounts[liveItem.liveId] >= 3) {
  1211. console.error(`直播间 ${liveItem.liveId} 视频加载失败,停止重试`);
  1212. // 显示错误提示
  1213. uni.showToast({
  1214. title: "视频加载失败,请检查网络",
  1215. icon: 'none',
  1216. duration: 2000
  1217. });
  1218. return;
  1219. }
  1220. this.videoRetryCounts[liveItem.liveId]++;
  1221. // 延迟重试
  1222. setTimeout(() => {
  1223. if (liveItem.liveId === this.liveId) {
  1224. console.log(`第${this.videoRetryCounts[liveItem.liveId]}次重试播放视频`);
  1225. this.playVideo(liveItem);
  1226. }
  1227. }, 2000);
  1228. },
  1229. // 修改获取直播信息方法
  1230. async getliving(liveItem) {
  1231. if (!liveItem || !liveItem.liveId) return;
  1232. // console.log("获取直播信息:", liveItem.liveId);
  1233. const param = {
  1234. id: liveItem.liveId
  1235. };
  1236. try {
  1237. const res = await getlive({
  1238. id: liveItem.liveId
  1239. });
  1240. if (res.code !== 200) {
  1241. uni.showToast({
  1242. title: res.msg,
  1243. icon: 'none'
  1244. });
  1245. return;
  1246. }
  1247. // 强制更新视频源(覆盖旧值)
  1248. if (res.data.liveType === 2) {
  1249. // 回放视频
  1250. this.$set(liveItem, 'videoUrl', res.data.videoUrl);
  1251. this.$set(liveItem, 'livingUrl', ''); // 清空直播流
  1252. this.$set(liveItem, 'placeholderText', "说点什么...");
  1253. } else if (res.data.liveType === 1) {
  1254. // 直播流
  1255. this.$set(liveItem, 'livingUrl', res.data.flvHlsUrl);
  1256. this.$set(liveItem, 'videoUrl', ''); // 清空回放视频
  1257. this.$set(liveItem, 'placeholderText', "说点什么...");
  1258. } else {
  1259. // 未开播
  1260. this.$set(liveItem, 'livingUrl', '');
  1261. this.$set(liveItem, 'videoUrl', '');
  1262. }
  1263. // 更新其他直播信息
  1264. // 仅当是当前激活的直播间时,才允许自动播放
  1265. const isCurrentLive = this.currentSwiperIndex === this.list.findIndex(item => item.liveId ===
  1266. liveItem.liveId);
  1267. this.$set(liveItem, 'autoplay', isCurrentLive && res.data.liveType !== 0);
  1268. this.$set(liveItem, 'showType', res.data.showType);
  1269. this.$set(liveItem, 'storeId', res.storeId);
  1270. this.queryCollect();
  1271. } catch (err) {
  1272. console.error("获取直播信息失败:", err);
  1273. uni.showToast({
  1274. title: "获取直播信息失败",
  1275. icon: 'none'
  1276. });
  1277. }
  1278. }, // 设置视频播放
  1279. setupVideoPlayback(liveItem, liveData) {
  1280. if (!liveItem.livingUrl) return;
  1281. // H5 平台:使用 HLS.js 处理 .m3u8 流
  1282. // #ifdef H5
  1283. if (liveItem.livingUrl.includes('.m3u8')) {
  1284. this.initHlsPlayer(liveItem);
  1285. return;
  1286. }
  1287. // #endif
  1288. // 小程序/App 平台:直接使用 video 组件
  1289. const videoId = `myVideo_${liveItem.liveId}`;
  1290. const videoContext = uni.createVideoContext(videoId, this);
  1291. if (liveData.liveType === 1) {
  1292. // 回放:跳转到指定位置
  1293. videoContext.seek(liveData.nowDuration || 0);
  1294. } else {
  1295. // 直播:直接播放
  1296. videoContext.play();
  1297. }
  1298. },
  1299. maskString(str, maskChar = '*') {
  1300. // 如果str是undefined或null,直接返回空字符串
  1301. if (!str) return '';
  1302. // 确保str是字符串(如果是数字等类型,先转为字符串)
  1303. const strVal = String(str);
  1304. return strVal.split('').map((char, index) => (index === 0 ? char : maskChar)).join('');
  1305. },
  1306. getPureDecimal(num, precision = 6) {
  1307. const decimalPart = Math.abs(num).toFixed(precision).split('.')[1];
  1308. return decimalPart?.replace(/0+$/, '') || ''; // 移除末尾多余的0
  1309. },
  1310. // 返回上一个页面并关闭WebSocket
  1311. goBack() {
  1312. // 暂停当前视频
  1313. const currentLive = this.list[this.currentSwiperIndex];
  1314. if (currentLive) {
  1315. this.pauseVideo(currentLive);
  1316. }
  1317. // 关闭所有WebSocket连接
  1318. this.closeWebSocket();
  1319. // 导航返回
  1320. const pages = getCurrentPages();
  1321. if (pages.length > 1) {
  1322. uni.navigateBack();
  1323. } else {
  1324. uni.reLaunch({
  1325. url: '/pages/list/index'
  1326. });
  1327. }
  1328. },
  1329. //正在购买
  1330. async getliveOrder() {
  1331. const currentLiveId = this.liveId;
  1332. if (!currentLiveId) return;
  1333. try {
  1334. const res = await liveOrderUser(currentLiveId);
  1335. if (res.code === 200) {
  1336. // 绑定到当前 liveItem
  1337. const currentLive = this.list[this.currentSwiperIndex];
  1338. if (currentLive) {
  1339. this.$set(currentLive, 'orderUser', res);
  1340. this.orderUser = res; // 同步全局变量
  1341. }
  1342. }
  1343. } catch (error) {
  1344. console.error('获取正在购买用户失败:', error);
  1345. }
  1346. },
  1347. // 获取直播间信息接口
  1348. getLiveinformation() {
  1349. if (!this.liveId) return;
  1350. getLiveInfo(this.liveId).then(res => {
  1351. if (res.code == 200) {
  1352. console.log("获取直播间信息接口>>>>", res)
  1353. this.livingUrl = res.livingUrl
  1354. } else {
  1355. uni.showToast({
  1356. title: res.msg,
  1357. icon: 'none'
  1358. });
  1359. }
  1360. },
  1361. rej => {}
  1362. );
  1363. },
  1364. // 点赞
  1365. async onLike(liveItem) {
  1366. if (!liveItem || !liveItem.liveId) return;
  1367. try {
  1368. const res = await liveDataLike(liveItem.liveId);
  1369. if (res?.like) {
  1370. liveItem.liveViewData.like++; // 只更新当前直播间的点赞数
  1371. } else {
  1372. uni.showToast({
  1373. title: res.msg,
  1374. icon: 'none'
  1375. });
  1376. }
  1377. } catch (error) {
  1378. console.error('点赞失败:', error);
  1379. }
  1380. },
  1381. //直播间点赞、关注、在线人数数据
  1382. async getliveViewData(liveItem) {
  1383. if (!liveItem || !liveItem.liveId) return;
  1384. try {
  1385. const res = await getLiveViewData(liveItem.liveId);
  1386. if (res.code == 200) {
  1387. // 强制响应式更新,确保数据实时显示
  1388. this.$set(liveItem, 'liveViewData', res);
  1389. }
  1390. } catch (error) {
  1391. console.error("获取直播间数据失败:", error);
  1392. // 失败时兜底,避免显示异常
  1393. this.$set(liveItem, 'liveViewData', {
  1394. like: 0,
  1395. watchCount: 0
  1396. });
  1397. }
  1398. },
  1399. //弹出商品卡片
  1400. // async getShowGoods(liveItem) {
  1401. // if (!liveItem || !liveItem.liveId) return;
  1402. // try {
  1403. // const res = await showGoods(liveItem.liveId);
  1404. // if (res.code == 200) {
  1405. // this.$set(liveItem, 'goodsCard', res);
  1406. // }
  1407. // } catch (error) {
  1408. // console.error("暂无商品卡片", error);
  1409. // }
  1410. // },
  1411. // 去购买,跳商品详情
  1412. goShop(productId, goodsId) {
  1413. const currentLive = this.list[this.currentSwiperIndex];
  1414. if (!currentLive || !currentLive.liveId) return;
  1415. uni.navigateTo({
  1416. url: '/pages_shop/goods?productId=' + productId + '&liveId=' + currentLive.liveId +
  1417. '&goodsId=' + goodsId + '&storeId=' + currentLive.storeId
  1418. })
  1419. },
  1420. // 查询店铺
  1421. async queryCollect() {
  1422. const currentLive = this.list[this.currentSwiperIndex];
  1423. if (!currentLive || !currentLive.liveId) return;
  1424. const {
  1425. storeId
  1426. } = currentLive;
  1427. // console.log("小黄车数据>>>", currentLive)
  1428. if (!storeId) return;
  1429. try {
  1430. const res = await store(currentLive.storeId, currentLive.inputInfo, currentLive.liveId);
  1431. if (res.code === 200) {
  1432. // 数据绑定到当前 liveItem,避免全局污染
  1433. this.$set(currentLive, 'products', res.data.goodsList);
  1434. this.$set(currentLive, 'store', res.data);
  1435. // 同步更新全局变量(供弹窗使用)
  1436. this.products = res.data.goodsList;
  1437. this.store = res.data;
  1438. }
  1439. } catch (error) {
  1440. console.error('获取小黄车商品失败:', error);
  1441. }
  1442. },
  1443. // 店铺收藏
  1444. onStoreCollect(liveItem) {
  1445. console.log("店铺收藏", liveItem)
  1446. if (!liveItem.storeId) return;
  1447. collectStore(liveItem.storeId).then(res => {
  1448. if (res.code == 200) {
  1449. uni.showToast({
  1450. title: res.msg,
  1451. icon: 'none'
  1452. });
  1453. liveItem.store.isFavorite = !liveItem.store.isFavorite
  1454. } else {
  1455. uni.showToast({
  1456. title: res.msg,
  1457. icon: 'none'
  1458. });
  1459. }
  1460. },
  1461. rej => {}
  1462. );
  1463. },
  1464. // 商品收藏
  1465. onGoodsCollect(item) {
  1466. console.log("商品收藏", item)
  1467. if (!item || item.length === 0 || !item.goodsId) {
  1468. return;
  1469. }
  1470. collectGoods(item.goodsId).then(res => {
  1471. if (res.code == 200) {
  1472. uni.showToast({
  1473. title: res.msg,
  1474. icon: 'none'
  1475. });
  1476. item.isFavorite = !item.isFavorite
  1477. } else {
  1478. uni.showToast({
  1479. title: res.msg,
  1480. icon: 'none'
  1481. });
  1482. }
  1483. },
  1484. rej => {}
  1485. );
  1486. },
  1487. // 关注
  1488. // async onFollow(liveItem) {
  1489. // if (!liveItem || !liveItem.liveId) return;
  1490. // try {
  1491. // const res = await follow(liveItem.liveId);
  1492. // liveItem.isFollow = !liveItem.isFollow;
  1493. // uni.showToast({
  1494. // title: res.msg,
  1495. // icon: 'none'
  1496. // });
  1497. // } catch (error) {
  1498. // console.error("关注操作失败:", error);
  1499. // }
  1500. // },
  1501. // 时间戳
  1502. initTime() {
  1503. const now = new Date();
  1504. this.timestamp = now.getTime(); // 例如:'2023-04-01 12:00:00'
  1505. },
  1506. openShop() {
  1507. },
  1508. openViews() {
  1509. this.$nextTick(() => {
  1510. const query = uni.createSelectorQuery().in(this);
  1511. query.select('.view-box').boundingClientRect(data => {
  1512. if (data) {
  1513. this.scrollHeight = data.height - 80 - 120;
  1514. }
  1515. }).exec();
  1516. });
  1517. },
  1518. openCart(liveItem) {
  1519. if (!liveItem.storeId) {
  1520. uni.showToast({
  1521. title: "当前直播间暂未开通店铺",
  1522. icon: 'none'
  1523. });
  1524. return;
  1525. }
  1526. liveItem.shopping = true
  1527. },
  1528. close() {
  1529. this.showadd = false
  1530. },
  1531. // 关闭小黄车
  1532. closeShop() {
  1533. // 通过当前swiper索引获取当前直播间实例
  1534. const currentLive = this.list[this.currentSwiperIndex];
  1535. if (currentLive) {
  1536. this.$set(currentLive, 'shopping', false);
  1537. }
  1538. },
  1539. // 关闭红包
  1540. closeRed() {
  1541. this.showRed = !this.showRed
  1542. },
  1543. getEWechatSdk() {
  1544. // 只在H5平台执行
  1545. // #ifdef H5
  1546. let eWechatSdk = '';
  1547. if (/(Android)/i.test(navigator.userAgent)) {
  1548. eWechatSdk = 'jWeixin';
  1549. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  1550. eWechatSdk = 'wx';
  1551. } else {
  1552. eWechatSdk = 'jWeixin';
  1553. }
  1554. uni.setStorageSync("wxSdk", eWechatSdk);
  1555. // #endif
  1556. },
  1557. // 修改关闭WebSocket方法
  1558. closeWebSocket(liveId = null) {
  1559. this.isManualClose = true;
  1560. clearInterval(this.pingpangTimes);
  1561. clearInterval(this.reconnectTimer);
  1562. if (liveId) {
  1563. // 添加空值检查
  1564. if (this.socketInstances[liveId] && this.socketInstances[liveId].instance) {
  1565. try {
  1566. this.socketInstances[liveId].instance.close();
  1567. console.log(`直播间 ${liveId} WebSocket 已安全关闭`);
  1568. } catch (e) {
  1569. console.error(`关闭直播间 ${liveId} WebSocket 时出错:`, e);
  1570. }
  1571. }
  1572. delete this.socketInstances[liveId];
  1573. } else {
  1574. Object.keys(this.socketInstances).forEach(id => {
  1575. // 添加空值检查
  1576. if (this.socketInstances[id] && this.socketInstances[id].instance) {
  1577. try {
  1578. this.socketInstances[id].instance.close();
  1579. console.log(`直播间 ${id} WebSocket 已安全关闭`);
  1580. } catch (e) {
  1581. console.error(`关闭直播间 ${id} WebSocket 时出错:`, e);
  1582. }
  1583. }
  1584. });
  1585. this.socketInstances = {};
  1586. }
  1587. isSocketOpen = false;
  1588. },
  1589. reConnect() {
  1590. var that = this;
  1591. try {
  1592. uni.closeSocket();
  1593. } catch (e) {
  1594. }
  1595. setTimeout(function() {
  1596. that.initSocket();
  1597. }, 10000);
  1598. },
  1599. startHeartbeat(liveId) {
  1600. clearInterval(this.pingpangTimes);
  1601. this.pingpangTimes = setInterval(() => {
  1602. const inst = this.socketInstances[liveId];
  1603. if (!inst || !inst.isOpen || !inst.instance) return;
  1604. const pingData = {
  1605. cmd: 'ping',
  1606. timestamp: Date.now()
  1607. };
  1608. try {
  1609. inst.instance.send({
  1610. data: JSON.stringify(pingData)
  1611. });
  1612. } catch (err) {
  1613. console.error('心跳发送失败:', err);
  1614. this.scheduleReconnect(liveId);
  1615. }
  1616. }, 30000);
  1617. },
  1618. async initSocket(liveItem) {
  1619. const currentLive = this.list[this.currentSwiperIndex];
  1620. if (!currentLive || currentLive.liveId !== liveItem.liveId) {
  1621. console.log(`拒绝为非活跃直播间 ${liveItem.liveId} 建立WebSocket`);
  1622. return;
  1623. }
  1624. if (!liveItem || !liveItem.liveId) return;
  1625. const liveId = liveItem.liveId;
  1626. // 检查是否已经是当前连接或正在连接
  1627. if (this.connectingLiveId === liveId ||
  1628. (this.socketInstances[liveId] && this.socketInstances[liveId].isOpen)) {
  1629. console.log(`直播间 ${liveId} 的WebSocket已经连接或正在连接,跳过初始化`);
  1630. return;
  1631. }
  1632. // 检查是否是当前活跃的直播间
  1633. if (this.currentSwiperIndex !== this.list.findIndex(item => item.liveId === liveId)) {
  1634. console.log(`不初始化非当前直播间 ${liveId} 的WebSocket`);
  1635. return;
  1636. }
  1637. // 关闭之前的连接(如果存在)
  1638. if (this.socketInstances[liveId]) {
  1639. this.closeWebSocket(liveId);
  1640. }
  1641. // 设置正在连接的直播间ID
  1642. this.connectingLiveId = liveId;
  1643. // 防止重复创建
  1644. if (this.reconnectCount >= this.maxReconnectAttempts) {
  1645. this.connectingLiveId = null;
  1646. return;
  1647. }
  1648. this.isManualClose = false;
  1649. const connectTimeout = setTimeout(() => {
  1650. if (!this.socketInstances[liveId]?.isOpen) {
  1651. console.error(`WebSocket连接超时: ${liveId}`);
  1652. this.scheduleReconnect(liveId);
  1653. }
  1654. }, 10000); // 10秒超时
  1655. // 生成签名
  1656. const signature = CryptoJS.HmacSHA256(
  1657. `${liveId}${this.userinfo.userId}${this.userType}${this.timestamp}`,
  1658. this.timestamp.toString()
  1659. ).toString(CryptoJS.enc.Hex);
  1660. // 初始化实例状态
  1661. this.socketInstances[liveId] = {
  1662. instance: null,
  1663. isOpen: false
  1664. };
  1665. try {
  1666. const socketTask = uni.connectSocket({
  1667. url: `${wsUrl}?userId=${this.userinfo.userId}&liveId=${liveId}&userType=${this.userType}&timestamp=${this.timestamp}&signature=${signature}`,
  1668. success: () => {
  1669. console.log('WebSocket API 调用成功,正在建立连接...');
  1670. },
  1671. fail: (err) => {
  1672. console.error('WebSocket 连接失败:', err);
  1673. this.connectingLiveId = null;
  1674. this.scheduleReconnect(liveId);
  1675. }
  1676. });
  1677. socketTask.onOpen((res) => {
  1678. console.log('WebSocket 连接已成功建立', res);
  1679. this.socketInstances[liveId].instance = socketTask;
  1680. this.socketInstances[liveId].isOpen = true;
  1681. clearTimeout(connectTimeout);
  1682. isSocketOpen = true;
  1683. this.connectingLiveId = null;
  1684. this.reconnectCount = 0;
  1685. // 改用 socketTask 的 send 作为心跳
  1686. this.startHeartbeat(liveId); // 修改 startHeartbeat 以接受 liveId 并使用 socketTask.send
  1687. });
  1688. socketTask.onMessage((res) => {
  1689. // 找到对应的直播间并更新数据
  1690. const targetLive = this.list.find(item => item.liveId === liveId);
  1691. if (targetLive) {
  1692. this.handleSocketMessage(res, targetLive);
  1693. }
  1694. });
  1695. socketTask.onError((err) => {
  1696. clearTimeout(connectTimeout);
  1697. console.error('WebSocket 连接错误:', err);
  1698. this.connectingLiveId = null;
  1699. this.scheduleReconnect(liveId);
  1700. });
  1701. socketTask.onClose(() => {
  1702. console.log(`WebSocket 连接已关闭: ${liveId}`);
  1703. this.connectingLiveId = null;
  1704. });
  1705. } catch (e) {
  1706. console.error('创建 WebSocket 异常:', e);
  1707. this.connectingLiveId = null;
  1708. this.scheduleReconnect(liveId);
  1709. }
  1710. },
  1711. // 处理Socket消息
  1712. handleSocketMessage(message, liveItem) {
  1713. let data = JSON.parse(message.data)
  1714. const socketMessage = data.data // 服务端返回的消息体
  1715. if (data.code == 200) {
  1716. const messageData = {
  1717. ...socketMessage,
  1718. cmd: socketMessage.cmd || '', // 确保cmd字段存在
  1719. ts: Date.now() // 新增时间戳,用于排序(可选)
  1720. };
  1721. // 【关键修改2】处理服务端返回的sendMsg消息,加入本地列表
  1722. if (socketMessage.cmd == 'sendMsg') {
  1723. // 1. 将消息追加到当前直播间的talklist中(响应式更新)
  1724. this.$set(liveItem, 'talklist', [...(liveItem.talklist || []), messageData]);
  1725. // 2. 延迟下一帧滚动到最新消息(确保DOM已更新)
  1726. this.$nextTick(() => {
  1727. const lastIndex = liveItem.talklist.length - 1;
  1728. this.$set(liveItem, 'scrollIntoView', `list_${lastIndex}`);
  1729. });
  1730. }
  1731. // 以下是原有其他cmd的处理逻辑,保持不变
  1732. else if (socketMessage.cmd == 'red') {
  1733. this.$set(liveItem, 'redInfo', JSON.parse(socketMessage.data));
  1734. this.isShowRed = true
  1735. this.timer = setInterval(() => {
  1736. this.isShowRed = true
  1737. }, liveItem.redInfo.duration * 1000)
  1738. } else if (socketMessage.cmd == 'entry' || socketMessage.cmd == 'out') {
  1739. this.$set(liveItem, 'talklist', [...liveItem.talklist, messageData]);
  1740. this.$set(liveItem, 'showWelcomeMessage', false);
  1741. this.$set(liveItem, 'showWelcomeMessage', true);
  1742. this.getliveUser(false);
  1743. if (liveItem.welcomeTimer) clearTimeout(liveItem.welcomeTimer);
  1744. liveItem.welcomeTimer = setTimeout(() => {
  1745. this.$set(liveItem, 'showWelcomeMessage', false);
  1746. }, 1000);
  1747. } else if (socketMessage.cmd == 'Integral') {
  1748. let integral = {
  1749. msg: socketMessage.msg,
  1750. status: true
  1751. }
  1752. this.$set(liveItem, 'integral', integral);
  1753. } else if (socketMessage.cmd == 'blockUser') {
  1754. uni.removeStorage({
  1755. key: 'AppToken',
  1756. success: () => {
  1757. uni.reLaunch({
  1758. url: '/pages/auth/login'
  1759. });
  1760. }
  1761. });
  1762. } else if (socketMessage.cmd == 'goods') {
  1763. this.$set(liveItem, 'goodsCard', JSON.parse(socketMessage.data));
  1764. }
  1765. } else {
  1766. uni.showToast({
  1767. title: data.msg,
  1768. icon: 'none'
  1769. });
  1770. }
  1771. },
  1772. scheduleReconnect(liveId) {
  1773. if (this.isManualClose || this.reconnectCount >= this.maxReconnectAttempts) return;
  1774. this.reconnectCount++;
  1775. const delay = Math.min(3000 * this.reconnectCount, 30000);
  1776. clearInterval(this.reconnectTimer);
  1777. this.reconnectTimer = setTimeout(() => {
  1778. // 使用传入的liveId
  1779. const liveItem = this.list.find(item => item.liveId === liveId) || {
  1780. liveId
  1781. };
  1782. this.initSocket(liveItem);
  1783. }, delay);
  1784. },
  1785. sendMsg(liveItem) {
  1786. if (!liveItem || !liveItem.liveId) return;
  1787. // 防止连续点击发送两次(短时锁定)
  1788. if (liveItem.isSending) return;
  1789. liveItem.isSending = true;
  1790. setTimeout(() => {
  1791. liveItem.isSending = false;
  1792. }, 800); // 800ms 内禁止再次发送
  1793. const text = (liveItem.value || '').trim();
  1794. if (!text) {
  1795. uni.showToast({
  1796. title: "不能发送空消息",
  1797. icon: 'none'
  1798. });
  1799. return;
  1800. }
  1801. const liveId = liveItem.liveId;
  1802. const socketItem = this.socketInstances[liveId];
  1803. // 【关键修改1】删除“发送时直接加入本地列表”的代码,仅清空输入框
  1804. this.$set(liveItem, 'value', ''); // 立即清空输入框,提升体验
  1805. // 检查socket连接状态
  1806. if (!socketItem || !socketItem.instance || !socketItem.isOpen) {
  1807. this.initSocket(liveItem); // 尝试重连
  1808. uni.showToast({
  1809. title: "连接未建立,正在重连",
  1810. icon: 'none'
  1811. });
  1812. return;
  1813. }
  1814. // 构造发送给服务端的消息数据
  1815. const data = {
  1816. liveId,
  1817. userId: this.userinfo.userId,
  1818. userType: 0,
  1819. cmd: "sendMsg", // 指令标识,服务端会根据此返回sendMsg消息
  1820. msg: text,
  1821. nickName: this.userinfo.nickName,
  1822. avatar: this.userinfo.avatar
  1823. };
  1824. // 发送socket消息
  1825. try {
  1826. socketItem.instance.send({
  1827. data: JSON.stringify(data),
  1828. success: () => {
  1829. // 发送成功无需额外操作,等待服务端返回消息后再更新列表
  1830. },
  1831. fail: (err) => {
  1832. console.error("发送失败:", err);
  1833. uni.showToast({
  1834. title: "发送失败,请重试",
  1835. icon: 'none'
  1836. });
  1837. // 发送失败时可恢复输入框内容(可选,提升体验)
  1838. this.$set(liveItem, 'value', text);
  1839. }
  1840. });
  1841. } catch (err) {
  1842. console.error('socket send 异常', err);
  1843. // 异常时恢复输入框内容(可选)
  1844. this.$set(liveItem, 'value', text);
  1845. }
  1846. }
  1847. }
  1848. };
  1849. </script>
  1850. <style scoped lang="scss">
  1851. .swiper-wrapper {
  1852. position: relative;
  1853. width: 100%;
  1854. height: 100vh;
  1855. overflow: hidden;
  1856. background-color: #000000;
  1857. }
  1858. .swiper-container {
  1859. width: 100%;
  1860. height: 100vh;
  1861. }
  1862. .container {
  1863. width: 100%;
  1864. height: 100%;
  1865. position: relative;
  1866. // 添加渐变过渡效果
  1867. transition: opacity 0.3s ease;
  1868. &.active {
  1869. opacity: 1;
  1870. }
  1871. // 非活动项半透明显示
  1872. // &:not(.active) {
  1873. // opacity: 0.7;
  1874. // }
  1875. }
  1876. .loading-tip {
  1877. position: absolute;
  1878. top: 50%;
  1879. left: 50%;
  1880. transform: translate(-50%, -50%);
  1881. display: flex;
  1882. flex-direction: column;
  1883. align-items: center;
  1884. justify-content: center;
  1885. z-index: 10;
  1886. .loading-text {
  1887. color: #ffffff;
  1888. font-size: 28rpx;
  1889. margin-top: 20rpx;
  1890. }
  1891. }
  1892. .loading-more,
  1893. .no-more {
  1894. display: flex;
  1895. justify-content: center;
  1896. align-items: center;
  1897. padding: 20rpx 0;
  1898. color: #999;
  1899. font-size: 24rpx;
  1900. }
  1901. .loading-more {
  1902. flex-direction: column;
  1903. }
  1904. .preload-indicator {
  1905. position: absolute;
  1906. top: 50%;
  1907. right: 30rpx;
  1908. transform: translateY(-50%);
  1909. z-index: 100;
  1910. background: rgba(0, 0, 0, 0.5);
  1911. border-radius: 50%;
  1912. padding: 16rpx;
  1913. }
  1914. /* button自带样式清除 */
  1915. .student-orther-icon button::after {
  1916. border: none !important;
  1917. padding: 0 !important;
  1918. margin: 0 !important;
  1919. }
  1920. .student-orther-icon button {
  1921. background-color: transparent !important;
  1922. padding: 0 !important;
  1923. line-height: inherit !important;
  1924. margin: 0 !important;
  1925. width: auto !important;
  1926. font-weight: 500 !important;
  1927. border-radius: none !important;
  1928. }
  1929. .welcome-message {
  1930. position: fixed;
  1931. width: 100%;
  1932. bottom: 460rpx;
  1933. left: 50%;
  1934. transform: translateX(-50%);
  1935. color: white;
  1936. padding: 10px 20px;
  1937. border-radius: 20px;
  1938. // animation: fadeOut 1s ease 1s forwards;
  1939. z-index: 1000;
  1940. transition: opacity 0.3s ease;
  1941. }
  1942. @keyframes fadeOut {
  1943. from {
  1944. opacity: 1;
  1945. }
  1946. to {
  1947. opacity: 0;
  1948. }
  1949. }
  1950. .send {
  1951. background-color: #fafafa;
  1952. border-radius: 28rpx;
  1953. padding: 14rpx 16rpx;
  1954. color: #181818;
  1955. font-weight: 500;
  1956. }
  1957. // .container {
  1958. // position: relative;
  1959. // width: 100%;
  1960. // height: 100vh;
  1961. // overflow: hidden;
  1962. // }
  1963. // .talktext {
  1964. // border-radius: 8rpx;
  1965. // background-color: rgba(255, 255, 255, 0.1);
  1966. // view {
  1967. // width: 100%;
  1968. // }
  1969. // }
  1970. .talk-list {
  1971. border-radius: 30rpx;
  1972. background-color: rgba(33, 33, 33, 0.5);
  1973. padding: 10rpx 30rpx;
  1974. }
  1975. // .zoom-button-active {
  1976. // transform: scale(1.5);
  1977. // }
  1978. // .background-image {
  1979. // position: absolute;
  1980. // top: 0;
  1981. // left: -40rpx;
  1982. // width: 110%;
  1983. // height: 110%;
  1984. // object-fit: cover;
  1985. // filter: blur(20px);
  1986. // z-index: 0;
  1987. // }
  1988. // .background-images {
  1989. // position: absolute;
  1990. // top: 0;
  1991. // left: -40rpx;
  1992. // width: 110%;
  1993. // height: 110%;
  1994. // object-fit: cover;
  1995. // filter: blur(20px);
  1996. // z-index: 6;
  1997. // }
  1998. // .blackbg {
  1999. // position: absolute;
  2000. // top: 0;
  2001. // left: 0;
  2002. // width: 100%;
  2003. // height: 100%;
  2004. // background: rgba(0, 0, 0, 0.7);
  2005. // object-fit: cover;
  2006. // filter: blur(10px);
  2007. // z-index: 1;
  2008. // }
  2009. .content {
  2010. position: relative;
  2011. z-index: 2;
  2012. height: 100%;
  2013. width: 100%;
  2014. top: 0;
  2015. left: 0;
  2016. display: flex;
  2017. flex-direction: column;
  2018. justify-content: space-between;
  2019. .hongbao-box {
  2020. position: fixed;
  2021. top: 200rpx;
  2022. left: 30rpx;
  2023. z-index: 5;
  2024. border-radius: 16rpx;
  2025. background-color: rgba(77, 77, 77, 0.5);
  2026. width: 90rpx;
  2027. height: 100rpx;
  2028. .tip {
  2029. position: absolute;
  2030. width: 100%;
  2031. bottom: 0;
  2032. color: #fafcff;
  2033. font-size: 20rpx;
  2034. background-color: rgba(15, 15, 15, 0.8);
  2035. border-radius: 16rpx;
  2036. text-align: center;
  2037. }
  2038. .item {
  2039. padding: 10rpx 4rpx;
  2040. image {
  2041. width: 80rpx;
  2042. height: 100%;
  2043. }
  2044. }
  2045. }
  2046. .siderow-group {
  2047. position: absolute;
  2048. top: 65%;
  2049. right: 30rpx;
  2050. z-index: 9;
  2051. display: flex;
  2052. flex-direction: column;
  2053. align-items: center;
  2054. .side-item {
  2055. font-weight: 500;
  2056. font-size: 24rpx;
  2057. color: #FFFFFF;
  2058. margin-bottom: 32rpx;
  2059. text-align: center;
  2060. .button {
  2061. background-color: transparent;
  2062. margin: 0;
  2063. line-height: 1;
  2064. padding: 0;
  2065. }
  2066. .image {
  2067. width: 72rpx;
  2068. height: 72rpx;
  2069. }
  2070. }
  2071. }
  2072. .side-group {
  2073. position: absolute;
  2074. // top: 30%;
  2075. top: 65%;
  2076. right: 30rpx;
  2077. z-index: 9;
  2078. display: flex;
  2079. flex-direction: column;
  2080. align-items: center;
  2081. .side-item {
  2082. font-weight: 500;
  2083. font-size: 24rpx;
  2084. color: #FFFFFF;
  2085. margin-bottom: 32rpx;
  2086. text-align: center;
  2087. .button {
  2088. background-color: transparent;
  2089. margin: 0;
  2090. line-height: 1;
  2091. padding: 0;
  2092. }
  2093. .image {
  2094. width: 72rpx;
  2095. height: 72rpx;
  2096. }
  2097. }
  2098. }
  2099. .content-top {
  2100. width: 100%;
  2101. margin-top: 68rpx;
  2102. display: flex;
  2103. justify-content: space-between;
  2104. padding: 0 24rpx;
  2105. box-sizing: border-box;
  2106. .sum {
  2107. width: 80rpx;
  2108. height: 52rpx;
  2109. background: rgba(0, 0, 0, 0.5);
  2110. border-radius: 26rpx 26rpx 26rpx 26rpx;
  2111. font-size: 24rpx;
  2112. color: #FFFFFF;
  2113. text-align: center;
  2114. line-height: 52rpx;
  2115. }
  2116. }
  2117. .follow-btn {
  2118. padding: 8rpx 16rpx;
  2119. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  2120. border-radius: 26rpx;
  2121. font-weight: 500;
  2122. font-size: 26rpx;
  2123. color: #FFFFFF;
  2124. }
  2125. }
  2126. .videolist {
  2127. position: relative;
  2128. .video {
  2129. height: 100vh;
  2130. /* 占屏幕高度的80% */
  2131. width: 100%;
  2132. background-color: rgba(57, 57, 57, 0.4);
  2133. .videolist {
  2134. position: relative;
  2135. .video {
  2136. height: 100vh;
  2137. width: 100%;
  2138. background-color: rgba(57, 57, 57, 0.4);
  2139. .time {
  2140. color: #ffffff;
  2141. font-size: 20rpx;
  2142. margin-left: 10rpx;
  2143. }
  2144. // 横竖屏样式(直播和回放共用)
  2145. .videotop {
  2146. width: 100%;
  2147. height: 100%;
  2148. }
  2149. .video_row {
  2150. width: 100%;
  2151. max-height: 500rpx;
  2152. overflow: hidden;
  2153. margin-top: 360rpx;
  2154. }
  2155. }
  2156. }
  2157. .time {
  2158. color: #ffffff;
  2159. font-size: 20rpx;
  2160. margin-left: 10rpx;
  2161. }
  2162. .videotop {
  2163. width: 100%;
  2164. height: 100%;
  2165. }
  2166. .video_row {
  2167. width: 100%;
  2168. max-height: 500rpx;
  2169. overflow: hidden;
  2170. margin-top: 360rpx;
  2171. }
  2172. }
  2173. }
  2174. .integral-box {
  2175. width: 300rpx;
  2176. display: flex;
  2177. flex-direction: column;
  2178. align-items: center;
  2179. border-radius: 20rpx;
  2180. overflow: hidden;
  2181. .top {
  2182. width: 100%;
  2183. position: relative;
  2184. .title {
  2185. width: 100%;
  2186. font-weight: 600;
  2187. font-size: 30rpx;
  2188. color: #FFFFFF;
  2189. text-shadow: 0px 4px 8px rgba(255, 89, 2, 0.8);
  2190. position: absolute;
  2191. top: 30rpx;
  2192. text-align: center;
  2193. }
  2194. .photo {
  2195. width: 100%;
  2196. }
  2197. }
  2198. .item {
  2199. padding: 20rpx;
  2200. .title {
  2201. font-weight: 500;
  2202. font-size: 30rpx;
  2203. text-align: center;
  2204. }
  2205. .button {
  2206. margin-top: 20rpx;
  2207. background: linear-gradient(270deg, #ff4702 0%, #fe6304 100%);
  2208. color: #fff;
  2209. text-align: center;
  2210. padding: 16rpx 30rpx;
  2211. border-radius: 10rpx;
  2212. font-weight: 500;
  2213. }
  2214. }
  2215. }
  2216. .goods {
  2217. position: fixed;
  2218. // bottom: 160rpx;
  2219. // right: 10rpx;
  2220. top: 50%;
  2221. left: 50%;
  2222. transform: translate(-50%, -50%);
  2223. z-index: 5;
  2224. background-color: #fff;
  2225. border-radius: 20rpx;
  2226. overflow: hidden;
  2227. width: 300rpx;
  2228. // width: 220rpx;
  2229. padding: 4rpx;
  2230. .top {
  2231. position: absolute;
  2232. top: 4rpx;
  2233. display: flex;
  2234. justify-content: space-between;
  2235. align-items: center;
  2236. color: #fff;
  2237. width: 100%;
  2238. padding-right: 10rpx;
  2239. box-sizing: border-box;
  2240. .left {
  2241. background-color: rgba(0, 0, 0, 0.8);
  2242. padding: 10rpx;
  2243. font-size: 20rpx;
  2244. display: flex;
  2245. justify-content: space-between;
  2246. align-items: center;
  2247. border-radius: 10rpx;
  2248. }
  2249. }
  2250. .photo {
  2251. width: 100%;
  2252. height: 250rpx;
  2253. border-radius: 16rpx 16rpx 0 0;
  2254. overflow: hidden;
  2255. }
  2256. .item {
  2257. padding: 4rpx;
  2258. .price {
  2259. .red {
  2260. color: #FF5701;
  2261. font-weight: 600;
  2262. margin-right: 10rpx;
  2263. }
  2264. .del {
  2265. color: #828282;
  2266. font-weight: 500;
  2267. font-size: 24rpx;
  2268. text-decoration: line-through
  2269. }
  2270. }
  2271. .title {
  2272. font-weight: 500;
  2273. font-size: 26rpx;
  2274. margin: 10rpx 0 12rpx;
  2275. }
  2276. .button {
  2277. background: linear-gradient(270deg, #ff4702 0%, #fe6304 100%);
  2278. color: #fff;
  2279. text-align: center;
  2280. padding: 16rpx 0;
  2281. border-radius: 10rpx;
  2282. font-weight: 500;
  2283. }
  2284. }
  2285. }
  2286. .icon-bg {
  2287. background-color: rgba(57, 57, 57, 0.8);
  2288. border-radius: 50%;
  2289. width: 72rpx;
  2290. height: 72rpx;
  2291. display: flex;
  2292. justify-content: center;
  2293. align-items: center;
  2294. transition: transform 0.2s ease;
  2295. .button {
  2296. background-color: transparent;
  2297. margin: 0;
  2298. line-height: 1;
  2299. padding: 0;
  2300. }
  2301. }
  2302. .list {
  2303. width: 80%;
  2304. margin-bottom: 20rpx;
  2305. animation: xxxawdawd .2s;
  2306. }
  2307. @keyframes xxxawdawd {
  2308. from {
  2309. margin-top: 0rpx;
  2310. opacity: 0;
  2311. }
  2312. to {
  2313. margin-top: 20rpx;
  2314. opacity: 1;
  2315. }
  2316. }
  2317. .shop-prompt {
  2318. position: absolute;
  2319. bottom: 600rpx;
  2320. left: 24rpx;
  2321. padding: 6rpx 20rpx;
  2322. background: rgba(230, 154, 34, 0.7);
  2323. border-radius: 24rpx;
  2324. z-index: 9;
  2325. font-weight: 500;
  2326. font-size: 26rpx;
  2327. color: #FFFFFF;
  2328. transition: opacity 0.3s ease;
  2329. }
  2330. .red-box {
  2331. .button {
  2332. margin: 0 auto;
  2333. width: 80%;
  2334. line-height: 88rpx;
  2335. height: 88rpx;
  2336. background: linear-gradient(90deg, #FF5701 0%, #FFB501 100%);
  2337. box-shadow: 0rpx 8rpx 8rpx 0rpx rgba(238, 124, 80, 0.2);
  2338. border-radius: 44rpx 44rpx 44rpx 44rpx;
  2339. font-weight: 600;
  2340. font-size: 32rpx;
  2341. color: #FFFFFF;
  2342. text-align: center;
  2343. }
  2344. }
  2345. .view-box {
  2346. position: relative;
  2347. height: 40vh;
  2348. /* 设置弹出层高度为视窗高度的70% */
  2349. padding: 40rpx 0rpx 120rpx;
  2350. box-sizing: border-box;
  2351. display: flex;
  2352. flex-direction: column;
  2353. .scroll-content {
  2354. flex: 1;
  2355. overflow-y: auto;
  2356. padding: 0 40rpx;
  2357. }
  2358. .bottom {
  2359. padding: 20rpx 40rpx;
  2360. position: absolute;
  2361. bottom: 0;
  2362. width: 100%;
  2363. box-shadow: 0rpx -4rpx 10rpx 0rpx rgba(195, 195, 195, 0.3);
  2364. background: #fff;
  2365. }
  2366. }
  2367. // 抽奖
  2368. .answerpop {
  2369. background: linear-gradient(to right, #fff7f8, #fee1e2);
  2370. margin-top: 30rpx;
  2371. padding: 10rpx 20rpx;
  2372. width: calc(100% - 40rpx);
  2373. border-radius: 20rpx;
  2374. display: flex;
  2375. align-items: center;
  2376. justify-content: space-between;
  2377. box-shadow: 2px 2px 4px 0 rgba(255, 124, 126, 0.1);
  2378. .answera {
  2379. display: flex;
  2380. justify-content: center;
  2381. background-color: #fff;
  2382. padding: 20rpx 0;
  2383. width: 35%;
  2384. border-radius: 40rpx;
  2385. align-items: center;
  2386. margin: 10rpx 0;
  2387. box-shadow: 2px 2px 4px 0 rgba(72, 72, 72, 0.1);
  2388. image {
  2389. width: 40rpx;
  2390. height: 40rpx;
  2391. margin-right: 10rpx;
  2392. }
  2393. }
  2394. }
  2395. .shoppop {
  2396. padding: 22rpx 16rpx;
  2397. .shoppop-top {
  2398. display: flex;
  2399. justify-content: space-between;
  2400. align-items: center;
  2401. padding: 0 16rpx 22rpx;
  2402. .search-input {
  2403. width: 414rpx;
  2404. height: 76rpx;
  2405. background: #FFFFFF;
  2406. border-radius: 36rpx;
  2407. margin-left: 20rpx;
  2408. padding: 0 32rpx;
  2409. box-sizing: border-box;
  2410. font-size: 24rpx;
  2411. margin-right: 24rpx;
  2412. }
  2413. .search-top {
  2414. font-size: 20rpx;
  2415. color: #222222;
  2416. }
  2417. }
  2418. .shop-list {
  2419. overflow: hidden;
  2420. .list-item {
  2421. display: flex;
  2422. align-items: center;
  2423. padding: 20rpx 16rpx;
  2424. background: #FFFFFF;
  2425. border-radius: 16rpx;
  2426. margin-bottom: 16rpx;
  2427. .goods-img {
  2428. width: 200rpx;
  2429. height: 200rpx;
  2430. border-radius: 16rpx;
  2431. overflow: hidden;
  2432. position: relative;
  2433. margin-right: 24rpx;
  2434. image {
  2435. width: 100%;
  2436. height: 100%;
  2437. }
  2438. .goods-label {
  2439. position: absolute;
  2440. top: 0;
  2441. width: 64rpx;
  2442. height: 40rpx;
  2443. background: rgba(0, 0, 0, 0.5);
  2444. border-radius: 16rpx 0rpx 16rpx 0rpx;
  2445. text-align: center;
  2446. font-weight: 500;
  2447. font-size: 28rpx;
  2448. color: #FFFFFF;
  2449. }
  2450. }
  2451. .goods-right {
  2452. flex: 1;
  2453. .goods-title {
  2454. font-weight: 500;
  2455. font-size: 28rpx;
  2456. color: #000000;
  2457. }
  2458. .goods-details {
  2459. font-size: 24rpx;
  2460. color: #999999;
  2461. margin: 10rpx 0 20rpx;
  2462. }
  2463. .goods-people {
  2464. font-size: 22rpx;
  2465. color: #E69A22;
  2466. height: 56rpx;
  2467. }
  2468. .goods-shop {
  2469. display: flex;
  2470. justify-content: space-between;
  2471. .nummber {
  2472. color: #FF5C03;
  2473. font-size: 22rpx;
  2474. font-weight: 500;
  2475. }
  2476. .btn-group {
  2477. text-align: center;
  2478. line-height: 56rpx;
  2479. .collect-btn {
  2480. width: 72rpx;
  2481. background: #F5F7FA;
  2482. border-radius: 8rpx 0rpx 0rpx 8rpx;
  2483. }
  2484. .shop-btn {
  2485. width: 152rpx;
  2486. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  2487. border-radius: 0rpx 8rpx 8rpx 0rpx;
  2488. font-weight: 500;
  2489. font-size: 26rpx;
  2490. color: #FFFFFF;
  2491. }
  2492. }
  2493. }
  2494. }
  2495. }
  2496. }
  2497. }
  2498. :deep(.u-list-item) {
  2499. width: 100%;
  2500. display: flex;
  2501. align-items: center;
  2502. }
  2503. :deep(.u-safe-area-inset-bottom) {
  2504. padding-bottom: 0
  2505. }
  2506. </style>