index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <template>
  2. <div class="home-wrap">
  3. <div class="play-container">
  4. <div class="bg"></div>
  5. <div class="slider-wrap">
  6. <Slider
  7. v-if="interactionList.length"
  8. :list="interactionList"
  9. :row="2"
  10. :speed="60"
  11. ></Slider>
  12. </div>
  13. <div class="container">
  14. <div class="left">
  15. <div
  16. v-if="currentLiveRoom?.live_room?.cdn === 1"
  17. class="cdn-ico"
  18. >
  19. <div class="txt">CDN</div>
  20. </div>
  21. <div
  22. class="cover"
  23. :style="{
  24. backgroundImage: `url(${
  25. currentLiveRoom?.live_room?.cover_img ||
  26. currentLiveRoom?.user?.avatar
  27. })`,
  28. }"
  29. ></div>
  30. <div v-loading="videoLoading">
  31. <div
  32. v-if="currentLiveRoom?.live_room?.flv_url"
  33. ref="remoteVideoRef"
  34. ></div>
  35. </div>
  36. <template v-if="currentLiveRoom">
  37. <VideoControls></VideoControls>
  38. <div
  39. class="join-btn"
  40. :style="{
  41. display: !isMobile() ? 'none' : showControls ? 'block' : 'none',
  42. }"
  43. >
  44. <div
  45. v-if="
  46. currentLiveRoom.live_room?.type ===
  47. LiveRoomTypeEnum.user_wertc
  48. "
  49. class="btn webrtc"
  50. @click="joinRtcRoom()"
  51. >
  52. 进入直播(webrtc)
  53. </div>
  54. <div
  55. v-if="
  56. currentLiveRoom.live_room?.type !==
  57. LiveRoomTypeEnum.user_wertc
  58. "
  59. class="btn flv"
  60. @click="
  61. joinRoom({
  62. isFlv: true,
  63. roomId: currentLiveRoom.live_room_id!,
  64. })
  65. "
  66. >
  67. 进入直播(flv)
  68. </div>
  69. <div
  70. v-if="
  71. currentLiveRoom.live_room?.type !==
  72. LiveRoomTypeEnum.user_wertc
  73. "
  74. class="btn hls"
  75. @click="
  76. joinRoom({
  77. isFlv: false,
  78. roomId: currentLiveRoom.live_room_id!,
  79. })
  80. "
  81. >
  82. 进入直播(hls)
  83. </div>
  84. </div>
  85. </template>
  86. </div>
  87. <div class="right">
  88. <div
  89. v-if="topLiveRoomList.length"
  90. class="list"
  91. >
  92. <div
  93. v-for="(item, index) in topLiveRoomList"
  94. :key="index"
  95. :class="{
  96. item: 1,
  97. active: item.live_room_id === currentLiveRoom?.live_room_id,
  98. }"
  99. :style="{
  100. backgroundImage: `url(${
  101. item.live_room?.cover_img || item?.user?.avatar
  102. })`,
  103. }"
  104. @click="changeLiveRoom(item)"
  105. >
  106. <div
  107. class="border"
  108. :style="{
  109. opacity:
  110. item.live_room_id === currentLiveRoom?.live_room_id ? 1 : 0,
  111. }"
  112. ></div>
  113. <div
  114. v-if="item.live_room_id === currentLiveRoom?.live_room_id"
  115. class="triangle"
  116. ></div>
  117. <div class="txt">{{ item.live_room?.name }}</div>
  118. </div>
  119. </div>
  120. <div
  121. v-else
  122. class="none"
  123. >
  124. 当前没有在线的直播间
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. <div class="area-container">
  130. <div class="area-item">
  131. <div class="title">推荐直播</div>
  132. <div class="live-room-list">
  133. <div
  134. v-for="(iten, indey) in otherLiveRoomList"
  135. :key="indey"
  136. class="live-room"
  137. @click="
  138. joinRoom({
  139. isFlv: false,
  140. roomId: iten.live_room?.id!,
  141. })
  142. "
  143. >
  144. <div
  145. class="cover"
  146. :style="{
  147. backgroundImage: `url('${
  148. iten?.live_room?.cover_img || iten?.user?.avatar
  149. }')`,
  150. }"
  151. >
  152. <div
  153. v-if="iten?.live_room?.cdn === 1"
  154. class="cdn-ico"
  155. >
  156. <div class="txt">CDN</div>
  157. </div>
  158. <div class="txt">{{ iten?.user?.username }}</div>
  159. </div>
  160. <div class="desc">{{ iten?.live_room?.name }}</div>
  161. </div>
  162. <div
  163. v-if="!otherLiveRoomList.length"
  164. class="null"
  165. >
  166. 暂无数据
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. <div class="foot">*部分内容来源网络,如有侵权,请联系我删除~</div>
  172. </div>
  173. </template>
  174. <script lang="ts" setup>
  175. import { isMobile } from 'billd-utils';
  176. import { onMounted, ref, watch } from 'vue';
  177. import { useRoute, useRouter } from 'vue-router';
  178. import { fetchLiveList } from '@/api/live';
  179. import { sliderList } from '@/constant';
  180. import { usePull } from '@/hooks/use-pull';
  181. import { ILive, LiveRoomTypeEnum, LiveTypeEnum } from '@/interface';
  182. import { routerName } from '@/router';
  183. const route = useRoute();
  184. const router = useRouter();
  185. const canvasRef = ref<Element>();
  186. const showControls = ref(false);
  187. const topNums = ref(6);
  188. const topLiveRoomList = ref<ILive[]>([]);
  189. const otherLiveRoomList = ref<ILive[]>([]);
  190. const currentLiveRoom = ref<ILive>();
  191. const interactionList = ref(sliderList);
  192. const remoteVideoRef = ref<HTMLDivElement>();
  193. const { handleHlsPlay, videoLoading, remoteVideo, handleStopDrawing } = usePull(
  194. {
  195. liveType: route.query.liveType as LiveTypeEnum,
  196. }
  197. );
  198. watch(
  199. () => remoteVideo.value,
  200. (newVal) => {
  201. newVal.forEach((item) => {
  202. remoteVideoRef.value?.appendChild(item);
  203. });
  204. },
  205. {
  206. deep: true,
  207. immediate: true,
  208. }
  209. );
  210. function changeLiveRoom(item: ILive) {
  211. handleStopDrawing();
  212. if (item.id === currentLiveRoom.value?.id) return;
  213. currentLiveRoom.value = item;
  214. canvasRef.value?.childNodes?.forEach((item) => {
  215. item.remove();
  216. });
  217. if (
  218. [
  219. LiveRoomTypeEnum.user_srs,
  220. LiveRoomTypeEnum.user_obs,
  221. LiveRoomTypeEnum.system,
  222. ].includes(item.live_room?.type!)
  223. ) {
  224. handleHlsPlay(item.live_room?.hls_url!);
  225. }
  226. }
  227. async function getLiveRoomList() {
  228. try {
  229. const res = await fetchLiveList({
  230. orderName: 'created_at',
  231. orderBy: 'desc',
  232. });
  233. if (res.code === 200) {
  234. topLiveRoomList.value = res.data.rows.slice(0, topNums.value);
  235. otherLiveRoomList.value = res.data.rows.slice(topNums.value);
  236. if (res.data.total) {
  237. currentLiveRoom.value = topLiveRoomList.value[0];
  238. if (
  239. [
  240. LiveRoomTypeEnum.user_srs,
  241. LiveRoomTypeEnum.user_obs,
  242. LiveRoomTypeEnum.system,
  243. ].includes(currentLiveRoom.value?.live_room?.type!)
  244. ) {
  245. handleHlsPlay(currentLiveRoom.value.live_room?.hls_url!);
  246. }
  247. }
  248. }
  249. } catch (error) {
  250. console.log(error);
  251. }
  252. }
  253. onMounted(() => {
  254. getLiveRoomList();
  255. });
  256. function joinRtcRoom() {
  257. if (currentLiveRoom.value?.live_room?.type === LiveRoomTypeEnum.user_srs) {
  258. router.push({
  259. name: routerName.pull,
  260. params: {
  261. roomId: currentLiveRoom.value.live_room_id,
  262. },
  263. query: {
  264. liveType: LiveTypeEnum.srsWebrtcPull,
  265. },
  266. });
  267. } else {
  268. router.push({
  269. name: routerName.pull,
  270. params: {
  271. roomId: currentLiveRoom.value?.live_room_id,
  272. },
  273. query: {
  274. liveType: LiveTypeEnum.webrtcPull,
  275. },
  276. });
  277. }
  278. }
  279. function joinRoom(data: { roomId: number; isFlv: boolean }) {
  280. router.push({
  281. name: routerName.pull,
  282. params: { roomId: data.roomId },
  283. query: {
  284. liveType: data.isFlv ? LiveTypeEnum.srsFlvPull : LiveTypeEnum.srsHlsPull,
  285. },
  286. });
  287. }
  288. </script>
  289. <style lang="scss" scoped>
  290. .home-wrap {
  291. .play-container {
  292. position: relative;
  293. padding-bottom: 20px;
  294. z-index: 1;
  295. .bg {
  296. position: absolute;
  297. top: 0;
  298. right: 0;
  299. left: 0;
  300. z-index: -1;
  301. width: 100%;
  302. height: 100%;
  303. background-color: papayawhip;
  304. background-position: center;
  305. background-repeat: no-repeat;
  306. }
  307. .slider-wrap {
  308. padding: 2px 0 4px 0;
  309. }
  310. .container {
  311. display: flex;
  312. justify-content: center;
  313. box-sizing: border-box;
  314. margin: 0 auto;
  315. height: calc($w-1100 / $video-ratio);
  316. .left {
  317. position: relative;
  318. display: inline-block;
  319. overflow: hidden;
  320. flex-shrink: 0;
  321. box-sizing: border-box;
  322. margin-right: 20px;
  323. width: $w-1100;
  324. height: 100%;
  325. border-radius: 4px;
  326. background-color: rgba($color: #000000, $alpha: 0.3);
  327. @extend %coverBg;
  328. .cdn-ico {
  329. position: absolute;
  330. top: -9px;
  331. right: -10px;
  332. z-index: 2;
  333. width: 70px;
  334. height: 32px;
  335. background-color: #f87c48;
  336. color: white;
  337. transform: rotate(45deg);
  338. transform-origin: bottom;
  339. .txt {
  340. margin-top: 11px;
  341. margin-left: 20px;
  342. background-image: initial !important;
  343. font-size: 14px;
  344. }
  345. }
  346. .cover {
  347. position: absolute;
  348. background-position: center center;
  349. background-size: cover;
  350. filter: blur(10px);
  351. inset: 0;
  352. }
  353. :deep(canvas) {
  354. position: absolute;
  355. top: 0;
  356. left: 50%;
  357. width: 100%;
  358. height: 100%;
  359. transform: translate(-50%);
  360. user-select: none;
  361. }
  362. :deep(video) {
  363. position: absolute;
  364. top: 0;
  365. left: 50%;
  366. width: 100%;
  367. height: 100%;
  368. transform: translate(-50%);
  369. user-select: none;
  370. }
  371. .controls {
  372. display: none;
  373. }
  374. &:hover {
  375. .join-btn {
  376. display: inline-flex !important;
  377. }
  378. }
  379. .join-btn {
  380. position: absolute;
  381. top: 50%;
  382. left: 50%;
  383. z-index: 20;
  384. display: none;
  385. align-items: center;
  386. align-items: center;
  387. justify-content: center;
  388. box-sizing: border-box;
  389. width: 80%;
  390. transform: translate(-50%, -50%);
  391. .btn {
  392. padding: 14px 26px;
  393. border: 2px solid rgba($color: $theme-color-gold, $alpha: 0.5);
  394. border-radius: 6px;
  395. background-color: rgba(0, 0, 0, 0.3);
  396. color: $theme-color-gold;
  397. font-size: 16px;
  398. cursor: pointer;
  399. &:hover {
  400. background-color: $theme-color-gold;
  401. color: white;
  402. }
  403. &.webrtc {
  404. margin-right: 10px;
  405. }
  406. &.flv {
  407. margin-right: 10px;
  408. }
  409. }
  410. }
  411. }
  412. .right {
  413. display: inline-block;
  414. overflow: scroll;
  415. flex-shrink: 0;
  416. box-sizing: border-box;
  417. padding: 12px 10px;
  418. height: 100%;
  419. border-radius: 4px;
  420. background-color: rgba($color: #000000, $alpha: 0.3);
  421. @extend %hideScrollbar;
  422. .list {
  423. .item {
  424. position: relative;
  425. box-sizing: border-box;
  426. margin-bottom: 10px;
  427. width: 200px;
  428. height: 110px;
  429. border-radius: 4px;
  430. background-color: rgba($color: #000000, $alpha: 0.3);
  431. cursor: pointer;
  432. @extend %coverBg;
  433. &:last-child {
  434. margin-bottom: 0;
  435. }
  436. .border {
  437. position: absolute;
  438. top: 0;
  439. right: 0;
  440. bottom: 0;
  441. left: 0;
  442. z-index: 1;
  443. border: 2px solid $theme-color-gold;
  444. border-radius: 4px;
  445. }
  446. .triangle {
  447. position: absolute;
  448. top: 50%;
  449. left: 0;
  450. display: inline-block;
  451. border: 5px solid transparent;
  452. border-right-color: $theme-color-gold;
  453. transform: translate(-100%, -50%);
  454. }
  455. &.active {
  456. &::before {
  457. background-color: transparent;
  458. }
  459. }
  460. &:hover {
  461. &::before {
  462. background-color: transparent;
  463. }
  464. }
  465. &::before {
  466. position: absolute;
  467. display: block;
  468. width: 100%;
  469. height: 100%;
  470. border-radius: 4px;
  471. background-color: rgba(0, 0, 0, 0.4);
  472. content: '';
  473. transition: all cubic-bezier(0.22, 0.58, 0.12, 0.98) 0.4s;
  474. }
  475. .txt {
  476. position: absolute;
  477. bottom: 0;
  478. left: 0;
  479. box-sizing: border-box;
  480. padding: 4px 8px;
  481. width: 100%;
  482. border-radius: 0 0 4px 4px;
  483. background-image: linear-gradient(
  484. -180deg,
  485. rgba(0, 0, 0, 0),
  486. rgba(0, 0, 0, 0.6)
  487. );
  488. color: white;
  489. text-align: initial;
  490. font-size: 13px;
  491. @extend %singleEllipsis;
  492. }
  493. }
  494. }
  495. .none {
  496. width: 200px;
  497. color: white;
  498. text-align: center;
  499. font-size: 14px;
  500. }
  501. }
  502. }
  503. }
  504. .area-container {
  505. box-sizing: border-box;
  506. margin: 10px auto;
  507. width: $w-1350;
  508. .area-item {
  509. .title {
  510. padding: 10px 0;
  511. font-size: 26px;
  512. }
  513. .live-room {
  514. display: inline-block;
  515. margin-right: 32px;
  516. margin-bottom: 10px;
  517. width: 300px;
  518. cursor: pointer;
  519. .cover {
  520. position: relative;
  521. overflow: hidden;
  522. width: 100%;
  523. height: 150px;
  524. border-radius: 8px;
  525. background-position: center center;
  526. background-size: cover;
  527. .cdn-ico {
  528. position: absolute;
  529. top: -10px;
  530. right: -10px;
  531. z-index: 2;
  532. width: 70px;
  533. height: 28px;
  534. background-color: #f87c48;
  535. color: white;
  536. transform: rotate(45deg);
  537. transform-origin: bottom;
  538. .txt {
  539. margin-left: 18px;
  540. background-image: initial !important;
  541. font-size: 13px;
  542. }
  543. }
  544. .txt {
  545. position: absolute;
  546. bottom: 0;
  547. left: 0;
  548. box-sizing: border-box;
  549. padding: 4px 8px;
  550. width: 100%;
  551. border-radius: 0 0 4px 4px;
  552. background-image: linear-gradient(
  553. -180deg,
  554. rgba(0, 0, 0, 0),
  555. rgba(0, 0, 0, 0.6)
  556. );
  557. color: white;
  558. text-align: initial;
  559. font-size: 13px;
  560. @extend %singleEllipsis;
  561. }
  562. }
  563. .desc {
  564. margin-top: 4px;
  565. font-size: 14px;
  566. @extend %singleEllipsis;
  567. }
  568. }
  569. }
  570. }
  571. .foot {
  572. margin-top: 10px;
  573. text-align: center;
  574. }
  575. }
  576. // 屏幕宽度小于1330的时候
  577. @media screen and (max-width: 1330px) {
  578. .home-wrap {
  579. .play-container {
  580. .container {
  581. height: calc($w-900 / $video-ratio);
  582. .left {
  583. width: $w-900;
  584. }
  585. .right {
  586. }
  587. }
  588. }
  589. .area-container {
  590. width: $w-1150;
  591. }
  592. }
  593. }
  594. </style>