| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import {
- fetchRoom,
- leaveRoom,
- socketServer,
- shopServer,
- getUserInfo,
- authorizeRecord,
- voiceFactory,
- getRTCSig,
- enterRoom
- } from '../shared/index'
- import { io } from '../lib/socket.io-v4-no-msgpack.js';
- const btoa = require('../../../utils/btoa')
- // pages/sync/room/room.js
- Page({
- data: {
- room: null,
- shareMode: false,
- userInfo: null,
- authRecord: false,
- role: 'customer',
- // role: 'leader',
- webUrl: null,
- voice: null,
- socket: null
- },
- onLoad: async function (options) {
- console.log('room options', options)
- try {
- const room = await fetchRoom(options.id)
- if (options.role) {
- this.setData({ role: options.role })
- }
- this.setData({ room })
- this.setWebUrl()
- } catch (e) {
- wx.showModal({
- title: '提示',
- content: e.message,
- showCancel: false,
- confirmText: '我知道了',
- confirmColor: '#ED5D18',
- success (res) {
- wx.reLaunch({
- url: '/pages/sync/sync',
- })
- }
- })
- }
- },
- onShow() {
- if (wx.getStorageSync('userInfo')) {
- this.bindUserInfo()
- } else {
- getApp().setLoginProps(false)
- }
- if (this.data.role === 'leader' && this.data.room) {
- enterRoom(this.data.room.id)
- }
- },
- onHide() {
- if (this.data.role === 'leader' && !this.froce) {
- leaveRoom(this.data.room.id)
- }
- },
- async bindUserInfo() {
- const userInfo = await getUserInfo()
- this.setData({ userInfo: userInfo || wx.getStorageSync('userInfo') })
- this.setWebUrl()
- console.log(userInfo)
- },
- setWebUrl() {
- if (this.data.room && this.data.userInfo) {
- let params = {
- m: this.data.room.scenes[0].num,
- roomId: `roomId_${this.data.room.id}`,
- }
- if (this.data.room.status === 1 || this.data.role === 'leader') {
- params = {
- ...params,
- sync: 1,
- vruserId: `user_${this.data.userInfo.userId}`,
-
- role: this.data.role,
- avatar: this.data.userInfo.avatar,
- name: this.data.userInfo.nickName || this.data.userInfo.nickName,
- isMiniApp: 1
- }
- this.joinSocket(params)
- } else {
- this.setData({
- webUrl: `${shopServer}?${Object.keys(params).map(key => `${key}=${params[key]}`).join('&')}`
- })
- }
- }
- },
- // 操作音频
- async openAudio(isOpen = this.isOpenAudio && this.data.role === 'leader') {
- if (this.membersCount > 1) {
- if (!this.voice) {
- const rtcData = await getRTCSig(this.data.userInfo.userId)
- console.log('创建', rtcData)
- this.voice = voiceFactory({
- roomId: this.data.room.id,
- user: this.data.userInfo,
- sig: rtcData.sign,
- sdkAppId: rtcData.sdkAppId
- })
- }
- console.log('人数', this.membersCount, isOpen)
- if (isOpen) {
- const auth = await authorizeRecord()
- isOpen = auth
- }
- if (!this.voice.ring) {
- this.voice.start()
- }
- this.voice.changeMute(!isOpen)
- this.socket.emit('signal', { type: 'openAudioCallback', payload: isOpen })
- } else if (this.voice) {
- console.log('人数不够,停止')
- this.voice.stop()
- }
- this.isOpenAudio = isOpen
- },
- // 关联socket
- joinSocket(params) {
- const updateMembersCount = (data) => {
- this.membersCount = data.members.length
- this.openAudio()
- }
- this.socket = io(socketServer, {
- path: "/ws-sync",
- transport: ["websocket"],
- parser: false
- });
- this.socket.on('connect', async () => {
- this.socket.emit('join', {
- userId: params.vruserId,
- roomId: params.roomId,
- role: params.role,
- isClient:true
- })
- });
- this.socket.on('join', (data) => {
- debugger
- updateMembersCount(data)
- console.error(data.members, params)
- if (params.role === 'leader' && data.members.some(user => user.Role === 'leader' && user.UserId !== params.vruserId)) {
- this.froce = true
- wx.showModal({
- title: '提示',
- content: '该房间已存在主持人!',
- showCancel: false,
- confirmText: "确定",
- confirmColor: "#52a2d8",
- success: res => {
- console.log('?????')
- wx.reLaunch({
- url: '/pages/sync/sync',
- })
- // wx.navigateTo({
- // url: '/pages/sync/sync',
- // })
- },
- fail: res => {
- wx.navigateTo({
- url: '/pages/sync/sync',
- })
- }
- })
- } else {
- this.setData({
- webUrl: `${shopServer}?${Object.keys(params).map(key => `${key}=${params[key]}`).join('&')}`
- })
- }
- })
- this.socket.on('action', data => {
- if (['user-leave', 'user-join'].includes(data.type)) {
- updateMembersCount(data)
- }
- })
- this.socket.on('signal', async (data) => {
- if (data.type in this) {
- this[data.type](data.payload)
- }
- })
- },
- onUnload() {
- this.voice && this.voice.stop()
- this.socket && this.socket.close()
- this.setData({ shareMode: false })
- console.error(this.data)
- if (this.data.role === 'leader' && !this.froce) {
- console.log('离开啦')
- leaveRoom(this.data.room.id)
- }
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function (res) {
- const id = this.data.room.id
- const newPicUrl = this.data.room.cover || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
- const base = {
- imageUrl: newPicUrl,
- path: `/pages/sync/room/room?id=${id}&role=customer`
- }
- console.error('share', base)
- if (res.from === 'button') {
- this.cancelShareMode()
- return {
- ...base,
- title: '【好友邀请】一起来逛店吧!',
- }
- } else {
- return {
- ...base,
- title: '【好友邀请】一起来逛店吧!',
- }
- }
- },
- enterShareMode() {
- this.setData({ shareMode: true })
- },
- cancelShareMode() {
- this.setData({ shareMode: false })
- },
-
- })
|