room.js 5.0 KB

1
  1. import api from"../../common/api/index";import md5 from"../../common/utils/md5";const TAG_NAME="ROOM";Component({properties:{tokenParams:{type:Object,value:{}},rtcMode:{type:Boolean,value:!1},teacherNameplateInfo:{type:Object,value:{}},viewerNameplateInfo:{type:Object,value:{}},rtcDirection:{type:String,value:"horizontal"}},data:{rtcConfig:{sdkAppID:"",userID:"",userSig:"",template:""},showTipToast:!1,subscribeList:{}},methods:{getMicAuthSign(e){const t=Object.keys(e).sort().reduce(((t,o)=>{const s=e[o];return s||0===s?t+o+String(s):t}),"");return md5(`polyv_trtc_api_inner${t}polyv_trtc_api_inner`).toLocaleUpperCase()},async getChannelKey(){const e=this.data.tokenParams,t=this.getMicAuthSign(e),o={...e,sign:t};return(await api.getTRTCChannelKey(o)).data.data},async enterRoom(e){this.options=e,e.template=e.template||"1v1",e.roomID=e.roomID||this.randomRoomID(),e.userID=e.userID||this.randomUserID();const t=await this.getChannelKey();e.sdkAppID=Number(t.sdkAppId),e.userSig=t.userSign,e.userID=t.userId,this.template=e.template,"grid"===e.template?this.data.rtcConfig={sdkAppID:e.sdkAppID,userID:e.userID,userSig:e.userSig,template:e.template,debugMode:e.debugMode,frontCamera:e.frontCamera,enableEarMonitor:e.enableEarMonitor,enableAutoFocus:e.enableAutoFocus,localMirror:e.localMirror,enableAgc:e.enableAgc,enableAns:e.enableAns,videoWidth:e.videoWidth,videoHeight:e.videoHeight,maxBitrate:e.maxBitrate,minBitrate:e.minBitrate,beautyLevel:0,enableIM:!1,audioVolumeType:e.audioVolumeType,audioQuality:e.audioQuality,scene:e.scene,encsmall:e.encsmall?1:0,cloudenv:e.cloudenv,enableBlackStream:e.enableBlackStream,streamID:e.streamID,userDefineRecordID:e.userDefineRecordID,privateMapKey:e.privateMapKey,pureAudioMode:e.pureAudioMode,recvMode:e.recvMode}:this.data.rtcConfig={sdkAppID:e.sdkAppID,userID:e.userID,userSig:e.userSig,template:e.template,debugMode:e.debugMode,beautyLevel:0,enableIM:!1,audioVolumeType:e.audioVolumeType},this.setData({rtcConfig:this.data.rtcConfig},(()=>{this.trtcComponent.enterRoom({roomID:e.roomID}).then((()=>{"custom"===this.template&&this.trtcComponent.setViewRect({userID:e.userID,xAxis:"480rpx",yAxis:"160rpx",width:"240rpx",height:"320rpx"})})).catch((e=>{console.error("* room joinRoom 进房失败:",e)}))}))},bindTRTCRoomEvent:function(){const e=this.trtcComponent.EVENT;this.timestamp=[],this.trtcComponent.on(e.LOCAL_JOIN,(e=>{!0===this.options.localVideo&&this.trtcComponent.publishLocalVideo(),!0===this.options.localAudio&&this.trtcComponent.publishLocalAudio(),"custom"===this.options.template&&this.trtcComponent.setViewRect({userID:e.userID,xAxis:"0rpx",yAxis:"0rpx",width:"240rpx",height:"320rpx"}),this.triggerEvent("publishStreamSuccess")})),this.trtcComponent.on(e.LOCAL_LEAVE,(e=>{this.triggerEvent("disconnectRoom")})),this.trtcComponent.on(e.ERROR,(e=>{})),this.trtcComponent.on(e.REMOTE_USER_JOIN,(e=>{if(this.timestamp.push(new Date),"1v1"===this.template&&this.timestamp.length>1){this.timestamp[1]-this.timestamp[0]<1e3&&this.setData({showTipToast:!0},(()=>{setTimeout((()=>{this.setData({showTipToast:!1}),wx.navigateBack({delta:1})}),4e3)}))}})),this.trtcComponent.on(e.REMOTE_USER_LEAVE,(e=>{"1v1"===this.template&&(this.timestamp=[]),"1v1"===this.template&&this.remoteUser===e.data.userID&&(this.remoteUser=null)})),this.trtcComponent.on(e.REMOTE_VIDEO_ADD,(e=>{const t=this.trtcComponent.getRemoteUserList(),o=e.data;if("1v1"!==this.template||this.remoteUser&&this.remoteUser!==o.userID?(this.trtcComponent.subscribeRemoteVideo({userID:o.userID,streamType:o.streamType}),this.data.subscribeList[o.userID+"-video"]=!0):(this.remoteUser=o.userID,this.trtcComponent.subscribeRemoteVideo({userID:o.userID,streamType:o.streamType})),"custom"===this.template&&o.userID&&o.streamType){let e=t.findIndex((e=>e.userID===o.userID));e++;const s=320*e+160;this.trtcComponent.setViewRect({userID:o.userID,streamType:o.streamType,xAxis:"480rpx",yAxis:s+"rpx",width:"240rpx",height:"320rpx"})}})),this.trtcComponent.on(e.REMOTE_VIDEO_REMOVE,(e=>{})),this.trtcComponent.on(e.REMOTE_AUDIO_ADD,(e=>{const t=e.data;"1v1"!==this.template||this.remoteUser&&this.remoteUser!==t.userID?(this.trtcComponent.subscribeRemoteAudio({userID:t.userID}),this.data.subscribeList[t.userID+"-audio"]=!0):(this.remoteUser=t.userID,this.trtcComponent.subscribeRemoteAudio({userID:t.userID}))})),this.trtcComponent.on(e.REMOTE_AUDIO_REMOVE,(e=>{})),this.trtcComponent.on(e.IM_READY,(e=>{})),this.trtcComponent.on(e.IM_MESSAGE_RECEIVED,(e=>{}))},randomUserID:function(){return(new Date).getTime().toString(16).split("").reverse().join("")},randomRoomID:function(){return parseInt(9999*Math.random())},hangUp(){this.trtcComponent.exitRoom()},enableVideo(){this.trtcComponent.enableVideo()},disableVideo(){this.trtcComponent.disableVideo()},enableAudio(){this.trtcComponent.enableAudio()},disableAudio(){this.trtcComponent.disableAudio()}},lifetimes:{created(){},attached(){wx.setKeepScreenOn({keepScreenOn:!0})},ready(){wx.setKeepScreenOn({keepScreenOn:!0}),this.trtcComponent=this.selectComponent("#trtc-component"),this.bindTRTCRoomEvent()},detached(){wx.setKeepScreenOn({keepScreenOn:!1})}}});