index.vue 15 KB

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