index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <template>
  2. <div class="live">
  3. <!--顶部-->
  4. <div class="live-top">
  5. <div class="live-title">
  6. <div class="live-title-name">{{ roomInfo.cs_item_name }} {{ roomInfo.task_name }}</div>
  7. <div>
  8. <el-button v-if="!liveStat" @click="startLive">开启直播</el-button>
  9. <el-button v-if="liveStat" icon="el-icon-switch-button" @click="closeLiveRoom">
  10. 结束直播
  11. </el-button>
  12. <el-button v-if="liveStat" @click="publishStream">推流</el-button>
  13. </div>
  14. </div>
  15. <div class="live-course-name">{{ roomInfo.course_name }}</div>
  16. <div class="live-teacher">
  17. <span class="live-teacher-name">
  18. <svg-icon icon-class="person" />{{ roomInfo.teacher_name }}
  19. </span>
  20. <span><svg-icon icon-class="people" />{{ roomInfo.student_count }}</span>
  21. </div>
  22. </div>
  23. <!-- 主容器 -->
  24. <div class="live-container">
  25. <!-- 左侧 -->
  26. <div class="live-container-left">
  27. <div v-show="callLoading" class="loading">
  28. <div class="loading-wrapper">
  29. <p class="loading-title">正在呼叫,等待对方接通...</p>
  30. <div>
  31. <el-button type="danger" circle @click="handsDown('loading')">
  32. <svg-icon icon-class="hang-up" />
  33. </el-button>
  34. </div>
  35. </div>
  36. </div>
  37. <div v-show="connect" class="student-parent">
  38. <div v-show="roomInfo.video_mode === 1 || remoteStreamType === 1" id="student"></div>
  39. <template v-if="remoteStreamType !== 1">
  40. <template v-if="roomInfo.video_mode === 1">
  41. <el-button type="danger" @click="handsDown">
  42. <svg-icon icon-class="hang-up" /> 挂断
  43. </el-button>
  44. </template>
  45. <template v-else>
  46. <div class="student-audio">
  47. <el-avatar icon="el-icon-user" :src="connectStudent.student_image_url" />
  48. <el-button type="danger" circle @click="handsDown">
  49. <svg-icon icon-class="hang-up" />
  50. </el-button>
  51. </div>
  52. </template>
  53. </template>
  54. </div>
  55. <div v-show="isDraw" id="draw-parent">
  56. <div v-show="isDrawSetting" class="draw-setting">
  57. <span class="brush-shape">
  58. <svg-icon icon-class="brush-shape" />
  59. </span>
  60. <span
  61. v-for="item in drawColorList"
  62. :key="item"
  63. :class="['draw-color', item === '#FF4747' ? 'current' : '']"
  64. :style="{ 'background-color': item }"
  65. ></span>
  66. <span v-for="item in drawThicknessList" :key="item" class="draw-thickness">
  67. <span :style="{ width: item * 2 + 'px', height: item * 2 + 'px' }"></span>
  68. </span>
  69. <span class="brush-clear">
  70. <svg-icon icon-class="clear" />
  71. </span>
  72. </div>
  73. </div>
  74. <div class="button-group">
  75. <div class="button-group-left">
  76. <span title="屏幕共享" @click="publishShareStream">
  77. <svg-icon icon-class="share" />
  78. </span>
  79. <span title="白板" @click="showDrawSetting">
  80. <svg-icon icon-class="draw" />
  81. </span>
  82. <span title="课件推送" @click="dialogVisible = true">
  83. <svg-icon icon-class="push" />
  84. </span>
  85. </div>
  86. <div class="button-group-right"></div>
  87. </div>
  88. <div class="live-container-left-chat">
  89. <div class="chat-top">
  90. <span>聊天</span>
  91. <label @click="chatBans">
  92. <input v-model="roomData.allow_chat" type="checkbox" class="allow-chat" />
  93. <span>禁言</span>
  94. </label>
  95. </div>
  96. <div class="chat-window">
  97. <ul class="chat-window-ul">
  98. <li v-for="(item, i) in chatList" :key="i">
  99. <div class="msg-normal">
  100. <span>{{ item.username }}: </span>
  101. <span>{{ item.msg }}</span>
  102. </div>
  103. </li>
  104. </ul>
  105. </div>
  106. <div class="chat-speak">
  107. <el-input
  108. v-model="msg"
  109. placeholder="输入发言"
  110. maxlength="400"
  111. @keydown.enter.native="sendMsg"
  112. >
  113. <el-button slot="append" @click="sendMsg">发送</el-button>
  114. </el-input>
  115. </div>
  116. </div>
  117. </div>
  118. <!-- 右侧 -->
  119. <div class="live-container-right">
  120. <div
  121. class="live-teacher-lens"
  122. @mouseover="liveMenuShow = true"
  123. @mouseout="liveMenuShow = false"
  124. >
  125. <div id="live"></div>
  126. <div :style="{ bottom: liveMenuShow ? '0' : '-40px' }" class="live-wrapper">
  127. <div>
  128. {{ roomInfo.teacher_name }}
  129. </div>
  130. <div></div>
  131. </div>
  132. </div>
  133. <div class="student-list">
  134. <div class="student-list-title">学生列表</div>
  135. <ul>
  136. <li v-for="item in student_list" :key="item.room_user_id">
  137. <div class="student-list-left">
  138. <el-avatar icon="el-icon-user" size="small" :src="item.student_image_url" />
  139. <span class="name">{{ item.student_name }}</span>
  140. </div>
  141. <div class="student-list-right">
  142. <svg-icon icon-class="video" @click="invite(item, 1)" />
  143. <svg-icon icon-class="voice" @click="invite(item, 2)" />
  144. </div>
  145. </li>
  146. </ul>
  147. </div>
  148. </div>
  149. </div>
  150. <!-- 推送资料 -->
  151. <select-material
  152. :dialog-visible="dialogVisible"
  153. :task-id="task_id"
  154. @dialogClose="dialogClose"
  155. @dialogPush="dialogPush"
  156. />
  157. <!-- 教师查看当前完成列表 -->
  158. <complete-list
  159. :task-id="task_id"
  160. :dialog-visible-complete="dialogVisibleComplete"
  161. @dialogCompleteClose="dialogCompleteClose"
  162. />
  163. </div>
  164. </template>
  165. <script>
  166. import {
  167. GetLiveRoomStudentList,
  168. CloseLiveRoom,
  169. GetLiveRoomInfo,
  170. StudentExitLiveRoom
  171. } from '@/api/live';
  172. import SelectMaterial from '@/components/live/SelectMaterial.vue';
  173. import CompleteList from './CompleteList.vue';
  174. import * as common from './live';
  175. export default {
  176. name: 'Live',
  177. components: {
  178. SelectMaterial,
  179. CompleteList
  180. },
  181. data() {
  182. return {
  183. task_id: this.$route.query.task_id,
  184. // 连麦
  185. connect: false,
  186. connectStudent: {},
  187. // 等待接通
  188. callLoading: false,
  189. dialogVisible: false,
  190. // 学员完成
  191. dialogVisibleComplete: false,
  192. // 定时器
  193. timer: null,
  194. remoteStreamType: -1,
  195. rtc: null,
  196. roomData: {
  197. desc: '直播间标题',
  198. name: '姓名',
  199. user: {
  200. id: '',
  201. name: '',
  202. role: 'talker',
  203. rommid: ''
  204. },
  205. max_users: 1,
  206. allow_chat: true,
  207. allow_audio: true,
  208. allow_speak: true
  209. },
  210. roomInfo: {
  211. room_id: '',
  212. video_mode: 1,
  213. task_name: '',
  214. cs_item_name: '',
  215. course_name: '',
  216. teacher_name: '',
  217. student_count: 0
  218. },
  219. loadedNumber: 0,
  220. speakData: {},
  221. roomContext: {},
  222. msg: '',
  223. chatList: [],
  224. isDrawSetting: false,
  225. drawColorList: ['#FF4747', '#343434', '#628EFF', '#FFCA0E'],
  226. drawThicknessList: ['1', '3', '5'],
  227. // 直播间学员列表
  228. student_list: [],
  229. // 直播状态
  230. liveStat: false,
  231. liveMenuShow: false
  232. };
  233. },
  234. computed: {
  235. // 画板模式
  236. isDraw() {
  237. return !this.connect && !this.callLoading;
  238. }
  239. },
  240. watch: {
  241. loadedNumber(newVal) {
  242. if (newVal === 5) {
  243. common.createScript(
  244. 'https://class.csslcloud.net/static/SDK/docSDK/drawSdk_3.0.js'
  245. ).onload = () => {
  246. this.initSDK();
  247. this.$loading().close();
  248. };
  249. }
  250. }
  251. },
  252. created() {
  253. this.$loading({
  254. text: '加载直播所需SDK中...',
  255. background: '#fff'
  256. });
  257. common.downloadWebSDK(this);
  258. this.getLiveRoomStudentList();
  259. this.getLiveRoomInfo();
  260. },
  261. mounted() {
  262. document.addEventListener(
  263. 'click',
  264. e => {
  265. let target = e.target;
  266. let isHasClass = false;
  267. do {
  268. if (target === null) {
  269. break;
  270. }
  271. if (target.className === 'draw-setting') {
  272. isHasClass = true;
  273. }
  274. target = target.parentElement;
  275. } while (!isHasClass);
  276. if (!isHasClass) {
  277. this.isDrawSetting = false;
  278. }
  279. },
  280. true
  281. );
  282. this.getLiveRoomStudentListPolling();
  283. },
  284. beforeDestroy() {
  285. // 清除所有定时器
  286. // let end = setInterval(() => {}, 1000);
  287. // for (let i = 1; i <= end; i++) {
  288. // clearInterval(i);
  289. // }
  290. clearInterval(this.timer);
  291. },
  292. methods: {
  293. initSDK() {
  294. const { live_room_sys_user_id, room_id, session_id } = this.$route.query;
  295. this.rtc = common.initSDK({
  296. userid: live_room_sys_user_id,
  297. roomid: room_id,
  298. sessionid: session_id
  299. });
  300. common.initListener(this); // 注册监听事件
  301. this.getLiveStat();
  302. },
  303. getLiveRoomInfo() {
  304. GetLiveRoomInfo({ task_id: this.task_id }).then(
  305. ({
  306. room_id,
  307. video_mode,
  308. task_name,
  309. cs_item_name,
  310. course_name,
  311. teacher_name,
  312. student_count
  313. }) => {
  314. this.roomInfo = {
  315. room_id,
  316. video_mode,
  317. task_name,
  318. cs_item_name,
  319. course_name,
  320. teacher_name,
  321. student_count
  322. };
  323. }
  324. );
  325. },
  326. closeLiveRoom() {
  327. CloseLiveRoom({ task_id: this.task_id }).then(() => {
  328. this.$router.push('/');
  329. this.$message.success('关闭直播成功');
  330. });
  331. },
  332. startLive() {
  333. common.startLive();
  334. },
  335. getLiveStat() {
  336. common.getLiveStat({
  337. success: data => {
  338. this.liveStat = data.started;
  339. },
  340. fail: str => {
  341. this.liveStat = false;
  342. console.log('直播关闭状态或查询直播失败', str);
  343. }
  344. });
  345. },
  346. // 推送桌面共享
  347. publishShareStream() {
  348. common.publishShareStream();
  349. },
  350. // 关闭桌面共享
  351. unPubShareStream() {
  352. common.unPubShareStream();
  353. },
  354. // 老师邀请学生上麦
  355. invite(student, mode) {
  356. if (this.connect || this.callLoading) {
  357. this.$message.warning('正在连麦中');
  358. return;
  359. }
  360. this.callLoading = true;
  361. this.connectStudent = student;
  362. GetLiveRoomInfo({ task_id: this.task_id })
  363. .then(({ video_mode }) => {
  364. let uid = student.room_user_id;
  365. if (video_mode === mode) {
  366. common.invite(uid);
  367. } else {
  368. common.roomUpdate({
  369. video_mode: mode,
  370. roomUpdateSuccess: data => {
  371. console.log(data, '连麦音视频模式更新请求成功!');
  372. this.roomInfo.video_mode = mode;
  373. common.invite(uid);
  374. },
  375. roomUpdateFailed: data => {
  376. this.callLoading = false;
  377. this.connectStudent = '';
  378. this.$message.error('连麦音视频模式更新请求失败! 请稍后再试!');
  379. }
  380. });
  381. }
  382. })
  383. .catch(() => {
  384. this.callLoading = false;
  385. this.connectStudent = '';
  386. });
  387. },
  388. // 下麦
  389. handsDown(type) {
  390. common.handsDown({
  391. uid: this.connectStudent.room_user_id,
  392. success: str => {
  393. if (type === 'loading') {
  394. common.sendPublishMessage({
  395. type: 'handsDown-load',
  396. uid: this.connectStudent.room_user_id
  397. });
  398. }
  399. this.callLoading = false;
  400. this.connectStudent = '';
  401. this.$message.success('下麦成功');
  402. },
  403. fail: data => {
  404. console.log('下麦失败', data);
  405. }
  406. });
  407. },
  408. // 发消息
  409. sendMsg() {
  410. common.sendMsg(this.msg);
  411. this.msg = '';
  412. },
  413. chatBans() {
  414. common.roomUpdate({
  415. allow_chat: !this.roomData.allow_chat,
  416. roomUpdateSuccess: function (data) {
  417. console.log(data, '房间模板配置更新请求成功!');
  418. },
  419. roomUpdateFailed: function (data) {
  420. console.log(data, '房间模板配置更新请求失败! 请稍后再试!');
  421. }
  422. });
  423. },
  424. // 画笔变更
  425. drawChange(action, value) {
  426. common.drawChange(action, value);
  427. },
  428. getDevice() {
  429. common.getDevice();
  430. },
  431. showDrawSetting() {
  432. this.isDrawSetting = !this.isDrawSetting;
  433. },
  434. getLiveRoomStudentList() {
  435. GetLiveRoomStudentList({ task_id: this.task_id }).then(({ student_list }) => {
  436. this.student_list = student_list;
  437. });
  438. },
  439. getLiveRoomStudentListPolling() {
  440. this.timer = setInterval(() => {
  441. this.getLiveRoomStudentList();
  442. }, 5000);
  443. },
  444. studentExitLiveRoom(room_user_id) {
  445. StudentExitLiveRoom({ task_id: this.task_id, room_user_id });
  446. },
  447. // 弹出框方法
  448. publishStream() {
  449. common.publishStream('main');
  450. },
  451. dialogClose() {
  452. this.dialogVisible = false;
  453. },
  454. dialogPush() {
  455. this.dialogVisible = false;
  456. this.dialogVisibleComplete = true;
  457. },
  458. dialogCompleteClose() {
  459. this.dialogVisibleComplete = false;
  460. }
  461. }
  462. };
  463. </script>
  464. <style lang="scss">
  465. @import '~@/styles/mixin.scss';
  466. $live-bc: #3d3938;
  467. .live {
  468. @include container;
  469. // 顶部
  470. &-top {
  471. background-color: #fff;
  472. padding: 24px 32px;
  473. border-top-left-radius: 8px;
  474. border-top-right-radius: 8px;
  475. .live-title {
  476. display: flex;
  477. justify-content: space-between;
  478. &-name {
  479. font-size: 22px;
  480. }
  481. .el-button {
  482. border-radius: 4px;
  483. padding: 7px 12px;
  484. }
  485. }
  486. .live-course-name {
  487. font-size: 14px;
  488. color: #737373;
  489. line-height: 30px;
  490. }
  491. .live-teacher {
  492. margin-top: 12px;
  493. .svg-icon {
  494. margin-right: 8px;
  495. }
  496. &-name {
  497. margin-right: 60px;
  498. }
  499. }
  500. }
  501. // 主容器
  502. &-container {
  503. display: flex;
  504. justify-content: left;
  505. &-left {
  506. width: 832px;
  507. background-color: #fff;
  508. border-radius: 8px;
  509. .loading {
  510. position: relative;
  511. width: 100%;
  512. height: 468px;
  513. color: #fff;
  514. background-color: #646464;
  515. display: flex;
  516. justify-content: center;
  517. align-items: center;
  518. &-wrapper {
  519. text-align: center;
  520. .loading-title {
  521. margin-bottom: 24px;
  522. }
  523. }
  524. }
  525. .student-parent {
  526. position: relative;
  527. width: 100%;
  528. height: 468px;
  529. #student {
  530. width: 100%;
  531. height: 468px;
  532. border: 1px solid #ccc;
  533. position: relative;
  534. background-color: $live-bc;
  535. }
  536. > .el-button {
  537. position: absolute;
  538. bottom: 40px;
  539. left: calc(50% - 44px);
  540. }
  541. .student-audio {
  542. width: 100%;
  543. height: 100%;
  544. background-color: #646464;
  545. display: flex;
  546. flex-direction: column;
  547. justify-content: center;
  548. align-items: center;
  549. .el-avatar {
  550. width: 96px;
  551. height: 96px;
  552. line-height: 96px;
  553. margin-bottom: 24px;
  554. &--icon {
  555. font-size: 36px;
  556. }
  557. }
  558. }
  559. }
  560. #draw-parent {
  561. width: 100%;
  562. height: 468px;
  563. border: 1px solid #ccc;
  564. position: relative;
  565. background-color: $live-bc;
  566. overflow: hidden;
  567. // 设置屏幕画笔
  568. .draw-setting {
  569. position: absolute;
  570. bottom: 13px;
  571. left: 22px;
  572. z-index: 9999;
  573. background-color: #a0a0a0;
  574. padding: 6px;
  575. border-radius: 40px;
  576. height: 40px;
  577. width: 275px;
  578. line-height: 28px;
  579. & > span {
  580. display: inline-block;
  581. text-align: center;
  582. margin-right: 10px;
  583. }
  584. & > span.brush-shape {
  585. width: 28px;
  586. height: 28px;
  587. border-radius: 50%;
  588. background-color: #fff;
  589. cursor: pointer;
  590. }
  591. .draw-color {
  592. position: relative;
  593. top: 5px;
  594. height: 18px;
  595. width: 18px;
  596. border-radius: 50%;
  597. cursor: pointer;
  598. }
  599. .current::after {
  600. content: '';
  601. position: absolute;
  602. bottom: -7px;
  603. left: 7px;
  604. width: 4px;
  605. height: 4px;
  606. border-radius: 50%;
  607. background-color: #292929;
  608. }
  609. .draw-thickness {
  610. height: 18px;
  611. width: 18px;
  612. cursor: pointer;
  613. display: inline-flex;
  614. flex-direction: column;
  615. justify-content: center;
  616. vertical-align: middle;
  617. align-items: center;
  618. span {
  619. border-radius: 50%;
  620. background-color: #000;
  621. }
  622. }
  623. & > .brush-clear {
  624. width: 28px;
  625. height: 28px;
  626. border-radius: 50%;
  627. background-color: #666;
  628. cursor: pointer;
  629. margin-right: 0;
  630. }
  631. }
  632. }
  633. .button-group {
  634. display: flex;
  635. justify-content: space-between;
  636. height: 48px;
  637. background-color: #4d4d4d;
  638. padding: 0 15px;
  639. border-bottom-left-radius: 5px;
  640. .svg-icon {
  641. font-size: 20px;
  642. }
  643. &-left {
  644. > span {
  645. display: inline-block;
  646. height: 100%;
  647. padding: 14px 16px;
  648. cursor: pointer;
  649. &:active,
  650. &:hover {
  651. background-color: #3d3d3d;
  652. }
  653. }
  654. }
  655. }
  656. // 聊天窗口
  657. &-chat {
  658. height: 278px;
  659. border: 1px solid #ccc;
  660. border-bottom-left-radius: 8px;
  661. display: flex;
  662. flex-direction: column;
  663. justify-content: space-between;
  664. .chat-top {
  665. display: flex;
  666. justify-content: space-between;
  667. padding: 15px 15px 10px;
  668. border-bottom: 1px solid #e6e6e6;
  669. color: #959595;
  670. label {
  671. cursor: pointer;
  672. }
  673. .allow-chat {
  674. margin-right: 12px;
  675. }
  676. }
  677. .chat-window {
  678. position: relative;
  679. width: 100%;
  680. height: 100%;
  681. overflow: hidden;
  682. &-ul {
  683. position: absolute;
  684. top: 0;
  685. left: 0;
  686. width: 100%;
  687. height: 100%;
  688. overflow: auto;
  689. .msg-normal {
  690. padding: 7px 16px;
  691. }
  692. }
  693. }
  694. .chat-speak {
  695. padding: 16px;
  696. }
  697. }
  698. }
  699. &-right {
  700. padding: 8px;
  701. background-color: #2c2c2c;
  702. border-end-end-radius: 8px;
  703. .live-teacher-lens {
  704. position: relative;
  705. overflow: hidden;
  706. #live {
  707. width: 352px;
  708. height: 198px;
  709. background-color: $live-bc;
  710. }
  711. .live-wrapper {
  712. position: absolute;
  713. height: 40px;
  714. width: 100%;
  715. background-color: #000;
  716. opacity: 0.7;
  717. color: #fff;
  718. line-height: 40px;
  719. padding: 0 16px;
  720. transition: all 300ms ease-in 0s;
  721. }
  722. }
  723. // 学员列表
  724. .student-list {
  725. width: 100%;
  726. padding: 24px 16px;
  727. margin-top: 2px;
  728. height: calc(100% - 200px);
  729. background-color: #2c2c2c;
  730. font-size: 14px;
  731. color: #fff;
  732. &-title {
  733. margin-bottom: 16px;
  734. }
  735. li {
  736. display: flex;
  737. margin-bottom: 16px;
  738. .student-list-left {
  739. flex: 8;
  740. .name {
  741. vertical-align: super;
  742. margin-left: 8px;
  743. }
  744. }
  745. .student-list-right {
  746. flex: 2;
  747. .svg-icon {
  748. font-size: 18px;
  749. cursor: pointer;
  750. margin-top: 7px;
  751. margin-right: 8px;
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. </style>