gemercheung 3 years ago
parent
commit
2b5a837528
2 changed files with 84 additions and 27 deletions
  1. 4 3
      pages/webview/index.js
  2. 80 24
      socket.js

+ 4 - 3
pages/webview/index.js

@@ -47,7 +47,7 @@ Page({
     console.log(this.options)
     console.log(this.options)
     getApp().checkNetStatu();
     getApp().checkNetStatu();
 
 
-    
+
     // setTimeout(() => this.login(), 3000)
     // setTimeout(() => this.login(), 3000)
     this.setData({
     this.setData({
       type: options.type || this.options.type,
       type: options.type || this.options.type,
@@ -126,11 +126,12 @@ Page({
 
 
     this.getBrand(options.id, m)
     this.getBrand(options.id, m)
     this.mcode = m
     this.mcode = m
-
+    //TODO 有问题, 其他模式的房间号如何处理?
     let socketOptions = await this.socketStart({
     let socketOptions = await this.socketStart({
       sceneId: this.mcode,
       sceneId: this.mcode,
-      roomId: options.roomId
+      roomId: options.roomId || '888888'
     })
     })
+
     let url = this.getUrl(base, socketOptions, this.data.join) + this.urlPj
     let url = this.getUrl(base, socketOptions, this.data.join) + this.urlPj
 
 
     this.base = base
     this.base = base

+ 80 - 24
socket.js

@@ -18,6 +18,21 @@ wx.getSystemInfo({
   }
   }
 })
 })
 
 
+const debounce = (fn, wait) => {
+  let callback = fn;
+  let timerId = null;
+
+  function debounced() {
+    let context = this;
+    let args = arguments;
+    clearTimeout(timerId);
+    timerId = setTimeout(function () {
+      callback.apply(context, args);
+    }, wait);
+  }
+  return debounced;
+}
+
 
 
 let urlToJson = (url = window.location.href) => { // 箭头函数默认传值为当前页面url
 let urlToJson = (url = window.location.href) => { // 箭头函数默认传值为当前页面url
 
 
@@ -263,6 +278,7 @@ export default {
         businessId: roomId
         businessId: roomId
       }, 'POST', 'application/json')
       }, 'POST', 'application/json')
 
 
+
       if (result.code !== 200) {
       if (result.code !== 200) {
         wx.showModal({
         wx.showModal({
           content: result.error,
           content: result.error,
@@ -277,7 +293,13 @@ export default {
     }
     }
 
 
 
 
-    const capacities = !!result ? result.capacities : 50 // 房间限制人数
+    const capacities = !!result ? result.message.capacities : 50 // 房间限制人数
+
+    const {
+      isAnchor,
+      assistant,
+    } = !!result ? result.message : {}
+
     let userInfo = await this.getUserInfo()
     let userInfo = await this.getUserInfo()
 
 
     // console.log('---', userInfo)
     // console.log('---', userInfo)
@@ -292,7 +314,7 @@ export default {
     // this.role !== 'leader'
     // this.role !== 'leader'
     let roomType
     let roomType
     if ((!this.data.canShow && !this.data.join) || (this.data.join && !this.options.join)) {
     if ((!this.data.canShow && !this.data.join) || (this.data.join && !this.options.join)) {
-      roomType = '1v1'
+      // roomType = '1v1'
       if (this.options.roomId) {
       if (this.options.roomId) {
         this.role = 'leader'
         this.role = 'leader'
       }
       }
@@ -303,8 +325,19 @@ export default {
     //   title: this.data.peopleCount.toString(),
     //   title: this.data.peopleCount.toString(),
     // })
     // })
 
 
+    if (Number(isAnchor) === 1) {
+      this.role = "leader"
+    } else {
+      this.role = 'customer'
+    }
+
+    if (assistant && assistant.userId && assistant.userId == userInfo.userId) {
+      this.role = 'assistant'
+    }
+    console.log('进入房间角色,', this.role);
+
     return {
     return {
-      role: this.role || 'leader',
+      role: this.role,
       userId: userInfo.userId,
       userId: userInfo.userId,
       roomType,
       roomType,
       avatar: userInfo.avatarUrl,
       avatar: userInfo.avatarUrl,
@@ -342,7 +375,7 @@ export default {
         from: 2
         from: 2
       }
       }
     })
     })
- 
+
     console.error('新建socket Room', options.roomId)
     console.error('新建socket Room', options.roomId)
     this.setData({
     this.setData({
       socketStatus: 0
       socketStatus: 0
@@ -430,7 +463,10 @@ export default {
       console.log('on stopCall')
       console.log('on stopCall')
       this.stopCall(data)
       this.stopCall(data)
     })
     })
-    socket.on('someOneInRoom', this.startCall.bind(this))
+
+    this.handleSomeOneInRoom = this.handleSomeOneInRoom.bind(this)
+    // socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
+    socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
     socket.on('someOneLeaveRoom', (user, data) => {
     socket.on('someOneLeaveRoom', (user, data) => {
       this.handleSomeOneLeave(user)
       this.handleSomeOneLeave(user)
     })
     })
@@ -470,7 +506,7 @@ export default {
       }
       }
     });
     });
 
 
- 
+
     this.socketStop = () => {
     this.socketStop = () => {
       if (socket) {
       if (socket) {
         socket.close()
         socket.close()
@@ -539,8 +575,6 @@ export default {
 
 
   getUrl(url, socketOptions, isJoin) {
   getUrl(url, socketOptions, isJoin) {
     url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
     url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
-
-
     if (isJoin) {
     if (isJoin) {
       url += '&role=' + this.role + '&shopping'
       url += '&role=' + this.role + '&shopping'
     } else {
     } else {
@@ -574,8 +608,15 @@ export default {
       },
       },
     })
     })
   },
   },
+  async handleSomeOneInRoom(data) {
+    if (data && data.user) {
+      console.log('handleSomeOneInRoom', data)
+      this.startCall(data)
+    }
+  },
 
 
   async startCall(data) {
   async startCall(data) {
+    //TODO 触发三次
     console.log('startCall-data', data)
     console.log('startCall-data', data)
     // if( this.role =='leader'){
     // if( this.role =='leader'){
     this.setData({
     this.setData({
@@ -586,29 +627,44 @@ export default {
     this.setData({
     this.setData({
       surplus: this.data.peopleCount - data.roomsPerson.length
       surplus: this.data.peopleCount - data.roomsPerson.length
     })
     })
-    //限制只有主持人才可以开麦
-    if (this.role == 'leader') {
-      if (!this.isAuthorizeRecord) {
-        const voiceStatus = Number(await this.authorizeRecord())
-        this.isAuthorizeRecord = true
-
-        // getApp().setVoiceProps({
-        //   noMute: !voiceStatus
-        // })
-
-        // console.log(getApp().globalData.voiceProps.noMute)
-        // this.socketSendMessage('changeVoiceStatus', {
-        //   status: getApp().globalData.voiceProps.noMute ? 0 : 2
-        // })
-        // this.data.socketOptions.voiceStatus = 1
-        // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
+
+    //undefined是未授权,状态为3
+    let voiceStatus
+    if (!this.isAuthorizeRecord) {
+      const unAuth = await this.authorizeRecord();
+      if (typeof unAuth === 'undefined') {
+        // debugger
+        voiceStatus = 3
+      } else {
+        voiceStatus = Number(unAuth)
       }
       }
+
     }
     }
 
 
+    //限制只有主持人才可以开麦
+    // if (this.role == 'leader') {
+    //   if (!this.isAuthorizeRecord) {
+    //     const voiceStatus = Number(await this.authorizeRecord())
+    //     this.isAuthorizeRecord = true
+
+    //     // getApp().setVoiceProps({
+    //     //   noMute: !voiceStatus
+    //     // })
+
+    //     // console.log(getApp().globalData.voiceProps.noMute)
+    //     // this.socketSendMessage('changeVoiceStatus', {
+    //     //   status: getApp().globalData.voiceProps.noMute ? 0 : 2
+    //     // })
+    //     // this.data.socketOptions.voiceStatus = 1
+    //     // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
+    //   }
+    // }
+
 
 
     const socketOptions = this.data.socketOptions
     const socketOptions = this.data.socketOptions
     getApp().globalData.roomId = socketOptions.roomId
     getApp().globalData.roomId = socketOptions.roomId
     const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
     const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
+
     if (!user) {
     if (!user) {
       return
       return
     }
     }