look-model.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { defineComponent } from "vue";
  2. import { ElButton } from 'element-plus'
  3. import runtime, * as RuntimeUtils from '/src/components/live-broadcast/runtime'
  4. import styles from './index.module.less'
  5. import event, { LIVE_EVENT_MESSAGE } from '/src/components/live-broadcast/event';
  6. import { state } from '/src/state'
  7. import dayjs from 'dayjs';
  8. import Empty from "/src/components/empty";
  9. import runtimeModel, * as RuntimeModelUtils from '/src/components/live-message/model/runtime'
  10. import request from "/src/helpers/request";
  11. export default defineComponent({
  12. data() {
  13. return {
  14. loadingLook: false, // 观看列表状态
  15. upStatus: false,
  16. downStatus: false
  17. }
  18. },
  19. computed: {
  20. count() {
  21. let count = 0
  22. for (const key in runtimeModel.lookList) {
  23. if (Object.prototype.hasOwnProperty.call(runtimeModel.lookList, key)) {
  24. const item = runtimeModel.lookList[key];
  25. if (item.userRoomType === 2 || item.userRoomType === 4) {
  26. count += 1
  27. }
  28. if (count > 3) {
  29. break
  30. }
  31. }
  32. }
  33. return count
  34. },
  35. },
  36. async mounted() {
  37. await this._init()
  38. this.loadingLook = true
  39. event.on(LIVE_EVENT_MESSAGE["RC:Chatroom:Welcome"], this.onWelcome);
  40. event.on(LIVE_EVENT_MESSAGE["RC:Chatroom:MemberCountUp"], this.onMemberCount);
  41. setTimeout(() => {
  42. this.loadingLook = false;
  43. })
  44. },
  45. beforeUnmount() {
  46. event.off(LIVE_EVENT_MESSAGE["RC:Chatroom:Welcome"], this.onWelcome);
  47. event.off(LIVE_EVENT_MESSAGE["RC:Chatroom:MemberCountUp"], this.onMemberCount);
  48. },
  49. methods: {
  50. async _init() {
  51. try {
  52. const roomUid = sessionStorage.getItem('roomUid')
  53. const res = await request.get(`/api-web/imLiveBroadcastRoom/queryRoomUserInfo`, {
  54. params: {
  55. roomUid: roomUid,
  56. }
  57. })
  58. const resList = res.data
  59. resList.forEach((item: any) => {
  60. // 判断是已经,存在学生
  61. if(!runtimeModel.lookList[item.userId]) {
  62. runtimeModel.lookList[item.userId] = {
  63. id: item.userId,
  64. name: item.userName,
  65. type: 3,
  66. userRoomType: 1,
  67. time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  68. }
  69. }
  70. })
  71. } catch {
  72. //
  73. }
  74. },
  75. /**
  76. * 当后端发送人数消息时,更新人数
  77. */
  78. onMemberCount(evt: any) {
  79. runtime.lookCount = evt.content.count || 0
  80. RuntimeUtils.sendMessage({ count: runtime.lookCount }, 'MemberCount')
  81. },
  82. async onWelcome(value: any) {
  83. // console.log(value)
  84. if (value && value.user) {
  85. const sendTime = dayjs(value.$EventMessage.sentTime || new Date()).format('HH:mm:ss')
  86. let tempObj = {
  87. name: value.user.name,
  88. id: value.user.id,
  89. sendTime,
  90. userRoomType: 1,
  91. type: 3
  92. }
  93. // 判断是否有同一个人
  94. let isExist = runtimeModel.lookList[tempObj.id] ? true : false;
  95. if (!isExist) {
  96. RuntimeModelUtils.addLook(tempObj.id, tempObj);
  97. console.log('添加观看人员', tempObj)
  98. // runtime.lookCount += 1;
  99. }
  100. // 同步移动端观看人数
  101. // await RuntimeUtils.sendMessage({ count: runtime.lookCount }, 'MemberCount')
  102. this.loadingLook = false
  103. }
  104. },
  105. async onUpLook(item: any) {
  106. try {
  107. console.log(this.count, runtimeModel.lookList, 'this.count, runtimeModel.lookList')
  108. if(this.count > 3) {
  109. return
  110. }
  111. // RC:Chatroom:SeatResponse type teacherName teacherId audienceName audienceId
  112. // 操作类型 1 主讲人同意 2 主讲人拒绝 3 观众同意 4 观众拒绝
  113. // RC:Chatroom:SeatApply type teacherName teacherId audienceName audienceId
  114. // 操作类型 1 主讲人邀请 2 主讲人取消邀请 3 观众申请 4 观众取消申请 5将麦上观众踢下麦
  115. // userRoomType
  116. // 1 普通
  117. // 2 老师邀请
  118. // 3 学生申请
  119. // 4 连麦中
  120. const data = {
  121. audienceName: item.name,
  122. audienceId: String(item.id),
  123. teacherId: String(state.user?.id),
  124. teacherName: state.user?.speakerName,
  125. userRoomType: 2,
  126. type: 1
  127. }
  128. item.userRoomType = 2;
  129. await RuntimeUtils.sendMessage(data, 'SeatApply')
  130. } catch {
  131. //
  132. }
  133. },
  134. async onDownLook(item: any) {
  135. try {
  136. const data = {
  137. ...item,
  138. audienceName: item.name,
  139. audienceId: String(item.id),
  140. teacherId: String(state.user?.id),
  141. teacherName: state.user?.speakerName,
  142. type: 5,
  143. }
  144. RuntimeModelUtils.addJoin(item.id, data)
  145. RuntimeUtils.sendMessage(data, 'SeatApply')
  146. } catch {
  147. }
  148. },
  149. },
  150. render() {
  151. const list = Object.values(runtimeModel.lookList)
  152. return (
  153. <div>
  154. {list.length > 0 ? list.map((item : any) => (
  155. <div class={styles.itemContent}>
  156. <div class={styles.itemInfo} >
  157. <div class={styles.itemName}>
  158. <p class={styles.userName}>
  159. <span class={styles['name-style']}>{item.name}</span>
  160. </p>
  161. {/* 1、最多三个人上麦;2、老师主动邀请;3、没有开始直播 */}
  162. {item.userRoomType !== 4 ? <ElButton size="small" type="primary" disabled={this.count > 3 || item.userRoomType === 2 || runtime.videoStatus !== 'liveing'} class={styles.btn} onClick={() => this.onUpLook(item)}>上麦</ElButton> : <ElButton size="small" plain class={[styles.btn, styles.downBtn]} onClick={() => this.onDownLook(item)}>下麦</ElButton>}
  163. </div>
  164. </div>
  165. </div>
  166. )) : this.loadingLook ? <div class={styles.loadingStyle} ><div class="el-loading-mask" style="background-color: rgba(0, 0, 0, 0.8);"><div class="el-loading-spinner"><svg class="circular" viewBox="25 25 50 50"><circle class="path" cx="50" cy="50" r="20" fill="none"></circle></svg></div></div></div> : <Empty style={{ paddingTop: '120px' }} text="暂无学员观看!" icon="noData-no-user" />}
  167. </div>
  168. )
  169. }
  170. })