index.vue 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. <template>
  2. <!-- 直播中控台 start -->
  3. <el-row type="flex" justify="center" class="live-console" :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 style="height: 500px; width: 100%;" ref="manageRightRef">
  9. <el-row v-for="m in msgList" >
  10. <el-row v-if="m.userId !== userId" style="margin-top: 5px" type="flex" align="top" >
  11. <el-col :span="3" style="margin-left: 10px"><el-avatar :src="m.avatar"/></el-col>
  12. <el-col :span="15">
  13. <el-row style="margin-left: 10px">
  14. <el-col><div style="font-size: 12px; color: #999; margin-bottom: 3px;">{{ m.nickName }}</div></el-col>
  15. <el-col :span="24" style="max-width: 200px;">
  16. <div style="white-space: normal; word-wrap: break-word;background-color: #f0f2f5; padding: 8px; border-radius: 5px;font-size: 14px;width: 100%;">
  17. {{ m.msg }}
  18. </div>
  19. </el-col>
  20. <el-col>
  21. <a style="cursor: pointer;color: #ff0000;padding: 8px 8px 0 0;font-size: 12px;" @click="changeUserState(m)">{{ m.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  22. <a style="cursor: pointer;color: #ff0000;padding: 8px 8px 0 0;font-size: 12px;" @click="blockUser(m)">拉黑</a>
  23. </el-col>
  24. </el-row>
  25. </el-col>
  26. </el-row>
  27. <el-row v-if="m.userId === userId" style="padding: 8px 0" type="flex" align="top" justify="end">
  28. <div style="display: flex;justify-content: flex-end">
  29. <div style="display: flex;justify-content: flex-end;flex-direction: column;max-width: 200px;align-items: flex-end">
  30. <div style="font-size: 12px; color: #999; margin-bottom: 3px;">{{ m.nickName }}</div>
  31. <div style="white-space: normal; word-wrap: break-word;width: 100%; background-color: #e6f7ff; padding: 8px; border-radius: 5px;font-size: 14px;">{{ m.msg }}</div>
  32. </div>
  33. <el-avatar :src="m.avatar" style="margin-left: 10px; margin-right: 10px;"/>
  34. </div>
  35. </el-row>
  36. </el-row>
  37. <!-- 底部留白 -->
  38. <div style="height: 20px;"></div>
  39. </el-scrollbar>
  40. <!-- 消息输入区域 -->
  41. <div style="padding: 10px; border-top: 1px solid #ebeef5; background-color: #fff; min-height: 120px;">
  42. <el-input
  43. type="textarea"
  44. v-model="newMsg"
  45. placeholder="请输入消息..."
  46. :rows="8"
  47. @keyup.enter.native="sendMessage"
  48. clearable
  49. resize="none"
  50. style="flex: 1; margin-right: 10px;"
  51. >
  52. </el-input>
  53. <div style="display: flex; justify-content: flex-end; margin-top: 10px;">
  54. <el-button plain @click="sendMessage">发送</el-button>
  55. </div>
  56. </div>
  57. </el-tab-pane>
  58. </el-tabs>
  59. </el-col>
  60. <!-- 聊天 end -->
  61. <!-- 直播/视频 start -->
  62. <el-col class="live-console-col" :span="12">
  63. <div style="background: #000; border-radius: 5px; overflow: hidden; margin: 10px 5px;">
  64. <div style="border-radius: 5px; overflow: hidden;" v-if="!isAudit">
  65. <img :src="require('@/assets/image/videoIsAudit.png')" style="width: 100%; height: 45vh;">
  66. </div>
  67. <div style="border-radius: 5px; overflow: hidden;" v-else-if="status != 2 && status != 4">
  68. <img :src="require('@/assets/image/videoNotStart.png')" style="width: 100%; height: 45vh;">
  69. </div>
  70. <div style="border-radius: 5px; overflow: hidden;" v-else-if="liveType == 1">
  71. <video
  72. controls
  73. ref="livingPlayer"
  74. autoplay
  75. width="100%"
  76. @click.prevent
  77. @contextmenu.prevent
  78. class="custom-video"
  79. style="display: block; background: #000; height: 45vh;"
  80. ></video>
  81. <!-- 时间显示(可选) -->
  82. <div ref="liveElapsedTime" class="time-display">
  83. 已播放:<span id="liveElapsedTime">00:00:00</span>
  84. </div>
  85. </div>
  86. <div style="border-radius: 5px; overflow: hidden;" v-else-if="liveType == 2">
  87. <video
  88. controls
  89. ref="videoPlayer"
  90. loop
  91. autoplay
  92. width="100%"
  93. muted
  94. playsinline
  95. @click.prevent
  96. @contextmenu.prevent
  97. class="custom-video"
  98. style="display: block; background: #000; height: 40vh;"
  99. >
  100. <source :src="videoUrl" type="application/x-mpegURL">
  101. </video>
  102. <!-- 自定义进度条容器 -->
  103. <div ref="progressBar" class="progress-container">
  104. <div id="progressBar" class="progress-bar"></div>
  105. </div>
  106. <!-- 时间显示(可选) -->
  107. <div ref="elapsedTime" class="time-display">
  108. 已播放:<span id="elapsedTime">00:00:00</span>
  109. </div>
  110. </div>
  111. <div style="border-radius: 5px; overflow: hidden;" v-else-if="liveType == 3">
  112. <video
  113. controls
  114. ref="liveReplay"
  115. loop
  116. autoplay
  117. width="100%"
  118. playsinline
  119. style="display: block; background: #000; height: 40vh;"
  120. >
  121. <source :src="videoUrl" type="application/x-mpegURL">
  122. </video>
  123. </div>
  124. <div style="border-radius: 5px; overflow: hidden;" v-else>
  125. <img :src="require('@/assets/image/videoNotStart.png')" style="width: 100%; height: 45vh;">
  126. </div>
  127. </div>
  128. <!-- 底部导航栏 -->
  129. <div style="display: flex; justify-content: space-around; padding: 15px 0; background: #fff; border-top: 1px solid #f0f0f0;">
  130. <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="handleClickRed">
  131. <i class="el-icon-money" style="font-size: 20px;"></i>
  132. <span style="font-size: 12px; margin-top: 4px;">红包配置</span>
  133. </div>
  134. <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="handleClickLottery">
  135. <i class="el-icon-present" style="font-size: 20px;"></i>
  136. <span style="font-size: 12px; margin-top: 4px;">抽奖配置</span>
  137. </div>
  138. <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="handleClickGoods">
  139. <i class="el-icon-goods" style="font-size: 20px;"></i>
  140. <span style="font-size: 12px; margin-top: 4px;">商品</span>
  141. </div>
  142. <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="handleClickOrder">
  143. <i class="el-icon-goods" style="font-size: 20px;"></i>
  144. <span style="font-size: 12px; margin-top: 4px;">直播订单</span>
  145. </div>
  146. <div style="display: flex; flex-direction: column; align-items: center; cursor: pointer;" @click="handleClickCoupon">
  147. <i class="el-icon-goods" style="font-size: 20px;"></i>
  148. <span style="font-size: 12px; margin-top: 4px;">直播优惠券</span>
  149. </div>
  150. </div>
  151. <el-radio-group v-model="tableRadio" >
  152. <el-radio-button label="订单数">订单数</el-radio-button>
  153. </el-radio-group>
  154. <div style="position: relative;width: 100%; height: 300px;">
  155. <div ref="chartContainer" style="width: 100%; height: 100%;"></div>
  156. <div style="position: absolute; top: 10px; right: 10px; background: #fff; padding: 5px; z-index: 1;">
  157. <el-select v-model="searchQuery.timeOptions" placeholder="请选择" style="width: 150px" @change="timeChange">
  158. <el-option
  159. v-for="item in timeOptions"
  160. :key="item.value"
  161. :label="item.label"
  162. :value="item.value">
  163. </el-option>
  164. </el-select>
  165. <el-select v-model="searchQuery.timeGranularity" placeholder="请选择" style="width: 150px" @change="timeGranularityChange">
  166. <el-option
  167. v-for="item in timeGranularity"
  168. :key="item.value"
  169. :label="item.label"
  170. :value="item.value">
  171. </el-option>
  172. </el-select>
  173. <!-- <el-button type="primary" @click="applyFilter">搜索</el-button>-->
  174. </div>
  175. </div>
  176. </el-col>
  177. <!-- 直播/视频 end -->
  178. <!-- 用户列表 start -->
  179. <el-col class="live-console-col" :span="6">
  180. <el-tabs class="live-console-tab-right" v-model="tabLeft.activeName" @tab-click="handleClick" :stretch="true">
  181. <el-tab-pane :label="onlineLabel" name="online">
  182. <el-scrollbar ref="manageLeftRef_online" style="height: 800px; width: 100%;">
  183. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in onlineDisplayList" :key="u.userId">
  184. <el-col :span="20">
  185. <el-row type="flex" align="middle">
  186. <el-col :span="4" style="padding-left: 10px;"><el-avatar :src="u.avatar"></el-avatar></el-col>
  187. <el-col :span="19" :offset="1">{{ u.nickName }}</el-col>
  188. <el-col :span="19" :offset="1">{{ u.userId }}</el-col>
  189. </el-row>
  190. </el-col>
  191. <el-col :span="4" >
  192. <el-popover
  193. width="100"
  194. trigger="click">
  195. <a style="cursor: pointer;color: #ff0000;" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  196. <a style="cursor: pointer;color: #ff0000;margin-left:10px" @click="blockUser(u)">拉黑</a>
  197. <i class="el-icon-more" slot="reference"></i>
  198. </el-popover>
  199. </el-col>
  200. </el-row>
  201. </el-scrollbar>
  202. </el-tab-pane>
  203. <el-tab-pane :label="offlineLabel" name="offline">
  204. <el-scrollbar ref="manageLeftRef_offline" style="height: 800px; width: 100%;">
  205. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in offlineDisplayList" :key="u.userId">
  206. <el-col :span="20">
  207. <el-row type="flex" align="middle">
  208. <el-col :span="4" style="padding-left: 10px;"><el-avatar :src="u.avatar"></el-avatar></el-col>
  209. <el-col :span="19" :offset="1">{{ u.nickName }}</el-col>
  210. <el-col :span="19" :offset="1">{{ u.userId }}</el-col>
  211. </el-row>
  212. </el-col>
  213. <el-col :span="4" >
  214. <el-popover
  215. width="100"
  216. trigger="click">
  217. <a style="cursor: pointer;color: #ff0000;" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  218. <a style="cursor: pointer;color: #ff0000;margin-left:10px" @click="blockUser(u)">拉黑</a>
  219. <i class="el-icon-more" slot="reference"></i>
  220. </el-popover>
  221. </el-col>
  222. </el-row>
  223. </el-scrollbar>
  224. </el-tab-pane>
  225. <el-tab-pane :label="silencedUserLabel" name="silenced">
  226. <el-scrollbar ref="manageLeftRef_silenced" style="height: 800px; width: 100%;">
  227. <el-row style="margin-top: 10px" type="flex" align="middle" v-for="u in silencedDisplayList" :key="u.userId">
  228. <el-col :span="20">
  229. <el-row type="flex" align="middle">
  230. <el-col :span="4" style="padding-left: 10px;"><el-avatar :src="u.avatar"></el-avatar></el-col>
  231. <el-col :span="19" :offset="1">{{ u.nickName }}</el-col>
  232. <el-col :span="19" :offset="1">{{ u.userId }}</el-col>
  233. </el-row>
  234. </el-col>
  235. <el-col :span="4" >
  236. <el-popover
  237. width="100"
  238. trigger="click">
  239. <a style="cursor: pointer;color: #ff0000;" @click="changeUserState(u)">{{ u.msgStatus === 1 ? '解禁' : '禁言' }}</a>
  240. <a style="cursor: pointer;color: #ff0000;margin-left:10px" @click="blockUser(u)">拉黑</a>
  241. <i class="el-icon-more" slot="reference"></i>
  242. </el-popover>
  243. </el-col>
  244. </el-row>
  245. </el-scrollbar>
  246. </el-tab-pane>
  247. </el-tabs>
  248. </el-col>
  249. <!-- 用户列表 end -->
  250. </el-row>
  251. <!-- 直播中控台 end -->
  252. </template>
  253. <script>
  254. import { blockUser,changeUserStatus,getLiveUserTotals, watchUserList } from '@/api/live/liveWatchUser'
  255. import { getLiveVideoByLiveId } from '@/api/live/liveVideo'
  256. import {getLive, getLivingUrl} from '@/api/live/live'
  257. import { getLiveOrderTimeGranularity } from '@/api/live/liveOrder'
  258. import { listLiveMsg } from '@/api/live/liveMsg'
  259. import Hls from 'hls.js';
  260. import LiveLotteryConf from '@/views/live/liveConfig/liveLotteryConf.vue'
  261. import LiveRedConf from '@/views/live/liveConfig/liveRedConf.vue'
  262. import LiveGoods from '@/views/live/liveConfig/goods.vue'
  263. import LiveLiveCoupon from '@/views/live/liveConfig/liveCoupon.vue'
  264. import echarts from 'echarts'
  265. export default {
  266. name: "LiveConsole",
  267. components: { LiveLotteryConf,LiveRedConf },
  268. data() {
  269. return {
  270. loading:true,
  271. tabLeft: {
  272. activeName: "online",
  273. },
  274. tabRight: {
  275. activeName: "talk",
  276. },
  277. livingUrl:"",
  278. videoUrl: "",
  279. status: 0,
  280. loadMsgMaxPage: 2,
  281. liveVideo: {},
  282. liveWsUrl: process.env.VUE_APP_LIVE_WS_URL + '/app/webSocket',
  283. userParams:{
  284. pageNum: 1,
  285. pageSize: 10,
  286. liveId: null
  287. },
  288. msgParams: {
  289. pageNum: 1,
  290. pageSize: 10,
  291. liveId: null
  292. },
  293. userList: [],
  294. msgList: [],
  295. newMsg: '',
  296. isAudit: false,
  297. myChart: null, // 用于存储 ECharts 实例
  298. liveType: 1,
  299. tableRadio: '订单数',
  300. searchQuery: {timeOptions:'2',timeGranularity:'10',liveId: null},
  301. timeOptions: [
  302. {value:'2',label:'最近2小时',key:'2'},
  303. {value:'4',label:'最近4小时',key:'4'},
  304. {value:'all',label:'全场',key:'all'},
  305. ],
  306. timeGranularity: [
  307. {value:'10',label:'10分钟',key:'10'},
  308. {value:'30',label:'30分钟',key:'30'},
  309. {value:'60',label:'1小时',key:'60'},
  310. ],
  311. videoDuration: 0,
  312. startTime: null,
  313. processInterval: null,
  314. // ... 其他数据
  315. chatScrollTop: 0, // 保存聊天滚动位置
  316. socket: null,
  317. userTotal: {
  318. online: 0, // 在线总人数
  319. offline: 0, // 离线总人数
  320. silenced: 0 // 禁言总人数
  321. },
  322. // 各Tab的显示列表(仅存储当前需要展示的数据)
  323. onlineDisplayList: [], // 在线用户显示列表
  324. offlineDisplayList: [], // 离线用户显示列表
  325. silencedDisplayList: [], // 禁言用户显示列表
  326. // 各Tab的分页参数
  327. pageParams: {
  328. online: {
  329. currentPage: 1, // 当前页(下一页加载用)
  330. pageSize: 20, // 当前页(下一页加载用)
  331. prevPage: 0, // 上一页页码(上一页加载用)
  332. totalLoaded: 0, // 已加载总条数
  333. total: 0, // 总数据量
  334. hasMore: true, // 是否有下一页
  335. hasPrev: false // 是否有上一页
  336. },
  337. offline: {
  338. currentPage: 1,
  339. pageSize: 20,
  340. prevPage: 0,
  341. totalLoaded: 0,
  342. total: 0,
  343. hasMore: true,
  344. hasPrev: false
  345. },
  346. silenced: {
  347. currentPage: 1,
  348. pageSize: 20,
  349. prevPage: 0,
  350. totalLoaded: 0,
  351. total: 0,
  352. hasMore: true,
  353. hasPrev: false
  354. }
  355. },
  356. scrLoading: {
  357. online: { next: false, prev: false },
  358. offline: { next: false, prev: false },
  359. silenced: { next: false, prev: false }
  360. }
  361. }
  362. },
  363. created() {
  364. // this.getLiveVideo()
  365. this.getList()
  366. this.connectWebSocket()
  367. this.getLive()
  368. this.searchQuery.liveId = this.liveId
  369. },
  370. computed: {
  371. liveId() {
  372. return this.$route.params.liveId;
  373. },
  374. userId() {
  375. return this.$store.state.user.user.userId
  376. },
  377. companyId() {
  378. return this.$store.state.user.user.companyId
  379. },
  380. onlineLabel() {
  381. return `在线(${this.userTotal.online})`;
  382. },
  383. offlineLabel() {
  384. return `离线(${this.userTotal.offline})`;
  385. },
  386. silencedUserLabel() {
  387. return `禁言(${this.userTotal.silenced})`;
  388. }
  389. },
  390. mounted() {
  391. this.$nextTick(() => {
  392. this.restoreChatScrollPosition();
  393. });
  394. this.getEchartsTables();
  395. // 添加滚动事件监听器
  396. this.$nextTick(() => {
  397. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  398. this.$refs.manageRightRef.wrap.addEventListener('scroll', this.saveChatScrollPosition);
  399. }
  400. });
  401. this.initScrollListeners();
  402. },
  403. beforeDestroy() {
  404. this.saveTabScrollPositions()
  405. // 移除滚动监听(避免内存泄漏)
  406. const scrollRefs = {
  407. online: this.$refs.manageLeftRef_online,
  408. offline: this.$refs.manageLeftRef_offline,
  409. silenced: this.$refs.manageLeftRef_silenced
  410. };
  411. Object.keys(scrollRefs).forEach(tabName => {
  412. const scrollEl = scrollRefs[tabName]?.wrap;
  413. if (scrollEl) {
  414. scrollEl.removeEventListener('scroll', () =>
  415. this.handleTabScroll(tabName, scrollEl)
  416. );
  417. }
  418. })
  419. },
  420. // 使用 deactivated 和 activated 钩子替代 beforeDestroy 和 destroyed
  421. deactivated() {
  422. this.saveChatScrollPosition();
  423. },
  424. activated() {
  425. this.$nextTick(() => {
  426. this.restoreChatScrollPosition();
  427. });
  428. this.$nextTick(() => {
  429. const video = this.$refs.videoPlayer;
  430. if (video != null) {
  431. this.initVideoPlayer(this.liveInfo.startTime)
  432. }
  433. })
  434. },
  435. methods: {
  436. updateVideoPosition(video){
  437. const currentTime = new Date().getTime(); // 当前时间戳(毫秒)
  438. const elapsedTime = currentTime - this.startTime; // 已流逝时间(毫秒)
  439. if (elapsedTime < 0) {
  440. // 未开播:视频停在初始位置
  441. video.currentTime = 0;
  442. return;
  443. }
  444. // 已开播:计算视频循环后的位置(流逝时间 % 视频时长)
  445. const elapsedSeconds = elapsedTime / 1000; // 转换为秒
  446. const videoPosition = elapsedSeconds % this.videoDuration; // 视频内的播放位置(秒)
  447. // 设置视频播放位置
  448. video.currentTime = videoPosition;
  449. },
  450. updateProgress() {
  451. const progressBar = this.$refs.progressBar;
  452. const elapsedTimeEl = this.$refs.elapsedTime;
  453. if (!this.videoDuration) return; // 视频时长未加载时不更新
  454. const currentTime = new Date().getTime();
  455. const elapsedTime = currentTime - this.startTime; // 总流逝时间(毫秒)
  456. if (elapsedTime < 0) {
  457. // 未开播状态
  458. progressBar.style.width = '0%';
  459. elapsedTimeEl.textContent = '00:00:00';
  460. return;
  461. }
  462. // 计算进度百分比(基于视频循环)
  463. const elapsedSeconds = elapsedTime / 1000;
  464. const videoPosition = elapsedSeconds % this.videoDuration; // 当前在视频中的位置
  465. const progressPercent = (videoPosition / this.videoDuration) * 100; // 进度百分比
  466. // 更新进度条宽度
  467. progressBar.style.width = `${progressPercent}%`;
  468. // 格式化总流逝时间为“时:分:秒”并显示
  469. elapsedTimeEl.textContent = this.formatTime(elapsedTime);
  470. },
  471. formatTime(ms) {
  472. const totalSeconds = Math.floor(ms / 1000);
  473. const hours = Math.floor(totalSeconds / 3600);
  474. const minutes = Math.floor((totalSeconds % 3600) / 60);
  475. const seconds = totalSeconds % 60;
  476. // 补零处理(确保两位数)
  477. return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  478. },
  479. initVideoPlayer: function (startTime) {
  480. const video = this.$refs.videoPlayer;
  481. this.hls = new Hls();
  482. this.hls.attachMedia(video);
  483. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  484. this.hls.loadSource(this.videoUrl);
  485. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  486. video.play();
  487. });
  488. });
  489. this.hls.on(Hls.Events.ERROR, (event, data) => {
  490. console.error('HLS 错误:', data);
  491. });
  492. // 1. 初始化开播时间
  493. startTime = new Date(startTime).getTime();
  494. this.startTime = startTime;
  495. // 2. 监听视频元数据加载完成(获取视频时长)
  496. video.addEventListener('loadedmetadata', () => {
  497. this.videoDuration = video.duration; // 获取视频时长(秒)
  498. // 初始化视频播放位置
  499. this.updateVideoPosition(video);
  500. // 启动实时进度更新(每秒刷新一次)
  501. this.processInterval = setInterval(this.updateProgress, 1000);
  502. });
  503. },
  504. // 保存聊天滚动位置
  505. saveChatScrollPosition() {
  506. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  507. this.chatScrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight;
  508. }
  509. },
  510. // 恢复聊天滚动位置
  511. restoreChatScrollPosition() {
  512. if (this.$refs.manageRightRef && this.$refs.manageRightRef.wrap) {
  513. this.$refs.manageRightRef.wrap.scrollTop = this.chatScrollTop;
  514. }
  515. },
  516. // ... 其他方法 ...
  517. timeChange(val) {
  518. this.searchQuery.timeOptions = val
  519. this.getEchartsTables(this.searchQuery)
  520. this.initChart()
  521. },
  522. timeGranularityChange(val) {
  523. this.searchQuery.timeGranularity = val
  524. this.getEchartsTables()
  525. this.initChart()
  526. },
  527. getEchartsTables() {
  528. getLiveOrderTimeGranularity(this.searchQuery).then(res => {
  529. if (res.code == 200) {
  530. this.echartsXLine = res.hourlySlots
  531. this.echartsXValue = res.hourlySlotsValue
  532. this.initChart()
  533. }
  534. })
  535. },
  536. initChart() {
  537. const chartDom = this.$refs.chartContainer;
  538. this.myChart = echarts.init(chartDom);
  539. const option = {
  540. tooltip: {trigger: 'axis'},
  541. legend: {data: ['订单数']},
  542. xAxis: {type: 'category', boundaryGap: false, data: this.echartsXLine},
  543. yAxis: {type: 'value'},
  544. series: [
  545. {name: '订单数', type: 'line', data: this.echartsXValue}
  546. ],
  547. };
  548. this.myChart.setOption(option);
  549. },
  550. handleClickGoods(){
  551. this.$router.push({
  552. name: 'LiveGoods',
  553. query: {
  554. liveId: this.liveId
  555. }
  556. })
  557. },
  558. updateLiveProgress() {
  559. const elapsedTimeEl = this.$refs.liveElapsedTime;
  560. const currentTime = new Date().getTime();
  561. const elapsedTime = currentTime - this.startTime; // 总流逝时间(毫秒)
  562. if (elapsedTime < 0) {
  563. elapsedTimeEl.textContent = '00:00:00';
  564. return;
  565. }
  566. // 格式化总流逝时间为“时:分:秒”并显示
  567. elapsedTimeEl.textContent = this.formatTime(elapsedTime);
  568. },
  569. handleClickOrder(){
  570. this.$router.push({
  571. name: 'LiveOrder',
  572. query: {
  573. liveId: this.liveId
  574. }
  575. })
  576. },
  577. handleClickCoupon(){
  578. this.$router.push({
  579. name: 'LiveLiveCoupon',
  580. query: {
  581. liveId: this.liveId
  582. }
  583. })
  584. },
  585. getLive(){
  586. getLive(this.liveId).then(res => {
  587. if (res.code == 200) {
  588. if (res.data.isAudit != 1) {
  589. this.$message.error("当前直播间未经审核");
  590. this.loading = false
  591. return
  592. }
  593. this.isAudit = true
  594. this.status = res.data.status
  595. if(res.data.status == 4){
  596. this.liveType = 3
  597. this.videoUrl = res.data.videoUrl;
  598. }else {
  599. if (res.data.status != 2) {
  600. this.$message.error("当前直播间未直播");
  601. this.loading = false
  602. return
  603. }
  604. if (res.data.liveType == 1) {
  605. this.livingUrl = res.data.flvHlsUrl
  606. this.livingUrl = this.livingUrl.replace("flv","m3u8")
  607. this.$nextTick(() => {
  608. this.initPlayer()
  609. })
  610. this.startTime = new Date(res.data.startTime).getTime()
  611. this.processInterval = setInterval(this.updateLiveProgress, 1000);
  612. } else {
  613. this.liveType = 2
  614. this.videoUrl = res.data.videoUrl;
  615. this.$nextTick(() => {
  616. this.initVideoPlayer(res.data.startTime)
  617. })
  618. }
  619. }
  620. this.loading = false
  621. } else {
  622. this.$message.error(res.msg)
  623. this.loading = false
  624. }
  625. this.liveInfo = res.data
  626. })
  627. },
  628. handleClickLottery(){
  629. this.$router.push({
  630. name: 'LiveLotteryConf',
  631. query: {
  632. liveId: this.liveId
  633. }
  634. })
  635. },
  636. handleClickRed(){
  637. this.$router.push({
  638. name: 'LiveRedConf',
  639. query: {
  640. liveId: this.liveId
  641. }
  642. })
  643. },
  644. getLiveUrl(){
  645. getLivingUrl(this.liveId).then(res=>{
  646. if(res.code === 200){
  647. this.livingUrl = res.livingUrl
  648. this.initPlayer()
  649. }
  650. })
  651. },
  652. initPlayer(){
  653. var isUrl = this.livingUrl === null || this.livingUrl.trim() === ''
  654. if (isUrl) {
  655. console.error('直播地址为空,无法初始化播放器')
  656. return
  657. }
  658. if (Hls.isSupported() && !isUrl) {
  659. const videoElement = this.$refs.livingPlayer
  660. if (!videoElement) {
  661. console.error('找不到 video 元素')
  662. return
  663. }
  664. this.hls = new Hls();
  665. this.hls.attachMedia(videoElement);
  666. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  667. this.hls.loadSource(this.livingUrl);
  668. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  669. videoElement.play();
  670. });
  671. });
  672. this.hls.on(Hls.Events.ERROR, (event, data) => {
  673. console.error('HLS 错误:', data);
  674. });
  675. } else {
  676. console.error('浏览器不支持 HLS')
  677. }
  678. },
  679. handleClick(tab) {
  680. const tabName = tab.name;
  681. const params = this.pageParams[tabName];
  682. const displayList = this[`${tabName}DisplayList`];
  683. // 首次切换到该Tab或列表为空时初始化
  684. if (displayList.length < 20) {
  685. // 重置分页参数
  686. params.currentPage = 1;
  687. params.pageSize = 20;
  688. params.prevPage = 0;
  689. params.totalLoaded = 0;
  690. params.hasMore = true;
  691. params.hasPrev = false;
  692. // 加载第一页
  693. this.loadNextPage(tabName);
  694. } else {
  695. // 非首次切换,恢复滚动位置
  696. this.$nextTick(() => {
  697. const scrollEl = this.getScrollElement(tabName);
  698. if (scrollEl) {
  699. scrollEl.scrollTop = this.tabScrollPositions[tabName] || 0;
  700. }
  701. });
  702. }
  703. },
  704. saveTabScrollPositions() {
  705. this.tabScrollPositions = {
  706. online: this.getScrollElement('online')?.scrollTop || 0,
  707. offline: this.getScrollElement('offline')?.scrollTop || 0,
  708. silenced: this.getScrollElement('silenced')?.scrollTop || 0
  709. };
  710. },
  711. // 加载指定Tab的用户列表(核心加载逻辑)
  712. loadNextPage(tabName) {
  713. const params = this.pageParams[tabName];
  714. const displayList = this[`${tabName}DisplayList`];
  715. console.log(`加载 ${tabName} 用户列表`)
  716. console.log(!params.hasMore || this.scrLoading[tabName].next)
  717. console.log(params.currentPage)
  718. // 若没有更多数据或正在加载,直接返回
  719. if (!params.hasMore || this.scrLoading[tabName].next) {
  720. return;
  721. }
  722. this.scrLoading[tabName].next = true;
  723. const queryParams = {
  724. liveId: this.liveId,
  725. pageNum: params.currentPage,
  726. pageSize: 20,
  727. online: tabName === 'online' ? 0 : 1,
  728. msgStatus: tabName === 'silenced' ? 1 : 0
  729. };
  730. // 调用接口加载对应状态的分页数据(需后端支持按状态筛选)
  731. watchUserList(queryParams).then(response => {
  732. this.scrLoading[tabName].next = false;
  733. if (response.code !== 200) return;
  734. const { rows, total } = response;
  735. params.total = total; // 记录总数据量
  736. // 过滤重复数据(基于userId)
  737. const newRows = rows.filter(row =>
  738. !displayList.some(u => u.userId === row.userId)
  739. );
  740. displayList.push(...newRows)
  741. // 添加新数据并限制最大长度(避免内存占用过大)
  742. if (displayList.length >= 40) { // 最大保留100条
  743. this[`${tabName}DisplayList`] = displayList.slice(-40);
  744. // 记录滚动位置(用于加载后校准)
  745. const scrollEl = this.getScrollElement(tabName);
  746. // 校准滚动位置(保持视觉连续性)
  747. this.$nextTick(() => {
  748. if (scrollEl) {
  749. scrollEl.scrollTop = scrollEl.scrollHeight * 0.5;
  750. }
  751. });
  752. }
  753. // 更新分页状态
  754. params.hasMore = params.currentPage * params.pageSize < total;
  755. params.currentPage += 1;
  756. params.hasPrev = params.currentPage > 2; // 当前页>2时一定有上一页
  757. params.prevPage = params.currentPage - 2;
  758. }).catch(() => {
  759. this.scrLoading[tabName].next = false;
  760. });
  761. },
  762. // 新增:加载上一页(向上滚动时)
  763. loadPrevPage(tabName) {
  764. const params = this.pageParams[tabName];
  765. const displayList = this[`${tabName}DisplayList`];
  766. // 边界校验:无上一页/正在加载/当前页<=1
  767. console.log(`加载 ${tabName} 上一页`);
  768. console.log(!params.hasPrev || this.scrLoading[tabName].prev || params.currentPage <= 1)
  769. if (!params.hasPrev || this.scrLoading[tabName].prev || params.currentPage <= 1) {
  770. return;
  771. }
  772. this.scrLoading[tabName].prev = true;
  773. const targetPage = params.prevPage > 0 ? params.prevPage : params.currentPage - 2;
  774. const queryParams = {
  775. liveId: this.liveId,
  776. pageNum: targetPage,
  777. pageSize: 20,
  778. online: tabName === 'online' ? 0 : 1,
  779. msgStatus: tabName === 'silenced' ? 1 : 0
  780. };
  781. watchUserList(queryParams).then(response => {
  782. this.scrLoading[tabName].prev = false;
  783. if (response.code !== 200) return;
  784. const { rows } = response;
  785. if (rows.length === 0) {
  786. params.hasPrev = false;
  787. return;
  788. }
  789. // 记录滚动位置(用于加载后校准)
  790. const scrollEl = this.getScrollElement(tabName);
  791. const scrollTop = scrollEl?.scrollTop || 0;
  792. const itemHeight = 80; // 预估行高(根据实际样式调整)
  793. const newItemsHeight = rows.length * itemHeight;
  794. // 过滤重复数据并添加到列表头部
  795. const newRows = rows.filter(row => !displayList.some(u => u.userId === row.userId));
  796. this[`${tabName}DisplayList`] = [...newRows, ...displayList];
  797. params.totalLoaded += newRows.length;
  798. // 限制最大长度
  799. if (this[`${tabName}DisplayList`].length > 40) {
  800. this[`${tabName}DisplayList`] = this[`${tabName}DisplayList`].slice(0, 40);
  801. }
  802. // 更新分页状态
  803. params.prevPage = targetPage - 1;
  804. params.hasPrev = targetPage > 1; // 上一页页码>1时还有更多上一页
  805. params.currentPage = params.currentPage - 1;
  806. if(params.currentPage * 20 < params.total) params.hasMore = true;
  807. // 校准滚动位置(保持视觉连续性)
  808. this.$nextTick(() => {
  809. if (scrollEl) {
  810. scrollEl.scrollTop = scrollEl.scrollHeight * 0.5;
  811. }
  812. });
  813. }).catch(() => {
  814. this.scrLoading[tabName].prev = false;
  815. });
  816. },
  817. // 辅助:获取Tab对应的滚动容器
  818. getScrollElement(tabName) {
  819. const scrollRefs = {
  820. online: this.$refs.manageLeftRef_online,
  821. offline: this.$refs.manageLeftRef_offline,
  822. silenced: this.$refs.manageLeftRef_silenced
  823. };
  824. return scrollRefs[tabName]?.wrap;
  825. },
  826. getLiveVideo() {
  827. getLiveVideoByLiveId(this.liveId).then(res => {
  828. this.liveVideo = res.data
  829. })
  830. },
  831. getList() {
  832. this.resetParams()
  833. // this.loadUserList()
  834. this.loadUserTotals(); // 先加载总人数
  835. // this.handleClick('online')
  836. this.handleClick({name:'online'})
  837. this.loadMsgList()
  838. },
  839. loadUserTotals() {
  840. if (!this.liveId) return;
  841. // 假设后端提供一个接口返回总人数(如果没有,可通过首次加载全量数据后统计)
  842. getLiveUserTotals({ liveId: this.liveId }).then(res => {
  843. if (res.code === 200) {
  844. this.userTotal = res.data; // { online, offline, silenced }
  845. }
  846. });
  847. },
  848. resetParams() {
  849. // 重置各Tab的显示列表和分页参数
  850. this.onlineDisplayList = [];
  851. this.offlineDisplayList = [];
  852. this.silencedDisplayList = [];
  853. this.pageParams = {
  854. online: {
  855. currentPage: 1, // 当前页(下一页加载用)
  856. pageSize: 20, // 当前页(下一页加载用)
  857. prevPage: 0, // 上一页页码(上一页加载用)
  858. totalLoaded: 0, // 已加载总条数
  859. total: 0, // 总数据量
  860. hasMore: true, // 是否有下一页
  861. hasPrev: false // 是否有上一页
  862. },
  863. offline: {
  864. currentPage: 1,
  865. pageSize: 20,
  866. prevPage: 0,
  867. totalLoaded: 0,
  868. total: 0,
  869. hasMore: true,
  870. hasPrev: false
  871. },
  872. silenced: {
  873. currentPage: 1,
  874. pageSize: 20,
  875. prevPage: 0,
  876. totalLoaded: 0,
  877. total: 0,
  878. hasMore: true,
  879. hasPrev: false
  880. }
  881. };
  882. // 消息参数保留
  883. this.msgList = [];
  884. this.msgParams = {
  885. pageNum: 1,
  886. pageSize: 10,
  887. liveId: this.liveId
  888. };
  889. },
  890. loadUserList() {
  891. if(this.liveId == null) return
  892. // 直播间用户
  893. watchUserList({
  894. liveId: this.liveId,
  895. pageNum: this.userParams.pageNum,
  896. pageSize: this.userParams.pageSize
  897. }).then(response => {
  898. let {code,rows,total} = response
  899. if (code === 200) {
  900. let totalPage = (total % this.userParams.pageSize == 0) ? Math.floor(total / this.userParams.pageSize) : Math.floor(total / this.userParams.pageSize + 1);
  901. rows.forEach(row => {
  902. if (!this.userList.some(u => u.userId === row.userId)) {
  903. this.userList.push(row)
  904. }
  905. })
  906. // 没加载完继续加载
  907. if (this.userParams.pageNum < totalPage) {
  908. this.userParams.pageNum = parseInt(this.userParams.pageNum) + 1;
  909. this.loadUserList()
  910. }
  911. }
  912. })
  913. },
  914. loadMsgList() {
  915. // 直播间消息
  916. listLiveMsg({
  917. liveId:this.liveId,
  918. pageNum: this.msgParams.pageNum,
  919. pageSize: this.msgParams.pageSize
  920. }).then(response => {
  921. let {code, rows,total} = response;
  922. if (code === 200) {
  923. let totalPage = (total % this.msgParams.pageSize == 0) ? Math.floor(total / this.msgParams.pageSize) : Math.floor(total / this.msgParams.pageSize + 1);
  924. rows.forEach(row => {
  925. if (!this.msgList.some(m => m.msgId === row.msgId)) {
  926. let user = this.userList.find(u => u.userId === row.userId)
  927. if (user) {
  928. row.msgStatus = user.msgStatus
  929. } else {
  930. row.msgStatus = 0
  931. }
  932. this.msgList.push(row)
  933. // 移动到底部
  934. this.$nextTick(() => {
  935. setTimeout(() => {
  936. this.$refs.manageRightRef.wrap.scrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  937. }, 200)
  938. })
  939. }
  940. })
  941. // 没加载完继续加载
  942. if (this.msgParams.pageNum < totalPage) {
  943. this.msgParams.pageNum = parseInt(this.msgParams.pageNum) + 1;
  944. this.loadMsgList()
  945. }
  946. // 同步更新消息列表中相同用户的状态
  947. this.userList.forEach(u => {
  948. this.msgList.filter(m => m.userId === u.userId).forEach(m => m.msgStatus = u.msgStatus)
  949. })
  950. }
  951. })
  952. // 添加滚动监听
  953. this.$nextTick(() => {
  954. this.$refs.manageRightRef.wrap.addEventListener("scroll", this.manageRightScroll)
  955. })
  956. },
  957. manageRightScroll() {
  958. this.saveChatScrollPosition();
  959. },
  960. blockUser(u){
  961. this.$confirm('是否确认封禁用户账号为:"' + u.nickName + '-' + u.userId + '"?', "警告", {
  962. confirmButtonText: "确定",
  963. cancelButtonText: "取消",
  964. type: "warning"
  965. }).then(function() {
  966. let msg = {
  967. msg: "",
  968. liveId: this.liveId,
  969. userId: u.userId,
  970. userType: 0,
  971. cmd: 'blockUser',
  972. avatar: this.$store.state.user.user.avatar,
  973. nickName: this.$store.state.user.user.nickName
  974. }
  975. this.socket.send(JSON.stringify(msg))
  976. return blockUser(u.userId);
  977. }).then(() => {
  978. this.msgSuccess("封禁成功");
  979. }).catch(() => {});
  980. },
  981. changeUserState(u) {
  982. // 修改状态
  983. changeUserStatus({liveId: u.liveId, userId: u.userId}).then(response => {
  984. let { code } = response;
  985. if (200 === code) {
  986. u.msgStatus = u.msgStatus === 0 ? 1 : 0
  987. // 同步更新消息列表中相同用户的状态
  988. this.msgList.forEach(msg => {
  989. if (msg.userId === u.userId) {
  990. msg.msgStatus = u.msgStatus;
  991. }
  992. });
  993. this.userList.forEach(user => {
  994. if (user.userId === u.userId) {
  995. user.msgStatus = u.msgStatus;
  996. }
  997. });
  998. // 4. 关键:重新筛选所有Tab的显示列表,确保状态同步
  999. this.refreshUserDisplayLists(u);
  1000. let msg = u.msgStatus === 0 ? "已解禁" : "已禁言"
  1001. this.msgSuccess(msg);
  1002. return
  1003. }
  1004. this.msgError("操作失败");
  1005. })
  1006. },
  1007. // 新增:重新筛选所有Tab的显示列表
  1008. refreshUserDisplayLists(user) {
  1009. const { userId, msgStatus: newStatus, online } = user;
  1010. const oldStatus = newStatus === 1 ? 0 : 1; // 操作前的状态(反向)
  1011. // 1. 禁言操作(newStatus=1):从原在线/离线列表移除,加入禁言列表
  1012. if (newStatus === 1) {
  1013. // 从在线/离线列表中移除该用户(根据当前在线状态)
  1014. if (online === 0) {
  1015. this.onlineDisplayList = this.onlineDisplayList.filter(u => u.userId !== userId);
  1016. this.userTotal.online = Math.max(0, this.userTotal.online - 1);
  1017. } else {
  1018. this.offlineDisplayList = this.offlineDisplayList.filter(u => u.userId !== userId);
  1019. this.userTotal.offline = Math.max(0, this.userTotal.offline - 1);
  1020. }
  1021. this.userTotal.silenced = this.userTotal.silenced + 1;
  1022. // 添加到禁言列表(去重+限制长度)
  1023. const silencedList = this.silencedDisplayList.filter(u => u.userId !== userId);
  1024. silencedList.push(user);
  1025. this.silencedDisplayList = silencedList.slice(-40);
  1026. }
  1027. // 2. 解禁操作(newStatus=0):从禁言列表移除,回到原在线/离线列表
  1028. else {
  1029. // 从禁言列表移除
  1030. this.silencedDisplayList = this.silencedDisplayList.filter(u => u.userId !== userId);
  1031. this.userTotal.silenced = Math.max(0, this.userTotal.silenced - 1);
  1032. // 回到对应的在线/离线列表(根据当前在线状态)
  1033. if (online === 0) {
  1034. // 加入在线列表(去重+限制长度)
  1035. const onlineList = this.onlineDisplayList.filter(u => u.userId !== userId);
  1036. onlineList.push(user);
  1037. this.onlineDisplayList = onlineList.slice(-40);
  1038. this.userTotal.online = this.userTotal.online + 1;
  1039. } else {
  1040. // 加入离线列表(去重+限制长度)
  1041. const offlineList = this.offlineDisplayList.filter(u => u.userId !== userId);
  1042. offlineList.push(user);
  1043. this.offlineDisplayList = offlineList.slice(-40);
  1044. this.userTotal.offline = this.userTotal.offline + 1;
  1045. }
  1046. }
  1047. },
  1048. connectWebSocket() {
  1049. this.$store.dispatch('initLiveWs', {
  1050. liveWsUrl: this.liveWsUrl,
  1051. liveId: this.liveId,
  1052. userId: this.userId
  1053. })
  1054. this.socket = this.$store.state.liveWs[this.liveId]
  1055. this.socket.onmessage = (event) => this.handleWsMessage(event)
  1056. },
  1057. handleWsMessage(event) {
  1058. let { code, data } = JSON.parse(event.data)
  1059. if (code === 200) {
  1060. let { cmd } = data
  1061. if (cmd === 'sendMsg') {
  1062. let message = JSON.parse(data.data)
  1063. let user = this.userList.find(u => u.userId === message.userId)
  1064. if (user) {
  1065. message.msgStatus = user.msgStatus
  1066. } else {
  1067. message.msgStatus = 0
  1068. }
  1069. delete message.params
  1070. if(this.msgList.length > 50){
  1071. this.msgList.shift()
  1072. }
  1073. this.msgList.push(message)
  1074. // 移动到底部
  1075. this.$nextTick(() => {
  1076. setTimeout(() => {
  1077. this.$refs.manageRightRef.wrap.scrollTop = this.$refs.manageRightRef.wrap.scrollHeight - this.$refs.manageRightRef.wrap.clientHeight
  1078. }, 200)
  1079. })
  1080. }
  1081. else if (cmd === 'entry' || cmd === 'out') {
  1082. const user = data;
  1083. const online = cmd === 'entry' ? 0 : 1; // 0=在线,1=离线
  1084. const info = {
  1085. online:online,
  1086. msgStatus: user.msgStatus || 0,
  1087. nickName: user.nickName || '',
  1088. userType: user.userType || 0,
  1089. userId: user.userId || '',
  1090. };
  1091. // 1. 更新总人数(在线/离线互转)
  1092. if (cmd === 'entry') {
  1093. this.userTotal.online += 1;
  1094. this.userTotal.offline = Math.max(0, this.userTotal.offline - 1); // 确保不小于0
  1095. } else {
  1096. this.userTotal.offline += 1;
  1097. this.userTotal.online = Math.max(0, this.userTotal.online - 1); // 确保不小于0
  1098. }
  1099. // 2. 强制更新相关列表(无论当前激活哪个Tab)
  1100. if (cmd === 'entry') {
  1101. // 用户进入:从离线列表删除,添加到在线列表
  1102. this.offlineDisplayList = this.offlineDisplayList.filter(u => u.userId !== user.userId);
  1103. const newOnlineList = this.onlineDisplayList.filter(u => u.userId !== user.userId);
  1104. newOnlineList.push(info);
  1105. this.onlineDisplayList = newOnlineList.slice(-40); // 限制最大50条
  1106. } else {
  1107. // 用户离开:从在线列表删除,添加到离线列表
  1108. this.onlineDisplayList = this.onlineDisplayList.filter(u => u.userId !== user.userId);
  1109. const newOfflineList = this.offlineDisplayList.filter(u => u.userId !== user.userId);
  1110. newOfflineList.push(info);
  1111. this.offlineDisplayList = newOfflineList.slice(-40); // 限制最大50条
  1112. }
  1113. // 3. 处理禁言列表(如果用户是禁言状态,无论进出都要同步)
  1114. if (info.msgStatus === 1) {
  1115. // 禁言用户:从禁言列表删除旧数据,添加新数据
  1116. const newSilencedList = this.silencedDisplayList.filter(u => u.userId !== user.userId);
  1117. newSilencedList.push(info);
  1118. this.silencedDisplayList = newSilencedList.slice(-40);
  1119. } else {
  1120. // 非禁言用户:从禁言列表删除(如果存在)
  1121. this.silencedDisplayList = this.silencedDisplayList.filter(u => u.userId !== user.userId);
  1122. }
  1123. } else if (cmd === 'live_start') {
  1124. } else if (cmd === 'live_end') {
  1125. }
  1126. }
  1127. },
  1128. sendMessage() {
  1129. // 发送前简单校验
  1130. if (this.newMsg.trim() === '') {
  1131. return;
  1132. }
  1133. let msg = {
  1134. msg: this.newMsg,
  1135. liveId: this.liveId,
  1136. userId: this.userId,
  1137. userType: 1,
  1138. cmd: 'sendMsg',
  1139. avatar: this.$store.state.user.user.avatar,
  1140. nickName: this.$store.state.user.user.nickName
  1141. }
  1142. this.socket.send(JSON.stringify(msg))
  1143. this.newMsg = '';
  1144. },
  1145. // 初始化滚动监听(在mounted中调用)
  1146. initScrollListeners() {
  1147. // 为每个Tab的滚动容器添加监听
  1148. this.$nextTick(() => {
  1149. const scrollRefs = {
  1150. online: this.$refs.manageLeftRef_online,
  1151. offline: this.$refs.manageLeftRef_offline,
  1152. silenced: this.$refs.manageLeftRef_silenced
  1153. };
  1154. Object.keys(scrollRefs).forEach(tabName => {
  1155. const scrollEl = scrollRefs[tabName]?.wrap;
  1156. if (scrollEl) {
  1157. scrollEl.addEventListener('scroll', () =>
  1158. this.handleTabScroll(tabName, scrollEl)
  1159. );
  1160. }
  1161. });
  1162. });
  1163. },
  1164. handleTabScroll(tabName, scrollEl) {
  1165. const { scrollTop, scrollHeight, clientHeight } = scrollEl;
  1166. const bottomThreshold = 50; // 距离底部100px触发下一页
  1167. const topThreshold = 50; // 距离顶部100px触发上一页
  1168. // 加载下一页(滚动到底部附近)
  1169. if (scrollHeight - scrollTop - clientHeight < bottomThreshold) {
  1170. this.loadNextPage(tabName);
  1171. }
  1172. // 加载上一页(滚动到顶部附近)
  1173. if (scrollTop < topThreshold) {
  1174. this.loadPrevPage(tabName);
  1175. }
  1176. },
  1177. },
  1178. destroyed() {
  1179. this.hls?.destroy();
  1180. clearInterval(this.processInterval)
  1181. }
  1182. }
  1183. </script>
  1184. <style scoped>
  1185. .talk-list{
  1186. display: flex;
  1187. }
  1188. .live-console {
  1189. width: 90vw;
  1190. padding: 10px 0;
  1191. }
  1192. .live-console .live-console-col {
  1193. height: 88vh;
  1194. margin-left: 5px;
  1195. padding: 0 10px;
  1196. background-color: white;
  1197. border-radius: 4px;
  1198. }
  1199. /*隐藏水平滚动条*/
  1200. ::v-deep .el-scrollbar__wrap {
  1201. overflow-x: hidden;
  1202. }
  1203. /* 消息输入区域 */
  1204. .chat-input {
  1205. display: flex;
  1206. padding: 10px;
  1207. border-top: 1px solid #ebeef5;
  1208. background-color: #fff;
  1209. min-height: 120px;
  1210. }
  1211. .chat-input .el-input {
  1212. flex: 1;
  1213. margin-right: 10px;
  1214. }
  1215. .chat-input .el-textarea__inner {
  1216. resize: none;
  1217. min-height: 100px;
  1218. }
  1219. ::v-deep .el-textarea__inner {
  1220. border: none !important;
  1221. box-shadow: none !important;
  1222. resize: none !important;
  1223. }
  1224. ::v-deep .el-textarea__inner:focus {
  1225. border: none !important;
  1226. box-shadow: none !important;
  1227. }
  1228. </style>