index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. :style="{ height: videoWrapHeight + 'px' }"
  28. >
  29. <div
  30. class="cover"
  31. :style="{
  32. backgroundImage: `url(${liveRoomInfo?.cover_img})`,
  33. }"
  34. ></div>
  35. <div
  36. v-if="!roomLiving"
  37. class="no-live"
  38. >
  39. 主播还没开播~
  40. </div>
  41. <div
  42. class="media-list"
  43. ref="remoteVideoRef"
  44. :class="{ item: appStore.allTrack.length > 1 }"
  45. ></div>
  46. <div
  47. v-if="showPlayBtn && roomLiving"
  48. class="tip-btn"
  49. @click="startPull"
  50. >
  51. 点击播放
  52. </div>
  53. <VideoControls v-else></VideoControls>
  54. </div>
  55. <div class="danmu-list">
  56. <div class="title">弹幕专区</div>
  57. <div
  58. ref="containerRef"
  59. class="list"
  60. :style="{ height: containerHeight + 'px' }"
  61. >
  62. <div
  63. v-for="(item, index) in damuList"
  64. :key="index"
  65. class="item"
  66. >
  67. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  68. <span class="name">
  69. {{ item.userInfo?.username || item.socket_id }}:
  70. </span>
  71. <span class="msg">{{ item.msg }}</span>
  72. </template>
  73. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  74. <span class="name system">系统通知:</span>
  75. <span class="msg">
  76. {{ item.userInfo?.username || item.socket_id }}进入直播!
  77. </span>
  78. </template>
  79. <template v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved">
  80. <span class="name system">系统通知:</span>
  81. <span class="msg">
  82. {{ item.userInfo?.username || item.socket_id }}离开直播!
  83. </span>
  84. </template>
  85. </div>
  86. </div>
  87. </div>
  88. <div
  89. ref="bottomRef"
  90. class="send-msg"
  91. >
  92. <input
  93. v-model="danmuStr"
  94. class="ipt"
  95. @keydown="keydownDanmu"
  96. />
  97. <n-button
  98. type="info"
  99. size="small"
  100. color="#ffd700"
  101. @click="sendDanmu"
  102. >
  103. 发送
  104. </n-button>
  105. </div>
  106. </div>
  107. </template>
  108. <script lang="ts" setup>
  109. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
  110. import { useRoute } from 'vue-router';
  111. import { fetchFindLiveRoom } from '@/api/liveRoom';
  112. import { usePull } from '@/hooks/use-pull';
  113. import { DanmuMsgTypeEnum, LiveRoomTypeEnum, LiveTypeEnum } from '@/interface';
  114. import router, { mobileRouterName } from '@/router';
  115. import { useAppStore } from '@/store/app';
  116. const route = useRoute();
  117. const appStore = useAppStore();
  118. const bottomRef = ref<HTMLDivElement>();
  119. const containerRef = ref<HTMLDivElement>();
  120. const showPlayBtn = ref(false);
  121. const containerHeight = ref(0);
  122. const videoWrapHeight = ref(0);
  123. const remoteVideoRef = ref<HTMLDivElement>();
  124. const {
  125. initPull,
  126. keydownDanmu,
  127. sendDanmu,
  128. handleHlsPlay,
  129. roomLiveType,
  130. autoplayVal,
  131. videoLoading,
  132. damuList,
  133. danmuStr,
  134. roomLiving,
  135. liveRoomInfo,
  136. anchorInfo,
  137. remoteVideo,
  138. closeRtc,
  139. closeWs,
  140. } = usePull({
  141. liveType: LiveTypeEnum.srsHlsPull,
  142. });
  143. watch(
  144. () => remoteVideo.value,
  145. (newVal) => {
  146. newVal.forEach((item) => {
  147. remoteVideoRef.value?.appendChild(item);
  148. });
  149. },
  150. {
  151. deep: true,
  152. immediate: true,
  153. }
  154. );
  155. watch(
  156. () => damuList.value.length,
  157. () => {
  158. setTimeout(() => {
  159. if (containerRef.value) {
  160. containerRef.value.scrollTop = containerRef.value.scrollHeight;
  161. }
  162. }, 0);
  163. }
  164. );
  165. async function getLiveRoomInfo() {
  166. try {
  167. videoLoading.value = true;
  168. const res = await fetchFindLiveRoom(route.params.roomId as string);
  169. if (res.code === 200) {
  170. if (res.data.type === LiveRoomTypeEnum.user_wertc) {
  171. autoplayVal.value = true;
  172. roomLiveType.value = LiveTypeEnum.webrtcPull;
  173. showPlayBtn.value = false;
  174. } else {
  175. showPlayBtn.value = true;
  176. }
  177. initPull(autoplayVal.value);
  178. }
  179. } catch (error) {
  180. console.error(error);
  181. } finally {
  182. videoLoading.value = false;
  183. }
  184. }
  185. function startPull() {
  186. appStore.setMuted(false);
  187. showPlayBtn.value = false;
  188. handleHlsPlay(liveRoomInfo.value?.hls_url);
  189. }
  190. onUnmounted(() => {
  191. closeWs();
  192. closeRtc();
  193. });
  194. onMounted(() => {
  195. setTimeout(() => {
  196. scrollTo(0, 0);
  197. }, 100);
  198. videoWrapHeight.value =
  199. document.documentElement.clientWidth / appStore.videoRatio;
  200. nextTick(() => {
  201. if (containerRef.value && bottomRef.value) {
  202. const res =
  203. bottomRef.value.getBoundingClientRect().top -
  204. containerRef.value.getBoundingClientRect().top;
  205. containerHeight.value = res;
  206. }
  207. });
  208. getLiveRoomInfo();
  209. });
  210. </script>
  211. <style lang="scss" scoped>
  212. .h5-room-wrap {
  213. .head {
  214. display: flex;
  215. align-items: center;
  216. justify-content: space-between;
  217. box-sizing: border-box;
  218. padding: 0 20px;
  219. width: 100%;
  220. height: 70px;
  221. background-color: black;
  222. color: white;
  223. .left {
  224. display: flex;
  225. align-items: center;
  226. .avatar {
  227. width: 40px;
  228. height: 40px;
  229. border-radius: 50%;
  230. @extend %containBg;
  231. }
  232. .username {
  233. margin-left: 10px;
  234. }
  235. }
  236. .right {
  237. .btn {
  238. }
  239. }
  240. }
  241. .video-wrap {
  242. position: relative;
  243. overflow: hidden;
  244. flex: 1;
  245. background-color: rgba($color: #000000, $alpha: 0.5);
  246. .cover {
  247. position: absolute;
  248. background-position: center center;
  249. background-size: cover;
  250. filter: blur(10px);
  251. inset: 0;
  252. }
  253. .no-live {
  254. position: absolute;
  255. top: 50%;
  256. left: 50%;
  257. z-index: 20;
  258. color: white;
  259. font-size: 28px;
  260. transform: translate(-50%, -50%);
  261. }
  262. .media-list {
  263. position: relative;
  264. overflow-y: scroll;
  265. :deep(video) {
  266. display: block;
  267. width: 100%;
  268. height: 100%;
  269. }
  270. :deep(canvas) {
  271. display: block;
  272. width: 100%;
  273. height: 100%;
  274. }
  275. // &.item {
  276. // :deep(video) {
  277. // width: 50%;
  278. // height: initial !important;
  279. // }
  280. // :deep(canvas) {
  281. // width: 50%;
  282. // height: initial !important;
  283. // }
  284. // }
  285. }
  286. // :deep(video) {
  287. // position: absolute;
  288. // top: 0;
  289. // left: 50%;
  290. // width: 100%;
  291. // height: 100%;
  292. // transform: translate(-50%);
  293. // user-select: nones;
  294. // }
  295. // :deep(canvas) {
  296. // position: absolute;
  297. // top: 0;
  298. // left: 50%;
  299. // width: 100%;
  300. // height: 100%;
  301. // transform: translate(-50%);
  302. // user-select: nones;
  303. // }
  304. .tip-btn {
  305. position: absolute;
  306. top: 50%;
  307. left: 50%;
  308. z-index: 1;
  309. align-items: center;
  310. padding: 10px 20px;
  311. border: 2px solid rgba($color: papayawhip, $alpha: 0.5);
  312. border-radius: 6px;
  313. background-color: rgba(0, 0, 0, 0.3);
  314. color: $theme-color-gold;
  315. font-size: 14px;
  316. cursor: pointer;
  317. transform: translate(-50%, -50%);
  318. &:hover {
  319. background-color: rgba($color: papayawhip, $alpha: 0.5);
  320. color: white;
  321. }
  322. }
  323. }
  324. .danmu-list {
  325. box-sizing: border-box;
  326. padding: 0 15px;
  327. background-color: #0c1622;
  328. text-align: initial;
  329. .title {
  330. padding: 15px 0;
  331. color: #fff;
  332. font-size: 16px;
  333. }
  334. .list {
  335. overflow-y: scroll;
  336. height: 100vh;
  337. @extend %hideScrollbar;
  338. }
  339. .item {
  340. margin-bottom: 10px;
  341. font-size: 12px;
  342. .name {
  343. color: #ccc;
  344. &.system {
  345. color: red;
  346. }
  347. }
  348. .msg {
  349. color: #fff;
  350. }
  351. }
  352. }
  353. .send-msg {
  354. position: fixed;
  355. bottom: 0;
  356. left: 0;
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-evenly;
  360. box-sizing: border-box;
  361. padding: 0 10px;
  362. width: 100%;
  363. height: 40px;
  364. background-color: #0c1622;
  365. .ipt {
  366. display: block;
  367. box-sizing: border-box;
  368. padding: 10px;
  369. width: 80%;
  370. height: 30px;
  371. outline: none;
  372. border: 1px solid hsla(0, 0%, 60%, 0.2);
  373. border-radius: 4px;
  374. background-color: #f1f2f3;
  375. font-size: 14px;
  376. }
  377. }
  378. }
  379. </style>