index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <template>
  2. <div class="pull-wrap">
  3. <template v-if="roomNoLive">当前房间没在直播~</template>
  4. <template v-else>
  5. <div class="left">
  6. <div
  7. ref="topRef"
  8. class="head"
  9. >
  10. <div class="info">
  11. <div
  12. class="avatar"
  13. :style="{
  14. backgroundImage: `url(${roomLiveing?.live?.user?.avatar})`,
  15. }"
  16. ></div>
  17. <div class="detail">
  18. <div class="top">{{ roomLiveing?.live?.user?.username }}</div>
  19. <div class="bottom">
  20. <span>{{ roomLiveing?.live?.live_room?.name }}</span>
  21. <span v-if="NODE_ENV === 'development'">
  22. socketId:{{ getSocketId() }}
  23. </span>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <div
  29. ref="containerRef"
  30. class="container"
  31. >
  32. <div
  33. v-loading="videoLoading"
  34. class="video-wrap"
  35. >
  36. <div
  37. class="cover"
  38. :style="{
  39. backgroundImage: `url(${
  40. roomLiveing?.live?.live_room?.cover_img ||
  41. roomLiveing?.live?.user?.avatar
  42. })`,
  43. }"
  44. ></div>
  45. <div
  46. ref="canvasRef"
  47. class="media-list"
  48. :class="{ item: appStore.allTrack.length > 1 }"
  49. ></div>
  50. <AudioRoomTip></AudioRoomTip>
  51. <VideoControls></VideoControls>
  52. </div>
  53. </div>
  54. <div
  55. ref="bottomRef"
  56. v-loading="giftLoading"
  57. class="gift-list"
  58. >
  59. <div
  60. v-for="(item, index) in giftGoodsList"
  61. :key="index"
  62. class="item"
  63. >
  64. <div
  65. class="ico"
  66. :style="{ backgroundImage: `url(${item.cover})` }"
  67. >
  68. <div
  69. v-if="item.badge"
  70. class="badge"
  71. :style="{ backgroundColor: item.badge_bg }"
  72. >
  73. <span class="txt">{{ item.badge }}</span>
  74. </div>
  75. </div>
  76. <div class="name">{{ item.name }}</div>
  77. <div class="price">¥{{ item.price }}</div>
  78. </div>
  79. <div
  80. class="item"
  81. @click="handleRecharge"
  82. >
  83. <div class="ico wallet"></div>
  84. <div class="name">
  85. 余额:{{ userStore.userInfo?.wallet?.balance }}
  86. </div>
  87. <div class="price">立即充值</div>
  88. </div>
  89. </div>
  90. </div>
  91. <div class="right">
  92. <div class="tab">
  93. <span>在线用户</span>
  94. <span> | </span>
  95. <span>排行榜</span>
  96. </div>
  97. <div class="user-list">
  98. <div
  99. v-for="(item, index) in liveUserList.filter(
  100. (item) => item.id !== getSocketId()
  101. )"
  102. :key="index"
  103. class="item"
  104. >
  105. <div class="info">
  106. <div
  107. class="avatar"
  108. :style="{ backgroundImage: `url(${item.userInfo?.avatar})` }"
  109. ></div>
  110. <div class="username">
  111. {{ item.userInfo?.username || item.id }}
  112. </div>
  113. </div>
  114. </div>
  115. <div
  116. v-if="userStore.userInfo"
  117. class="item"
  118. >
  119. <div class="info">
  120. <img
  121. :src="userStore.userInfo.avatar"
  122. class="avatar"
  123. alt=""
  124. />
  125. <div class="username">{{ userStore.userInfo.username }}</div>
  126. </div>
  127. </div>
  128. </div>
  129. <div
  130. ref="danmuListRef"
  131. class="danmu-list"
  132. >
  133. <div
  134. v-for="(item, index) in damuList"
  135. :key="index"
  136. class="item"
  137. >
  138. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  139. <span class="name">
  140. {{ item.userInfo?.username || item.socket_id }}:
  141. </span>
  142. <span class="msg">{{ item.msg }}</span>
  143. </template>
  144. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  145. <span class="name system">系统通知:</span>
  146. <span class="msg">
  147. {{ item.userInfo?.username || item.socket_id }}进入直播!
  148. </span>
  149. </template>
  150. <template v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved">
  151. <span class="name system">系统通知:</span>
  152. <span class="msg">
  153. {{ item.userInfo?.username || item.socket_id }}离开直播!
  154. </span>
  155. </template>
  156. </div>
  157. </div>
  158. <div class="send-msg">
  159. <textarea
  160. v-model="danmuStr"
  161. class="ipt"
  162. @keydown="keydownDanmu"
  163. ></textarea>
  164. <div
  165. class="btn"
  166. @click="sendDanmu"
  167. >
  168. 发送
  169. </div>
  170. </div>
  171. </div>
  172. <RechargeCpt v-if="showRecharge"></RechargeCpt>
  173. </template>
  174. </div>
  175. </template>
  176. <script lang="ts" setup>
  177. import { NODE_ENV } from 'script/constant';
  178. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
  179. import { useRoute } from 'vue-router';
  180. import { fetchGoodsList } from '@/api/goods';
  181. import { loginTip } from '@/hooks/use-login';
  182. import { usePull } from '@/hooks/use-pull';
  183. import {
  184. DanmuMsgTypeEnum,
  185. GoodsTypeEnum,
  186. IGoods,
  187. liveTypeEnum,
  188. } from '@/interface';
  189. import { useAppStore } from '@/store/app';
  190. import { useUserStore } from '@/store/user';
  191. import RechargeCpt from './recharge/index.vue';
  192. const route = useRoute();
  193. const userStore = useUserStore();
  194. const appStore = useAppStore();
  195. const giftGoodsList = ref<IGoods[]>([]);
  196. const height = ref(0);
  197. const giftLoading = ref(false);
  198. const showRecharge = ref(false);
  199. const showJoin = ref(true);
  200. const showSidebar = ref(true);
  201. const topRef = ref<HTMLDivElement>();
  202. const bottomRef = ref<HTMLDivElement>();
  203. const danmuListRef = ref<HTMLDivElement>();
  204. const canvasRef = ref<HTMLDivElement>();
  205. const containerRef = ref<HTMLDivElement>();
  206. const localVideoRef = ref<HTMLVideoElement[]>([]);
  207. const {
  208. initPull,
  209. closeWs,
  210. closeRtc,
  211. getSocketId,
  212. keydownDanmu,
  213. sendDanmu,
  214. addVideo,
  215. roomLiveing,
  216. autoplayVal,
  217. videoLoading,
  218. roomNoLive,
  219. damuList,
  220. liveUserList,
  221. sidebarList,
  222. danmuStr,
  223. liveRoomInfo,
  224. } = usePull({
  225. localVideoRef,
  226. canvasRef,
  227. liveType: route.query.liveType as liveTypeEnum,
  228. isSRS: [
  229. liveTypeEnum.srsWebrtcPull,
  230. liveTypeEnum.srsFlvPull,
  231. liveTypeEnum.srsHlsPull,
  232. ].includes(route.query.liveType as liveTypeEnum),
  233. });
  234. async function getGoodsList() {
  235. try {
  236. giftLoading.value = true;
  237. const res = await fetchGoodsList({
  238. type: GoodsTypeEnum.gift,
  239. orderName: 'created_at',
  240. orderBy: 'desc',
  241. });
  242. if (res.code === 200) {
  243. giftGoodsList.value = res.data.rows;
  244. }
  245. } catch (error) {
  246. console.log(error);
  247. } finally {
  248. giftLoading.value = false;
  249. }
  250. }
  251. function handleRecharge() {
  252. if (!loginTip()) return;
  253. showRecharge.value = !showRecharge.value;
  254. }
  255. function handleJoin() {
  256. window.$message.info('维护中~');
  257. return;
  258. showJoin.value = !showJoin.value;
  259. nextTick(() => {
  260. addVideo();
  261. });
  262. }
  263. watch(
  264. () => damuList.value.length,
  265. () => {
  266. setTimeout(() => {
  267. if (danmuListRef.value) {
  268. danmuListRef.value.scrollTop = danmuListRef.value.scrollHeight;
  269. }
  270. }, 0);
  271. }
  272. );
  273. onUnmounted(() => {
  274. closeWs();
  275. closeRtc();
  276. });
  277. onMounted(() => {
  278. getGoodsList();
  279. if (
  280. [
  281. liveTypeEnum.srsHlsPull,
  282. liveTypeEnum.srsFlvPull,
  283. liveTypeEnum.srsWebrtcPull,
  284. ].includes(route.query.liveType as liveTypeEnum)
  285. ) {
  286. showSidebar.value = false;
  287. }
  288. if (topRef.value && bottomRef.value && containerRef.value) {
  289. const res =
  290. bottomRef.value.getBoundingClientRect().top -
  291. (topRef.value.getBoundingClientRect().top +
  292. topRef.value.getBoundingClientRect().height);
  293. height.value = res;
  294. }
  295. initPull();
  296. });
  297. </script>
  298. <style lang="scss" scoped>
  299. .pull-wrap {
  300. display: flex;
  301. justify-content: space-around;
  302. margin: 15px auto 0;
  303. width: $w-1275;
  304. .left {
  305. position: relative;
  306. display: inline-block;
  307. overflow: hidden;
  308. box-sizing: border-box;
  309. width: $w-1000;
  310. height: 100%;
  311. border-radius: 6px;
  312. background-color: papayawhip;
  313. color: #9499a0;
  314. vertical-align: top;
  315. .head {
  316. display: flex;
  317. justify-content: space-between;
  318. padding: 10px 20px;
  319. .info {
  320. display: flex;
  321. align-items: center;
  322. text-align: initial;
  323. .avatar {
  324. margin-right: 20px;
  325. width: 50px;
  326. height: 50px;
  327. border-radius: 50%;
  328. @extend %containBg;
  329. }
  330. .detail {
  331. .top {
  332. margin-bottom: 10px;
  333. color: #18191c;
  334. }
  335. .bottom {
  336. font-size: 14px;
  337. }
  338. }
  339. }
  340. .other {
  341. display: flex;
  342. flex-direction: column;
  343. justify-content: center;
  344. font-size: 12px;
  345. .top {
  346. display: flex;
  347. align-items: center;
  348. .item {
  349. display: flex;
  350. align-items: center;
  351. margin-right: 20px;
  352. .ico {
  353. display: inline-block;
  354. margin-right: 4px;
  355. width: 10px;
  356. height: 10px;
  357. border-radius: 50%;
  358. }
  359. }
  360. }
  361. .bottom {
  362. margin-top: 10px;
  363. }
  364. }
  365. }
  366. .container {
  367. display: flex;
  368. align-items: center;
  369. justify-content: space-between;
  370. height: 562px;
  371. .video-wrap {
  372. position: relative;
  373. overflow: hidden;
  374. flex: 1;
  375. height: 100%;
  376. background-color: rgba($color: #000000, $alpha: 0.5);
  377. .cover {
  378. position: absolute;
  379. background-position: center center;
  380. background-size: cover;
  381. filter: blur(10px);
  382. inset: 0;
  383. }
  384. .media-list {
  385. position: relative;
  386. :deep(video) {
  387. display: block;
  388. width: 100%;
  389. height: 100%;
  390. }
  391. :deep(canvas) {
  392. display: block;
  393. width: 100%;
  394. height: 100%;
  395. }
  396. &.item {
  397. :deep(video) {
  398. width: 50%;
  399. height: initial !important;
  400. }
  401. :deep(canvas) {
  402. width: 50%;
  403. height: initial !important;
  404. }
  405. }
  406. }
  407. .controls {
  408. display: none;
  409. }
  410. .tip-btn {
  411. position: absolute;
  412. top: 50%;
  413. left: 50%;
  414. z-index: 1;
  415. align-items: center;
  416. padding: 12px 26px;
  417. border: 2px solid rgba($color: papayawhip, $alpha: 0.5);
  418. border-radius: 6px;
  419. background-color: rgba(0, 0, 0, 0.3);
  420. color: $theme-color-gold;
  421. cursor: pointer;
  422. transform: translate(-50%, -50%);
  423. &:hover {
  424. background-color: rgba($color: papayawhip, $alpha: 0.5);
  425. color: white;
  426. }
  427. }
  428. }
  429. .sidebar {
  430. overflow: scroll;
  431. width: 120px;
  432. height: 100%;
  433. background-color: rgba($color: #000000, $alpha: 0.3);
  434. @extend %hideScrollbar;
  435. .join {
  436. color: white;
  437. cursor: pointer;
  438. }
  439. video {
  440. max-width: 100%;
  441. }
  442. .name {
  443. word-wrap: break-word;
  444. }
  445. }
  446. }
  447. .gift-list {
  448. position: relative;
  449. display: flex;
  450. align-items: center;
  451. justify-content: space-around;
  452. box-sizing: border-box;
  453. margin: 5px 0;
  454. height: 100px;
  455. .item {
  456. display: flex;
  457. align-items: center;
  458. flex-direction: column;
  459. justify-content: center;
  460. box-sizing: border-box;
  461. width: 100px;
  462. height: 100px;
  463. text-align: center;
  464. cursor: pointer;
  465. &:hover {
  466. background-color: #ebe0ce;
  467. }
  468. .ico {
  469. position: relative;
  470. width: 45px;
  471. height: 45px;
  472. background-position: center center;
  473. background-size: cover;
  474. background-repeat: no-repeat;
  475. &.wallet {
  476. background-image: url('@/assets/img/wallet.webp');
  477. }
  478. .badge {
  479. position: absolute;
  480. top: -8px;
  481. right: -10px;
  482. display: flex;
  483. align-items: center;
  484. justify-content: center;
  485. padding: 2px;
  486. border-radius: 2px;
  487. color: white;
  488. .txt {
  489. display: inline-block;
  490. line-height: 1;
  491. transform-origin: center !important;
  492. @include minFont(10);
  493. }
  494. }
  495. }
  496. .name {
  497. color: #18191c;
  498. font-size: 12px;
  499. }
  500. .price {
  501. color: #9499a0;
  502. font-size: 12px;
  503. }
  504. }
  505. }
  506. }
  507. .right {
  508. position: relative;
  509. display: inline-block;
  510. box-sizing: border-box;
  511. width: $w-250;
  512. border-radius: 6px;
  513. background-color: papayawhip;
  514. color: #9499a0;
  515. .tab {
  516. display: flex;
  517. align-items: center;
  518. justify-content: space-evenly;
  519. padding: 5px 0;
  520. font-size: 12px;
  521. }
  522. .user-list {
  523. overflow-y: scroll;
  524. padding: 0 15px;
  525. height: 100px;
  526. background-color: papayawhip;
  527. @extend %hideScrollbar;
  528. .item {
  529. display: flex;
  530. align-items: center;
  531. justify-content: space-between;
  532. margin-bottom: 10px;
  533. font-size: 12px;
  534. .info {
  535. display: flex;
  536. align-items: center;
  537. .avatar {
  538. margin-right: 5px;
  539. width: 25px;
  540. height: 25px;
  541. border-radius: 50%;
  542. @extend %containBg;
  543. }
  544. .username {
  545. color: black;
  546. }
  547. }
  548. }
  549. }
  550. .danmu-list {
  551. overflow-y: scroll;
  552. padding: 0 15px;
  553. height: 480px;
  554. text-align: initial;
  555. @extend %hideScrollbar;
  556. .item {
  557. margin-bottom: 10px;
  558. font-size: 12px;
  559. .name {
  560. color: #9499a0;
  561. &.system {
  562. color: red;
  563. }
  564. }
  565. .msg {
  566. color: #61666d;
  567. }
  568. }
  569. }
  570. .send-msg {
  571. position: absolute;
  572. bottom: 15px;
  573. box-sizing: border-box;
  574. padding: 0 10px;
  575. width: 100%;
  576. .ipt {
  577. display: block;
  578. box-sizing: border-box;
  579. margin: 0 auto;
  580. padding: 10px;
  581. width: 100%;
  582. height: 60px;
  583. outline: none;
  584. border: 1px solid hsla(0, 0%, 60%, 0.2);
  585. border-radius: 4px;
  586. background-color: #f1f2f3;
  587. font-size: 14px;
  588. }
  589. .btn {
  590. box-sizing: border-box;
  591. margin-top: 10px;
  592. margin-left: auto;
  593. padding: 5px;
  594. width: 80px;
  595. border-radius: 4px;
  596. background-color: $theme-color-gold;
  597. color: white;
  598. text-align: center;
  599. font-size: 12px;
  600. cursor: pointer;
  601. }
  602. }
  603. }
  604. }
  605. // 屏幕宽度大于1500的时候
  606. @media screen and (min-width: $w-1500) {
  607. .pull-wrap {
  608. width: $w-1350;
  609. .left {
  610. width: $w-1000;
  611. }
  612. .right {
  613. width: $w-300;
  614. }
  615. }
  616. }
  617. </style>