wws vor 2 Jahren
Ursprung
Commit
6bb34575cd

BIN
src/assets/live/emote.png


+ 29 - 18
src/components/chat-wrapper/ChatWrapper.vue

@@ -1,6 +1,17 @@
 <script>
 export default {
-  name: "ChatWrapper"
+  name: "ChatWrapper",
+  props: {
+    list: {
+      type: Array,
+      default: () => []
+    }
+  },
+  data() {
+    return {
+
+    }
+  }
 }
 </script>
 
@@ -12,28 +23,28 @@ export default {
     </div>
 
     <!--  直播标题  -->
-    <div class="chat-item live-title-row">
-      <div class="live-title-content">
-            <span>
-              啸谈股经—大势研判、市场点评、行情动态解说
-            </span>
-        <span class="live-title-desc">
-              两市震荡反弹,板块轮动修复周三两市震荡反弹,板块轮动修复。CRO概念,医疗服务,减肥药,BC电池,创新药多有反弹EDR概念,光刻机,Wifi6,酒店及餐饮,黑色家电多有承压。两市上涨2934家,下跌2064家,成交7100亿,小幅放量,北上资金净买入18.03亿。
-            </span>
-      </div>
-      <img src="@/assets/live/arrow-bottom.png" class="arrow-bottom" />
-    </div>
+<!--    <div class="chat-item live-title-row">-->
+<!--      <div class="live-title-content">-->
+<!--            <span>-->
+<!--              啸谈股经—大势研判、市场点评、行情动态解说-->
+<!--            </span>-->
+<!--        <span class="live-title-desc">-->
+<!--              两市震荡反弹,板块轮动修复周三两市震荡反弹,板块轮动修复。CRO概念,医疗服务,减肥药,BC电池,创新药多有反弹EDR概念,光刻机,Wifi6,酒店及餐饮,黑色家电多有承压。两市上涨2934家,下跌2064家,成交7100亿,小幅放量,北上资金净买入18.03亿。-->
+<!--            </span>-->
+<!--      </div>-->
+<!--      <img src="@/assets/live/arrow-bottom.png" class="arrow-bottom" />-->
+<!--    </div>-->
 
     <!--  聊天内容  -->
-    <div class="chat-item chat-user-item">
-      <span class="chat-user-name">十月:</span>
-      <span class="chat-user-content">今天汪老师在直播间会和大家进行互动么?</span>
+    <div class="chat-item chat-user-item" v-for="item in list" :key="item.id">
+      <span class="chat-user-name">{{ item.user?.nick }}:</span>
+      <span class="chat-user-content">{{ item.content }}</span>
     </div>
 
     <!--  系统通知  -->
-    <div class="chat-item sys-notice" v-for="item in 10" :key="item">
-      欢迎 行知OK 进入直播间
-    </div>
+<!--    <div class="chat-item sys-notice" v-for="item in 10" :key="item">-->
+<!--      欢迎 行知OK 进入直播间-->
+<!--    </div>-->
 
 
   </div>

+ 12 - 1
src/components/live-footer/LiveFooter.vue

@@ -1,19 +1,28 @@
 <script>
+import LiveInput from "@/components/live-input/LiveInput.vue";
+
 export default {
   name: "LiveFooter",
+  components: {LiveInput},
   props: {
     // 是否全屏
     isFullScreen: {
       type: Boolean,
       default: false,
     }
+  },
+  methods: {
+    handleShowInput() {
+      console.log("点击了")
+      this.$refs.liveInput.show();
+    }
   }
 }
 </script>
 
 <template>
   <div class="live-footer" :class="{'horiz': isFullScreen}">
-    <div class="live-input-row">发个弹幕吧~</div>
+    <div class="live-input-row" @click="handleShowInput">发个弹幕吧~</div>
     <div class="live-footer-item" style="padding: 6px 5px">
       <img src="@/assets/cart.png" class="live-footer-icon" />
     </div>
@@ -27,6 +36,8 @@ export default {
       <img src="@/assets/like_active.png" class="live-footer-icon" />
       <span class="live-footer-count">999.9K</span>
     </div>
+
+    <live-input ref="liveInput" v-on="$listeners" />
   </div>
 </template>
 

+ 1 - 1
src/components/live-header/LiveHeader.vue

@@ -41,7 +41,7 @@ export default {
     </div>
     <div class="preside-row">主持人:汪老师 · 职业编号:S0570618010086</div>
     <div class="notice-row">
-      <live-notice />
+      <live-notice v-bind="$attrs" />
     </div>
     <div class="live-status">
       <img src="@/assets/undulate.png" class="undulate-icon" />

+ 89 - 0
src/components/live-input/LiveInput.vue

@@ -0,0 +1,89 @@
+<template>
+  <van-popup v-model="showPopup" position="bottom" get-container="body">
+    <div class="live-input">
+      <div class="input-box">
+        <div class="input-row">
+          <input class="input" ref="input" placeholder="发个弹幕吧~" @blur="onBlur" v-model="inputValue" />
+          <img src="@/assets/live/emote.png" class="emote-icon" />
+        </div>
+        <span class="send-btn" @click="$emit('send', inputValue)">发送</span>
+      </div>
+<!--      <div>键盘底下内容</div>-->
+    </div>
+  </van-popup>
+</template>
+
+<script>
+export default {
+  name: "LiveInput",
+  data() {
+    return {
+      showPopup: false,
+      inputValue: "",
+    };
+  },
+  methods: {
+    onBlur() {
+      this.showPopup = false;
+    },
+
+    show() {
+      this.showPopup = true;
+      this.$nextTick(() => {
+        this.handleOpenKeyboard();
+      })
+    },
+
+    handleOpenKeyboard() {
+      console.log("调用了");
+      this.$refs.input.focus();
+    }
+  }
+};
+</script>
+
+<style scoped>
+.live-input {
+  width: 100%;
+  border-radius: 8px 8px 0 0;
+  box-sizing: border-box;
+  padding: 8px 14px;
+}
+
+.input-box {
+  display: flex;
+  align-items: center;
+  column-gap: 14px;
+}
+
+.input-row {
+  width: 100%; /* 占满整个宽度 */
+  height: 40px;
+  background: #F8F8F8;
+  border-radius: 20px;
+  display: flex;
+  justify-content: space-between;
+  align-items: center;
+  column-gap: 14px;
+  box-sizing: border-box;
+  padding: 0 10px;
+}
+
+.input {
+  flex: 1;
+  border: none;
+  background: #F8F8F8;
+}
+
+.emote-icon {
+  width: 20px;
+  height: 20px;
+}
+
+.send-btn {
+  flex-shrink: 0;
+  font-weight: 500;
+  font-size: 16px;
+  color: #EB3C37;
+}
+</style>

+ 5 - 4
src/components/live-video/LiveVideo.vue

@@ -9,7 +9,7 @@ export default {
     }
   },
   mounted() {
-    this.getWatchCore();
+    // this.getWatchCore();
   },
   methods: {
     async getWatchCore() {
@@ -82,17 +82,18 @@ export default {
 </script>
 
 <template>
-  <div class="live-video" id="liveVideoContainer"></div>
+  <div class="live-video" id="liveVideoContainer">
+    <slot></slot>
+  </div>
 </template>
 
 <style scoped>
 
 .live-video {
   width: 100%;
-  //height: 56.25vw;
 }
 
-/deep/ .plwrap {
+/deep/ .live-video .plwrap {
   background: transparent;
 }
 </style>

+ 36 - 3
src/components/live/LiveInteract.vue

@@ -13,6 +13,10 @@ export default {
     value: {
       type: Boolean,
       default: false,
+    },
+    liveInstance: {
+      type: Object,
+      default: () => ({})
     }
   },
   components: {LiveHeader, LiveFooter, ChatWrapper},
@@ -28,12 +32,41 @@ export default {
 
       // 是否是横屏
       isLandscape: false,
+
+      // 聊天列表
+      chatList: [],
+      // 公告内容
+      noticeContent: null,
     }
   },
   mounted() {
+    this.$nextTick(() => {
+      this.liveInstance.on("init_live", () => {
+        this.liveInstance.getChatContent((list) => {
+          this.chatList = list;
+        });
+        console.log("初始化完成并获取聊天")
+      })
+
+
+      this.liveInstance.on("chat_msg", (data) => {
+        this.chatList.push(data.chatMsg)
+      })
+
+      this.liveInstance.on("on_notice", (data) => {
+        console.log("公告内容", data)
+        this.noticeContent = data?.content;
+      })
 
+    })
   },
   methods: {
+    // 发送消息
+    handleSendMessage(val) {
+      this.liveInstance.sendMessage(val);
+      console.log("val", val)
+    },
+
     // 点击返回
     handleClickBack() {
       // 判断当前状态是否全屏,如果是则退出全屏
@@ -157,10 +190,10 @@ export default {
       </van-swipe-item>
       <van-swipe-item>
         <div class="interact-box">
-          <live-header @back="handleClickBack" :is-full-screen="isLandscape" />
+          <live-header @back="handleClickBack" :is-full-screen="isLandscape" :noticeContent="noticeContent" />
           <div class="footer">
-            <chat-wrapper v-show="!isLandscape" />
-            <live-footer :is-full-screen="isLandscape" />
+            <chat-wrapper :list="chatList" v-show="!isLandscape" />
+            <live-footer :is-full-screen="isLandscape" @send="handleSendMessage" />
             <div class="shop-and-gift">
               <div class="guide-box">
                 <img src="https://picsum.photos/200" class="guide-cover" />

+ 31 - 4
src/components/notcie/LiveNotice.vue

@@ -1,17 +1,44 @@
 <script>
 export default {
-  name: "LiveNotice"
+  name: "LiveNotice",
+  props: {
+  noticeContent: {
+      type: String,
+      default: ""
+    }
+  },
+  data() {
+    return {
+      showNotice: true,
+    }
+  },
+  methods: {
+    // 关闭公告
+    handleCloseNotice() {
+      this.showNotice = false;
+    }
+  },
+  watch: {
+    noticeContent: {
+      handler(val) {
+        if(val && !this.showNotice) {
+          this.showNotice = true;
+        }
+      },
+      immediate: true,
+    }
+  }
 }
 </script>
 
 <template>
-  <van-notice-bar color="#ffffff" background="#5290E0" class="notice-box">
-    公告:官方公告:文明交流,禁止违规交易。投禁止违规交易。
+  <van-notice-bar color="#ffffff" background="#5290E0" class="notice-box" v-if="showNotice">
+    <div v-html="noticeContent"></div>
     <template #left-icon>
       <img src="@/assets/live/notice.png" class="notice-icon" />
     </template>
     <template #right-icon>
-      <van-icon name="cross" color="#ffffff" />
+      <van-icon name="cross" color="#ffffff" @click="handleCloseNotice" />
     </template>
   </van-notice-bar>
 </template>

+ 125 - 0
src/utils/Live.js

@@ -0,0 +1,125 @@
+import { createWatchCore, PlayerEvents, ChatEvents} from '@polyv/live-watch-sdk';
+
+export default class Live {
+    watchCore = null;
+    channelId = "4247961";
+    /**
+     * 回调方法:
+     *  init_live 初始化完成
+     *  chat_msg 监听聊天信息
+     *  on_notice 监听公告
+     * @type {{}}
+     */
+    callbackMap = {}
+
+    constructor(channelId) {
+        this.channelId = channelId;
+    }
+
+
+    async initWatchCore(callback) {
+        this.watchCore = createWatchCore({
+            channelId: this.channelId,
+            userInfo: {
+                userId: "e6soizn34h"
+            }
+        })
+        try {
+            await this.watchAuth()
+            await this.watchCore.setup();
+            // 注册互动sdk
+            await this.watchCore.interactReceive.setupIarCore();
+            // 连接
+            await this.watchCore.connect();
+            callback && callback(this.watchCore);
+            this.emit("init_live", this.watchCore);
+
+            this.getNotice();
+        } catch (err) {
+            console.error(err);
+        }
+
+    }
+
+    // 观看授权
+    watchAuth() {
+        return new Promise(  (resolve, reject) => {
+            this.watchCore.auth.verifyNoneAuth().then(result => {
+                if (result.success) {
+                    resolve(result)
+                } else {
+                    reject(result)
+                }
+            });
+        })
+    }
+
+
+    // 创建播放器
+    async createPlayer(container) {
+        this.watchCore.player.eventEmitter.on(PlayerEvents.PlayerInfoChange, (data) => {
+            const playerInfo = data.playerInfo;
+            console.log('播放状态', playerInfo.playStatus);
+        });
+        await this.watchCore.player.setupPlayer({
+            container,
+            barrageEnabled: false,
+            autoplay: true,
+            barrageShow: false,
+            showController: false,
+            // lowLatency: true, // 使用无延迟播放
+        });
+    }
+
+    // 获取聊天内容
+    async getChatContent(callback) {
+        const list = await this.watchCore.chat.getChatHistory();
+        callback && callback(list);
+        console.log("聊天内容", list);
+        this.listenChat();
+    }
+
+    // 监听聊天信息
+    listenChat() {
+        this.watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
+            console.log("聊天信息", data);
+            this.emit("chat_msg", data);
+        });
+    }
+
+    // 发送消息
+    sendMessage(content) {
+        this.watchCore.chat.sendSpeakMsg({
+            content
+        })
+    }
+
+    // 获取公告
+    getNotice() {
+        const announcementSdk = this.watchCore.interactReceive.getAnnouncement();
+        announcementSdk.on(announcementSdk.events.BULLETIN, (msg) => {
+            this.emit("on_notice", msg);
+        });
+    }
+
+    on(key, callback) {
+        if (!this.callbackMap[key]) {
+            this.callbackMap[key] = new Set();
+        }
+        this.callbackMap[key].add(callback);
+    }
+
+    emit(key, data) {
+        if (this.callbackMap[key]) {
+            this.callbackMap[key].forEach((callback) => {
+                callback(data);
+            })
+        }
+    }
+
+    off(key, callback) {
+        if (!this.callbackMap[key]) return;
+        this.callbackMap[key].delete(callback);
+    }
+
+}

+ 16 - 2
src/views/live/LiveItem.vue

@@ -1,6 +1,7 @@
 <script>
 import LiveInteract from "@/components/live/LiveInteract.vue";
 import LiveVideo from "@/components/live-video/LiveVideo.vue";
+import Live from "@/utils/Live";
 
 export default {
   name: "LiveItem",
@@ -8,8 +9,19 @@ export default {
   data() {
     return {
       fullScreen: false,
+      // 直播实例
+      liveInstance: null
     }
   },
+  mounted() {
+    this.liveInstance = new Live(4247961);
+    this.liveInstance.initWatchCore(() => {
+      this.liveInstance.createPlayer(document.getElementById("livePlayer"))
+
+      // 测试获取公告
+
+    });
+  },
   methods: {
     handleClick() {
       console.log("测试点击")
@@ -23,7 +35,9 @@ export default {
   <div class="live-item">
     <div class="live-video-box">
       <div class="video-box" :class="{'full-video-box': fullScreen}">
-        <live-video />
+        <live-video>
+          <div id="livePlayer"></div>
+        </live-video>
       </div>
       <div class="video-tip">
         市场有风险 投资需谨慎
@@ -31,7 +45,7 @@ export default {
       <img src="@/assets/live/live-full.png" class="full-icon" @click="fullScreen = true" />
     </div>
 
-    <live-interact class="interact1" v-model="fullScreen"  style="flex: 1" />
+    <live-interact :liveInstance="liveInstance" class="interact1" v-model="fullScreen"  style="flex: 1" />
   </div>
 </template>
 

+ 26 - 3
src/views/live/VerticalLive.vue

@@ -1,20 +1,42 @@
 <script>
 import LiveInteract from "@/components/live/LiveInteract.vue";
+import Live from "@/utils/Live";
+import LiveVideo from "@/components/live-video/LiveVideo.vue";
 
 export default {
   name: "VerticalLive",
-  components: {LiveInteract}
+  components: {LiveVideo, LiveInteract},
+  data() {
+    return {
+      fullScreen: false,
+      // 直播实例
+      liveInstance: null
+    }
+  },
+  mounted() {
+    this.liveInstance = new Live(4247961);
+    this.liveInstance.initWatchCore(() => {
+      this.liveInstance.createPlayer(document.getElementById("livePlayer"))
+
+      // 测试获取公告
+
+    });
+  },
 }
 </script>
 
 <template>
   <div>
-    <div class="video-box"></div>
+    <div class="video-box">
+      <live-video>
+        <div id="livePlayer"></div>
+      </live-video>
+    </div>
 
 <!--    <div class="live-video-box">-->
 <!--      <div class="video-tip">市场有风险 投资需谨慎 </div>-->
 <!--    </div>-->
-    <live-interact class="interact"  style="flex: 1" />
+    <live-interact :live-instance="liveInstance" class="interact"  style="flex: 1" />
   </div>
 </template>
 
@@ -32,6 +54,7 @@ export default {
   left: 0;
   width: 100vw;
   height: 100vh;
+  z-index: 9;
 }
 
 </style>

+ 1 - 0
src/views/test/Test.vue

@@ -28,6 +28,7 @@ export default {
         autoplay
         :muted="muted"
         playsInline
+        controls
         src="https://vod.pipi.cn/fec9203cvodtransbj1251246104/b40735381397757895972901262/v.f42905.mp4"></video>
 
     <button @click="handlePlay">播放</button>