import { GetCheckoutRoomNum, PostAnonLogin, GetAgoraConf, GetRoomRtcToken, GetOpenId } from '../../api/form/index' import { Message } from 'tdesign-miniprogram'; import { startSignalr, onStop } from '../../utils/singlr'; import { agora } from '../../utils/agora' Page({ data: { meetingForm: { roomNum: '', roomName: '', }, isRecord: false, isCamera: false, netWorkErrorDialog: false, historicalList: [], isJoin: false }, onShow() { const that = this; onStop() this.setData({ isJoin: false }) wx.getStorage({ key: 'isConnected', success(res: any) { if (res.data) { that.setData({ netWorkErrorDialog: true, }) } }, }) wx.getStorage({ key: 'historicalList', success(res: any) { that.setData({ historicalList: res.data }) if (that.data.meetingForm.roomNum) { that.setData({ meetingForm: { roomNum: that.data.meetingForm.roomNum, roomName: res.data[res.data.length - 1].nickName }, }) } else { that.setData({ meetingForm: { roomNum: res.data[res.data.length - 1].roomNum, roomName: res.data[res.data.length - 1].nickName }, }) } }, }) }, onLoad(option) { if (option.scene) { const scene = decodeURIComponent(option.scene) const room = scene.split('r=')[1]; this.setData({ meetingForm: { roomNum: room, }, }, () => { this.joinMeeting() }) } wx.authorize({ scope: 'scope.record', success: () => { this.setData({ isRecord: true, }) }, fail: () => { this.setData({ isRecord: false, }) } }); wx.authorize({ scope: 'scope.camera', success: () => { this.setData({ isCamera: true, }) }, fail: () => { this.setData({ isCamera: false, }) } }); }, async getRoomRtcToken(roomNum: string, callBack: Function): Promise { Promise.all([GetRoomRtcToken(roomNum), GetAgoraConf()]).then(res => { if (res[0].code === 200 && res[1].code === 200) { callBack({ token: res[0].data, appid: res[1].data }) } }) }, closeDialog() { wx.setStorage({ key: "isConnected", data: false }) this.setData({ netWorkErrorDialog: false }) }, joinMeeting() { this.closeDialog() if (this.data.isJoin) { return } this.setData({ isJoin: true }) const navigateToMeeting = async (deviceId: string): Promise => { await PostAnonLogin({ deviceId, nickName: this.data.meetingForm.roomName, roomNum: this.data.meetingForm.roomNum, }).then(res => { if (res.code === 200) { wx.setStorage({ key: "user", data: res.data }) setTimeout(() => { startSignalr((str) => { if (str) { this.setData({ isJoin: false }) Message.error({ context: this, offset: [90, 32], duration: 3000, content: str, }); } else { this.getRoomRtcToken(this.data.meetingForm.roomNum, (option) => { agora.setOption({ token: option.token, channelId: this.data.meetingForm.roomNum, appId: option.appid, uid: res.data.uid, screenShareId: res.data.screenShareId, }) wx.navigateTo({ url: `/pages/meeting/index?roomNum=${this.data.meetingForm.roomNum}&nickName=${this.data.meetingForm.roomName}` }) }) } }) }, 1000); } else { this.setData({ isJoin: false }) } }).catch(() => { this.setData({ isJoin: false }) }) } if (!this.data.meetingForm.roomNum) { Message.error({ context: this, offset: [90, 32], duration: 3000, content: '请输入会议号!', }); this.setData({ isJoin: false }) return } if (!this.data.meetingForm.roomName) { Message.error({ context: this, offset: [90, 32], duration: 3000, content: '请输入入会名称!', }); this.setData({ isJoin: false }) return } const that = this; GetCheckoutRoomNum(this.data.meetingForm.roomNum).then(res => { if (res.code === 200) { if (res.data) { wx.login({ success: (res) => { const { code } = res GetOpenId(code).then(res => { if (res.code === 200) { navigateToMeeting(res.data) } else { that.setData({ isJoin: false }) } }).catch(() => { that.setData({ isJoin: false }) }) }, fail: () => { that.setData({ isJoin: false }) } }) } else { this.setData({ isJoin: false }) Message.error({ context: this, offset: [90, 32], duration: 3000, content: '房间号不存在!', }); } } }).catch(() => { this.setData({ isJoin: false }) }) }, setting() { wx.openSetting({ success: (res) => { this.setData({ isCamera: res.authSetting['scope.camera'], isRecord: res.authSetting['scope.record'], }) } }) }, changeMeetingForm(e) { switch (e.target.dataset.type) { case 'roomNum': this.setData({ meetingForm: { roomNum: e.type === 'clear' ? '' : e.detail.value, roomName: this.data.meetingForm.roomName, } }) break; case 'roomName': this.setData({ meetingForm: { roomNum: this.data.meetingForm.roomNum, roomName: e.type === 'clear' ? '' : e.detail.value, } }) break; } }, setForm(e) { const { item } = e.currentTarget.dataset; this.setData({ meetingForm: { roomNum: item.roomNum, roomName: item.nickName }, }) this.joinMeeting() } })