1 |
- import EVENT from"./EVENT";import{request}from"../api/plv-request";const{SHOW_BOARD:SHOW_BOARD,HIDE_BOARD:HIDE_BOARD,PPT_RELOAD:PPT_RELOAD}=EVENT;class SocketEvent{constructor({chat:t,pptCtrl:e,paintCtrl:a,isLive:s=!0,getCurrentTime:r=(()=>{}),videoId:i,delayTime:n=3e3,requestUrl:o}){this.pptCtrl=e,this.paintbrushCtrl=a,this.chat=t,this.currentAutoId="",this.subFn=[],this._setLiveStatus(s),this.currentPlayTime=0,this.isRedraw=!0,this.getCurrentTime=r,this.videoId=i,this.happenResize=!1,this.requestUrlPre=o,this.delayTime=n,this.currentWhiteNum=0,t&&this.setChat(t)}setChat(t,e){void 0!==e&&this._setLiveStatus(e),this.chat=t,this.initEvent()}initEvent(){if(!this.chat)return;const t=this.chatEvent=this.getChatEvent(!0);Object.keys(t).forEach((e=>{this.chat.on(e,t[e])}))}getChatEvent(t=!1){const e=this.chat&&this.chat.events,a=()=>new Promise((e=>{t&&this.delayTime>0?setTimeout(e,this.delayTime):e()})),s=this;return e?{[e.SLICEID](e,a){t&&!s.isLive||s.onSliceId(a.data)},[e.SLICESTART](e,a){if(t){const t={roomId:a.roomId,sessionId:a.sessionId},e=s.reloadData=Object.assign(t,a.data);s.reload(e)}else s.onSliceStart(a)},[e.SLICEDOPEN](t,e){a().then((()=>s.onSliceOpen(e.data)))},[e.SLICECONTROL](t,e){a().then((()=>s.onSliceControl(e.data)))},[e.SLICEDRAW](t,e){a().then((()=>s.onSliceDraw(e.data)))}}:null}reload(t){const e=this.chatEvent;Object.keys(e).forEach((t=>{this.chat.off(t,e[t])})),this.emit({EVENT:PPT_RELOAD,data:t})}init(t,e,a){const s=1===parseInt(a);if(parseInt(t)===parseInt(this.currentAutoId)){if(0===parseInt(t))return Promise.resolve();if(this.deleteWhiteBoard(),s===this.pptCtrl.isNew)return Promise.resolve()}return 0===parseInt(t)?(this.openWhiteBoard(e),Promise.resolve()):this.pptCtrl.init(t,s).then((a=>{!0===a&&(this.paintbrushCtrl.clear(),this.deleteWhiteBoard(),this.pptCtrl.gotoPage(parseInt(e)),this.currentAutoId=t)}))}onSliceDraw(t){const{pageId:e,autoId:a}=t;0!==parseInt(a)&&parseInt(a)===parseInt(this.currentAutoId)&&parseInt(e)!==parseInt(this.pptCtrl.currentPageNum)&&this.pptCtrl.gotoPage(parseInt(e)),this.paintbrushCtrl.drawFromSocket(t,this.pageType)}onSliceOpen(t){const{pageId:e,autoId:a,type:s,docType:r}=t;this.currentTeacherOp={autoId:a,pageId:e},0===parseInt(a)?this.openWhiteBoard(e):(this.deleteWhiteBoard(),this.pageType="page",this.paintbrushCtrl&&(this.paintbrushCtrl.autoId=a),new Promise((t=>{if(parseInt(this.currentAutoId)!==parseInt(a)){const i=this.currentAutoId,n=1===parseInt(s)||1===parseInt(r);this.currentAutoId=a,this.pptCtrl.init(a,n).then((a=>{!0===a?(this.pptCtrl.gotoPage(parseInt(e)),void 0!==i&&0!==parseInt(i)&&this.paintbrushCtrl.clearHistory(i),setTimeout(t,500)):t()}))}else t()})).then((()=>{this.isRedraw&&(this.paintbrushCtrl.clear(),this.paintbrushCtrl.redraw(e||0,this.pageType))})))}onSliceControl(t){const{pageId:e,ID:a,type:s,autoId:r,drawID:i}=t;switch(this.currentTeacherOp={autoId:r,pageId:e},s){case"switchpage":case"switchAnimation":this._switchpage(e,r);break;case"delete":this.paintbrushCtrl.delete({pageId:e,ID:a,pageType:this.pageType,drawID:i});break;case"alldelete":this.paintbrushCtrl.clear(),this.paintbrushCtrl.clearHistory(r,e);break;case"laserPaint":this.paintbrushCtrl&&this.paintbrushCtrl.showLaserPaint(t)}}onSliceId(t){const{pageId:e,autoId:a,sessionId:s,roomId:r,type:i,docType:n}=t;this.currentTeacherOp={autoId:a,pageId:e},this._getPlayHistory(s,r,n||i).then((t=>{0===t&&this.init(a,e,n||i)}))}onSliceStart(t){this.paintbrushCtrl.clearHistory();const e={roomId:t.roomId,sessionId:t.sessionId};this.onSliceId(Object.assign(e,t.data))}_switchpage(t,e){if(0===parseInt(e))return this.openWhiteBoard(t);this.pageType=0===parseInt(e)?"whiteBoard":"page",parseInt(e)===parseInt(this.currentAutoId)?parseInt(this.pptCtrl.currentPageNum)!==parseInt(t)&&(this.paintbrushCtrl&&(this.paintbrushCtrl.clear(),this.paintbrushCtrl.redraw(t,this.pageType)),this.pptCtrl.gotoPage(parseInt(t))):this.paintbrushCtrl&&this.paintbrushCtrl.clear()}_getPlayHistory(t,e,a){return t&&e?request(`${this.requestUrlPre}get-ppt-content`,{qs:{channelId:e,sessionId:t}}).then((t=>{if(!t)return console.error("请求接口/front/pptContent返回数据为空");const{statusCode:e}=t||{};if(200!==parseInt(e))return console.error("请求接口/front/pptContent失败");const{code:s,data:r}=t.data;if(200!==s)return console.error("请求接口/front/pptContent失败");const i=r;if(!i)return console.error("接口/front/pptContent返回的json数据格式有问题");if(!i.length)return 0;let n=i[0];for(let t=1,e=i.length;t<e;t++){const e=i[t];if(e.time>n.time){const t=SocketEvent.parseJson(e.content);if(!t)continue;this._isRightEvent(t.EVENT,t.data.type)&&(n=e)}}const o=SocketEvent.parseJson(n.content);if(!o)return console.error("json格式有问题");const h=o.data;let p;return this.init(h.autoId,h.pageId,a).then((()=>{i.forEach((t=>{const e=SocketEvent.parseJson(t.content);if(!e)return;const{EVENT:a,data:s,userId:r}=e,{autoId:i,pageId:n,ID:o,type:h,drawID:c}=s;if("onSliceDraw"===a)this.paintbrushCtrl.saveHistory(s,r);else if("onSliceControl"===a)switch(h){case"delete":this.paintbrushCtrl.clearHistory(i,n,o,c);break;case"alldelete":this.paintbrushCtrl.clearHistory(i,n)}else 0!==parseInt(i)&&p&&parseInt(i)!==parseInt(p)&&"onSliceOpen"===a&&this.paintbrushCtrl.clearHistory(p);0!==parseInt(i)&&(p=i)})),this.paintbrushCtrl.autoId=h.autoId,this.paintbrushCtrl.pageId=h.pageId,this.paintbrushCtrl.redraw(h.pageId,0===parseInt(h.autoId)?"whiteBoard":"page")}))})):Promise.resolve("sessionId和roomId不为空")}deleteWhiteBoard(){this.emit({EVENT:HIDE_BOARD,pageId:this.pptCtrl.currentPageNum})}openWhiteBoard(t=0){this.openWhiteBoard.maxTeacherOp=this.openWhiteBoard.maxTeacherOp||{autoId:0,pageId:t},t>this.openWhiteBoard.maxTeacherOp.pageId&&(this.openWhiteBoard.maxTeacherOp={autoId:0,pageId:t}),this.pageType="whiteBoard",this.isRedraw&&(this.paintbrushCtrl.clear(),this.paintbrushCtrl.redraw(t,this.pageType)),this.currentAutoId=0,this.currentWhiteNum=t,this.emit({EVENT:SHOW_BOARD,pageId:t})}subscribe(t){t instanceof Function&&this.subFn.push(t)}emit(t){this.subFn.forEach((e=>e(t)))}_isRightEvent(t,e){return"onSliceStart"===t||"onSliceOpen"===t||"onSliceID"===t||"onSliceDraw"===t||"onSliceControl"===t&&("switchpage"===e||"delete"===e||"alldelete"===e||"switchAnimation"===e)}setChatData(t){const e=this.isLive;if(this._setLiveStatus("Y"===t.status),!e&&this.isLive&&this.reloadData)return this.reload(this.reloadData);this.chatData=t;const a=t.recordFileSimpleModel&&t.recordFileSimpleModel.fileId,s=t.hasPlayback;!this.isLive&&(s&&this.videoId||!s&&a)&&this.vodStart({id:s?this.videoId:t.recordFileSimpleModel.fileId,channelId:t.channelId,type:s?"playback":"record"})}setVideoId(t){this.videoId=t,this.isLive||this.vodStart({id:t,channelId:this.chatData.channelId})}vodStart(t){const e="string"==typeof t?SocketEvent.parseJson(t):t;if("object"!=typeof e)return console.warn("vodStart的json数据格式有问题");const{channelId:a,id:s,type:r="playback"}=e;if(!a||!s)return console.warn("vodStart的json数据必须有roomId和id");request(`${this.requestUrlPre}get-ppt-history`,{qs:{channelId:a,id:s,type:r}}).then((t=>{this._VODStartResult(t)})).catch((e=>console.error(`vodStart,err:${JSON.stringify(e)},data:${t}`)))}_VODStartResult(t){const{statusCode:e}=t;if(200!==parseInt(e))return console.warn("请求画笔回看数据失败");const{code:a,data:s}=t.data;if(200!==parseInt(a))return console.warn("请求画笔回看数据失败");const r="string"==typeof s?SocketEvent.parseJson(s):s;return r?r.length?(this.paintData=r.filter((t=>{const e=SocketEvent.parseJson(t.content);return e?!("onSliceControl"===e.EVENT&&"closeCamera"===e.data.type):`获取历史数据时格式有问题:${t.content}`})).sort(((t,e)=>{const a=SocketEvent.parseJson(t.content),s=SocketEvent.parseJson(e.content);return(void 0===a.data.timeStamp?a.timeStamp:a.data.timeStamp)>(void 0===s.data.timeStamp?s.timeStamp:s.data.timeStamp)?1:-1})),this.sliceStartTime=[],this.sliceOpenObj={},this.drawDataKeyUsePage=this.paintData.reduce(((t,e,a)=>{const s=SocketEvent.parseJson(e.content);if(!s)return console.warn(`获取历史数据时格式有问题:${e.content}`);"onSliceStart"===s.EVENT&&(this.startAutoId=s&&s.data&&s.data.autoId,this.sliceStartTime.push(s.timeStamp)),"onSliceOpen"!==s.EVENT&&"onSliceStart"!==s.EVENT||(this.sliceOpenObj[`autoId${s.data.autoId}`]=e.type||e.docType),0===a&&this.init(s.data.autoId,s.data.pageId,s.data.docType||e.type||e.docType);const r=s.data,i=`${0===parseInt(r.autoId)?"white":"page"}${r.autoId}${r.pageId}`;return e.isExecuted=!1,t[i]=t[i]||[],t[i].push(e),t}),{}),this.paintbrushCtrl&&this.paintbrushCtrl.clearHistory(),void(this.vodPlayEx=this.vodPlay())):console.warn("请求的画笔数据为空"):console.warn("获取的画笔json数据格式有问题")}vodPlay(){clearTimeout(this.VODPlayTimeout);let t=!0;const e=this;this.isRedraw=!1,this._setLiveStatus(!1);const a=this.sliceStartTime?this.sliceStartTime.length:0;let s=null;return r(),{stop:function(){clearTimeout(e.VODPlayTimeout),t=!1},start(){t=!0,r()}};function r(){t&&!e.isLive&&(e.VODPlayTimeout=setTimeout((()=>{e.isLive||e.getCurrentTime(i)}),500))}function i(t){const i="string"==typeof t?SocketEvent.parseJson(t):t;if(!i)return console.log("获取当前播放时间的json字符串有问题");const n=i.time;e.currentPlayTime=n;const{startTime:o,endTime:h}=e._getMergeVODTime(n,a),p=e._findDataUseTime(n||0);if(!p)return r();const c=SocketEvent.parseJson(p.content),{pageId:d,autoId:l=e.startAutoId}=c.data,u=e._getIsToTakeExecuted(c,s);s=c;const I=e._gethistoryData(d,l),m=e.sliceOpenObj[`autoId${l}`];e.init(l,d,m).then((()=>{u||e&&e.paintbrushCtrl&&e.paintbrushCtrl.clear(),I.forEach((t=>{u||(t.isExecuted=!1);const a=SocketEvent.parseJson(t.content),s=a.data.timeStamp;void 0!==s&&s<=n&&"switchAnimation"!==a.data.type&&(o===h&&s>=o||s>=o&&s<=h)&&(!u||u&&!t.isExecuted)&&(e._drawForBackPlay(a),t.isExecuted=!0)})),r()}))}}_getMergeVODTime(t,e){let a=0,s=0;if(1===e)a=s=this.sliceStartTime[0];else for(let r=0;r<e;r++){const e=this.sliceStartTime[r],i=this.sliceStartTime[r+1]||e;if(e===i||t>=e&&t<=i){a=e,s=i;break}}return{startTime:a,endTime:s}}_findDataUseTime(t){return this.paintData?function e(a){const s=a.length,r=parseInt(s/2),i=SocketEvent.parseJson(a[r].content),n=void 0===i.timeStamp?i.data.timeStamp:i.timeStamp;if(r===s-1)return t>n?a[r]:1===s?null:e([a[0]]);return parseInt(t)===parseInt(n)?a[r]:e(t<n?a.slice(0,r):a.slice(r,a.length))}(this.paintData):null}_getIsToTakeExecuted(t,e){const a=e?void 0===e.data.timeStamp?e.timeStamp:e.data.timeStamp:0,s=t?void 0===t.data.timeStamp?t.timeStamp:t.data.timeStamp:0,r=e&&parseInt(e.data.autoId)===parseInt(t.data.autoId)&&parseInt(e.data.pageId)===parseInt(t.data.pageId)&&a<=s&&!this.happenResize;if(e&&!r){this._gethistoryData(e.data.pageId,e.data.autoId).forEach((t=>{t.isExecuted=!1}))}return this.happenResize&&(this.happenResize=!1),r}_gethistoryData(t,e){const a=0===parseInt(e)?"white":"page";return this.drawDataKeyUsePage[`${a}${e}${t}`]||[]}_drawForBackPlay(t={}){const e=this.getChatEvent();if(!e)return;const{EVENT:a}=t;a&&e[a]instanceof Function&&e[a].call(this,a,t)}_setLiveStatus(t){this.isLive=t,this.pptCtrl&&(this.pptCtrl.isShowBtn=!t)}toCurrentTeacherOp(){if(!this.currentTeacherOp)return;const{pageId:t,autoId:e}=this.currentTeacherOp;this._isCurrent({autoId:e,pageId:t})||(0===parseInt(e)?this.openWhiteBoard(t):this.pptCtrl.gotoPage(parseInt(t)))}_isCurrent({autoId:t,pageId:e}){const a=parseInt(e),s=parseInt(t);return 0===s&&"whiteBoard"===this.pageType&&a===parseInt(this.currentWhiteNum)||s===parseInt(this.currentAutoId)&&a===parseInt(this.pptCtrl.currentPageNum)}static parseJson(t){try{return JSON.parse(t)}catch(t){return!1}}get maxTeacherOp(){return 0===parseInt(this.currentAutoId)?this.openWhiteBoard.maxTeacherOp||{autoId:0,pageId:0}:this.pptCtrl.maxTeacherOp}}export default SocketEvent;
|