index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <div class="h5-room-wrap">
  3. <div class="head">
  4. <div class="left">
  5. <div
  6. class="avatar"
  7. :style="{
  8. backgroundImage: `url(${anchorInfo?.avatar})`,
  9. }"
  10. ></div>
  11. <div class="username">
  12. {{ anchorInfo?.username }}
  13. </div>
  14. </div>
  15. <div class="right">
  16. <div
  17. class="btn"
  18. @click="router.push({ name: mobileRouterName.h5 })"
  19. >
  20. 返回主页
  21. </div>
  22. </div>
  23. </div>
  24. <div
  25. v-loading="videoLoading"
  26. class="video-wrap"
  27. ref="videoWrapTmpRef"
  28. :style="{
  29. height: videoWrapHeight + 'px',
  30. '--max-height': videoWrapHeight + 'px',
  31. }"
  32. >
  33. <div
  34. class="cover"
  35. :style="{
  36. backgroundImage: `url(${appStore.liveRoomInfo?.cover_img})`,
  37. }"
  38. ></div>
  39. <div
  40. v-if="!roomLiving"
  41. class="no-live"
  42. >
  43. 主播还没开播~
  44. </div>
  45. <div
  46. class="media-list"
  47. ref="remoteVideoRef"
  48. :class="{ item: appStore.allTrack.length > 1 }"
  49. ></div>
  50. <div
  51. v-if="showPlayBtn && roomLiving && appStore.liveRoomInfo"
  52. class="tip-btn"
  53. @click="startPull"
  54. >
  55. 点击播放
  56. </div>
  57. <VideoControls
  58. v-else
  59. :resolution="videoHeight"
  60. @refresh="handleRefresh"
  61. ></VideoControls>
  62. </div>
  63. <div class="n-tab-wrap">
  64. <n-tabs
  65. type="line"
  66. animated
  67. >
  68. <n-tab-pane
  69. name="danmu"
  70. tab="聊天"
  71. >
  72. <div class="danmu-list">
  73. <div
  74. ref="danmuListRef"
  75. class="list"
  76. :style="{ height: containerHeight + 'px' }"
  77. >
  78. <div
  79. v-for="(item, index) in damuList"
  80. :key="index"
  81. class="item"
  82. >
  83. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  84. <span class="time"
  85. >[{{ formatTimeHour(item.send_msg_time) }}]</span
  86. >
  87. <span class="name">
  88. <span v-if="item.userInfo">
  89. <span>{{ item.userInfo.username }}</span>
  90. <span v-if="MODULE_CONFIG_SWITCH.pullShowAuth">
  91. [{{
  92. item.userInfo.roles?.map((v) => v.role_name).join()
  93. }}]
  94. </span>
  95. </span>
  96. <span v-else>
  97. <span>{{ item.socket_id }}</span>
  98. <span v-if="MODULE_CONFIG_SWITCH.pullShowAuth">
  99. [游客]
  100. </span>
  101. </span>
  102. <span>:</span>
  103. </span>
  104. <span
  105. class="msg"
  106. v-if="item.msgIsFile === WsMessageMsgIsFileEnum.no"
  107. >
  108. {{ item.msg }}
  109. </span>
  110. <div
  111. class="msg img"
  112. v-else
  113. >
  114. <img
  115. :src="item.msg"
  116. alt=""
  117. @load="handleScrollTop"
  118. />
  119. </div>
  120. </template>
  121. <template
  122. v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin"
  123. >
  124. <span class="name system">系统通知:</span>
  125. <span class="msg">
  126. {{ item.userInfo?.username || item.socket_id }}进入直播!
  127. </span>
  128. </template>
  129. <template
  130. v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved"
  131. >
  132. <span class="name system">系统通知:</span>
  133. <span class="msg">
  134. {{ item.userInfo?.username || item.socket_id }}离开直播!
  135. </span>
  136. </template>
  137. </div>
  138. </div>
  139. </div>
  140. </n-tab-pane>
  141. <n-tab-pane
  142. name="customerService"
  143. tab="客服"
  144. >
  145. <div
  146. class="customerService-wrap"
  147. :style="{ height: containerHeight + 'px' }"
  148. >
  149. <img
  150. class="qrcode"
  151. v-if="frontendWechatQrcode !== ''"
  152. :src="frontendWechatQrcode"
  153. alt=""
  154. />
  155. <div class="tip">打开微信扫一扫添加客服</div>
  156. </div>
  157. </n-tab-pane>
  158. <n-tab-pane
  159. name="liveRoomInfo"
  160. tab="直播间信息"
  161. >
  162. <div
  163. class="liveRoomInfo-wrap"
  164. :style="{ height: containerHeight + 'px' }"
  165. >
  166. <div>直播间名称:{{ appStore.liveRoomInfo?.name }}</div>
  167. <div>直播间简介:{{ appStore.liveRoomInfo?.desc }}</div>
  168. <div>
  169. 直播间分区:{{
  170. appStore.liveRoomInfo?.areas?.[0].name || '暂无分区'
  171. }}
  172. </div>
  173. </div>
  174. </n-tab-pane>
  175. </n-tabs>
  176. </div>
  177. <div
  178. ref="bottomRef"
  179. class="send-msg"
  180. >
  181. <div
  182. class="emoji-list"
  183. v-if="showEmoji"
  184. >
  185. <div
  186. class="item"
  187. v-for="(item, index) in emojiArray"
  188. :key="index"
  189. @click="handlePushStr(item)"
  190. >
  191. {{ item }}
  192. </div>
  193. </div>
  194. <div
  195. class="face"
  196. @click="showEmoji = !showEmoji"
  197. ></div>
  198. <input
  199. v-model="danmuStr"
  200. class="ipt"
  201. placeholder="发个弹幕吧~"
  202. @keydown="keydownDanmu"
  203. />
  204. <n-button
  205. type="info"
  206. size="small"
  207. :color="THEME_COLOR"
  208. @click="sendDanmu"
  209. >
  210. 发送
  211. </n-button>
  212. </div>
  213. <LoginModal v-if="appStore.showLoginModal"></LoginModal>
  214. </div>
  215. </template>
  216. <script lang="ts" setup>
  217. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
  218. import { useRoute } from 'vue-router';
  219. import { fetchFindLiveConfigByKey } from '@/api/liveConfig';
  220. import { fetchFindLiveRoom } from '@/api/liveRoom';
  221. import { MODULE_CONFIG_SWITCH, THEME_COLOR } from '@/constant';
  222. import { emojiArray } from '@/emoji';
  223. import { usePull } from '@/hooks/use-pull';
  224. import { DanmuMsgTypeEnum, WsMessageMsgIsFileEnum } from '@/interface';
  225. import router, { mobileRouterName } from '@/router';
  226. import { useAppStore } from '@/store/app';
  227. import { usePiniaCacheStore } from '@/store/cache';
  228. import { LiveRoomTypeEnum } from '@/types/ILiveRoom';
  229. import { formatTimeHour } from '@/utils';
  230. const route = useRoute();
  231. const cacheStore = usePiniaCacheStore();
  232. const appStore = useAppStore();
  233. const videoWrapTmpRef = ref<HTMLDivElement>();
  234. const bottomRef = ref<HTMLDivElement>();
  235. const danmuListRef = ref<HTMLDivElement>();
  236. const showPlayBtn = ref(false);
  237. const showEmoji = ref(false);
  238. const containerHeight = ref(0);
  239. const videoWrapHeight = ref(0);
  240. const frontendWechatQrcode = ref('');
  241. const remoteVideoRef = ref<HTMLDivElement>();
  242. const roomId = ref(route.params.roomId as string);
  243. const {
  244. videoWrapRef,
  245. handlePlay,
  246. initPull,
  247. keydownDanmu,
  248. sendDanmu,
  249. closeRtc,
  250. closeWs,
  251. handleSendGetLiveUser,
  252. isPlaying,
  253. autoplayVal,
  254. videoLoading,
  255. damuList,
  256. danmuStr,
  257. roomLiving,
  258. anchorInfo,
  259. remoteVideo,
  260. videoHeight,
  261. } = usePull(roomId.value);
  262. onUnmounted(() => {
  263. closeWs();
  264. closeRtc();
  265. appStore.showLoginModal = false;
  266. });
  267. onMounted(() => {
  268. videoWrapRef.value = videoWrapTmpRef.value;
  269. getWechatQrcode();
  270. setTimeout(() => {
  271. scrollTo(0, 0);
  272. }, 100);
  273. videoWrapHeight.value =
  274. document.documentElement.clientWidth / appStore.videoRatio;
  275. nextTick(() => {
  276. if (danmuListRef.value && bottomRef.value) {
  277. const res =
  278. bottomRef.value.getBoundingClientRect().top -
  279. danmuListRef.value.getBoundingClientRect().top;
  280. containerHeight.value = res;
  281. }
  282. });
  283. getLiveRoomInfo();
  284. handleSendGetLiveUser(Number(roomId.value));
  285. });
  286. function handlePushStr(str) {
  287. danmuStr.value += str;
  288. showEmoji.value = false;
  289. }
  290. watch(
  291. () => remoteVideo.value,
  292. (newVal) => {
  293. newVal.forEach((item) => {
  294. remoteVideoRef.value?.appendChild(item);
  295. });
  296. },
  297. {
  298. deep: true,
  299. immediate: true,
  300. }
  301. );
  302. watch(
  303. () => isPlaying.value,
  304. (newVal) => {
  305. if (newVal) {
  306. showPlayBtn.value = false;
  307. }
  308. }
  309. );
  310. watch(
  311. () => damuList.value.length,
  312. () => {
  313. setTimeout(() => {
  314. handleScrollTop();
  315. }, 0);
  316. }
  317. );
  318. function handleScrollTop() {
  319. if (danmuListRef.value) {
  320. danmuListRef.value.scrollTop = danmuListRef.value.scrollHeight + 10000;
  321. }
  322. }
  323. function handleRefresh() {
  324. if (appStore.liveRoomInfo) {
  325. handlePlay(appStore.liveRoomInfo);
  326. }
  327. }
  328. async function getLiveRoomInfo() {
  329. try {
  330. videoLoading.value = true;
  331. const res = await fetchFindLiveRoom(roomId.value);
  332. if (res.code === 200) {
  333. appStore.setLiveRoomInfo(res.data);
  334. if (res.data.type === LiveRoomTypeEnum.user_wertc_live) {
  335. autoplayVal.value = true;
  336. showPlayBtn.value = false;
  337. } else {
  338. showPlayBtn.value = true;
  339. }
  340. initPull(autoplayVal.value);
  341. }
  342. } catch (error) {
  343. console.error(error);
  344. } finally {
  345. videoLoading.value = false;
  346. }
  347. }
  348. function startPull() {
  349. cacheStore.setMuted(false);
  350. showPlayBtn.value = false;
  351. handlePlay(appStore.liveRoomInfo!);
  352. }
  353. async function getWechatQrcode() {
  354. try {
  355. const res = await fetchFindLiveConfigByKey('frontend_wechat_qrcode');
  356. if (res.code === 200) {
  357. frontendWechatQrcode.value = res.data.value;
  358. }
  359. } catch (error) {
  360. console.log(error);
  361. }
  362. }
  363. </script>
  364. <style lang="scss" scoped>
  365. .h5-room-wrap {
  366. height: 100vh;
  367. background-color: #0c1622;
  368. .head {
  369. display: flex;
  370. align-items: center;
  371. justify-content: space-between;
  372. box-sizing: border-box;
  373. padding: 0 20px;
  374. width: 100%;
  375. height: 70px;
  376. background-color: black;
  377. color: white;
  378. .left {
  379. display: flex;
  380. align-items: center;
  381. .avatar {
  382. width: 40px;
  383. height: 40px;
  384. border-radius: 50%;
  385. @extend %containBg;
  386. }
  387. .username {
  388. margin-left: 10px;
  389. }
  390. }
  391. .right {
  392. .btn {
  393. }
  394. }
  395. }
  396. .video-wrap {
  397. position: relative;
  398. overflow: hidden;
  399. background-color: rgba($color: #000000, $alpha: 0.5);
  400. .cover {
  401. position: absolute;
  402. z-index: -1;
  403. background-position: center center;
  404. background-size: cover;
  405. filter: blur(10px);
  406. inset: 0;
  407. }
  408. .no-live {
  409. position: absolute;
  410. top: 50%;
  411. left: 50%;
  412. z-index: 20;
  413. color: white;
  414. font-size: 28px;
  415. transform: translate(-50%, -50%);
  416. }
  417. .media-list {
  418. position: relative;
  419. :deep(video) {
  420. position: absolute;
  421. left: 50%;
  422. display: block;
  423. max-width: 100vw;
  424. max-height: var(--max-height);
  425. transform: translateX(-50%);
  426. }
  427. :deep(canvas) {
  428. position: absolute;
  429. left: 50%;
  430. display: block;
  431. max-width: 100vw;
  432. max-height: var(--max-height);
  433. transform: translateX(-50%);
  434. }
  435. }
  436. .tip-btn {
  437. position: absolute;
  438. top: 50%;
  439. left: 50%;
  440. z-index: 20;
  441. align-items: center;
  442. padding: 10px 20px;
  443. border: 2px solid rgba($color: papayawhip, $alpha: 0.5);
  444. border-radius: 6px;
  445. background-color: rgba(0, 0, 0, 0.3);
  446. color: $theme-color-gold;
  447. font-size: 14px;
  448. cursor: pointer;
  449. transform: translate(-50%, -50%);
  450. &:hover {
  451. background-color: rgba($color: papayawhip, $alpha: 0.5);
  452. color: white;
  453. }
  454. }
  455. }
  456. .n-tab-wrap {
  457. padding-left: 10px;
  458. background: #0c1622;
  459. color: white;
  460. :deep(.n-tabs-tab) {
  461. --n-tab-text-color: white;
  462. }
  463. :deep(.n-tabs-nav-scroll-content) {
  464. border-bottom: 0 !important;
  465. }
  466. // :deep(.n-tabs-pane-wrapper) {
  467. // --n-pane-text-color: white;
  468. // }
  469. }
  470. .danmu-list {
  471. box-sizing: border-box;
  472. padding: 0;
  473. background-color: #0c1622;
  474. text-align: initial;
  475. .title {
  476. padding: 15px 0;
  477. color: #fff;
  478. font-size: 16px;
  479. }
  480. .list {
  481. overflow-y: scroll;
  482. height: 100vh;
  483. @extend %hideScrollbar;
  484. }
  485. .item {
  486. box-sizing: border-box;
  487. margin-bottom: 4px;
  488. padding: 2px;
  489. white-space: normal;
  490. word-wrap: break-word;
  491. font-size: 13px;
  492. .name,
  493. .time {
  494. color: white;
  495. opacity: 0.8;
  496. cursor: pointer;
  497. &.system {
  498. color: red;
  499. }
  500. }
  501. .msg {
  502. margin-top: 4px;
  503. color: white;
  504. &.img {
  505. img {
  506. width: 80%;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. .customerService-wrap,
  513. .liveRoomInfo-wrap {
  514. height: 100%;
  515. height: 300px;
  516. color: white;
  517. }
  518. .customerService-wrap {
  519. position: relative;
  520. text-align: center;
  521. .qrcode {
  522. display: block;
  523. margin: 0 auto 10px;
  524. width: 200px;
  525. height: 200px;
  526. }
  527. }
  528. .send-msg {
  529. position: fixed;
  530. bottom: 0;
  531. left: 0;
  532. display: flex;
  533. align-items: center;
  534. justify-content: space-evenly;
  535. box-sizing: border-box;
  536. padding: 0;
  537. width: 100%;
  538. height: 40px;
  539. background-color: white;
  540. .emoji-list {
  541. position: absolute;
  542. top: 0;
  543. right: 0;
  544. left: 0;
  545. overflow: scroll;
  546. box-sizing: border-box;
  547. padding-top: 5px;
  548. padding-left: 5px;
  549. height: 160px;
  550. background-color: #fff;
  551. transform: translateY(-100%);
  552. @extend %customScrollbar;
  553. .item {
  554. display: inline-flex;
  555. align-items: center;
  556. justify-content: center;
  557. box-sizing: border-box;
  558. width: 8vw;
  559. height: 8vw;
  560. border: 1px solid #f8f8f8;
  561. font-size: 20px;
  562. }
  563. }
  564. .face {
  565. width: 20px;
  566. height: 20px;
  567. @include setBackground('@/assets/img/msg-face.webp');
  568. }
  569. .ipt {
  570. display: block;
  571. box-sizing: border-box;
  572. padding: 10px;
  573. width: 80%;
  574. height: 30px;
  575. outline: none;
  576. border: 1px solid hsla(0, 0%, 60%, 0.2);
  577. border-radius: 4px;
  578. background-color: #f1f2f3;
  579. font-size: 14px;
  580. }
  581. }
  582. }
  583. </style>