room.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {
  2. fetchRoom,
  3. leaveRoom,
  4. socketServer,
  5. shopServer,
  6. getUserInfo,
  7. authorizeRecord,
  8. voiceFactory,
  9. getRTCSig,
  10. enterRoom
  11. } from '../shared/index'
  12. import { io } from '../lib/socket.io-v4-no-msgpack.js';
  13. const btoa = require('../../../utils/btoa')
  14. // pages/sync/room/room.js
  15. Page({
  16. data: {
  17. room: null,
  18. shareMode: false,
  19. userInfo: null,
  20. authRecord: false,
  21. role: 'customer',
  22. // role: 'leader',
  23. webUrl: null,
  24. voice: null,
  25. socket: null
  26. },
  27. onLoad: async function (options) {
  28. console.log('room options', options)
  29. try {
  30. const room = await fetchRoom(options.id)
  31. if (options.role) {
  32. this.setData({ role: options.role })
  33. }
  34. this.setData({ room })
  35. this.setWebUrl()
  36. } catch (e) {
  37. wx.showModal({
  38. title: '提示',
  39. content: e.message,
  40. showCancel: false,
  41. confirmText: '我知道了',
  42. confirmColor: '#ED5D18',
  43. success (res) {
  44. wx.reLaunch({
  45. url: '/pages/sync/sync',
  46. })
  47. }
  48. })
  49. }
  50. },
  51. onShow() {
  52. if (wx.getStorageSync('userInfo')) {
  53. this.bindUserInfo()
  54. } else {
  55. getApp().setLoginProps(false)
  56. }
  57. if (this.data.role === 'leader' && this.data.room) {
  58. enterRoom(this.data.room.id)
  59. }
  60. },
  61. onHide() {
  62. if (this.data.role === 'leader' && !this.froce) {
  63. leaveRoom(this.data.room.id)
  64. }
  65. },
  66. async bindUserInfo() {
  67. const userInfo = await getUserInfo()
  68. this.setData({ userInfo: userInfo || wx.getStorageSync('userInfo') })
  69. this.setWebUrl()
  70. console.log(userInfo)
  71. },
  72. setWebUrl() {
  73. if (this.data.room && this.data.userInfo) {
  74. let params = {
  75. m: this.data.room.scenes[0].num,
  76. roomId: `roomId_${this.data.room.id}`,
  77. }
  78. if (this.data.room.status === 1 || this.data.role === 'leader') {
  79. params = {
  80. ...params,
  81. sync: 1,
  82. vruserId: `user_${this.data.userInfo.userId}`,
  83. role: this.data.role,
  84. avatar: this.data.userInfo.avatar,
  85. name: this.data.userInfo.nickName || this.data.userInfo.nickName,
  86. isMiniApp: 1
  87. }
  88. this.joinSocket(params)
  89. } else {
  90. this.setData({
  91. webUrl: `${shopServer}?${Object.keys(params).map(key => `${key}=${params[key]}`).join('&')}`
  92. })
  93. }
  94. }
  95. },
  96. // 操作音频
  97. async openAudio(isOpen = this.isOpenAudio && this.data.role === 'leader') {
  98. if (this.membersCount > 1) {
  99. if (!this.voice) {
  100. const rtcData = await getRTCSig(this.data.userInfo.userId)
  101. console.log('创建', rtcData)
  102. this.voice = voiceFactory({
  103. roomId: this.data.room.id,
  104. user: this.data.userInfo,
  105. sig: rtcData.sign,
  106. sdkAppId: rtcData.sdkAppId
  107. })
  108. }
  109. console.log('人数', this.membersCount, isOpen)
  110. if (isOpen) {
  111. const auth = await authorizeRecord()
  112. isOpen = auth
  113. }
  114. if (!this.voice.ring) {
  115. this.voice.start()
  116. }
  117. this.voice.changeMute(!isOpen)
  118. this.socket.emit('signal', { type: 'openAudioCallback', payload: isOpen })
  119. } else if (this.voice) {
  120. console.log('人数不够,停止')
  121. this.voice.stop()
  122. }
  123. this.isOpenAudio = isOpen
  124. },
  125. // 关联socket
  126. joinSocket(params) {
  127. const updateMembersCount = (data) => {
  128. this.membersCount = data.members.length
  129. this.openAudio()
  130. }
  131. this.socket = io(socketServer, {
  132. path: "/ws-sync",
  133. transport: ["websocket"],
  134. parser: false
  135. });
  136. this.socket.on('connect', async () => {
  137. this.socket.emit('join', {
  138. userId: params.vruserId,
  139. roomId: params.roomId,
  140. role: params.role,
  141. isClient:true
  142. })
  143. });
  144. this.socket.on('join', (data) => {
  145. debugger
  146. updateMembersCount(data)
  147. console.error(data.members, params)
  148. if (params.role === 'leader' && data.members.some(user => user.Role === 'leader' && user.UserId !== params.vruserId)) {
  149. this.froce = true
  150. wx.showModal({
  151. title: '提示',
  152. content: '该房间已存在主持人!',
  153. showCancel: false,
  154. confirmText: "确定",
  155. confirmColor: "#52a2d8",
  156. success: res => {
  157. console.log('?????')
  158. wx.reLaunch({
  159. url: '/pages/sync/sync',
  160. })
  161. // wx.navigateTo({
  162. // url: '/pages/sync/sync',
  163. // })
  164. },
  165. fail: res => {
  166. wx.navigateTo({
  167. url: '/pages/sync/sync',
  168. })
  169. }
  170. })
  171. } else {
  172. this.setData({
  173. webUrl: `${shopServer}?${Object.keys(params).map(key => `${key}=${params[key]}`).join('&')}`
  174. })
  175. }
  176. })
  177. this.socket.on('action', data => {
  178. if (['user-leave', 'user-join'].includes(data.type)) {
  179. updateMembersCount(data)
  180. }
  181. })
  182. this.socket.on('signal', async (data) => {
  183. if (data.type in this) {
  184. this[data.type](data.payload)
  185. }
  186. })
  187. },
  188. onUnload() {
  189. this.voice && this.voice.stop()
  190. this.socket && this.socket.close()
  191. this.setData({ shareMode: false })
  192. console.error(this.data)
  193. if (this.data.role === 'leader' && !this.froce) {
  194. console.log('离开啦')
  195. leaveRoom(this.data.room.id)
  196. }
  197. },
  198. /**
  199. * 用户点击右上角分享
  200. */
  201. onShareAppMessage: function (res) {
  202. const id = this.data.room.id
  203. const newPicUrl = this.data.room.cover || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  204. const base = {
  205. imageUrl: newPicUrl,
  206. path: `/pages/sync/room/room?id=${id}&role=customer`
  207. }
  208. console.error('share', base)
  209. if (res.from === 'button') {
  210. this.cancelShareMode()
  211. return {
  212. ...base,
  213. title: '【好友邀请】一起来逛店吧!',
  214. }
  215. } else {
  216. return {
  217. ...base,
  218. title: '【好友邀请】一起来逛店吧!',
  219. }
  220. }
  221. },
  222. enterShareMode() {
  223. this.setData({ shareMode: true })
  224. },
  225. cancelShareMode() {
  226. this.setData({ shareMode: false })
  227. },
  228. })