index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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(${appStore.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 && appStore.liveRoomInfo"
  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 } 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. handlePlay,
  126. initPull,
  127. keydownDanmu,
  128. sendDanmu,
  129. autoplayVal,
  130. videoLoading,
  131. damuList,
  132. danmuStr,
  133. roomLiving,
  134. anchorInfo,
  135. remoteVideo,
  136. closeRtc,
  137. closeWs,
  138. } = usePull();
  139. watch(
  140. () => remoteVideo.value,
  141. (newVal) => {
  142. newVal.forEach((item) => {
  143. remoteVideoRef.value?.appendChild(item);
  144. });
  145. },
  146. {
  147. deep: true,
  148. immediate: true,
  149. }
  150. );
  151. watch(
  152. () => damuList.value.length,
  153. () => {
  154. setTimeout(() => {
  155. if (containerRef.value) {
  156. containerRef.value.scrollTop = containerRef.value.scrollHeight;
  157. }
  158. }, 0);
  159. }
  160. );
  161. async function getLiveRoomInfo() {
  162. try {
  163. videoLoading.value = true;
  164. const res = await fetchFindLiveRoom(route.params.roomId as string);
  165. if (res.code === 200) {
  166. appStore.setLiveRoomInfo(res.data);
  167. if (res.data.type === LiveRoomTypeEnum.user_wertc) {
  168. autoplayVal.value = true;
  169. showPlayBtn.value = false;
  170. } else {
  171. showPlayBtn.value = true;
  172. }
  173. initPull(autoplayVal.value);
  174. }
  175. } catch (error) {
  176. console.error(error);
  177. } finally {
  178. videoLoading.value = false;
  179. }
  180. }
  181. function startPull() {
  182. appStore.setMuted(false);
  183. showPlayBtn.value = false;
  184. handlePlay(appStore.liveRoomInfo!);
  185. }
  186. onUnmounted(() => {
  187. closeWs();
  188. closeRtc();
  189. });
  190. onMounted(() => {
  191. setTimeout(() => {
  192. scrollTo(0, 0);
  193. }, 100);
  194. videoWrapHeight.value =
  195. document.documentElement.clientWidth / appStore.videoRatio;
  196. nextTick(() => {
  197. if (containerRef.value && bottomRef.value) {
  198. const res =
  199. bottomRef.value.getBoundingClientRect().top -
  200. containerRef.value.getBoundingClientRect().top;
  201. containerHeight.value = res;
  202. }
  203. });
  204. getLiveRoomInfo();
  205. });
  206. </script>
  207. <style lang="scss" scoped>
  208. .h5-room-wrap {
  209. .head {
  210. display: flex;
  211. align-items: center;
  212. justify-content: space-between;
  213. box-sizing: border-box;
  214. padding: 0 20px;
  215. width: 100%;
  216. height: 70px;
  217. background-color: black;
  218. color: white;
  219. .left {
  220. display: flex;
  221. align-items: center;
  222. .avatar {
  223. width: 40px;
  224. height: 40px;
  225. border-radius: 50%;
  226. @extend %containBg;
  227. }
  228. .username {
  229. margin-left: 10px;
  230. }
  231. }
  232. .right {
  233. .btn {
  234. }
  235. }
  236. }
  237. .video-wrap {
  238. position: relative;
  239. overflow: hidden;
  240. flex: 1;
  241. background-color: rgba($color: #000000, $alpha: 0.5);
  242. .cover {
  243. position: absolute;
  244. background-position: center center;
  245. background-size: cover;
  246. filter: blur(10px);
  247. inset: 0;
  248. }
  249. .no-live {
  250. position: absolute;
  251. top: 50%;
  252. left: 50%;
  253. z-index: 20;
  254. color: white;
  255. font-size: 28px;
  256. transform: translate(-50%, -50%);
  257. }
  258. .media-list {
  259. position: relative;
  260. overflow-y: scroll;
  261. :deep(video) {
  262. display: block;
  263. width: 100%;
  264. height: 100%;
  265. }
  266. :deep(canvas) {
  267. display: block;
  268. width: 100%;
  269. height: 100%;
  270. }
  271. // &.item {
  272. // :deep(video) {
  273. // width: 50%;
  274. // height: initial !important;
  275. // }
  276. // :deep(canvas) {
  277. // width: 50%;
  278. // height: initial !important;
  279. // }
  280. // }
  281. }
  282. // :deep(video) {
  283. // position: absolute;
  284. // top: 0;
  285. // left: 50%;
  286. // width: 100%;
  287. // height: 100%;
  288. // transform: translate(-50%);
  289. // user-select: nones;
  290. // }
  291. // :deep(canvas) {
  292. // position: absolute;
  293. // top: 0;
  294. // left: 50%;
  295. // width: 100%;
  296. // height: 100%;
  297. // transform: translate(-50%);
  298. // user-select: nones;
  299. // }
  300. .tip-btn {
  301. position: absolute;
  302. top: 50%;
  303. left: 50%;
  304. z-index: 20;
  305. align-items: center;
  306. padding: 10px 20px;
  307. border: 2px solid rgba($color: papayawhip, $alpha: 0.5);
  308. border-radius: 6px;
  309. background-color: rgba(0, 0, 0, 0.3);
  310. color: $theme-color-gold;
  311. font-size: 14px;
  312. cursor: pointer;
  313. transform: translate(-50%, -50%);
  314. &:hover {
  315. background-color: rgba($color: papayawhip, $alpha: 0.5);
  316. color: white;
  317. }
  318. }
  319. }
  320. .danmu-list {
  321. box-sizing: border-box;
  322. padding: 0 15px;
  323. background-color: #0c1622;
  324. text-align: initial;
  325. .title {
  326. padding: 15px 0;
  327. color: #fff;
  328. font-size: 16px;
  329. }
  330. .list {
  331. overflow-y: scroll;
  332. height: 100vh;
  333. @extend %hideScrollbar;
  334. }
  335. .item {
  336. margin-bottom: 10px;
  337. font-size: 12px;
  338. .name {
  339. color: #ccc;
  340. &.system {
  341. color: red;
  342. }
  343. }
  344. .msg {
  345. color: #fff;
  346. }
  347. }
  348. }
  349. .send-msg {
  350. position: fixed;
  351. bottom: 0;
  352. left: 0;
  353. display: flex;
  354. align-items: center;
  355. justify-content: space-evenly;
  356. box-sizing: border-box;
  357. padding: 0 10px;
  358. width: 100%;
  359. height: 40px;
  360. background-color: #0c1622;
  361. .ipt {
  362. display: block;
  363. box-sizing: border-box;
  364. padding: 10px;
  365. width: 80%;
  366. height: 30px;
  367. outline: none;
  368. border: 1px solid hsla(0, 0%, 60%, 0.2);
  369. border-radius: 4px;
  370. background-color: #f1f2f3;
  371. font-size: 14px;
  372. }
  373. }
  374. }
  375. </style>