living.vue 48 KB

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