index.vue 16 KB

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