index.vue 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. <template>
  2. <!-- 直播中控台 start -->
  3. <el-row type="flex" justify="center" class="live-console live-page" :gutter="10" v-loading="loading">
  4. <!-- 聊天 start -->
  5. <el-col class="live-console-col" :span="6">
  6. <el-tabs class="live-console-tab-left" v-model="tabRight.activeName" @tab-click="handleClick" :stretch="true">
  7. <el-tab-pane label="讨论" name="talk">
  8. <el-scrollbar class="chat-scroll" ref="manageRightRef">
  9. <div class="chat-msg" v-for="m in msgList" :key="m.msgId || m.userId + '-' + m.msg">
  10. <div v-if="m.userId !== userId" class="chat-msg__row chat-msg__row--left">
  11. <el-avatar class="chat-msg__avatar" :src="m.avatar!==null?m.avatar: require('@/assets/image/default-a.png')"/>
  12. <div class="chat-msg__body">
  13. <div class="chat-msg__meta">
  14. <span class="chat-msg__name">{{ m.nickName }}</span>
  15. <div class="chat-msg__ops">
  16. <a class="chat-msg__op" @click="changeUserState(m)">{{ m.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  17. <a class="chat-msg__op" @click="blockUser(m)">拉黑</a>
  18. <a class="chat-msg__op chat-msg__op--danger" @click="deleteMsg(m)">删除</a>
  19. </div>
  20. </div>
  21. <div class="msg-box">{{ m.msg }}</div>
  22. </div>
  23. </div>
  24. <div v-else class="chat-msg__row chat-msg__row--right">
  25. <div class="chat-msg__body chat-msg__body--right">
  26. <div class="chat-msg__meta chat-msg__meta--right">
  27. <span class="chat-msg__name">{{ m.nickName }}</span>
  28. <el-avatar class="chat-msg__avatar" :src="m.avatar?m.avatar: require('@/assets/image/default-a.png')"/>
  29. </div>
  30. <div class="msg-box-right">{{ m.msg }}</div>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="chat-scroll__spacer"></div>
  35. </el-scrollbar>
  36. <!-- 消息输入区域 -->
  37. <div class="shuru">
  38. <el-input
  39. type="textarea"
  40. v-model="newMsg"
  41. placeholder="请输入消息..."
  42. :rows="3"
  43. @keyup.enter.native="sendMessage"
  44. clearable
  45. resize="none"
  46. >
  47. </el-input>
  48. <div class="shuru__actions">
  49. <el-button class="btn-accent" @click="showTopMsgDialog">顶部消息</el-button>
  50. <el-button class="btn-accent" @click="sendMessage">发送</el-button>
  51. </div>
  52. </div>
  53. </el-tab-pane>
  54. </el-tabs>
  55. </el-col>
  56. <!-- 聊天 end -->
  57. <!-- 直播/视频 start -->
  58. <el-col class="live-console-col" :span="12">
  59. <div class="player-panel">
  60. <div class="player-panel__inner" v-if="!isAudit">
  61. <img :src="require('@/assets/image/videoIsAudit.png')" class="player-panel__poster">
  62. </div>
  63. <div class="player-panel__inner" v-else-if="status != 2 && status != 4">
  64. <img :src="require('@/assets/image/videoNotStart.png')" class="player-panel__poster">
  65. </div>
  66. <div class="player-panel__inner" v-else-if="liveType == 1">
  67. <video
  68. id="live-tcplayer-container"
  69. ref="livingPlayer"
  70. preload="auto"
  71. playsinline
  72. webkit-playsinline
  73. class="custom-video tcplayer-live"
  74. style="display: block; background: #000; height: 45vh; width: 100%;"
  75. ></video>
  76. <!-- 时间显示(可选) -->
  77. <div ref="liveElapsedTime" class="time-display">
  78. 已播放:<span id="liveElapsedTime">00:00:00</span>
  79. </div>
  80. </div>
  81. <div class="player-panel__inner" v-else-if="liveType == 2">
  82. <video
  83. controls
  84. ref="videoPlayer"
  85. loop
  86. autoplay
  87. width="100%"
  88. muted
  89. playsinline
  90. @click.prevent
  91. @contextmenu.prevent
  92. class="custom-video"
  93. style="display: block; background: #000; height: 40vh;"
  94. >
  95. <source :src="videoUrl" type="application/x-mpegURL">
  96. </video>
  97. <!-- 自定义进度条容器 -->
  98. <div ref="progressBar" class="progress-container">
  99. <div id="progressBar" class="progress-bar"></div>
  100. </div>
  101. <!-- 时间显示(可选) -->
  102. <div ref="elapsedTime" class="time-display">
  103. 已播放:<span id="elapsedTime">00:00:00</span>
  104. </div>
  105. </div>
  106. <div class="player-panel__inner" v-else-if="liveType == 3">
  107. <video
  108. controls
  109. ref="liveReplay"
  110. loop
  111. autoplay
  112. width="100%"
  113. playsinline
  114. style="display: block; background: #000; height: 40vh;"
  115. >
  116. <source :src="videoUrl" type="application/x-mpegURL">
  117. </video>
  118. </div>
  119. <div class="player-panel__inner" v-else>
  120. <img :src="require('@/assets/image/videoNotStart.png')" class="player-panel__poster">
  121. </div>
  122. </div>
  123. <!-- 底部导航栏 -->
  124. <div class="tool-bar">
  125. <div class="top-box" @click="handleClickRed">
  126. <img src="@/assets/image/hbpz.png"/>
  127. <span>红包配置</span>
  128. </div>
  129. <div class="top-box" @click="handleClickLottery">
  130. <img src="@/assets/image/cjpz.png"/>
  131. <span>抽奖配置</span>
  132. </div>
  133. <div class="top-box" @click="handleClickGoods">
  134. <img src="@/assets/image/sp.png"/>
  135. <span>商品</span>
  136. </div>
  137. <div class="top-box" @click="handleClickOrder">
  138. <img src="@/assets/image/zbdd.png"/>
  139. <span>直播订单</span>
  140. </div>
  141. <div class="top-box" @click="handleClickCoupon">
  142. <img src="@/assets/image/zbyhq.png"/>
  143. <span>直播优惠券</span>
  144. </div>
  145. </div>
  146. <!-- <el-radio-group v-model="tableRadio" >
  147. <el-radio-button label="订单数">订单数</el-radio-button>
  148. </el-radio-group> -->
  149. <div class="chart-panel">
  150. <div ref="chartContainer" class="chart-panel__canvas"></div>
  151. <div class="chart-panel__filters">
  152. <el-select v-model="searchQuery.timeOptions" placeholder="请选择" style="width: 150px" @change="timeChange">
  153. <el-option
  154. v-for="item in timeOptions"
  155. :key="item.value"
  156. :label="item.label"
  157. :value="item.value">
  158. </el-option>
  159. </el-select>
  160. <el-select v-model="searchQuery.timeGranularity" placeholder="请选择" style="width: 150px" @change="timeGranularityChange">
  161. <el-option
  162. v-for="item in timeGranularity"
  163. :key="item.value"
  164. :label="item.label"
  165. :value="item.value">
  166. </el-option>
  167. </el-select>
  168. <!-- <el-button type="primary" @click="applyFilter">搜索</el-button>-->
  169. </div>
  170. </div>
  171. </el-col>
  172. <!-- 直播/视频 end -->
  173. <!-- 用户列表 start -->
  174. <el-col class="live-console-col" :span="6">
  175. <el-tabs class="live-console-tab-right" v-model="tabLeft.activeName" @tab-click="handleClick" :stretch="true">
  176. <el-tab-pane :label="onlineLabel" name="online" style="height: 100%;">
  177. <el-scrollbar ref="manageLeftRef_online" class="user-scroll">
  178. <div class="row-box" v-for="u in onlineDisplayList" :key="u.userId">
  179. <el-avatar class="user-row__avatar" :src="u.avatar"></el-avatar>
  180. <div class="user-row__info">
  181. <div class="user-row__name">{{ u.nickName }}</div>
  182. <div class="user-row__id">{{ u.userId }}</div>
  183. </div>
  184. <el-popover width="100" trigger="click">
  185. <div class="user-row__pop">
  186. <a class="user-row__pop-op" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  187. <a class="user-row__pop-op" @click="blockUser(u)">拉黑</a>
  188. </div>
  189. <i class="el-icon-more" slot="reference"></i>
  190. </el-popover>
  191. </div>
  192. </el-scrollbar>
  193. </el-tab-pane>
  194. <el-tab-pane :label="offlineLabel" name="offline" style="height: 100%;">
  195. <el-scrollbar ref="manageLeftRef_offline" class="user-scroll">
  196. <div class="row-box" v-for="u in offlineDisplayList" :key="u.userId">
  197. <el-avatar class="user-row__avatar" :src="u.avatar"></el-avatar>
  198. <div class="user-row__info">
  199. <div class="user-row__name">{{ u.nickName }}</div>
  200. <div class="user-row__id">{{ u.userId }}</div>
  201. </div>
  202. <el-popover width="100" trigger="click">
  203. <div class="user-row__pop">
  204. <a class="user-row__pop-op" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  205. <a class="user-row__pop-op" @click="blockUser(u)">拉黑</a>
  206. </div>
  207. <i class="el-icon-more" slot="reference"></i>
  208. </el-popover>
  209. </div>
  210. </el-scrollbar>
  211. </el-tab-pane>
  212. <el-tab-pane :label="silencedUserLabel" name="silenced" style="height: 100%;">
  213. <el-scrollbar ref="manageLeftRef_silenced" class="user-scroll user-scroll--tall">
  214. <div class="row-box" v-for="u in silencedDisplayList" :key="u.userId">
  215. <el-avatar class="user-row__avatar" :src="u.avatar"></el-avatar>
  216. <div class="user-row__info">
  217. <div class="user-row__name">{{ u.nickName }}</div>
  218. <div class="user-row__id">{{ u.userId }}</div>
  219. </div>
  220. <el-popover width="100" trigger="click">
  221. <div class="user-row__pop">
  222. <a class="user-row__pop-op" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  223. <a class="user-row__pop-op" @click="blockUser(u)">拉黑</a>
  224. </div>
  225. <i class="el-icon-more" slot="reference"></i>
  226. </el-popover>
  227. </div>
  228. </el-scrollbar>
  229. </el-tab-pane>
  230. </el-tabs>
  231. </el-col>
  232. <!-- 顶部消息对话框 -->
  233. <el-dialog title="发送顶部消息" :visible.sync="topMsgDialogVisible" width="500px">
  234. <el-form :model="topMsgForm" label-width="100px">
  235. <el-form-item label="消息内容">
  236. <el-input
  237. type="textarea"
  238. v-model="topMsgForm.msg"
  239. placeholder="请输入消息内容"
  240. :rows="3"
  241. ></el-input>
  242. </el-form-item>
  243. <el-form-item label="显示时长">
  244. <el-input-number
  245. v-model="topMsgForm.duration"
  246. :min="1"
  247. :max="60"
  248. placeholder="请输入显示时长(分钟)"
  249. ></el-input-number>
  250. <span style="margin-left: 10px;">分钟</span>
  251. </el-form-item>
  252. </el-form>
  253. <div slot="footer" class="dialog-footer">
  254. <el-button @click="topMsgDialogVisible = false">取 消</el-button>
  255. <el-button type="primary" @click="sendTopMessage">确 定</el-button>
  256. </div>
  257. </el-dialog>
  258. <!-- 用户列表 end -->
  259. </el-row>
  260. <!-- 直播中控台 end -->
  261. </template>
  262. <script>
  263. import {blockUser, changeUserStatus, getLiveUserTotals, watchUserList} from '@/api/live/liveWatchUser'
  264. import { getLiveVideoByLiveId } from '@/api/live/liveVideo'
  265. import {getLivingUrl, getLive, delLive} from '@/api/live/live'
  266. import { getLiveOrderTimeGranularity } from '@/api/live/liveOrder'
  267. import { listLiveMsg,delLiveMsg } from '@/api/live/liveMsg'
  268. import Hls from 'hls.js';
  269. import {onBeforeUnmount} from 'vue';
  270. import LiveLotteryConf from '@/views/live/liveConfig/liveLotteryConf.vue'
  271. import LiveRedConf from '@/views/live/liveConfig/liveRedConf.vue'
  272. import LiveGoods from '@/views/live/liveConfig/goods.vue'
  273. import LiveOrder from '@/views/live/liveOrder/index.vue'
  274. import LiveCoupon from '@/views/live/liveConfig/liveCoupon.vue'
  275. import echarts from 'echarts'
  276. export default {
  277. name: "LiveConsole",
  278. components: { LiveLotteryConf,LiveRedConf,LiveGoods },
  279. data() {
  280. return {
  281. loading: true,
  282. tabLeft: {
  283. activeName: "online",
  284. },
  285. tabRight: {
  286. activeName: "talk",
  287. },
  288. livingUrl: "",
  289. videoUrl: "",
  290. tcPlayer: null,
  291. tcPlayerSdkReady: false,
  292. status: 0,
  293. loadMsgMaxPage: 2,
  294. his: null,
  295. liveVideo: {},
  296. socket: null,
  297. liveWsUrl: process.env.VUE_APP_LIVE_WS_URL + '/app/webSocket',
  298. userParams:{
  299. pageNum: 1,
  300. pageSize: 10,
  301. liveId: null
  302. },
  303. msgParams: {
  304. pageNum: 1,
  305. pageSize: 10,
  306. liveId: null
  307. },
  308. userList: [],
  309. msgList: [],
  310. newMsg: '',
  311. isAudit: false,
  312. myChart: null, // 用于存储 ECharts 实例
  313. liveType: 1,
  314. tableRadio: '订单数',
  315. // ... 其他数据 ...
  316. searchQuery: {timeOptions:'2',timeGranularity:'10',liveId: null}, // 搜索查询条件
  317. timeOptions: [
  318. {value:'2',label:'最近2小时',key:'2'},
  319. {value:'4',label:'最近4小时',key:'4'},
  320. {value:'all',label:'全场',key:'all'},
  321. ],
  322. timeGranularity: [
  323. {value:'10',label:'10分钟',key:'10'},
  324. {value:'30',label:'30分钟',key:'30'},
  325. {value:'60',label:'1小时',key:'60'},
  326. ],
  327. videoDuration: 0,
  328. startTime: null,
  329. processInterval: null,
  330. // ... 其他数据
  331. chatScrollTop: 0, // 保存聊天滚动位置,
  332. liveId: null,
  333. userTotal: {
  334. online: 0, // 在线总人数
  335. offline: 0, // 离线总人数
  336. silenced: 0 // 禁言总人数
  337. },
  338. // 各Tab的显示列表(仅存储当前需要展示的数据)
  339. onlineDisplayList: [], // 在线用户显示列表
  340. offlineDisplayList: [], // 离线用户显示列表
  341. silencedDisplayList: [], // 禁言用户显示列表
  342. // 各Tab的分页参数
  343. pageParams: {
  344. online: {
  345. currentPage: 1, // 当前页(下一页加载用)
  346. pageSize: 20, // 当前页(下一页加载用)
  347. prevPage: 0, // 上一页页码(上一页加载用)
  348. totalLoaded: 0, // 已加载总条数
  349. total: 0, // 总数据量
  350. hasMore: true, // 是否有下一页
  351. hasPrev: false // 是否有上一页
  352. },
  353. offline: {
  354. currentPage: 1,
  355. pageSize: 20,
  356. prevPage: 0,
  357. totalLoaded: 0,
  358. total: 0,
  359. hasMore: true,
  360. hasPrev: false
  361. },
  362. silenced: {
  363. currentPage: 1,
  364. pageSize: 20,
  365. prevPage: 0,
  366. totalLoaded: 0,
  367. total: 0,
  368. hasMore: true,
  369. hasPrev: false
  370. }
  371. },
  372. scrLoading: {
  373. online: { next: false, prev: false },
  374. offline: { next: false, prev: false },
  375. silenced: { next: false, prev: false }
  376. },
  377. topMsgDialogVisible: false,
  378. topMsgForm: {
  379. msg: '',
  380. duration: 5
  381. }
  382. }
  383. },
  384. created() {
  385. if (this.$route.params.liveId) {
  386. this.liveId = this.$route.params.liveId;
  387. }else {
  388. this.liveId = this.$route.query.liveId;
  389. }
  390. // this.getLiveVideo()
  391. this.getList()
  392. this.connectWebSocket()
  393. this.getLive()
  394. this.searchQuery.liveId = this.liveId
  395. },
  396. computed: {
  397. userId() {
  398. return this.$store.state.user.user.userId
  399. },
  400. companyId() {
  401. return this.$store.state.user.user.companyId
  402. },
  403. onlineLabel() {
  404. return `在线(${this.userTotal.online})`;
  405. },
  406. offlineLabel() {
  407. return `离线(${this.userTotal.offline})`;
  408. },
  409. silencedUserLabel() {
  410. return `禁言(${this.userTotal.silenced})`;
  411. }
  412. },
  413. mounted() {
  414. this.$nextTick(() => {
  415. this.restoreChatScrollPosition();
  416. });
  417. this.getEchartsTables();
  418. // 添加滚动事件监听器
  419. this.$nextTick(() => {
  420. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  421. this.$refs.manageRightRef.wrap.addEventListener('scroll', this.saveChatScrollPosition);
  422. }
  423. });
  424. this.initScrollListeners();
  425. },
  426. beforeDestroy() {
  427. this.saveTabScrollPositions()
  428. // 移除滚动监听(避免内存泄漏)
  429. const scrollRefs = {
  430. online: this.$refs.manageLeftRef_online,
  431. offline: this.$refs.manageLeftRef_offline,
  432. silenced: this.$refs.manageLeftRef_silenced
  433. };
  434. Object.keys(scrollRefs).forEach(tabName => {
  435. const scrollEl = scrollRefs[tabName]?.wrap;
  436. if (scrollEl) {
  437. scrollEl.removeEventListener('scroll', () =>
  438. this.handleTabScroll(tabName, scrollEl)
  439. );
  440. }
  441. })
  442. this.releaseWebSocket()
  443. },
  444. // 使用 deactivated 和 activated 钩子替代 beforeDestroy 和 destroyed
  445. deactivated() {
  446. this.saveChatScrollPosition();
  447. this.destroyTcPlayer();
  448. },
  449. activated() {
  450. this.$nextTick(() => {
  451. this.restoreChatScrollPosition();
  452. });
  453. this.$nextTick(() => {
  454. if (this.liveType === 1 && this.livingUrl) {
  455. this.initPlayer();
  456. } else {
  457. const video = this.$refs.videoPlayer;
  458. if (video != null) {
  459. this.initVideoPlayer(this.liveInfo.startTime);
  460. }
  461. }
  462. });
  463. },
  464. methods: {
  465. deleteMsg(m){
  466. // 1. 弹出确认对话框
  467. this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
  468. confirmButtonText: '确定',
  469. cancelButtonText: '取消',
  470. type: 'warning'
  471. }).then(() => {
  472. delLiveMsg(m.msgId).then(res => {
  473. if (res.code === 200) {
  474. const index = this.msgList.findIndex(item => item.msgId === m.msgId);
  475. if (index !== -1) {
  476. this.msgList.splice(index, 1);
  477. console.log(`消息 ${m.msgId} 已在本地删除。`);
  478. }
  479. let msg = {
  480. liveId: this.liveId,
  481. userId: m.userId,
  482. msg: m.msgId, // 关键:将消息ID发送给后台
  483. cmd: 'deleteMsg',
  484. };
  485. this.socket.send(JSON.stringify(msg));
  486. // 可以在这里给用户一个删除成功的提示
  487. this.$message({
  488. type: 'success',
  489. message: '消息删除成功!'
  490. });
  491. }
  492. })
  493. }).catch(() => {
  494. // 3. 用户点击“取消”或关闭对话框后的回调
  495. this.$message({
  496. type: 'info',
  497. message: '已取消删除'
  498. });
  499. });
  500. },
  501. // 显示顶部消息对话框
  502. showTopMsgDialog() {
  503. this.topMsgForm.msg = '';
  504. this.topMsgForm.duration = 5;
  505. this.topMsgDialogVisible = true;
  506. },
  507. // 发送顶部消息
  508. sendTopMessage() {
  509. // 发送前简单校验
  510. if (this.topMsgForm.msg.trim() === '') {
  511. this.$message.warning('请输入消息内容');
  512. return;
  513. }
  514. if (!this.topMsgForm.duration || this.topMsgForm.duration < 1) {
  515. this.$message.warning('请输入有效的显示时长');
  516. return;
  517. }
  518. let msg = {
  519. msg: this.topMsgForm.msg,
  520. duration: this.topMsgForm.duration,
  521. liveId: this.liveId,
  522. userId: this.userId,
  523. userType: 1,
  524. cmd: 'sendTopMsg',
  525. avatar: this.$store.state.user.user.avatar,
  526. nickName: this.$store.state.user.user.nickName,
  527. }
  528. this.socket.send(JSON.stringify(msg))
  529. this.topMsgDialogVisible = false;
  530. this.$message.success('顶部消息发送成功');
  531. },
  532. handleClickCoupon(){
  533. this.$router.push({
  534. name: 'LiveCoupon',
  535. query: {
  536. liveId: this.liveId
  537. }
  538. })
  539. },
  540. // 保存聊天滚动位置
  541. saveChatScrollPosition() {
  542. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  543. this.chatScrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight;
  544. }
  545. },
  546. // 恢复聊天滚动位置
  547. restoreChatScrollPosition() {
  548. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  549. this.$refs.manageRightRef.wrap.scrollTop = this.chatScrollTop;
  550. }
  551. },
  552. getLive(){
  553. getLive(this.liveId).then(res => {
  554. if (res.code == 200) {
  555. if (res.data.isAudit != 1) {
  556. this.$message.error("当前直播间未经审核");
  557. this.loading = false
  558. return
  559. }
  560. this.isAudit = true
  561. this.status = res.data.status
  562. if(res.data.status == 4){
  563. this.liveType = 3
  564. this.videoUrl = res.data.videoUrl;
  565. }else {
  566. if (res.data.status != 2) {
  567. this.$message.error("当前直播间未直播");
  568. this.loading = false
  569. return
  570. }
  571. if (res.data.liveType == 1) {
  572. this.liveType = 1
  573. this.livingUrl = res.data.flvHlsUrl
  574. // this.livingUrl = this.livingUrl.replace("flv","m3u8")
  575. this.$nextTick(() => {
  576. this.initPlayer()
  577. })
  578. this.startTime = new Date(res.data.startTime).getTime()
  579. this.processInterval = setInterval(this.updateLiveProgress, 1000);
  580. } else {
  581. this.liveType = 2
  582. this.videoUrl = res.data.videoUrl;
  583. this.$nextTick(() => {
  584. this.initVideoPlayer(res.data.startTime)
  585. })
  586. }
  587. }
  588. this.loading = false
  589. } else {
  590. this.$message.error(res.msg)
  591. this.loading = false
  592. }
  593. this.liveInfo = res.data
  594. })
  595. },
  596. initVideoPlayer: function (startTime) {
  597. const video = this.$refs.videoPlayer;
  598. this.hls = new Hls();
  599. this.hls.attachMedia(video);
  600. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  601. this.hls.loadSource(this.videoUrl);
  602. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  603. video.play();
  604. });
  605. });
  606. this.hls.on(Hls.Events.ERROR, (event, data) => {
  607. console.error('HLS 错误:', data);
  608. });
  609. // 1. 初始化开播时间
  610. startTime = new Date(startTime).getTime();
  611. this.startTime = startTime;
  612. // 2. 监听视频元数据加载完成(获取视频时长)
  613. video.addEventListener('loadedmetadata', () => {
  614. this.videoDuration = video.duration; // 获取视频时长(秒)
  615. // 初始化视频播放位置
  616. this.updateVideoPosition(video);
  617. // 启动实时进度更新(每秒刷新一次)
  618. this.processInterval = setInterval(this.updateProgress, 1000);
  619. });
  620. },
  621. updateVideoPosition(video){
  622. const currentTime = new Date().getTime(); // 当前时间戳(毫秒)
  623. const elapsedTime = currentTime - this.startTime; // 已流逝时间(毫秒)
  624. if (elapsedTime < 0) {
  625. // 未开播:视频停在初始位置
  626. video.currentTime = 0;
  627. return;
  628. }
  629. // 已开播:计算视频循环后的位置(流逝时间 % 视频时长)
  630. const elapsedSeconds = elapsedTime / 1000; // 转换为秒
  631. const videoPosition = elapsedSeconds % this.videoDuration; // 视频内的播放位置(秒)
  632. // 设置视频播放位置
  633. video.currentTime = videoPosition;
  634. },
  635. updateProgress() {
  636. const progressBar = this.$refs.progressBar;
  637. const elapsedTimeEl = this.$refs.elapsedTime;
  638. if (!this.videoDuration) return; // 视频时长未加载时不更新
  639. const currentTime = new Date().getTime();
  640. const elapsedTime = currentTime - this.startTime; // 总流逝时间(毫秒)
  641. if (elapsedTime < 0) {
  642. // 未开播状态
  643. progressBar.style.width = '0%';
  644. elapsedTimeEl.textContent = '00:00:00';
  645. return;
  646. }
  647. // 计算进度百分比(基于视频循环)
  648. const elapsedSeconds = elapsedTime / 1000;
  649. const videoPosition = elapsedSeconds % this.videoDuration; // 当前在视频中的位置
  650. const progressPercent = (videoPosition / this.videoDuration) * 100; // 进度百分比
  651. // 更新进度条宽度
  652. progressBar.style.width = `${progressPercent}%`;
  653. // 格式化总流逝时间为“时:分:秒”并显示
  654. elapsedTimeEl.textContent = this.formatTime(elapsedTime);
  655. },
  656. updateLiveProgress() {
  657. const elapsedTimeEl = this.$refs.liveElapsedTime;
  658. const currentTime = new Date().getTime();
  659. const elapsedTime = currentTime - this.startTime; // 总流逝时间(毫秒)
  660. if (elapsedTime < 0) {
  661. elapsedTimeEl.textContent = '00:00:00';
  662. return;
  663. }
  664. // 格式化总流逝时间为“时:分:秒”并显示
  665. elapsedTimeEl.textContent = this.formatTime(elapsedTime);
  666. },
  667. formatTime(ms) {
  668. const totalSeconds = Math.floor(ms / 1000);
  669. const hours = Math.floor(totalSeconds / 3600);
  670. const minutes = Math.floor((totalSeconds % 3600) / 60);
  671. const seconds = totalSeconds % 60;
  672. // 补零处理(确保两位数)
  673. return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  674. },
  675. // ... 其他方法 ...
  676. timeChange(val) {
  677. this.searchQuery.timeOptions = val
  678. this.getEchartsTables(this.searchQuery)
  679. this.initChart()
  680. },
  681. timeGranularityChange(val) {
  682. this.searchQuery.timeGranularity = val
  683. this.getEchartsTables()
  684. this.initChart()
  685. },
  686. getEchartsTables() {
  687. getLiveOrderTimeGranularity(this.searchQuery).then(res => {
  688. if (res.code == 200) {
  689. this.echartsXLine = res.hourlySlots
  690. this.echartsXValue = res.hourlySlotsValue
  691. this.initChart()
  692. }
  693. })
  694. },
  695. initChart() {
  696. const chartDom = this.$refs.chartContainer;
  697. this.myChart = echarts.init(chartDom);
  698. const option = {
  699. tooltip: {trigger: 'axis'},
  700. legend: {data: ['订单数']},
  701. xAxis: {type: 'category', boundaryGap: false, data: this.echartsXLine},
  702. yAxis: {type: 'value'},
  703. series: [
  704. {name: '订单数', type: 'line', data: this.echartsXValue}
  705. ],
  706. };
  707. this.myChart.setOption(option);
  708. },
  709. handleClickRed(){
  710. this.$router.push({
  711. name: 'LiveRedConf',
  712. query: {
  713. liveId: this.liveId
  714. }
  715. })
  716. },
  717. handleClickLottery(){
  718. this.$router.push({
  719. name: 'LiveLotteryConf',
  720. query: {
  721. liveId: this.liveId
  722. }
  723. })
  724. },
  725. handleClickGoods(){
  726. this.$router.push({
  727. name: 'LiveGoods',
  728. query: {
  729. liveId: this.liveId
  730. }
  731. })
  732. },
  733. handleClickOrder(){
  734. this.$router.push({
  735. name: 'LiveOrder',
  736. query: {
  737. liveId: this.liveId
  738. }
  739. })
  740. },
  741. loadTcPlayerSdk() {
  742. if (this.tcPlayerSdkReady && window.TCPlayer) {
  743. return Promise.resolve();
  744. }
  745. if (this._tcPlayerSdkPromise) {
  746. return this._tcPlayerSdkPromise;
  747. }
  748. const loadScript = (src) => new Promise((resolve, reject) => {
  749. if (document.querySelector(`script[src="${src}"]`)) {
  750. resolve();
  751. return;
  752. }
  753. const script = document.createElement('script');
  754. script.src = src;
  755. script.onload = () => resolve();
  756. script.onerror = () => reject(new Error('Failed to load ' + src));
  757. document.head.appendChild(script);
  758. });
  759. const loadCss = (href) => {
  760. if (document.querySelector(`link[href="${href}"]`)) {
  761. return;
  762. }
  763. const link = document.createElement('link');
  764. link.rel = 'stylesheet';
  765. link.href = href;
  766. document.head.appendChild(link);
  767. };
  768. this._tcPlayerSdkPromise = Promise.resolve()
  769. .then(() => {
  770. loadCss('https://web.sdk.qcloud.com/player/tcplayer/release/v4.9.1/tcplayer.min.css');
  771. return loadScript('https://video.sdk.qcloudecdn.com/web/TXLivePlayer-1.3.2.min.js');
  772. })
  773. .then(() => loadScript('https://web.sdk.qcloud.com/player/tcplayer/release/v4.9.1/tcplayer.v4.9.1.min.js'))
  774. .then(() => {
  775. this.tcPlayerSdkReady = true;
  776. });
  777. return this._tcPlayerSdkPromise;
  778. },
  779. destroyTcPlayer() {
  780. if (this.tcPlayer) {
  781. try {
  782. this.tcPlayer.dispose();
  783. } catch (e) {
  784. console.error('销毁 TCPlayer 失败:', e);
  785. }
  786. this.tcPlayer = null;
  787. }
  788. },
  789. initPlayer(){
  790. const isUrl = this.livingUrl === null || this.livingUrl.trim() === '';
  791. if (isUrl) {
  792. console.error('直播地址为空,无法初始化播放器');
  793. return;
  794. }
  795. this.loadTcPlayerSdk().then(() => {
  796. this.destroyTcPlayer();
  797. this.$nextTick(() => {
  798. if (!this.$refs.livingPlayer || !window.TCPlayer) {
  799. console.error('找不到 video 元素或 TCPlayer 未加载');
  800. return;
  801. }
  802. this.tcPlayer = window.TCPlayer('live-tcplayer-container', {
  803. sources: [this.livingUrl],
  804. autoplay: true,
  805. width: '100%',
  806. height: '45vh',
  807. live: true,
  808. controls: true,
  809. playsinline: true,
  810. 'webkit-playsinline': true,
  811. x5_player: true,
  812. x5_type: 'h5-page',
  813. x5_fullscreen: false
  814. });
  815. });
  816. }).catch(err => {
  817. console.error('TCPlayer SDK 加载失败:', err);
  818. this.$message.error('直播播放器加载失败,请刷新页面重试');
  819. });
  820. },
  821. handleClick(tab) {
  822. const tabName = tab.name;
  823. const params = this.pageParams[tabName];
  824. const displayList = this[`${tabName}DisplayList`];
  825. // 首次切换到该Tab或列表为空时初始化
  826. if (displayList.length < 20) {
  827. // 重置分页参数
  828. params.currentPage = 1;
  829. params.pageSize = 20;
  830. params.prevPage = 0;
  831. params.totalLoaded = 0;
  832. params.hasMore = true;
  833. params.hasPrev = false;
  834. // 加载第一页
  835. this.loadNextPage(tabName);
  836. } else {
  837. // 非首次切换,恢复滚动位置
  838. this.$nextTick(() => {
  839. const scrollEl = this.getScrollElement(tabName);
  840. if (scrollEl) {
  841. scrollEl.scrollTop = this.tabScrollPositions[tabName] || 0;
  842. }
  843. });
  844. }
  845. },
  846. saveTabScrollPositions() {
  847. this.tabScrollPositions = {
  848. online: this.getScrollElement('online')?.scrollTop || 0,
  849. offline: this.getScrollElement('offline')?.scrollTop || 0,
  850. silenced: this.getScrollElement('silenced')?.scrollTop || 0
  851. };
  852. },
  853. // 加载指定Tab的用户列表(核心加载逻辑)
  854. loadNextPage(tabName) {
  855. const params = this.pageParams[tabName];
  856. const displayList = this[`${tabName}DisplayList`];
  857. console.log(`加载 ${tabName} 用户列表`)
  858. console.log(!params.hasMore || this.scrLoading[tabName].next)
  859. console.log(params.currentPage)
  860. // 若没有更多数据或正在加载,直接返回
  861. if (!params.hasMore || this.scrLoading[tabName].next) {
  862. return;
  863. }
  864. this.scrLoading[tabName].next = true;
  865. const queryParams = {
  866. liveId: this.liveId,
  867. pageNum: params.currentPage,
  868. pageSize: 20,
  869. online: tabName === 'online' ? 0 : 1,
  870. msgStatus: tabName === 'silenced' ? 1 : 0
  871. };
  872. // 调用接口加载对应状态的分页数据(需后端支持按状态筛选)
  873. watchUserList(queryParams).then(response => {
  874. this.scrLoading[tabName].next = false;
  875. if (response.code !== 200) return;
  876. const { rows, total } = response;
  877. params.total = total; // 记录总数据量
  878. // 过滤重复数据(基于userId)
  879. const newRows = rows.filter(row =>
  880. !displayList.some(u => u.userId === row.userId)
  881. );
  882. displayList.push(...newRows)
  883. // 添加新数据并限制最大长度(避免内存占用过大)
  884. if (displayList.length >= 40) { // 最大保留100条
  885. this[`${tabName}DisplayList`] = displayList.slice(-40);
  886. // 记录滚动位置(用于加载后校准)
  887. const scrollEl = this.getScrollElement(tabName);
  888. // 校准滚动位置(保持视觉连续性)
  889. this.$nextTick(() => {
  890. if (scrollEl) {
  891. scrollEl.scrollTop = scrollEl.scrollHeight * 0.5;
  892. }
  893. });
  894. }
  895. // 更新分页状态
  896. params.hasMore = params.currentPage * params.pageSize < total;
  897. params.currentPage += 1;
  898. params.hasPrev = params.currentPage > 2; // 当前页>2时一定有上一页
  899. params.prevPage = params.currentPage - 2;
  900. }).catch(() => {
  901. this.scrLoading[tabName].next = false;
  902. });
  903. },
  904. // 新增:加载上一页(向上滚动时)
  905. loadPrevPage(tabName) {
  906. const params = this.pageParams[tabName];
  907. const displayList = this[`${tabName}DisplayList`];
  908. // 边界校验:无上一页/正在加载/当前页<=1
  909. console.log(`加载 ${tabName} 上一页`);
  910. console.log(!params.hasPrev || this.scrLoading[tabName].prev || params.currentPage <= 1)
  911. if (!params.hasPrev || this.scrLoading[tabName].prev || params.currentPage <= 1) {
  912. return;
  913. }
  914. this.scrLoading[tabName].prev = true;
  915. const targetPage = params.prevPage > 0 ? params.prevPage : params.currentPage - 2;
  916. const queryParams = {
  917. liveId: this.liveId,
  918. pageNum: targetPage,
  919. pageSize: 20,
  920. online: tabName === 'online' ? 0 : 1,
  921. msgStatus: tabName === 'silenced' ? 1 : 0
  922. };
  923. watchUserList(queryParams).then(response => {
  924. this.scrLoading[tabName].prev = false;
  925. if (response.code !== 200) return;
  926. const { rows } = response;
  927. if (rows.length === 0) {
  928. params.hasPrev = false;
  929. return;
  930. }
  931. // 记录滚动位置(用于加载后校准)
  932. const scrollEl = this.getScrollElement(tabName);
  933. const scrollTop = scrollEl?.scrollTop || 0;
  934. const itemHeight = 80; // 预估行高(根据实际样式调整)
  935. const newItemsHeight = rows.length * itemHeight;
  936. // 过滤重复数据并添加到列表头部
  937. const newRows = rows.filter(row => !displayList.some(u => u.userId === row.userId));
  938. this[`${tabName}DisplayList`] = [...newRows, ...displayList];
  939. params.totalLoaded += newRows.length;
  940. // 限制最大长度
  941. if (this[`${tabName}DisplayList`].length > 40) {
  942. this[`${tabName}DisplayList`] = this[`${tabName}DisplayList`].slice(0, 40);
  943. }
  944. // 更新分页状态
  945. params.prevPage = targetPage - 1;
  946. params.hasPrev = targetPage > 1; // 上一页页码>1时还有更多上一页
  947. params.currentPage = params.currentPage - 1;
  948. if(params.currentPage * 20 < params.total) params.hasMore = true;
  949. // 校准滚动位置(保持视觉连续性)
  950. this.$nextTick(() => {
  951. if (scrollEl) {
  952. scrollEl.scrollTop = scrollEl.scrollHeight * 0.5;
  953. }
  954. });
  955. }).catch(() => {
  956. this.scrLoading[tabName].prev = false;
  957. });
  958. },
  959. // 辅助:获取Tab对应的滚动容器
  960. getScrollElement(tabName) {
  961. const scrollRefs = {
  962. online: this.$refs.manageLeftRef_online,
  963. offline: this.$refs.manageLeftRef_offline,
  964. silenced: this.$refs.manageLeftRef_silenced
  965. };
  966. return scrollRefs[tabName]?.wrap;
  967. },
  968. getList() {
  969. this.resetParams()
  970. // this.loadUserList()
  971. this.loadUserTotals(); // 先加载总人数
  972. // this.handleClick('online')
  973. this.handleClick({name:'online'})
  974. this.loadMsgList()
  975. },
  976. loadUserTotals() {
  977. if (!this.liveId) return;
  978. // 假设后端提供一个接口返回总人数(如果没有,可通过首次加载全量数据后统计)
  979. getLiveUserTotals({ liveId: this.liveId }).then(res => {
  980. if (res.code === 200) {
  981. this.userTotal = res.data; // { online, offline, silenced }
  982. }
  983. });
  984. },
  985. resetParams() {
  986. // 重置各Tab的显示列表和分页参数
  987. this.onlineDisplayList = [];
  988. this.offlineDisplayList = [];
  989. this.silencedDisplayList = [];
  990. this.pageParams = {
  991. online: {
  992. currentPage: 1, // 当前页(下一页加载用)
  993. pageSize: 20, // 当前页(下一页加载用)
  994. prevPage: 0, // 上一页页码(上一页加载用)
  995. totalLoaded: 0, // 已加载总条数
  996. total: 0, // 总数据量
  997. hasMore: true, // 是否有下一页
  998. hasPrev: false // 是否有上一页
  999. },
  1000. offline: {
  1001. currentPage: 1,
  1002. pageSize: 20,
  1003. prevPage: 0,
  1004. totalLoaded: 0,
  1005. total: 0,
  1006. hasMore: true,
  1007. hasPrev: false
  1008. },
  1009. silenced: {
  1010. currentPage: 1,
  1011. pageSize: 20,
  1012. prevPage: 0,
  1013. totalLoaded: 0,
  1014. total: 0,
  1015. hasMore: true,
  1016. hasPrev: false
  1017. }
  1018. };
  1019. // 消息参数保留
  1020. this.msgList = [];
  1021. this.msgParams = {
  1022. pageNum: 1,
  1023. pageSize: 10,
  1024. liveId: this.liveId
  1025. };
  1026. },
  1027. loadUserList() {
  1028. if(this.liveId == null) return
  1029. // 直播间用户
  1030. watchUserList({
  1031. liveId: this.liveId,
  1032. pageNum: this.userParams.pageNum,
  1033. pageSize: this.userParams.pageSize
  1034. }).then(response => {
  1035. let {code,rows,total} = response
  1036. if (code === 200) {
  1037. let totalPage = (total % this.userParams.pageSize == 0) ? Math.floor(total / this.userParams.pageSize) : Math.floor(total / this.userParams.pageSize + 1);
  1038. rows.forEach(row => {
  1039. if (!this.userList.some(u => u.userId === row.userId)) {
  1040. this.userList.push(row)
  1041. }
  1042. })
  1043. // 没加载完继续加载
  1044. if (this.userParams.pageNum < totalPage) {
  1045. this.userParams.pageNum = parseInt(this.userParams.pageNum) + 1;
  1046. this.loadUserList()
  1047. }
  1048. }
  1049. })
  1050. },
  1051. loadMsgList() {
  1052. // 直播间消息 - 只加载最近的30条
  1053. listLiveMsg({
  1054. liveId:this.liveId,
  1055. pageNum: 1,
  1056. pageSize: 30
  1057. }).then(response => {
  1058. let {code, rows} = response;
  1059. if (code === 200) {
  1060. // 清空现有消息列表,只保留最新的30条
  1061. this.msgList = [];
  1062. rows.forEach(row => {
  1063. let user = this.userList.find(u => u.userId === row.userId)
  1064. if (user) {
  1065. row.msgStatus = user.msgStatus
  1066. } else {
  1067. row.msgStatus = 0
  1068. }
  1069. this.msgList.push(row)
  1070. })
  1071. // 移动到底部
  1072. this.$nextTick(() => {
  1073. setTimeout(() => {
  1074. this.$refs.manageRightRef.wrap.scrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  1075. }, 200)
  1076. })
  1077. // 同步更新消息列表中相同用户的状态
  1078. this.userList.forEach(u => {
  1079. this.msgList.filter(m => m.userId === u.userId).forEach(m => m.msgStatus = u.msgStatus)
  1080. })
  1081. }
  1082. })
  1083. // 添加滚动监听
  1084. this.$nextTick(() => {
  1085. this.$refs.manageRightRef.wrap.addEventListener("scroll", this.manageRightScroll)
  1086. })
  1087. },
  1088. manageRightScroll() {
  1089. this.saveChatScrollPosition();
  1090. },
  1091. blockUser(u){
  1092. this.$confirm('是否确认封禁用户账号为:"' + u.nickName + '-' + u.userId + '"?', "警告", {
  1093. confirmButtonText: "确定",
  1094. cancelButtonText: "取消",
  1095. type: "warning"
  1096. }).then(() => {
  1097. return blockUser(u.userId);
  1098. }).then(() => {
  1099. let msg = {
  1100. msg: "",
  1101. liveId: this.liveId,
  1102. userId: u.userId,
  1103. userType: 0,
  1104. cmd: 'blockUser',
  1105. avatar: this.$store.state.user.user.avatar,
  1106. nickName: this.$store.state.user.user.nickName
  1107. }
  1108. this.socket.send(JSON.stringify(msg))
  1109. this.msgSuccess("封禁成功");
  1110. }).catch(() => {});
  1111. },
  1112. changeUserState(u) {
  1113. // 修改状态
  1114. changeUserStatus({liveId: u.liveId, userId: u.userId}).then(response => {
  1115. let { code } = response;
  1116. if (200 === code) {
  1117. u.msgStatus = u.msgStatus === 0 ? 1 : 0
  1118. // 同步更新消息列表中相同用户的状态
  1119. this.msgList.forEach(msg => {
  1120. if (msg.userId === u.userId) {
  1121. msg.msgStatus = u.msgStatus;
  1122. }
  1123. });
  1124. this.userList.forEach(user => {
  1125. if (user.userId === u.userId) {
  1126. user.msgStatus = u.msgStatus;
  1127. }
  1128. });
  1129. // 4. 关键:重新筛选所有Tab的显示列表,确保状态同步
  1130. this.refreshUserDisplayLists(u);
  1131. // 5. 通过 WebSocket 通知后端更新 Redis 禁言列表,发消息时会据此判断
  1132. const wsMsg = {
  1133. liveId: this.liveId,
  1134. userId: u.userId,
  1135. cmd: 'mutedUser',
  1136. status: u.msgStatus,
  1137. userType: 1
  1138. };
  1139. this.socket.send(JSON.stringify(wsMsg));
  1140. let msg = u.msgStatus === 0 ? "已解禁" : "已禁言"
  1141. this.msgSuccess(msg);
  1142. return
  1143. }
  1144. this.msgError("操作失败");
  1145. })
  1146. },
  1147. // 新增:重新筛选所有Tab的显示列表
  1148. refreshUserDisplayLists(user) {
  1149. const { userId, msgStatus: newStatus, online } = user;
  1150. const oldStatus = newStatus === 1 ? 0 : 1; // 操作前的状态(反向)
  1151. // 1. 禁言操作(newStatus=1):从原在线/离线列表移除,加入禁言列表
  1152. if (newStatus === 1) {
  1153. // 从在线/离线列表中移除该用户(根据当前在线状态)
  1154. if (online === 0) {
  1155. this.onlineDisplayList = this.onlineDisplayList.filter(u => u.userId !== userId);
  1156. this.userTotal.online = Math.max(0, this.userTotal.online - 1);
  1157. } else {
  1158. this.offlineDisplayList = this.offlineDisplayList.filter(u => u.userId !== userId);
  1159. this.userTotal.offline = Math.max(0, this.userTotal.offline - 1);
  1160. }
  1161. this.userTotal.silenced = this.userTotal.silenced + 1;
  1162. // 添加到禁言列表(去重+限制长度)
  1163. const silencedList = this.silencedDisplayList.filter(u => u.userId !== userId);
  1164. silencedList.push(user);
  1165. this.silencedDisplayList = silencedList.slice(-40);
  1166. }
  1167. // 2. 解禁操作(newStatus=0):从禁言列表移除,回到原在线/离线列表
  1168. else {
  1169. // 从禁言列表移除
  1170. this.silencedDisplayList = this.silencedDisplayList.filter(u => u.userId !== userId);
  1171. this.userTotal.silenced = Math.max(0, this.userTotal.silenced - 1);
  1172. // 回到对应的在线/离线列表(根据当前在线状态)
  1173. if (online === 0) {
  1174. // 加入在线列表(去重+限制长度)
  1175. const onlineList = this.onlineDisplayList.filter(u => u.userId !== userId);
  1176. onlineList.push(user);
  1177. this.onlineDisplayList = onlineList.slice(-40);
  1178. this.userTotal.online = this.userTotal.online + 1;
  1179. } else {
  1180. // 加入离线列表(去重+限制长度)
  1181. const offlineList = this.offlineDisplayList.filter(u => u.userId !== userId);
  1182. offlineList.push(user);
  1183. this.offlineDisplayList = offlineList.slice(-40);
  1184. this.userTotal.offline = this.userTotal.offline + 1;
  1185. }
  1186. }
  1187. },
  1188. connectWebSocket() {
  1189. this._wsMessageHandler = (event) => this.handleWsMessage(event)
  1190. this.$store.dispatch('acquireLiveWs', {
  1191. liveWsUrl: this.liveWsUrl,
  1192. liveId: this.liveId,
  1193. userId: this.userId,
  1194. owner: 'console'
  1195. }).then((ws) => {
  1196. this.socket = ws || this.$store.state.liveWs[String(this.liveId)]
  1197. if (this.socket) {
  1198. this.socket.addMessageListener(this._wsMessageHandler)
  1199. }
  1200. })
  1201. },
  1202. releaseWebSocket() {
  1203. if (this.socket && this._wsMessageHandler) {
  1204. this.socket.removeMessageListener(this._wsMessageHandler)
  1205. }
  1206. this._wsMessageHandler = null
  1207. this.$store.dispatch('releaseLiveWs', {
  1208. liveId: this.liveId,
  1209. owner: 'console'
  1210. })
  1211. this.socket = null
  1212. },
  1213. handleWsMessage(event) {
  1214. let { code, data } = JSON.parse(event.data)
  1215. if (code === 200) {
  1216. let { cmd } = data
  1217. if (cmd === 'sendMsg') {
  1218. let message = JSON.parse(data.data)
  1219. let user = this.userList.find(u => u.userId === message.userId)
  1220. if (user) {
  1221. message.msgStatus = user.msgStatus
  1222. } else {
  1223. message.msgStatus = 0
  1224. }
  1225. delete message.params
  1226. if(this.msgList.length > 50){
  1227. this.msgList.shift()
  1228. }
  1229. this.msgList.push(message)
  1230. // 移动到底部
  1231. this.$nextTick(() => {
  1232. setTimeout(() => {
  1233. this.$refs.manageRightRef.wrap.scrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  1234. }, 200)
  1235. })
  1236. } else if (cmd === 'deleteMsg') {
  1237. const msgId = data.msgId != null ? data.msgId : (data.msg != null ? Number(data.msg) : null)
  1238. if (msgId != null) {
  1239. const index = this.msgList.findIndex(item => item.msgId == msgId)
  1240. if (index !== -1) {
  1241. this.msgList.splice(index, 1)
  1242. }
  1243. }
  1244. } else if (cmd === 'entry' || cmd === 'out') {
  1245. const user = data;
  1246. const online = cmd === 'entry' ? 0 : 1; // 0=在线,1=离线
  1247. const info = {
  1248. online:online,
  1249. msgStatus: user.msgStatus || 0,
  1250. nickName: user.nickName || '',
  1251. userType: user.userType || 0,
  1252. userId: user.userId || '',
  1253. };
  1254. // 1. 更新总人数(在线/离线互转)
  1255. if (cmd === 'entry') {
  1256. this.userTotal.online += 1;
  1257. this.userTotal.offline = Math.max(0, this.userTotal.offline - 1); // 确保不小于0
  1258. } else {
  1259. this.userTotal.offline += 1;
  1260. this.userTotal.online = Math.max(0, this.userTotal.online - 1); // 确保不小于0
  1261. }
  1262. // 2. 强制更新相关列表(无论当前激活哪个Tab)
  1263. if (cmd === 'entry') {
  1264. // 用户进入:从离线列表删除,添加到在线列表
  1265. this.offlineDisplayList = this.offlineDisplayList.filter(u => u.userId !== user.userId);
  1266. const newOnlineList = this.onlineDisplayList.filter(u => u.userId !== user.userId);
  1267. newOnlineList.push(info);
  1268. this.onlineDisplayList = newOnlineList.slice(-40); // 限制最大50条
  1269. } else {
  1270. // 用户离开:从在线列表删除,添加到离线列表
  1271. this.onlineDisplayList = this.onlineDisplayList.filter(u => u.userId !== user.userId);
  1272. const newOfflineList = this.offlineDisplayList.filter(u => u.userId !== user.userId);
  1273. newOfflineList.push(info);
  1274. this.offlineDisplayList = newOfflineList.slice(-40); // 限制最大50条
  1275. }
  1276. // 3. 处理禁言列表(如果用户是禁言状态,无论进出都要同步)
  1277. if (info.msgStatus === 1) {
  1278. // 禁言用户:从禁言列表删除旧数据,添加新数据
  1279. const newSilencedList = this.silencedDisplayList.filter(u => u.userId !== user.userId);
  1280. newSilencedList.push(info);
  1281. this.silencedDisplayList = newSilencedList.slice(-40);
  1282. } else {
  1283. // 非禁言用户:从禁言列表删除(如果存在)
  1284. this.silencedDisplayList = this.silencedDisplayList.filter(u => u.userId !== user.userId);
  1285. }
  1286. } else if (cmd === 'live_start') {
  1287. } else if (cmd === 'live_end') {
  1288. }
  1289. }
  1290. },
  1291. sendMessage() {
  1292. // 发送前简单校验
  1293. if (this.newMsg.trim() === '') {
  1294. return;
  1295. }
  1296. let msg = {
  1297. msg: this.newMsg,
  1298. liveId: this.liveId,
  1299. userId: this.userId,
  1300. userType: 1,
  1301. cmd: 'sendMsg',
  1302. avatar: this.$store.state.user.user.avatar,
  1303. nickName: this.$store.state.user.user.nickName
  1304. }
  1305. this.socket.send(JSON.stringify(msg))
  1306. this.newMsg = '';
  1307. },
  1308. // 初始化滚动监听(在mounted中调用)
  1309. initScrollListeners() {
  1310. // 为每个Tab的滚动容器添加监听
  1311. this.$nextTick(() => {
  1312. const scrollRefs = {
  1313. online: this.$refs.manageLeftRef_online,
  1314. offline: this.$refs.manageLeftRef_offline,
  1315. silenced: this.$refs.manageLeftRef_silenced
  1316. };
  1317. Object.keys(scrollRefs).forEach(tabName => {
  1318. const scrollEl = scrollRefs[tabName]?.wrap;
  1319. if (scrollEl) {
  1320. scrollEl.addEventListener('scroll', () =>
  1321. this.handleTabScroll(tabName, scrollEl)
  1322. );
  1323. }
  1324. });
  1325. });
  1326. },
  1327. // 处理Tab滚动事件(判断是否触底)
  1328. handleTabScroll(tabName, scrollEl) {
  1329. const { scrollTop, scrollHeight, clientHeight } = scrollEl;
  1330. const bottomThreshold = 50; // 距离底部100px触发下一页
  1331. const topThreshold = 50; // 距离顶部100px触发上一页
  1332. // 加载下一页(滚动到底部附近)
  1333. if (scrollHeight - scrollTop - clientHeight < bottomThreshold) {
  1334. this.loadNextPage(tabName);
  1335. }
  1336. // 加载上一页(滚动到顶部附近)
  1337. if (scrollTop < topThreshold) {
  1338. this.loadPrevPage(tabName);
  1339. }
  1340. },
  1341. },
  1342. destroyed() {
  1343. this.destroyTcPlayer();
  1344. this.hls?.destroy();
  1345. clearInterval(this.processInterval);
  1346. }
  1347. }
  1348. </script>
  1349. <style scoped>
  1350. .live-console {
  1351. width: 90vw;
  1352. padding: 10px 0;
  1353. }
  1354. .live-console .live-console-col {
  1355. height: 88vh;
  1356. margin-left: 5px;
  1357. padding: 0 10px;
  1358. background-color: #fff;
  1359. border: 1px solid #e8eaed;
  1360. border-radius: 10px;
  1361. }
  1362. .chat-scroll {
  1363. height: 500px;
  1364. width: 100%;
  1365. }
  1366. .chat-scroll__spacer {
  1367. height: 12px;
  1368. }
  1369. .chat-msg {
  1370. margin-bottom: 14px;
  1371. }
  1372. .chat-msg__row {
  1373. display: flex;
  1374. align-items: flex-start;
  1375. gap: 10px;
  1376. }
  1377. .chat-msg__row--right {
  1378. justify-content: flex-end;
  1379. }
  1380. .chat-msg__avatar {
  1381. width: 32px !important;
  1382. height: 32px !important;
  1383. flex-shrink: 0;
  1384. }
  1385. .chat-msg__body {
  1386. min-width: 0;
  1387. flex: 1;
  1388. }
  1389. .chat-msg__body--right {
  1390. flex: 0 1 auto;
  1391. max-width: 220px;
  1392. display: flex;
  1393. flex-direction: column;
  1394. align-items: flex-end;
  1395. }
  1396. .chat-msg__meta {
  1397. display: flex;
  1398. align-items: center;
  1399. flex-wrap: wrap;
  1400. gap: 6px 10px;
  1401. margin-bottom: 6px;
  1402. }
  1403. .chat-msg__meta--right {
  1404. justify-content: flex-end;
  1405. }
  1406. .chat-msg__name {
  1407. font-size: 13px;
  1408. color: #8a919f;
  1409. }
  1410. .chat-msg__ops {
  1411. display: flex;
  1412. align-items: center;
  1413. gap: 8px;
  1414. }
  1415. .chat-msg__op {
  1416. cursor: pointer;
  1417. font-size: 12px;
  1418. color: #8a919f;
  1419. line-height: 1;
  1420. }
  1421. .chat-msg__op:hover {
  1422. color: #1a6d5b;
  1423. }
  1424. .chat-msg__op--danger:hover {
  1425. color: #c45656;
  1426. }
  1427. .msg-box {
  1428. word-break: break-all;
  1429. word-wrap: break-word;
  1430. white-space: pre-wrap;
  1431. background-color: #f4f6f8;
  1432. padding: 8px 10px;
  1433. border-radius: 0 10px 10px 10px;
  1434. font-size: 13px;
  1435. color: #1f2329;
  1436. display: inline-block;
  1437. line-height: 1.5;
  1438. }
  1439. .msg-box-right {
  1440. word-break: break-all;
  1441. word-wrap: break-word;
  1442. white-space: pre-wrap;
  1443. background-color: #1a6d5b;
  1444. padding: 8px 10px;
  1445. color: #fff;
  1446. border-radius: 10px 0 10px 10px;
  1447. font-size: 13px;
  1448. display: inline-block;
  1449. line-height: 1.5;
  1450. }
  1451. .shuru {
  1452. padding: 12px 14px;
  1453. border-top: 1px solid #e8eaed;
  1454. background-color: #fff;
  1455. }
  1456. .shuru__actions {
  1457. display: flex;
  1458. justify-content: flex-end;
  1459. gap: 8px;
  1460. margin-top: 8px;
  1461. }
  1462. .btn-accent {
  1463. background: #1a6d5b !important;
  1464. border-color: #1a6d5b !important;
  1465. color: #fff !important;
  1466. }
  1467. .shuru ::v-deep .el-textarea__inner {
  1468. background-color: #f4f6f8 !important;
  1469. border-radius: 8px;
  1470. border: none !important;
  1471. box-shadow: none !important;
  1472. resize: none !important;
  1473. min-height: 72px !important;
  1474. }
  1475. .player-panel {
  1476. background: #000;
  1477. border-radius: 10px;
  1478. overflow: hidden;
  1479. margin: 10px 5px;
  1480. }
  1481. .player-panel__inner {
  1482. overflow: hidden;
  1483. }
  1484. .player-panel__poster {
  1485. width: 100%;
  1486. height: 45vh;
  1487. display: block;
  1488. object-fit: cover;
  1489. }
  1490. .tool-bar {
  1491. display: flex;
  1492. justify-content: space-around;
  1493. align-items: center;
  1494. padding: 10px 8px;
  1495. background: #fff;
  1496. border-top: 1px solid #e8eaed;
  1497. }
  1498. .top-box {
  1499. display: flex;
  1500. flex-direction: column;
  1501. align-items: center;
  1502. justify-content: center;
  1503. cursor: pointer;
  1504. min-width: 64px;
  1505. padding: 4px 6px;
  1506. border-radius: 8px;
  1507. }
  1508. .top-box:hover {
  1509. background: #f4f6f8;
  1510. }
  1511. .top-box img {
  1512. width: 36px;
  1513. height: 36px;
  1514. display: block;
  1515. }
  1516. .top-box span {
  1517. font-size: 12px;
  1518. margin-top: 6px;
  1519. color: #5c6470;
  1520. }
  1521. .chart-panel {
  1522. position: relative;
  1523. width: 100%;
  1524. height: 270px;
  1525. margin-top: 20px;
  1526. }
  1527. .chart-panel__canvas {
  1528. width: 100%;
  1529. height: 100%;
  1530. }
  1531. .chart-panel__filters {
  1532. position: absolute;
  1533. top: 10px;
  1534. right: 10px;
  1535. background: #fff;
  1536. padding: 5px;
  1537. z-index: 1;
  1538. display: flex;
  1539. gap: 8px;
  1540. }
  1541. .user-scroll {
  1542. height: 100%;
  1543. width: 100%;
  1544. padding: 8px;
  1545. }
  1546. .user-scroll--tall {
  1547. height: 800px;
  1548. }
  1549. .row-box {
  1550. display: flex;
  1551. align-items: center;
  1552. gap: 10px;
  1553. padding: 10px 12px;
  1554. margin-bottom: 8px;
  1555. background: #fafbfc;
  1556. border-radius: 8px;
  1557. border: 1px solid transparent;
  1558. }
  1559. .row-box:hover {
  1560. border-color: #e8eaed;
  1561. }
  1562. .user-row__avatar {
  1563. width: 36px !important;
  1564. height: 36px !important;
  1565. flex-shrink: 0;
  1566. }
  1567. .user-row__info {
  1568. flex: 1;
  1569. min-width: 0;
  1570. }
  1571. .user-row__name {
  1572. font-size: 13px;
  1573. color: #1f2329;
  1574. line-height: 1.35;
  1575. overflow: hidden;
  1576. text-overflow: ellipsis;
  1577. white-space: nowrap;
  1578. }
  1579. .user-row__id {
  1580. margin-top: 2px;
  1581. font-size: 12px;
  1582. color: #8a919f;
  1583. overflow: hidden;
  1584. text-overflow: ellipsis;
  1585. white-space: nowrap;
  1586. }
  1587. .user-row__pop {
  1588. text-align: center;
  1589. }
  1590. .user-row__pop-op {
  1591. cursor: pointer;
  1592. color: #c45656;
  1593. font-size: 12px;
  1594. margin-right: 8px;
  1595. }
  1596. .el-icon-more {
  1597. color: #c0c4cc;
  1598. cursor: pointer;
  1599. }
  1600. .live-console-tab-right {
  1601. height: 100%;
  1602. }
  1603. .live-console-tab-right ::v-deep .el-tabs__content {
  1604. height: 90%;
  1605. }
  1606. ::v-deep .el-scrollbar__wrap {
  1607. overflow-x: hidden;
  1608. }
  1609. .tcplayer-live {
  1610. width: 100%;
  1611. }
  1612. ::v-deep .tcplayer {
  1613. margin: 0 auto;
  1614. }
  1615. </style>