living.vue 68 KB

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