index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <!-- 直播中控台 start -->
  3. <el-row type="flex" justify="center" class="live-console" :gutter="10">
  4. <!-- 用户列表 start -->
  5. <el-col class="live-console-col" :span="5">
  6. <el-tabs class="live-console-tab-left" v-model="tabLeft.activeName" @tab-click="handleClick">
  7. <el-tab-pane :label="onlineLabel" name="online">
  8. <el-scrollbar ref="manageLeftRef" style="height: 800px; width: 100%;">
  9. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in onlineUserList">
  10. <el-col :span="5"><el-avatar :src="u.avatar"></el-avatar></el-col>
  11. <el-col :span="12" :offset="2">{{ u.nickName }}</el-col>
  12. <el-col :span="3" >
  13. <el-switch
  14. v-model="u.msgStatus"
  15. :active-value="0"
  16. :inactive-value="1"
  17. active-color="#13ce66"
  18. inactive-color="#ff4949">
  19. </el-switch>
  20. </el-col>
  21. </el-row>
  22. </el-scrollbar>
  23. </el-tab-pane>
  24. <el-tab-pane :label="offlineLabel" name="offline">
  25. <el-scrollbar ref="manageLeftRef" style="height: 100%; width: 100%;">
  26. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in offlineUserList">
  27. <el-col :span="5"><el-avatar :src="u.avatar"></el-avatar></el-col>
  28. <el-col :span="12" :offset="2">{{ u.nickName }}</el-col>
  29. <el-col :span="3" >
  30. <el-switch
  31. v-model="u.msgStatus"
  32. :active-value="0"
  33. :inactive-value="1"
  34. active-color="#13ce66"
  35. inactive-color="#ff4949">
  36. </el-switch>
  37. </el-col>
  38. </el-row>
  39. </el-scrollbar>
  40. </el-tab-pane>
  41. <el-tab-pane :label="silencedUserLabel" name="silenced">
  42. <el-scrollbar ref="manageLeftRef" style="height: 100%; width: 100%;">
  43. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in silencedUserList">
  44. <el-col :span="5"><el-avatar :src="u.avatar"></el-avatar></el-col>
  45. <el-col :span="12" :offset="2">{{ u.nickName }}</el-col>
  46. <el-col :span="3" >
  47. <el-switch
  48. v-model="u.msgStatus"
  49. :active-value="0"
  50. :inactive-value="1"
  51. active-color="#13ce66"
  52. inactive-color="#ff4949">
  53. </el-switch>
  54. </el-col>
  55. </el-row>
  56. </el-scrollbar>
  57. </el-tab-pane>
  58. </el-tabs>
  59. </el-col>
  60. <!-- 用户列表 end -->
  61. <!-- 直播/视频 start -->
  62. <el-col class="live-console-col" :span="10">
  63. <video controls autoplay :src="this.liveVideo.videoUrl" width="100%"></video>
  64. </el-col>
  65. <!-- 直播/视频 end -->
  66. <!-- 聊天 start -->
  67. <el-col class="live-console-col" :span="5">
  68. <el-tabs class="live-console-tab-right" v-model="tabRight.activeName" @tab-click="handleClick">
  69. <el-tab-pane label="聊天" name="talk">
  70. <el-scrollbar style="height: 500px; width: 300px;" ref="manageRightRef">
  71. <el-row v-for="m in msgList">
  72. <el-row style="margin-top: 10px" type="flex" align="top">
  73. <el-col :span="3" :offset="1"><el-avatar :src="m.avatar"/></el-col>
  74. <el-col :span="15">
  75. <el-row>
  76. <el-col>{{ m.nickName }}</el-col>
  77. <el-col>{{ m.msg }}</el-col>
  78. </el-row>
  79. </el-col>
  80. </el-row>
  81. </el-row>
  82. </el-scrollbar>
  83. <!-- 消息输入区域 -->
  84. <div class="chat-input">
  85. <el-input
  86. v-model="newMsg"
  87. placeholder="请输入消息..."
  88. @keyup.enter.native="sendMessage"
  89. clearable
  90. ></el-input>
  91. <el-button type="primary" @click="sendMessage">发送</el-button>
  92. </div>
  93. </el-tab-pane>
  94. </el-tabs>
  95. </el-col>
  96. <!-- 聊天 end -->
  97. </el-row>
  98. <!-- 直播中控台 end -->
  99. </template>
  100. <script>
  101. import { changeUserStatus, watchUserList } from '@/api/live/liveWatchUser'
  102. import { getLiveVideoByLiveId } from '@/api/live/liveVideo'
  103. import { listLiveMsg } from '@/api/live/liveMsg'
  104. import { LiveWS } from '@/utils/webSocket'
  105. export default {
  106. name: "LiveConsole",
  107. data() {
  108. return {
  109. tabLeft: {
  110. activeName: "online",
  111. },
  112. tabRight: {
  113. activeName: "talk",
  114. },
  115. liveVideo: {},
  116. socket: null,
  117. userParams:{
  118. pageNum: 1,
  119. pageSize: 10,
  120. liveId: null
  121. },
  122. msgParams: {
  123. pageNum: 1,
  124. pageSize: 10,
  125. liveId: null
  126. },
  127. userList: [],
  128. msgList: [],
  129. newMsg: ''
  130. }
  131. },
  132. created() {
  133. this.getLiveVideo()
  134. this.getList()
  135. this.connectWebSocket()
  136. },
  137. computed: {
  138. liveId() {
  139. return this.$route.params.liveId;
  140. },
  141. userId() {
  142. return this.$store.state.user.user.userId
  143. },
  144. onlineUserList() {
  145. return this.userList.filter(u => u.online === 0)
  146. },
  147. onlineLabel() {
  148. if (this.onlineUserList.length > 0) {
  149. return '在线(' + this.onlineUserList.length + ')'
  150. }
  151. return '在线'
  152. },
  153. offlineUserList() {
  154. return this.userList.filter(u => u.online === 1)
  155. },
  156. offlineLabel() {
  157. if (this.offlineUserList.length > 0) {
  158. return '离线(' + this.offlineUserList.length + ')'
  159. }
  160. return '离线'
  161. },
  162. silencedUserList() {
  163. return this.userList.filter(u => u.msgStatus === 1)
  164. },
  165. silencedUserLabel() {
  166. if (this.silencedUserList.length > 0) {
  167. return '禁言(' + this.silencedUserList.length + ')'
  168. }
  169. return '禁言'
  170. }
  171. },
  172. methods: {
  173. handleClick(tab) {
  174. console.log("click",tab.name)
  175. console.log("liveId", this.liveId)
  176. },
  177. getLiveVideo() {
  178. getLiveVideoByLiveId(this.liveId).then(res => {
  179. this.liveVideo = res.data
  180. })
  181. },
  182. getList() {
  183. this.resetParams()
  184. this.loadUserList()
  185. this.loadMsgList()
  186. },
  187. resetParams() {
  188. this.userList= []
  189. this.userParams = {
  190. pageNum: 1,
  191. pageSize: 10,
  192. liveId: this.liveId
  193. }
  194. this.msgList = []
  195. this.msgParams = {
  196. pageNum: 1,
  197. pageSize: 10,
  198. liveId: this.liveId
  199. }
  200. },
  201. loadUserList() {
  202. // 直播间用户
  203. watchUserList({
  204. liveId: this.liveId,
  205. pageNum: this.userParams.pageNum,
  206. pageSize: this.userParams.pageSize
  207. }).then(response => {
  208. let {rows,total} = response;
  209. this.userParams.pageNum = (this.userParams.pageNum - 1) * this.userParams.pageSize;
  210. rows.forEach(row => {
  211. if (!this.userList.some(u => u.userId === row.userId)) {
  212. this.userList.push(row)
  213. }
  214. })
  215. // 没加载完继续加载
  216. if (this.userList.length < total) {
  217. this.loadUserList()
  218. }
  219. })
  220. },
  221. loadMsgList() {
  222. // 直播间消息
  223. listLiveMsg({
  224. liveId:this.liveId,
  225. pageNum: this.msgParams.pageNum,
  226. pageSize: this.msgParams.pageSize
  227. }).then(response => {
  228. let {rows,total} = response;
  229. console.log(rows);
  230. this.msgParams.pageNum = (this.msgParams.pageNum - 1) * this.msgParams.pageSize;
  231. rows.forEach(row => {
  232. if (!this.msgList.some(m => m.msgId === row.msgId)) {
  233. this.msgList.push(row)
  234. // 移动到底部
  235. this.$nextTick(() => {
  236. setTimeout(() => {
  237. this.$refs.manageRightRef.wrap.scrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  238. }, 200)
  239. })
  240. }
  241. })
  242. // 没加载完继续加载
  243. if (this.msgList.length < total) {
  244. this.loadMsgList()
  245. }
  246. })
  247. // 添加滚动监听
  248. this.$nextTick(() => {
  249. this.$refs.manageRightRef.wrap.addEventListener("scroll", this.manageRightScroll)
  250. })
  251. },
  252. manageRightScroll() {
  253. let max = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  254. let current = this.$refs.manageRightRef.wrap.scrollTop
  255. console.log("manageRightMax", max);
  256. console.log("manageRight", current)
  257. },
  258. changeUserState(u) {
  259. // 还原状态
  260. u.msgStatus = u.msgStatus === 0 ? 1 : 0
  261. // 修改状态
  262. changeUserStatus({liveId: u.liveId, userId: u.userId}).then(response => {
  263. let { code } = response;
  264. if (200 === code) {
  265. u.msgStatus = u.msgStatus === 0 ? 1 : 0
  266. let msg = u.msgStatus === 0 ? "已解禁" : "已禁言"
  267. this.msgSuccess(msg);
  268. return
  269. }
  270. this.msgError("操作失败");
  271. })
  272. },
  273. connectWebSocket() {
  274. // M3: WebSocket 地址从环境变量读取,缺省回退本地调试地址
  275. const wsUrl = process.env.VUE_APP_WS_URL || 'ws://localhost:7114/app/webSocket';
  276. let socket = new LiveWS(wsUrl, this.liveId, this.userId);
  277. socket.ws.onmessage = (e) => {
  278. this.handleWsMessage(e)
  279. }
  280. this.socket = socket
  281. },
  282. handleWsMessage(event) {
  283. console.log(event.data);
  284. },
  285. sendMessage() {
  286. console.log(this.newMsg);
  287. // 发送前简单校验
  288. if (this.newMsg.trim() === '') {
  289. return;
  290. }
  291. this.msgList.push({
  292. msg: this.newMsg,
  293. liveId: this.liveId,
  294. userId: this.userId,
  295. avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM4Rv7nYYRvbQcJD9OogfiaMWDj8LTiceZ5J3Bm6Ph9pIiac9MVcwdfwPUI9XQ50ibo4TFp0ZHjgzLH6dw/132',
  296. nickName: '测试123123'
  297. })
  298. this.newMsg = '';
  299. this.$nextTick(() => {
  300. // 新消息加入后自动滚动到底部
  301. if (this.$refs.manageRightRef) {
  302. this.$refs.manageRightRef.update();
  303. const scrollDom = this.$refs.manageRightRef.$el.querySelector('.el-scrollbar__wrap');
  304. scrollDom.scrollTop = scrollDom.scrollHeight;
  305. }
  306. });
  307. }
  308. },
  309. destroyed() {
  310. this.socket?.close()
  311. }
  312. }
  313. </script>
  314. <style scoped>
  315. .live-console {
  316. width: 90vw;
  317. padding: 10px 0;
  318. }
  319. .live-console .live-console-col {
  320. height: 88vh;
  321. margin-left: 5px;
  322. padding: 0 10px;
  323. background-color: white;
  324. border-radius: 4px;
  325. }
  326. /*隐藏水平滚动条*/
  327. ::v-deep .el-scrollbar__wrap {
  328. overflow-x: hidden;
  329. }
  330. /* 消息输入区域 */
  331. .chat-input {
  332. display: flex;
  333. padding: 10px;
  334. border-top: 1px solid #ebeef5;
  335. background-color: #fff;
  336. min-height: 60px;
  337. }
  338. .chat-input .el-input {
  339. flex: 1;
  340. margin-right: 10px;
  341. min-height: 40px;
  342. }
  343. </style>