web.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import * as SocketHandle from '../../utils/socket-handle'
  2. import { getQueryString } from './../../utils/tools'
  3. import HouseApi from './../../apis/house'
  4. // pages/web/web.js
  5. const app = getApp()
  6. function randomString(e) {
  7. for (var t = "", i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", n = 0; n < e; n++)
  8. t += i.charAt(Math.floor(Math.random() * i.length));
  9. return t
  10. }
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. url: "",
  17. pullUrl: [],
  18. pushUrl: '',
  19. live_base_url: "rtmp://120.24.85.77:1935/hls",
  20. socketOpts: {},
  21. room_id: 'test1',
  22. trtcConfig: {
  23. scene: 'rtc',
  24. // sdkAppID: '1400433526', // 开通实时音视频服务创建应用后分配的 SDKAppID
  25. sdkAppID: '1400537003', // 开通实时音视频服务创建应用后分配的 SDKAppID
  26. userID: '', // 用户 ID,可以由您的帐号系统指定
  27. userSig: '', // 身份签名,相当于登录密码的作用
  28. template: 'custom', // 画面排版模式
  29. }
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: function (options) {
  35. let { vr_link } = options
  36. vr_link = decodeURIComponent(vr_link)
  37. this.scene = getQueryString(vr_link, 'm')
  38. let opts = {
  39. roomId: options.room_id || getQueryString(vr_link, 'room_id') || randomString(18),
  40. userId: getApp().globalData.userinfo.user_id,
  41. customerId: getApp().globalData.userinfo.user_id,
  42. // nickname: getApp().globalData.userinfo.name,
  43. nickname: getApp().globalData.userinfo.nickname,
  44. role: 'agent',
  45. scene_num: this.scene
  46. }
  47. this.setData({
  48. socketOpts: opts,
  49. url: `${vr_link}&room_id=${opts.roomId}`,
  50. room_id: opts.roomId,
  51. house_id: options.house_id
  52. })
  53. this.roomId = opts.roomId
  54. console.log(this.data.url)
  55. this.getHouseDetail()
  56. this.socket = SocketHandle.initSocket(this, opts)
  57. },
  58. onShow() {
  59. if (app.globalData.token && !getQueryString(this.data.url, 'user_id')) {
  60. this.setData({
  61. url: `${this.data.url}&role=agent&user_id=${app.globalData.userinfo.user_id}&house_id=${this.data.house_id}`
  62. })
  63. }
  64. },
  65. onUnload () {
  66. this.socket.disconnect()
  67. },
  68. countLiveUrl(user_id) {
  69. return `${this.data.live_base_url}/${this.data.room_id}_${user_id}`
  70. },
  71. startPlay(data) {
  72. if (!app.globalData.token) {
  73. wx.showToast({
  74. title: '未登录',
  75. })
  76. wx.navigateTo({
  77. url: '/pages/login/login',
  78. })
  79. return
  80. }
  81. // const {
  82. // roomId
  83. // } = data
  84. // console.log(this.countLiveUrl(getApp().globalData.userinfo.user_id), 'this.countLiveUrl(getApp().globalData.userinfo.user_id)')
  85. // this.setData({
  86. // pushUrl: this.countLiveUrl(getApp().globalData.userinfo.user_id)
  87. // })
  88. },
  89. createLivePull({persons, roomId}) {
  90. let arr = []
  91. console.log(persons, 'persons')
  92. persons.forEach(item => {
  93. console.log(item)
  94. arr.push(this.countLiveUrl(item.userId))
  95. })
  96. console.log(persons)
  97. // const pullUrl = this.countLiveUrl(roomId)
  98. this.setData({
  99. pullUrl: arr
  100. })
  101. },
  102. someoneLeave() {
  103. this.trtcRoomContext && this.trtcRoomContext.exitRoom()
  104. },
  105. getHouseDetail () {
  106. if (!this.data.house_id) {
  107. return
  108. }
  109. HouseApi.houseDetail(this.data.house_id).then(res => {
  110. let house = res.data
  111. house.detail_images = JSON.parse(house.detail_images)
  112. this.setData({
  113. house
  114. })
  115. })
  116. },
  117. onShareAppMessage: function (res) {
  118. var path =`/pages/web/web?house_id=${this.data.house_id}&vr_link=${this.data.house.vrLink}`;
  119. var arr = this.data.house.detail_images;
  120. var imageurl = arr[0];
  121. console.log(path)
  122. return {
  123. title: this.data.house.title,
  124. path: path,
  125. imageUrl: imageurl, // 分享的封面图
  126. success: () => {
  127. // 转发成功
  128. },
  129. fail: function(err) {
  130. console.log(err)
  131. app.ShowModel('网络错误', '转发失败~');
  132. // 转发失败
  133. }
  134. }
  135. },
  136. enterAudioRoom(user, noEnter) {
  137. this.hasEnter = true
  138. getApp().globalData.audioUser = user
  139. console.log(user, 'user')
  140. this.setData({
  141. trtcConfig: {
  142. scene: 'rtc',
  143. // sdkAppID: '1400433526', // 开通实时音视频服务创建应用后分配的 SDKAppID
  144. sdkAppID: '1400537003', // 开通实时音视频服务创建应用后分配的 SDKAppID
  145. userID: user.userId, // 用户 ID,可以由您的帐号系统指定
  146. userSig: user.sig, // 身份签名,相当于登录密码的作用
  147. template: 'custom', // 画面排版模式
  148. debugMode:true,
  149. }
  150. }, () => {
  151. this.trtcRoomContext = this.selectComponent('#trtcroom')
  152. let EVENT =this.trtcRoomContext.EVENT
  153. if (this.trtcRoomContext) {
  154. // this.publishLocalAudio()
  155. this.trtcRoomContext.on(EVENT.LOCAL_JOIN, (event) => {
  156. this.publishLocalAudio()
  157. // wx.showToast({
  158. // title: '进房',
  159. // })
  160. })
  161. // 监听远端用户的音频流的变更事件
  162. this.trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, (event) => {
  163. // 订阅(即播放)远端用户的音频流
  164. let userID = event.data.userID
  165. // wx.showToast({
  166. // title: '订阅',
  167. // })
  168. this.trtcRoomContext.subscribeRemoteAudio({
  169. userID: userID
  170. })
  171. })
  172. // this.trtcRoomContext.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event => {
  173. // console.log(event)
  174. // })
  175. this.trtcRoomContext.enterRoom({
  176. roomID: this.roomId
  177. }).then(() => {
  178. console.log('成功进入房间')
  179. console.log(this.roomId)
  180. }).catch((res) => {
  181. console.error('room joinRoom 进房失败:', res)
  182. })
  183. }
  184. })
  185. },
  186. unpublishLocalAudio () {
  187. this.trtcRoomContext && this.trtcRoomContext.unpublishLocalAudio()
  188. },
  189. publishLocalAudio () {
  190. this.trtcRoomContext && this.trtcRoomContext.publishLocalAudio()
  191. // wx.showToast({
  192. // title: '推流',
  193. // })
  194. }
  195. })