living.vue 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. <template>
  2. <view class="container">
  3. <!-- <view class="popup-video " v-if="!autoplay">
  4. <view class="fs36 bold" v-if="livedata.status==1">——直播还未开始——</view>
  5. <view class="fs36 bold" v-if="livedata.status==3">——直播已经结束——</view>
  6. <view class="fs28 mtb20">了解更多,点击下方联系老师</view>
  7. <view class="more" @click="showadd=!showadd">联系老师</view>
  8. </view> -->
  9. <!-- <image :src="livedata.liveImgUrl"
  10. :class="livedata.status!=2?'background-images':'background-image'"></image> -->
  11. <view class="blackbg"></view>
  12. <view class="content">
  13. <!-- 页面内容 -->
  14. <view style=" position: fixed;top: 0;z-index: 5;" class="content-top">
  15. <view class="u-flex-y-center">
  16. <image @click="goBack" style="width: 60rpx;height: 64rpx;margin-right: 26rpx;"
  17. src="@/static/images/live/return.png"></image>
  18. <view class=" align-center"
  19. style="padding: 6rpx 4rpx;height: 64rpx;background: rgba(0,0,0,0.5);border-radius: 32rpx;">
  20. <u-avatar :src="livedata.liveImgUrl" size="32"></u-avatar>
  21. <view class="colorf" style="margin-left: 8rpx;">
  22. <view>{{livedata.liveName?truncateString(livedata.liveName,5):"未命名"}}</view>
  23. <view style="font-size: 16rpx;">
  24. {{ liveViewData.follow || liveViewData.follow === 0 ? liveViewData.follow : 0 }}位粉丝
  25. </view>
  26. </view>
  27. <view v-if="isFollow" @click="onFollow" class="follow-btn ml20">关注</view>
  28. <view v-else @click="onFollow" class="follow-btn ml20">已关注</view>
  29. </view>
  30. </view>
  31. <view class="u-flex-end align-center">
  32. <image class="w52 h52 mr4" v-for="(item,index) in filteredViewers" :key="index"
  33. style="border-radius: 26rpx;" :src="item.avatar"></image>
  34. <view class="sum">{{liveViewData.online||0}}</view>
  35. </view>
  36. </view>
  37. <!-- 右边的 -->
  38. <view :class=" showType==1 ? 'siderow-group' : 'side-group'">
  39. <view class="side-item">
  40. <image @click="onLike" src="/static/images/live/like.png"></image>
  41. <view>{{liveViewData.like}}</view>
  42. <!-- <view>{{liveData likeName}}</view> -->
  43. </view>
  44. <view class="side-item">
  45. <image @click="goStore()" src="/static/images/live/shop.png"></image>
  46. <view>店铺</view>
  47. </view>
  48. <!-- <view class="side-item" open-type="share">
  49. <image src="/static/images/live/share.png"></image>
  50. <view>分享</view>
  51. </view> -->
  52. <view class="side-item">
  53. <button open-type="share">
  54. <image src="/static/images/live/share.png" mode="widthFix"></image>
  55. </button>
  56. <view>分享</view>
  57. </view>
  58. </view>
  59. <view class="shop-prompt u-flex-y-center" v-if="showPurchasePrompt">
  60. <image class="w32 h32 mr8" src="/static/images/live/shopping.png"></image>
  61. <text> {{orderUser.userName ? maskString(orderUser.userName) : ''}} 等 {{orderUser.count || 0}}
  62. 人正在去购买</text>
  63. </view>
  64. <!-- object-fit:fill; -->
  65. <view class="videolist " style="margin: auto 0">
  66. <!-- <view class="video" :style="{height:isScreen?'100vh':''}"> -->
  67. <view class="video" style="height:100vh">
  68. <!-- 修改video组件以支持HLS -->
  69. <!-- showType 1 横屏 2 竖屏 -->
  70. <video v-if="livingUrl" id="myVideo" :class="showType == 1 ? 'video_row' : 'videotop'"
  71. :src="livingUrl" :autoplay="autoplay" :controls='false' object-fit='contain'
  72. :custom-cache="false" :enable-progress-gesture="false" vslide-gesture-in-fullscreen='true'
  73. :show-center-play-btn="false" :http-cache="false" @error="videoError">
  74. </video>
  75. <video v-if="videoUrl" :class="showType == 1 ? 'video_row' : 'videotop'" :src="videoUrl"
  76. :autoplay="autoplay" :controls='false' object-fit='contain' :custom-cache="false"
  77. :enable-progress-gesture="false" vslide-gesture-in-fullscreen='true'
  78. :show-center-play-btn="false" :http-cache="false" loop @error="videoError">
  79. </video>
  80. </view>
  81. </view>
  82. <view class="pb40 mt90" style="position: fixed;bottom: 0;">
  83. <view class="w100 h300 mt20">
  84. <scroll-view enable-flex scroll-y="true" class="talk p20 scrolly flex-1 column"
  85. style="width: calc(100% - 40rpx);height: calc(100% - 40rpx);"
  86. :scroll-into-view="scrollIntoView">
  87. <view>
  88. <view class="list justify-start" v-for="(item,index) in talklist" :key="item.index"
  89. :id="`list_${index}`" v-show="item.cmd=='announcement'">
  90. <view class="talk-list ml16 justify-start">
  91. <view class="fs24">
  92. <text class='fs24 colorf'> {{item.msg}}直播间{{messageContent}}</text>
  93. </view>
  94. </view>
  95. </view>
  96. </view>
  97. <!-- 弹幕 -->
  98. <!-- <u-list class="chat-list" height="184" @scrolltolower="scrollchat">
  99. <u-list-item class="chat-item" v-for="(item,index) in chatList" :key="index">
  100. <view class="chat-lable u-flex-y-center mr8" v-if="item.lable">
  101. <image class="w24 h24 mr8" src="@/static/images/live/diamond.png"></image>
  102. <text>{{item.lable}}</text>
  103. </view>
  104. <text style="color: #FFDA73;white-space: nowrap;" class=" mr8">{{item.name}}:</text>
  105. <text style="flex: 1;min-width: 0;word-break: break-word;">{{item.txt}}</text>
  106. </u-list-item>
  107. </u-list> -->
  108. <view class="list justify-start" v-for="(item,index) in talklist" :key="item.index"
  109. :id="`list_${index}`" v-show="item.cmd=='sendMsg'">
  110. <view class="talk-list ml16 justify-start">
  111. <view class="fs24">
  112. <!-- <text style="color: #3fc69b;transform: scale(0.8);display: inline-block; "v-if="item.userId==userinfo.userId&&item.cmd=='sendMsg'">我</text> -->
  113. <!-- <text style="color: #c84e1e;transform: scale(0.8);display: inline-block;" v-if="item.userType==1">[ 管理员 ]</text> -->
  114. <text style="color: #FFDA73;">{{item.nickName}}:</text>
  115. <text class='fs24 colorf'>{{item.msg}}</text>
  116. </view>
  117. </view>
  118. </view>
  119. <view v-if="showWelcomeMessage" class="welcome-message">
  120. <view class="list justify-start" v-for="(item,index) in talklist" :key="item.index"
  121. :id="`list_${index}`" v-show="item.cmd=='entry'||item.cmd=='out'">
  122. <view class="talk-list ml16 justify-start">
  123. <view class="fs24">
  124. <text style="color: #ff89d6;">{{item.nickName}} </text>
  125. <text class='fs24 colorf'> {{item.msg}}直播间{{messageContent}}</text>
  126. </view>
  127. </view>
  128. </view>
  129. </view>
  130. </scroll-view>
  131. </view>
  132. <view class="justify-between p24">
  133. <view class="u-flex-y-center"
  134. style="background:rgba(157, 157, 157, 0.8);padding:18rpx 14rpx 18rpx 32rpx;width: 526rpx;box-sizing:border-box;border-radius:36rpx;">
  135. <u-input :placeholder="placeholderText" border="none" customStyle='font-size:24rpx;'
  136. v-model="value" shape='circle' color='#fff' placeholderStyle='color:#e7e7e7' class="ml20"
  137. @keydown.enter="sendMsg" @confirm="sendMsg">
  138. </u-input>
  139. <!-- <image @click="sendMsg" class="w44 h44" src="@/static/images/live/like.png"></image> -->
  140. </view>
  141. <!-- :disabled='talkdisabled' ></u-input> -->
  142. <!-- <view @click="sendMsg" class="colorf center ml10 fs24">发送</view> -->
  143. <view class="justify-between mr30 align-center">
  144. <!-- <view class="icon-bg ml20" @click="answerbtn=!answerbtn">
  145. <image src="/static/images/redbag.png" class="w40 h40"></image>
  146. </view> -->
  147. <view class="icon-bg ml20" @click="shopping=!shopping">
  148. <image src="/static/images/shopping.png" class="w48 h48"></image>
  149. </view>
  150. <view class="icon-bg ml20" @click="showziliao=!showziliao">
  151. <image src="/static/images/more-icon.png" class="w48 h48"></image>
  152. </view>
  153. <!-- <view class="icon-bg ml20" :class="{ 'zoom-button-active': isZoom }"
  154. @touchstart="handleTouchStart" @touchend="handleTouchEnd">
  155. <image src="/static/images/dianzan.png" class="w40 h40"></image>
  156. </view> -->
  157. </view>
  158. </view>
  159. </view>
  160. </view>
  161. <!-- <u-popup :show="showadd" @close="close" @open="open" round='20rpx' bgColor='#fffee1'>
  162. <view class="addchat p20">
  163. <view class="u-flex-row-reverse u-flex">
  164. <u-icon name="close" size="18" @click="showadd=!showadd"></u-icon>
  165. </view>
  166. <view class="column align-center">
  167. <view class="fs36" style="color: #ff5c03;">扫码添加助教老师</view>
  168. <view class="fs28 color6">扫码添加助教老师</view>
  169. <view class="p10 mt40" style="border: #ff5c03 solid 2rpx;">
  170. <image :src="codeimg" class="wh180" @touchstart="longPress" @touchend="cancelLongPress">
  171. </image>
  172. </view>
  173. <view class="color6 mt20">长按识别二维码</view>
  174. </view>
  175. </view>
  176. </u-popup> -->
  177. <u-popup :show="showziliao" @close="closes" round='20' bgColor='#fffee1'>
  178. <view class="addchat p20 bgf" style="border-radius: 20rpx;">
  179. <view class="u-flex-row-reverse u-flex">
  180. <u-icon name="close" size="18" @click="showziliao=!showziliao"></u-icon>
  181. </view>
  182. <view class="column align-center h400">
  183. <view class="fs36 " style="color: #ff5c03;">资料</view>
  184. <view v-html="livedata.liveDesc"></view>
  185. </view>
  186. </view>
  187. </u-popup>
  188. <!-- 抽奖 -->
  189. <!-- <u-popup :show="answerbtn" @close="closeanswer" round='40rpx' bgColor='#fff'>
  190. <view class=" p20 bgf" style="border-radius: 40rpx;height: fit-content;">
  191. <view class="u-flex-row-reverse u-flex">
  192. <u-icon name="close" size="18" @click="answerbtn=!answerbtn"></u-icon>
  193. </view>
  194. <view class="column align-center h400">
  195. <view class="fs36 " style="color: #000000;font-weight: bold;">答题获取红包/积分奖励</view>
  196. <view class="answerpop" @click="noredanswer">
  197. <view class="color6 w350 fs24">
  198. 边玩边学,解锁你的知识巅峰!
  199. </view>
  200. <view class="answera">答题挑战</view>
  201. </view>
  202. <view class="answerpop">
  203. <view class="color6 fs24" v-if='redanswertips&&redanswertips.length>0'>
  204. 🔥 <text style="color: #ff5c03;" class="fs30 bold">{{redanswertips[1].probability}}</text>
  205. %用户首抽得
  206. <text style="color: #ff5c03;" class="fs40 bold">{{redanswertips[1].maxAmount}}</text>元
  207. </view>
  208. <view v-else class="color6 w350 fs24">
  209. 随机奖励
  210. </view>
  211. <view class="answera" @click="redbagAnswer">
  212. <image src="/static/images/baganswer.png"></image>
  213. <view> 获得红包</view>
  214. </view>
  215. </view>
  216. </view>
  217. </view>
  218. </u-popup> -->
  219. <!-- <u-popup :show="showAnswer" @close="closest" round='40' bgColor='#fffee1' mode="center">
  220. <view class="answerbox p20 bgf">
  221. <view class="u-flex-row-reverse u-flex">
  222. <u-icon name="close" size="18" @click="closest"></u-icon>
  223. </view>
  224. <view class="column align-center ">
  225. <view v-if="answerlist.type==1" style="width: 100%;">
  226. <view class="mb40" style="text-align: center;">
  227. <text class="color9 fs24">(单选)</text>{{answerlist.title}}
  228. </view>
  229. <view v-for="(item,index) in answerlist.content" :key="index"
  230. :class=" checkboxValue[0]== item.label?'answeract itemanswer':'itemanswer' "
  231. @click="handleCheckboxSelect(item.label)">{{item.label}}. {{item.content}}</view>
  232. </view>
  233. <view v-if="answerlist.type==2" style="width: 100%;">
  234. <view class="mb40 " style="text-align: center;">
  235. <text class="color9 fs24">(多选)</text>{{answerlist.title}}
  236. </view>
  237. <view v-for="(item,index) in answerlist.content" :key="index"
  238. :class=" checkboxValue.includes(item.label)?'answeract itemanswer':'itemanswer' "
  239. @click="handleCheckboxSelect(item.label)">{{item.label}}. {{item.content}}</view>
  240. <view class=" submitbtn" @click="submitAnswers">确认</view>
  241. </view>
  242. <view class="fs24 mtb20 " style="color: #717171;">
  243. {{answerfrist+1}}/{{Answerlistall}}
  244. </view>
  245. </view>
  246. </view>
  247. </u-popup> -->
  248. <!-- <u-popup :show="showAnswerred" @close="closestred" round='40' bgColor='#fffee1' mode="center">
  249. <view class="answerbox p20 bgf">
  250. <view class="u-flex-row-reverse u-flex">
  251. <u-icon name="close" size="18" @click="closestred"></u-icon>
  252. </view>
  253. <view class="column align-center ">
  254. <view v-if="answerlist.type==1" style="width: 100%;">
  255. <view class="mb40" style="text-align: center;">
  256. <text class="color9 fs24">(单选)</text>{{answerlist.title}}
  257. </view>
  258. <view v-for="(item,index) in answerlist.content" :key="index"
  259. :class=" checkboxValue[0]== item.label?'answeract itemanswer':'itemanswer' "
  260. @click="handleCheckboxSelect(item.label)">{{item.label}}. {{item.content}}</view>
  261. </view>
  262. <view v-if="answerlist.type==2" style="width: 100%;">
  263. <view class="mb40 " style="text-align: center;">
  264. <text class="color9 fs24">(多选)</text>{{answerlist.title}}
  265. </view>
  266. <view v-for="(item,index) in answerlist.content" :key="index"
  267. :class=" checkboxValue.includes(item.label)?'answeract itemanswer':'itemanswer' "
  268. @click="handleCheckboxSelect(item.label)">{{item.label}}. {{item.content}}</view>
  269. <view class=" submitbtn" @click="submitAnswers">确认</view>
  270. </view>
  271. <view class="fs24 mtb20 " style="color: #717171;">
  272. {{answerfrist+1}}/{{Answerlistall}}
  273. </view>
  274. </view>
  275. </view>
  276. </u-popup> -->
  277. <!-- 小黄车弹窗 -->
  278. <u-popup :show="shopping" @close="closeshop" @open="open" round='20rpx' bgColor='#f3f5f9'>
  279. <view class="shoppop">
  280. <view class="shoppop-top">
  281. <u-avatar :src="store.logoUrl" size="36" class="ml16"></u-avatar>
  282. <view class="search-input u-flex-y-center">
  283. <image style="width: 24rpx;height: 24rpx;margin-right: 16rpx;" src="@/static/images/search.png">
  284. </image>
  285. <input placeholder="搜索商品或序号" />
  286. </view>
  287. <view class="t-c search-top" style="margin-right: 48rpx;" @click="onStoreCollect">
  288. <image v-if="store.isFavorite" style="width: 32rpx;height: 32rpx;"
  289. src="@/static/images/collect_select.png"></image>
  290. <image v-else @click="onStoreCollect" style="width: 32rpx;height: 32rpx;"
  291. src="@/static/images/collect.png"></image>
  292. <view>收藏</view>
  293. </view>
  294. <view class="t-c search-top" @click="goOrderList">
  295. <image style="width: 32rpx;height: 32rpx;" src="@/static/images/order.png"></image>
  296. <view>订单</view>
  297. </view>
  298. </view>
  299. <scroll-view enable-flex scroll-y class="shop-list" :style="{ height: boxHeight + 'px' }">
  300. <view class="list-item " v-for="(item,index) in products" :key="index">
  301. <view class="goods-img">
  302. <image :src="item.imgUrl" mode="widthFix"></image>
  303. <view class="goods-label">{{index+1}}</view>
  304. </view>
  305. <view class="goods-right">
  306. <view class="goods-title">{{item.productName}}</view>
  307. <!-- <view class="goods-details">{{item.productIntroduce}}</view> -->
  308. <view class="goods-people">{{item.sales}} 人已购</view>
  309. <view class="goods-shop">
  310. <text class="nummber"><text style="font-size: 20rpx;font-weight: 600;">¥</text><text
  311. style="font-size: 36rpx;font-weight: bold;">{{Math.trunc(item.price)}}</text>.{{getPureDecimal(item.price)?getPureDecimal(item.price):'00'}}/日</text>
  312. <view class="btn-group u-flex-y-center">
  313. <view class="collect-btn">
  314. <image v-if="item.isFavorite" @click="onGoodsCollect(item)"
  315. style="width: 32rpx;height: 32rpx;" src="/static/images/collect_select.png">
  316. </image>
  317. <image v-else @click="onGoodsCollect(item)" style="width: 32rpx;height: 32rpx;"
  318. src="/static/images/collect.png">
  319. </image>
  320. </view>
  321. <view v-if="item.status==1" class="shop-btn"
  322. @click="goShop(item.productId,item.goodsId)">去购买
  323. </view>
  324. <view v-else-if="item.status==0" @click="goShop(item.productId)" class="shop-btn">
  325. 已下架</view>
  326. </view>
  327. </view>
  328. </view>
  329. </view>
  330. </scroll-view>
  331. </view>
  332. </u-popup>
  333. </view>
  334. </template>
  335. <script>
  336. import Hls from 'hls.js';
  337. import CryptoJS from 'crypto-js'
  338. import {
  339. liveDataLike, // 点赞
  340. collectStore, // 店铺收藏/取消收藏
  341. collectGoods, // 商品收藏/取消收藏
  342. store, // 查询店铺
  343. follow, // 关注/取消关注
  344. getRecentLiveViewers, //获取直播间用户(展示在线用户)
  345. // 小黄车
  346. liveStore, //店铺展示,
  347. // liveGoodsDetail, //商品详情,
  348. liveOrderUser, //正在购买
  349. getLiveInfo, //获取直播间信息接口
  350. getLiveViewData //直播间点赞、关注、在线人数数据
  351. } from '@/api/live'
  352. import {
  353. liveOrderList, // 订单列表
  354. } from '@/api/order'
  355. import parse from '../../uni_modules/uview-plus/libs/config/props/parse';
  356. import {
  357. LiveWS
  358. } from '@/utils/liveWS.js'
  359. import {
  360. getlive,
  361. gettextlist,
  362. getAnswerlist,
  363. submitAnswer
  364. } from '@/api/home'
  365. // var wsUrl = "ws://192.168.10.166:7114/app/webSocket";
  366. var wsUrl = "ws://192.168.10.166:7114/app/webSocket"; //余红奇
  367. // var wsUrl = "ws://live.test.ylrztop.com/prod-api/app/webSocket"; //余红奇
  368. // var wsUrl = "ws://nd383294.natappfree.cc/app/webSocket"; //余红奇
  369. // var wsUrl = "ws://v56c9b8e.natappfree.cc/app/webSocket"; //余红奇
  370. // var wsUrl = "ws://192.168.10.170:7114/app/webSocket"; //陈果
  371. // var wsUrl = "ws://live.ylrzcloud.com/socket/app/webSocket";
  372. var pingpangTimes = null;
  373. var initTimes = null;
  374. var isSocketOpen = false;
  375. var socket = null;
  376. export default {
  377. data() {
  378. return {
  379. storeId: null,
  380. reconnectCount: 0,
  381. maxReconnectAttempts: 3,
  382. reconnectTimer: null,
  383. isCollect: false,
  384. showPurchasePrompt: false,
  385. purchasePromptTimer: null,
  386. prevOrderCount: 0, // 用于记录上一次的购买人数
  387. videoUrl: null,
  388. showType: 1, //横屏1 竖屏2
  389. boxHeight: 300, //小黄车高度
  390. isFollow: true,
  391. liveViewData: {},
  392. liveViewers: [], //观众
  393. likeName: 0,
  394. hlsPlayer: null, // HLS播放器实例,
  395. livingUrl: "",
  396. products: {},
  397. store: {},
  398. orderUser: {}, //正在购买
  399. userType: 0,
  400. timestamp: '',
  401. liveId: null,
  402. // userId: uni.getStorageSync("userInfo.userId"),
  403. livedata: {},
  404. codeimg: '',
  405. placeholderText: '说点什么...',
  406. isZoom: false, //点赞按钮控制是否放大
  407. userinfo: '', //用户信息
  408. path: 'http://192.168.10.166/dev-api', //余红奇
  409. // path: 'http://v56c9b8e.natappfree.cc', //余红奇
  410. // path: 'live.test.ylrztop.com/prod-api', //余红奇
  411. // path: 'http://192.168.10.170/dev-api', //陈果
  412. value: '',
  413. talkdisabled: false, //输入框是否禁用
  414. autoplay: false, //视频自动播放
  415. showadd: false,
  416. talklist: [],
  417. scrollIntoView: '',
  418. bufferRate: 0, //视频缓冲时间
  419. playDuration: 0, //视频播放时间
  420. videoContext: '',
  421. thistime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
  422. upDown: true, //是否视频显示隐藏
  423. isLongPress: false, // 是否长按
  424. timeout: null, // 计时器
  425. showWelcomeMessage: false,
  426. isSubmit: false,
  427. messageContent: "",
  428. showziliao: false,
  429. isScreen: true,
  430. showAnswer: false, //展示答题
  431. Answerlistall: {}, //所有题目
  432. answerlist: {}, //当前题目
  433. answerfrist: 0, //当前选择
  434. checkboxValue: [], //多选数据
  435. checkboxFormValue: "", //多选数据
  436. allAnswerLists: [], // 新增:存储所有题目列表
  437. showAnswerred: false, //展示红包答题
  438. answerbtn: false, //答题按钮弹窗
  439. redallAnswerLists: [], //储存所有红包答题列表
  440. redanswerAll: [], //红包当前题目
  441. redanswerList: [], //红包答题列表
  442. redanswertips: [], //红包答题提示
  443. shopping: false, //小黄车弹窗
  444. scrollTop: 0, //弹幕
  445. old: {
  446. scrollTop: 0
  447. },
  448. // liveData: {}, //直播间点赞、关注、在线人数数据
  449. };
  450. },
  451. onLoad(options) {
  452. if (options.liveId) {
  453. this.liveId = options.liveId;
  454. // this.liveId = decodeURIComponent(options.liveId);
  455. console.log("接收到的liveId:", this.liveId);
  456. }
  457. uni.showShareMenu({
  458. withShareTicket: true
  459. });
  460. },
  461. onShareAppMessage() {
  462. return {
  463. // title: this.product.title,
  464. title: livedata.liveName,
  465. path: '/pages/home/index',
  466. imageUrl: this.product.image,
  467. success(res) {
  468. console.log("分享成功", res);
  469. },
  470. fail(err) {
  471. console.error("分享失败", err);
  472. }
  473. };
  474. },
  475. computed: {
  476. filteredViewers() {
  477. // 只返回前3项(索引0、1、2)
  478. return this.liveViewers.slice(0, 3);
  479. }
  480. },
  481. mounted() {
  482. // this.onLike()
  483. this.getliveViewData() ////直播间点赞、关注、在线人数数据
  484. this.intervalId = setInterval(() => {
  485. this.getliveViewData();
  486. }, 60 * 1000);
  487. // this.getLiveinformation() // 获取直播间信息接口
  488. this.getliveOrder() //正在购买
  489. // this.getliveStore() // 获取小黄车 店铺展示
  490. // this.getliveGoods() // 获取小黄车 商品详情
  491. this.getliveUser() // 获取直播间用户
  492. this.initTime()
  493. // this.initWebSocket()
  494. this.initSocket()
  495. var that = this;
  496. uni.$on('initSocket', () => {
  497. that.initSocket()
  498. })
  499. uni.$on('sendMsg', (item) => {
  500. that.sendMsg(item)
  501. })
  502. uni.$on('closeWebSocket', () => {
  503. that.closeWebSocket()
  504. })
  505. this.getEWechatSdk();
  506. this.getliving() //hls
  507. this.gettalklist()
  508. // this.getAnswerlists()
  509. this.userinfo = JSON.parse(uni.getStorageSync("userInfo"))
  510. },
  511. onReady: function(res) {
  512. this.videoContext = uni.createVideoContext('myVideo')
  513. // console.log(this.videoContext)
  514. },
  515. onUnload() {
  516. this.closeWebSocket(); // 安全关闭
  517. if (this.hlsPlayer) {
  518. this.hlsPlayer.destroy(); // 清理 HLS 播放器
  519. }
  520. if (this.pingpangTimes) {
  521. clearInterval(this.pingpangTimes); // 清除心跳定时器
  522. }
  523. uni.$off('refreshOrder'); // 移除全局事件监听
  524. },
  525. watch: {
  526. // 监听orderUser.count的变化
  527. 'orderUser.count': {
  528. handler(newVal, oldVal) {
  529. if (newVal !== this.prevOrderCount) {
  530. this.prevOrderCount = newVal;
  531. this.showPurchaseMessage();
  532. }
  533. },
  534. immediate: true
  535. }
  536. },
  537. methods: {
  538. // 显示购买提示信息
  539. showPurchaseMessage() {
  540. // 清除之前的定时器
  541. if (this.purchasePromptTimer) {
  542. clearTimeout(this.purchasePromptTimer);
  543. }
  544. // 显示提示
  545. this.showPurchasePrompt = true;
  546. // 2秒后自动隐藏
  547. this.purchasePromptTimer = setTimeout(() => {
  548. this.showPurchasePrompt = false;
  549. }, 2000);
  550. },
  551. truncateString(str, maxLength) {
  552. return str.length > maxLength ? str.slice(0, maxLength) + '...' : str;
  553. },
  554. goStore() {
  555. console.log("带过去storeId", this.storeId)
  556. uni.navigateTo({
  557. url: '/pages_shop/store?liveId=' + this.liveId + "&storeId=" + this.storeId
  558. })
  559. },
  560. // 去订单列表
  561. goOrderList() {
  562. uni.navigateTo({
  563. url: "/pages_shop/order"
  564. })
  565. },
  566. // 初始化HLS播放器
  567. initHlsPlayer() {
  568. if (Hls.isSupported() && this.livingUrl) {
  569. const video = document.getElementById('myVideo');
  570. if (video) {
  571. this.hlsPlayer = new Hls();
  572. this.hlsPlayer.loadSource(this.livingUrl);
  573. this.hlsPlayer.attachMedia(video);
  574. this.hlsPlayer.on(Hls.Events.MANIFEST_PARSED, () => {
  575. video.play();
  576. });
  577. }
  578. }
  579. },
  580. // 视频错误处理
  581. videoError(e) {
  582. console.error('视频播放错误:', e.detail.errMsg);
  583. // 尝试重新加载或切换到备用流
  584. if (this.livingUrl) {
  585. setTimeout(() => {
  586. this.videoContext.play();
  587. }, 2000);
  588. }
  589. },
  590. getliving() {
  591. if (!this.liveId) return;
  592. console.log("获取直播信息");
  593. const param = {
  594. id: this.liveId
  595. };
  596. getlive(param).then(res => {
  597. if (res.code !== 200) {
  598. uni.showToast({
  599. title: res.msg,
  600. icon: 'none',
  601. duration: 2000
  602. });
  603. return;
  604. }
  605. this.livedata = res.data;
  606. this.showType = res.data.showType;
  607. this.storeId = res.storeId
  608. this.queryCollect()//查询店铺
  609. console.log("横屏1或竖屏2", this.showType)
  610. // 1. 根据直播类型设置播放地址
  611. if (res.data.liveType === 2) {
  612. // 回放视频
  613. this.videoUrl = res.data.videoUrl;
  614. this.autoplay = true;
  615. this.placeholderText = "观看回放中";
  616. this.talkdisabled = false;
  617. } else if (res.data.liveType === 1) {
  618. // 直播流
  619. this.livingUrl = res.data.flvHlsUrl;
  620. this.autoplay = true;
  621. this.placeholderText = "说点什么...";
  622. this.talkdisabled = false;
  623. } else {
  624. // 未开播
  625. this.livingUrl = "";
  626. this.autoplay = false;
  627. this.placeholderText = "直播未开始,暂时无法发言";
  628. this.talkdisabled = true;
  629. return;
  630. }
  631. // 2. 根据不同平台处理播放逻辑
  632. this.$nextTick(() => {
  633. if (!this.livingUrl) return;
  634. // H5 平台:使用 HLS.js 处理 .m3u8 流
  635. // #ifdef H5
  636. if (this.livingUrl.includes('.m3u8')) {
  637. this.initHlsPlayer();
  638. return;
  639. }
  640. // #endif
  641. // 小程序/App 平台:直接使用 video 组件
  642. if (!this.videoContext) {
  643. this.videoContext = uni.createVideoContext('myVideo', this);
  644. }
  645. if (res.data.liveType === 1) {
  646. // 回放:跳转到指定位置
  647. this.videoContext.seek(res.data.nowDuration || 0);
  648. } else {
  649. // 直播:直接播放
  650. this.videoContext.play();
  651. }
  652. });
  653. }).catch(err => {
  654. console.error("获取直播信息失败:", err);
  655. uni.showToast({
  656. title: "获取直播信息失败",
  657. icon: 'none'
  658. });
  659. });
  660. },
  661. maskString(str, maskChar = '*') {
  662. // 新增:如果str是undefined或null,直接返回空字符串
  663. if (!str) return '';
  664. // 确保str是字符串(如果是数字等类型,先转为字符串)
  665. const strVal = String(str);
  666. return strVal.split('').map((char, index) => (index === 0 ? char : maskChar)).join('');
  667. },
  668. getPureDecimal(num, precision = 6) {
  669. const decimalPart = Math.abs(num).toFixed(precision).split('.')[1];
  670. return decimalPart?.replace(/0+$/, '') || ''; // 移除末尾多余的0
  671. },
  672. // 返回上一个页面并关闭WebSocket
  673. goBack() {
  674. const pages = getCurrentPages();
  675. // 如果页面栈长度大于1,说明有上一页可以返回
  676. this.closeWebSocket();
  677. if (pages.length > 1) {
  678. uni.navigateBack();
  679. } else {
  680. // 如果没有上一页,跳转到默认页面(比如首页)
  681. uni.switchTab({
  682. url: '/pages/list/index' // 替换为你的首页路径
  683. });
  684. }
  685. },
  686. //直播间点赞、关注、在线人数数据
  687. getliveViewData() {
  688. if (!this.liveId) return;
  689. getLiveViewData(this.liveId).then(res => {
  690. if (res.code == 200) {
  691. // console.log("直播间点赞、关注、在线人数数据>>>>", res)
  692. this.liveViewData = res
  693. } else {
  694. uni.showToast({
  695. title: res.msg,
  696. icon: 'none'
  697. });
  698. }
  699. },
  700. rej => {}
  701. );
  702. },
  703. //正在购买
  704. getliveOrder() {
  705. if (!this.liveId) return;
  706. liveOrderUser(this.liveId).then(res => {
  707. if (res.code == 200) {
  708. console.log("正在购买>>>>", res)
  709. this.orderUser = res
  710. } else {
  711. uni.showToast({
  712. title: res.msg,
  713. icon: 'none'
  714. });
  715. }
  716. },
  717. rej => {}
  718. );
  719. },
  720. //小黄车 店铺展示
  721. // getliveStore() {
  722. // if (!this.liveId) return;
  723. // let data = {
  724. // pageSize: 10,
  725. // page: 1
  726. // }
  727. // liveStore(this.liveId, data).then(res => {
  728. // if (res.code == 200) {
  729. // console.log("小黄车 店铺展示>>>>", res)
  730. // this.products = res.data
  731. // this.queryCollect()
  732. // } else {
  733. // uni.showToast({
  734. // title: res.msg,
  735. // icon: 'none'
  736. // });
  737. // }
  738. // },
  739. // rej => {}
  740. // );
  741. // },
  742. // 获取直播间信息接口
  743. getLiveinformation() {
  744. if (!this.liveId) return;
  745. getLiveInfo(this.liveId).then(res => {
  746. if (res.code == 200) {
  747. console.log("获取直播间信息接口>>>>", res)
  748. this.livingUrl = res.livingUrl
  749. } else {
  750. uni.showToast({
  751. title: res.msg,
  752. icon: 'none'
  753. });
  754. }
  755. },
  756. rej => {}
  757. );
  758. },
  759. // 获取直播间用户
  760. getliveUser() {
  761. if (!this.liveId) return;
  762. getRecentLiveViewers(this.liveId).then(res => {
  763. if (res.code == 200) {
  764. console.log("获取直播间用户>>>>", res)
  765. this.liveViewers = res.recentLiveViewers
  766. } else {
  767. uni.showToast({
  768. title: res.msg,
  769. icon: 'none'
  770. });
  771. }
  772. },
  773. rej => {}
  774. );
  775. },
  776. // 点赞
  777. onLike() {
  778. if (!this.liveId) return;
  779. liveDataLike(this.liveId).then(res => {
  780. if (res.code == 200) {
  781. if (res.like) {
  782. this.liveViewData.like++
  783. } else {
  784. uni.showToast({
  785. title: res.msg,
  786. icon: 'none'
  787. });
  788. }
  789. } else {
  790. uni.showToast({
  791. title: res.msg,
  792. icon: 'none'
  793. });
  794. }
  795. },
  796. rej => {}
  797. );
  798. },
  799. // 去购买,跳商品详情
  800. goShop(productId, goodsId) {
  801. uni.navigateTo({
  802. url: '/pages_shop/goods?productId=' + productId + '&liveId=' + this.liveId + '&goodsId=' +
  803. goodsId + '&storeId=' + this.storeId
  804. })
  805. },
  806. // 查询店铺
  807. queryCollect() {
  808. if (!this.storeId) return;
  809. store(this.storeId).then(res => {
  810. if (res.code == 200) {
  811. console.log("查询店铺>>", res)
  812. this.products = res.data.goodsList
  813. this.store=res.data
  814. } else {
  815. uni.showToast({
  816. title: res.msg,
  817. icon: 'none'
  818. });
  819. }
  820. },
  821. rej => {}
  822. );
  823. },
  824. // 店铺收藏
  825. onStoreCollect() {
  826. if (!this.storeId) return;
  827. collectStore(this.products[0].storeId).then(res => {
  828. if (res.code == 200) {
  829. uni.showToast({
  830. title: res.msg,
  831. icon: 'none'
  832. });
  833. this.store.isFavorite = !this.store.isFavorite
  834. } else {
  835. uni.showToast({
  836. title: res.msg,
  837. icon: 'none'
  838. });
  839. }
  840. },
  841. rej => {}
  842. );
  843. },
  844. // 商品收藏
  845. onGoodsCollect(item) {
  846. if (!item || item.length === 0 || !item.goodsId) {
  847. return;
  848. }
  849. collectGoods(item.goodsId).then(res => {
  850. if (res.code == 200) {
  851. uni.showToast({
  852. title: res.msg,
  853. icon: 'none'
  854. });
  855. item.isFavorite = !item.isFavorite
  856. } else {
  857. uni.showToast({
  858. title: res.msg,
  859. icon: 'none'
  860. });
  861. }
  862. },
  863. rej => {}
  864. );
  865. },
  866. // 关注
  867. onFollow() {
  868. if (!this.liveId) return;
  869. follow(this.liveId).then(res => {
  870. this.isFollow = !this.isFollow
  871. uni.showToast({
  872. title: res.msg,
  873. icon: 'none'
  874. });
  875. },
  876. rej => {}
  877. );
  878. },
  879. // 时间戳
  880. initTime() {
  881. const now = new Date();
  882. this.timestamp = now.getTime(); // 例如:'2023-04-01 12:00:00'
  883. },
  884. // initWebSocket() {
  885. // const liveWS = new LiveWS('ws://your-server.com', 123, 456);
  886. // // 从 URL 中解析 timestamp
  887. // const urlParams = new URL(liveWS.url).searchParams;
  888. // this.timestamp = urlParams.get('timestamp');
  889. // // console.log('Timestamp:', timestamp);
  890. // },
  891. // 弹幕滚动
  892. lowerChat: function(e) {
  893. console.log(e)
  894. },
  895. scroll: function(e) {
  896. console.log(e)
  897. this.old.scrollTop = e.detail.scrollTop
  898. },
  899. loadmore() {
  900. // for (let i = 0; i < 30; i++) {
  901. // this.indexList.push({
  902. // url: this.shopList[uni.$u.random(0, this.shopList.length - 1)],
  903. // });
  904. // }
  905. },
  906. // 弹幕
  907. scrollchat() {
  908. // this.loadmore();
  909. },
  910. noredanswer() {
  911. if (this.Answerlistall > 0) {
  912. this.showAnswer = !this.this.showAnswer
  913. } else {
  914. uni.showToast({
  915. title: '暂无题目',
  916. icon: 'none',
  917. });
  918. }
  919. },
  920. redbagAnswer() {
  921. this.showAnswerred = !this.showAnswerred
  922. this.answerbtn = !this.answerbtn
  923. },
  924. submitAnswers() {
  925. if (this.isSubmit) return;
  926. this.isSubmit = true;
  927. const data = {
  928. questionId: this.answerlist.id,
  929. answer: this.checkboxFormValue
  930. }
  931. submitAnswer(data).then(res => {
  932. if (res.code == 200) {
  933. uni.showToast({
  934. title: res.msg,
  935. icon: 'none',
  936. });
  937. }
  938. // 本地切换下一题
  939. if (this.answerfrist < this.Answerlistall - 1) {
  940. this.answerfrist++;
  941. this.answerlist = this.allAnswerLists[this.answerfrist];
  942. this.answerlist.content = JSON.parse(this.answerlist.content);
  943. } else {
  944. uni.showToast({
  945. title: '已是最后一题',
  946. icon: 'none'
  947. });
  948. this.showAnswer = false; // 自动关闭弹窗
  949. }
  950. this.checkboxValue = []
  951. this.checkboxFormValue = "";
  952. uni.showToast({
  953. title: res.msg,
  954. icon: 'none'
  955. });
  956. }).finally(e => {
  957. this.isSubmit = false;
  958. })
  959. },
  960. // handleCheckboxSelect(value) {
  961. // const index = this.checkboxValue.indexOf(value)
  962. // console.log(value)
  963. // if (this.answerlist.type == 1) {
  964. // this.checkboxValue = [value]
  965. // this.checkboxFormValue = this.checkboxValue.join(',')
  966. // setTimeout(() => {
  967. // uni.showToast({
  968. // title: '准备下一题',
  969. // icon: 'none',
  970. // duration: 2000
  971. // });
  972. // }, 1000);
  973. // this.submitAnswers()
  974. // console.log(this.checkboxValue)
  975. // } else if (this.answerlist.type == 2) {
  976. // if (index > -1) {
  977. // this.checkboxValue.splice(index, 1)
  978. // this.checkboxFormValue = this.checkboxValue.join(',')
  979. // } else {
  980. // this.checkboxValue.push(value)
  981. // this.checkboxFormValue = this.checkboxValue.join(',')
  982. // }
  983. // console.log(this.checkboxFormValue)
  984. // }
  985. // },
  986. getAnswerlists() {
  987. console.log(123)
  988. if (!this.liveId) return;
  989. const data = {
  990. liveId: this.liveId
  991. }
  992. getAnswerlist(data).then(res => {
  993. if (res.code == 200) {
  994. if (res.data.length > 0) {
  995. this.allAnswerLists = res.data; // 存储所有题目
  996. this.Answerlistall = res.data.length;
  997. if (this.allAnswerLists.length > 0) {
  998. this.answerlist = this.allAnswerLists[0];
  999. this.answerlist.content = JSON.parse(this.allAnswerLists[0].content);
  1000. }
  1001. this.showAnswer = true
  1002. } else {
  1003. this.showAnswer = false
  1004. }
  1005. }
  1006. })
  1007. },
  1008. gettalklist() {
  1009. if (!this.liveId) return;
  1010. const param = {
  1011. id: this.liveId
  1012. }
  1013. gettextlist(param).then(res => {
  1014. if (res.code == 200) {
  1015. this.talklist = res.data
  1016. // console.log(res.data);
  1017. this.$nextTick(() => {
  1018. this.scrollIntoView = `list_${this.talklist.length-1}`
  1019. })
  1020. }
  1021. })
  1022. },
  1023. open() {
  1024. },
  1025. close() {
  1026. this.showadd = !this.showadd
  1027. },
  1028. closes() {
  1029. this.showziliao = !this.showziliao
  1030. },
  1031. closest() {
  1032. this.showAnswer = !this.showAnswer
  1033. },
  1034. closestred() {
  1035. this.showAnswerred = !this.showAnswerred
  1036. },
  1037. closeanswer() {
  1038. this.answerbtn = !this.answerbtn
  1039. },
  1040. // 关闭小黄车
  1041. closeshop() {
  1042. this.shopping = !this.shopping
  1043. },
  1044. longPress() {
  1045. this.timeout = setTimeout(() => {
  1046. this.isLongPress = true;
  1047. // 执行保存图片的操作
  1048. uni.saveImageToPhotosAlbum({
  1049. filePath: this.livedata.qwQrCode, // 图片的本地路径或网络路径
  1050. success: () => {
  1051. uni.showToast({
  1052. title: '保存成功'
  1053. });
  1054. },
  1055. fail: () => {
  1056. uni.showToast({
  1057. title: '',
  1058. icon: 'none'
  1059. });
  1060. }
  1061. });
  1062. }, 500); // 设置长按的阈值时间,这里是500毫秒
  1063. },
  1064. cancelLongPress() {
  1065. clearTimeout(this.timeout);
  1066. this.isLongPress = false;
  1067. },
  1068. // 触摸开始
  1069. handleTouchStart() {
  1070. this.isZoom = true; // 触发放大效果
  1071. },
  1072. // 触摸结束
  1073. handleTouchEnd() {
  1074. this.isZoom = false; // 恢复原状
  1075. },
  1076. getEWechatSdk() {
  1077. // 只在H5平台执行
  1078. // #ifdef H5
  1079. let eWechatSdk = '';
  1080. if (/(Android)/i.test(navigator.userAgent)) {
  1081. eWechatSdk = 'jWeixin';
  1082. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  1083. eWechatSdk = 'wx';
  1084. } else {
  1085. eWechatSdk = 'jWeixin';
  1086. }
  1087. uni.setStorageSync("wxSdk", eWechatSdk);
  1088. // #endif
  1089. },
  1090. closeWebSocket() {
  1091. clearInterval(this.pingpangTimes);
  1092. clearTimeout(this.reconnectTimer);
  1093. if (socket && isSocketOpen) {
  1094. try {
  1095. uni.closeSocket();
  1096. isSocketOpen = false;
  1097. console.log('WebSocket已主动关闭');
  1098. } catch (e) {
  1099. console.error('关闭WebSocket时出错:', e);
  1100. }
  1101. }
  1102. },
  1103. reConnect() {
  1104. var that = this;
  1105. try {
  1106. uni.closeSocket();
  1107. } catch (e) {
  1108. }
  1109. setTimeout(function() {
  1110. that.initSocket();
  1111. }, 10000);
  1112. },
  1113. startHeartbeat() {
  1114. clearInterval(this.pingpangTimes);
  1115. // 实现心跳检测逻辑
  1116. console.log('开始心跳检测');
  1117. this.pingpangTimes = setInterval(() => {
  1118. if (isSocketOpen) {
  1119. const pingData = {
  1120. cmd: 'ping',
  1121. timestamp: new Date().getTime()
  1122. };
  1123. socket.send({
  1124. data: JSON.stringify(pingData),
  1125. success: () => {
  1126. console.log('心跳发送成功');
  1127. },
  1128. fail: (err) => {
  1129. console.error('心跳发送失败:', err);
  1130. // 心跳失败也触发重连
  1131. this.scheduleReconnect();
  1132. }
  1133. });
  1134. }
  1135. }, 30000);
  1136. },
  1137. initSocket() {
  1138. if (isSocketOpen) {
  1139. console.log('WebSocket 已连接,无需重复初始化');
  1140. return;
  1141. }
  1142. if (!this.liveId) {
  1143. console.warn('liveId为空,不尝试WebSocket连接');
  1144. return;
  1145. }
  1146. if (this.reconnectCount >= this.maxReconnectAttempts) {
  1147. console.log('已达到最大重连次数,不再尝试连接');
  1148. uni.showToast({
  1149. title: '连接失败,请刷新页面重试',
  1150. icon: 'none'
  1151. });
  1152. return;
  1153. }
  1154. this.userinfo = JSON.parse(uni.getStorageSync("userInfo") || '{}');
  1155. if (!this.userinfo.userId) {
  1156. console.warn('用户信息不存在,不尝试WebSocket连接');
  1157. return;
  1158. }
  1159. // 先清除之前的连接和心跳
  1160. this.closeWebSocket();
  1161. let signature = CryptoJS.HmacSHA256(
  1162. CryptoJS.enc.Utf8.parse(this.liveId?.toString() + this.userinfo.userId + this.userType + this
  1163. .timestamp),
  1164. CryptoJS.enc.Utf8.parse(this.timestamp)).toString(CryptoJS.enc.Hex);
  1165. const that = this;
  1166. // 创建一个新的socket连接
  1167. socket = uni.connectSocket({
  1168. url: wsUrl + "?userId=" + this.userinfo.userId + "&liveId=" + this.liveId + "&userType=" + this
  1169. .userType +
  1170. "&timestamp=" + this.timestamp + "&signature=" + signature,
  1171. multiple: true,
  1172. success: res => {
  1173. this.reconnectCount++; // 增加重连计数
  1174. // 清除之前的心跳定时器
  1175. clearInterval(this.pingpangTimes);
  1176. uni.onSocketMessage((res) => {
  1177. if (res.data.code == 500) {
  1178. uni.showToast({
  1179. title: res.data.msg,
  1180. icon: 'none',
  1181. duration: 2000
  1182. });
  1183. }
  1184. const redata = JSON.parse(res.data);
  1185. console.log("WebSocket拿到的东西", redata)
  1186. this.talklist.push(redata.data)
  1187. this.$nextTick(() => {
  1188. this.scrollIntoView = `list_${this.talklist.length-1}`
  1189. })
  1190. if (redata.cmd == 'deleteId') {
  1191. uni.$emit('deleteId');
  1192. } else if (redata.cmd == 'init') {
  1193. uni.$emit('init', redata.data);
  1194. } else if (redata.cmd == 'reload') {
  1195. uni.$emit('reload');
  1196. } else if (redata.data.cmd == 'sendRedPacketQuestion') {
  1197. const list = JSON.parse(redata.data.data)
  1198. this.redanswerAll = [...this.redanswerAll, ...list]
  1199. if (this.redanswerAll[1].randomAmount !== null) {
  1200. this.redanswertips = JSON.parse(this.redanswerAll[0].randomAmount)
  1201. }
  1202. console.log(this.redanswertips)
  1203. console.log(this.redanswerAll)
  1204. } else if (redata.data.cmd == 'entry') {
  1205. this.showWelcomeMessage = true
  1206. // setTimeout(() => {
  1207. // this.showWelcomeMessage = false;
  1208. // }, 1000);
  1209. uni.$emit('entry', redata.data);
  1210. }
  1211. })
  1212. },
  1213. fail: res => {
  1214. uni.$emit('websocket', 0);
  1215. console.log(res);
  1216. this.scheduleReconnect();
  1217. },
  1218. })
  1219. //监听socket打开
  1220. uni.onSocketOpen(() => {
  1221. isSocketOpen = true;
  1222. this.reconnectCount = 0; // 重置重连计数器
  1223. console.log('WebSocket 连接成功');
  1224. this.startHeartbeat();
  1225. });
  1226. //监听socket关闭
  1227. uni.onSocketClose(() => {
  1228. isSocketOpen = false;
  1229. console.log('WebSocket连接已关闭!');
  1230. this.scheduleReconnect();
  1231. });
  1232. //监听socket错误
  1233. uni.onSocketError((err) => {
  1234. isSocketOpen = false;
  1235. console.error('WebSocket 连接错误:', err);
  1236. this.scheduleReconnect();
  1237. });
  1238. },
  1239. scheduleReconnect() {
  1240. if (this.reconnectCount >= this.maxReconnectAttempts) {
  1241. console.log('已达到最大重连次数,不再尝试连接');
  1242. uni.showToast({
  1243. title: '连接失败,请刷新页面重试',
  1244. icon: 'none'
  1245. });
  1246. return;
  1247. }
  1248. // 清除之前的定时器和连接
  1249. clearTimeout(this.reconnectTimer);
  1250. this.closeWebSocket();
  1251. // 指数退避策略,重连间隔逐渐增加
  1252. const delay = Math.min(1000 * Math.pow(2, this.reconnectCount), 30000);
  1253. console.log(`将在 ${delay}ms 后尝试第 ${this.reconnectCount + 1} 次重连`);
  1254. this.reconnectTimer = setTimeout(() => {
  1255. this.initSocket();
  1256. }, delay);
  1257. },
  1258. sendMsg() {
  1259. if (isSocketOpen) {
  1260. const data = {
  1261. liveId: this.liveId,
  1262. userId: this.userinfo.userId,
  1263. userType: 0,
  1264. cmd: "sendMsg",
  1265. msg: this.value,
  1266. nickName: this.userinfo.nickName,
  1267. avatar: this.userinfo.avatar
  1268. }
  1269. if (this.value == "") {
  1270. uni.showToast({
  1271. title: "不能发送空消息",
  1272. icon: 'none',
  1273. });
  1274. } else {
  1275. socket.send({
  1276. data: JSON.stringify(data),
  1277. success: () => {
  1278. console.log("发送成功")
  1279. this.value = ''
  1280. },
  1281. fail: () => {
  1282. console.log("发送失败")
  1283. }
  1284. })
  1285. }
  1286. }
  1287. },
  1288. },
  1289. };
  1290. </script>
  1291. <style scoped lang="scss">
  1292. /* button自带样式清除 */
  1293. .student-orther-icon button::after {
  1294. border: none !important;
  1295. padding: 0 !important;
  1296. margin: 0 !important;
  1297. }
  1298. .student-orther-icon button {
  1299. background-color: transparent !important;
  1300. padding: 0 !important;
  1301. line-height: inherit !important;
  1302. margin: 0 !important;
  1303. width: auto !important;
  1304. font-weight: 500 !important;
  1305. border-radius: none !important;
  1306. }
  1307. // 抽奖
  1308. // .answerpop {
  1309. // background: linear-gradient(to right, #fff7f8, #fee1e2);
  1310. // margin-top: 30rpx;
  1311. // padding: 10rpx 20rpx;
  1312. // width: calc(100% - 40rpx);
  1313. // border-radius: 20rpx;
  1314. // display: flex;
  1315. // align-items: center;
  1316. // justify-content: space-between;
  1317. // box-shadow: 2px 2px 4px 0 rgba(255, 124, 126, 0.1);
  1318. // .answera {
  1319. // display: flex;
  1320. // justify-content: center;
  1321. // background-color: #fff;
  1322. // padding: 20rpx 0;
  1323. // width: 35%;
  1324. // border-radius: 40rpx;
  1325. // align-items: center;
  1326. // margin: 10rpx 0;
  1327. // box-shadow: 2px 2px 4px 0 rgba(72, 72, 72, 0.1);
  1328. // image {
  1329. // width: 40rpx;
  1330. // height: 40rpx;
  1331. // margin-right: 10rpx;
  1332. // }
  1333. // }
  1334. // }
  1335. // .submitbtn {
  1336. // width: 260rpx;
  1337. // margin: 0 auto;
  1338. // text-align: center;
  1339. // border-radius: 80rpx;
  1340. // padding: 20rpx 0;
  1341. // color: #fff;
  1342. // background-color: #ff5c03;
  1343. // }
  1344. // .itemanswer {
  1345. // padding: 20rpx 20rpx;
  1346. // text-align: center;
  1347. // border-radius: 80rpx;
  1348. // border: 2rpx solid #dddddd;
  1349. // margin: 12rpx 0;
  1350. // color: #555;
  1351. // width: calc(100% - 40rpx);
  1352. // }
  1353. // .answeract {
  1354. // background-color: rgba(0, 202, 166, 1);
  1355. // color: #fff;
  1356. // }
  1357. // .answerbox {
  1358. // width: 600rpx;
  1359. // border-radius: 20rpx;
  1360. // }
  1361. .welcome-message {
  1362. position: fixed;
  1363. width: 100%;
  1364. bottom: 120rpx;
  1365. left: 50%;
  1366. transform: translateX(-50%);
  1367. color: white;
  1368. padding: 10px 20px;
  1369. border-radius: 20px;
  1370. animation: fadeOut 1s ease 1s forwards;
  1371. z-index: 1000;
  1372. }
  1373. @keyframes fadeOut {
  1374. from {
  1375. opacity: 1;
  1376. }
  1377. to {
  1378. opacity: 0;
  1379. }
  1380. }
  1381. // .container {
  1382. // position: relative;
  1383. // width: 100%;
  1384. // height: 100vh;
  1385. // overflow: hidden;
  1386. // }
  1387. // .talktext {
  1388. // border-radius: 8rpx;
  1389. // background-color: rgba(255, 255, 255, 0.1);
  1390. // view {
  1391. // width: 100%;
  1392. // }
  1393. // }
  1394. .talk-list {
  1395. border-radius: 30rpx;
  1396. background-color: rgba(33, 33, 33, 0.5);
  1397. padding: 10rpx 30rpx;
  1398. }
  1399. // .zoom-button-active {
  1400. // transform: scale(1.5);
  1401. // }
  1402. // .background-image {
  1403. // position: absolute;
  1404. // top: 0;
  1405. // left: -40rpx;
  1406. // width: 110%;
  1407. // height: 110%;
  1408. // object-fit: cover;
  1409. // filter: blur(20px);
  1410. // z-index: 0;
  1411. // }
  1412. // .background-images {
  1413. // position: absolute;
  1414. // top: 0;
  1415. // left: -40rpx;
  1416. // width: 110%;
  1417. // height: 110%;
  1418. // object-fit: cover;
  1419. // filter: blur(20px);
  1420. // z-index: 6;
  1421. // }
  1422. // .blackbg {
  1423. // position: absolute;
  1424. // top: 0;
  1425. // left: 0;
  1426. // width: 100%;
  1427. // height: 100%;
  1428. // background: rgba(0, 0, 0, 0.7);
  1429. // object-fit: cover;
  1430. // filter: blur(10px);
  1431. // z-index: 1;
  1432. // }
  1433. .content {
  1434. position: relative;
  1435. z-index: 2;
  1436. height: 100%;
  1437. width: 100%;
  1438. top: 0;
  1439. left: 0;
  1440. display: flex;
  1441. flex-direction: column;
  1442. justify-content: space-between;
  1443. .content-top {
  1444. width: 100%;
  1445. margin-top: 150rpx;
  1446. display: flex;
  1447. align-items: center;
  1448. justify-content: space-between;
  1449. padding: 0 24rpx;
  1450. box-sizing: border-box;
  1451. .sum {
  1452. width: 80rpx;
  1453. height: 52rpx;
  1454. background: rgba(0, 0, 0, 0.5);
  1455. border-radius: 26rpx 26rpx 26rpx 26rpx;
  1456. font-size: 24rpx;
  1457. color: #FFFFFF;
  1458. text-align: center;
  1459. line-height: 52rpx;
  1460. }
  1461. }
  1462. .follow-btn {
  1463. padding: 8rpx 16rpx;
  1464. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  1465. border-radius: 26rpx;
  1466. font-weight: 500;
  1467. font-size: 26rpx;
  1468. color: #FFFFFF;
  1469. }
  1470. }
  1471. .videolist {
  1472. position: relative;
  1473. }
  1474. .video {
  1475. height: 100vh;
  1476. /* 占屏幕高度的80% */
  1477. width: 100%;
  1478. background-color: rgba(0, 0, 0, 0.8);
  1479. }
  1480. .videotop {
  1481. width: 100%;
  1482. height: 100%;
  1483. }
  1484. .video_row {
  1485. width: 100%;
  1486. max-height: 500rpx;
  1487. overflow: hidden;
  1488. margin-top: 300rpx;
  1489. }
  1490. .popup-video {
  1491. position: absolute;
  1492. top: 30%;
  1493. height: 500rpx;
  1494. display: flex;
  1495. flex-direction: column;
  1496. align-items: center;
  1497. justify-content: center;
  1498. width: 100%;
  1499. color: #fff;
  1500. z-index: 9;
  1501. .more {
  1502. background-color: #3280fe;
  1503. border-radius: 80rpx;
  1504. width: 280rpx;
  1505. text-align: center;
  1506. height: 60rpx;
  1507. line-height: 60rpx;
  1508. }
  1509. }
  1510. .icon-bg {
  1511. background-color: rgba(157, 157, 157, 0.8);
  1512. border-radius: 50%;
  1513. width: 72rpx;
  1514. height: 72rpx;
  1515. display: flex;
  1516. justify-content: center;
  1517. align-items: center;
  1518. transition: transform 0.2s ease;
  1519. }
  1520. .list {
  1521. width: 80%;
  1522. margin-bottom: 20rpx;
  1523. animation: xxxawdawd .2s;
  1524. }
  1525. @keyframes xxxawdawd {
  1526. from {
  1527. margin-top: 0rpx;
  1528. opacity: 0;
  1529. }
  1530. to {
  1531. margin-top: 20rpx;
  1532. opacity: 1;
  1533. }
  1534. }
  1535. .shop-prompt {
  1536. position: absolute;
  1537. bottom: 600rpx;
  1538. left: 24rpx;
  1539. padding: 6rpx 20rpx;
  1540. background: rgba(230, 154, 34, 0.7);
  1541. border-radius: 24rpx;
  1542. z-index: 9;
  1543. font-weight: 500;
  1544. font-size: 26rpx;
  1545. color: #FFFFFF;
  1546. transition: opacity 0.3s ease;
  1547. }
  1548. .siderow-group {
  1549. position: absolute;
  1550. top: 21%;
  1551. right: 50rpx;
  1552. z-index: 9;
  1553. display: flex;
  1554. flex-direction: column;
  1555. align-items: center;
  1556. .side-item {
  1557. font-weight: 500;
  1558. font-size: 24rpx;
  1559. color: #FFFFFF;
  1560. margin-bottom: 32rpx;
  1561. text-align: center;
  1562. button {
  1563. background-color: transparent;
  1564. margin: 0;
  1565. line-height: 1;
  1566. padding: 0;
  1567. }
  1568. image {
  1569. width: 72rpx;
  1570. height: 72rpx;
  1571. }
  1572. }
  1573. }
  1574. .side-group {
  1575. position: absolute;
  1576. top: 30%;
  1577. right: 50rpx;
  1578. z-index: 9;
  1579. display: flex;
  1580. flex-direction: column;
  1581. align-items: center;
  1582. .side-item {
  1583. font-weight: 500;
  1584. font-size: 24rpx;
  1585. color: #FFFFFF;
  1586. margin-bottom: 32rpx;
  1587. text-align: center;
  1588. button {
  1589. background-color: transparent;
  1590. margin: 0;
  1591. line-height: 1;
  1592. padding: 0;
  1593. }
  1594. image {
  1595. width: 72rpx;
  1596. height: 72rpx;
  1597. }
  1598. }
  1599. }
  1600. .shoppop {
  1601. padding: 22rpx 16rpx;
  1602. .shoppop-top {
  1603. display: flex;
  1604. justify-content: space-between;
  1605. align-items: center;
  1606. padding: 0 16rpx 22rpx;
  1607. .search-input {
  1608. width: 414rpx;
  1609. height: 76rpx;
  1610. background: #FFFFFF;
  1611. border-radius: 36rpx;
  1612. margin-left: 20rpx;
  1613. padding: 0 32rpx;
  1614. box-sizing: border-box;
  1615. font-size: 24rpx;
  1616. margin-right: 24rpx;
  1617. }
  1618. .search-top {
  1619. font-size: 18rpx;
  1620. color: #222222;
  1621. }
  1622. }
  1623. .shop-list {
  1624. overflow: hidden;
  1625. .list-item {
  1626. display: flex;
  1627. align-items: center;
  1628. padding: 20rpx 16rpx;
  1629. background: #FFFFFF;
  1630. border-radius: 16rpx;
  1631. margin-bottom: 16rpx;
  1632. .goods-img {
  1633. width: 200rpx;
  1634. height: 200rpx;
  1635. border-radius: 16rpx;
  1636. overflow: hidden;
  1637. position: relative;
  1638. margin-right: 24rpx;
  1639. image {
  1640. width: 100%;
  1641. height: 100%;
  1642. }
  1643. .goods-label {
  1644. position: absolute;
  1645. top: 0;
  1646. width: 64rpx;
  1647. height: 40rpx;
  1648. background: rgba(0, 0, 0, 0.5);
  1649. border-radius: 16rpx 0rpx 16rpx 0rpx;
  1650. text-align: center;
  1651. font-weight: 500;
  1652. font-size: 28rpx;
  1653. color: #FFFFFF;
  1654. }
  1655. }
  1656. .goods-right {
  1657. flex: 1;
  1658. .goods-title {
  1659. font-weight: 500;
  1660. font-size: 28rpx;
  1661. color: #000000;
  1662. }
  1663. .goods-details {
  1664. font-size: 24rpx;
  1665. color: #999999;
  1666. margin: 10rpx 0 20rpx;
  1667. }
  1668. .goods-people {
  1669. font-size: 22rpx;
  1670. color: #E69A22;
  1671. height: 56rpx;
  1672. }
  1673. .goods-shop {
  1674. display: flex;
  1675. justify-content: space-between;
  1676. .nummber {
  1677. color: #FF5C03;
  1678. font-size: 22rpx;
  1679. font-weight: 500;
  1680. }
  1681. .btn-group {
  1682. text-align: center;
  1683. line-height: 56rpx;
  1684. .collect-btn {
  1685. width: 72rpx;
  1686. background: #F5F7FA;
  1687. border-radius: 8rpx 0rpx 0rpx 8rpx;
  1688. }
  1689. .shop-btn {
  1690. width: 152rpx;
  1691. background: linear-gradient(270deg, #FF5C03 0%, #FFAC64 100%);
  1692. border-radius: 0rpx 8rpx 8rpx 0rpx;
  1693. font-weight: 500;
  1694. font-size: 26rpx;
  1695. color: #FFFFFF;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. }
  1701. }
  1702. }
  1703. :deep(.u-list-item) {
  1704. width: 100%;
  1705. display: flex;
  1706. align-items: center;
  1707. }
  1708. </style>