|
@@ -1,18 +1,25 @@
|
|
|
// pages/room/room.ts
|
|
|
+
|
|
|
import { fetchRoom } from '../../api/fetchRoom'
|
|
|
import { getRTCSig } from '../../api/sign'
|
|
|
import { server } from '../../config'
|
|
|
+import { authorizeRecord } from '../../utils/util'
|
|
|
+import { audioManger, AudioManger } from './libs/audioManager'
|
|
|
+const { io } = require('../../utils/socket.io-v4-no-msgpack')
|
|
|
|
|
|
Page({
|
|
|
-
|
|
|
+ socket: {} as SocketIOClient.Socket,
|
|
|
+ audioManger: {} as AudioManger,
|
|
|
/**
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
+ patchProfile: false,
|
|
|
role: 'customer',
|
|
|
roomId: '',
|
|
|
webUrl: '',
|
|
|
userInfo: {} as GlobalUserInfo,
|
|
|
+ webviewParams: {} as SocketParams,
|
|
|
isTour: 0,
|
|
|
m: ''
|
|
|
|
|
@@ -24,10 +31,14 @@ Page({
|
|
|
*/
|
|
|
async onLoad(options: { roomId: string, role: string, isTour: string }) {
|
|
|
console.log('options', options)
|
|
|
+
|
|
|
+ const auth = await authorizeRecord();
|
|
|
+ console.log('auth', auth)
|
|
|
+
|
|
|
if (options.roomId) {
|
|
|
const res = await fetchRoom(options.roomId);
|
|
|
console.log('data', res)
|
|
|
- this.setData({ roomId: options.roomId, m: res.sceneData[0].num })
|
|
|
+ this.setData({ roomDetail: res, roomId: options.roomId, m: res.sceneData[0].num })
|
|
|
}
|
|
|
if (options.role) {
|
|
|
this.setData({ role: options.role })
|
|
@@ -36,15 +47,15 @@ Page({
|
|
|
if (options.isTour) {
|
|
|
this.setData({ isTour: Number(options.isTour) })
|
|
|
}
|
|
|
- const userId = wx.getStorageSync('wxUserId')
|
|
|
- const sign = await getRTCSig(userId);
|
|
|
|
|
|
this.setWebViewUrl();
|
|
|
+ this.handleJoinSocket();
|
|
|
+ this.handleJoinRTC();
|
|
|
|
|
|
},
|
|
|
|
|
|
webViewParams() {
|
|
|
- const params = {
|
|
|
+ const params: SocketParams = {
|
|
|
vruserId: `user_${this.data.userInfo.wxUserId}`,
|
|
|
roomId: `roomId_${this.data.roomId}`,
|
|
|
role: this.data.role,
|
|
@@ -54,7 +65,9 @@ Page({
|
|
|
m: this.data.m,
|
|
|
fromMiniApp: 1,
|
|
|
}
|
|
|
- console.log('params', params)
|
|
|
+ this.setData({
|
|
|
+ webviewParams: params
|
|
|
+ })
|
|
|
type Keys = keyof typeof params;
|
|
|
return Object.keys(params).map(key => `${key}=${params[key as Keys]}`).join('&')
|
|
|
|
|
@@ -62,13 +75,20 @@ Page({
|
|
|
|
|
|
setWebViewUrl() {
|
|
|
const params = this.webViewParams();
|
|
|
- const webURL = server.webview + '?=' + params
|
|
|
+ const webURL = server.webview + '?' + params
|
|
|
console.log('webviewServer', webURL)
|
|
|
+ if (!this.data.webviewParams.name || !this.data.webviewParams.avatar) {
|
|
|
+ this.setData({
|
|
|
+ patchProfile: true
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
this.setData({
|
|
|
webUrl: webURL
|
|
|
})
|
|
|
},
|
|
|
updateUserInfo(data?: any) {
|
|
|
+ console.log('webview-updateUserInfo')
|
|
|
const app = getApp<IAppOption>();
|
|
|
const updateUserInfo = app.globalData.userInfo
|
|
|
const updateData = Object.assign({}, updateUserInfo, data)
|
|
@@ -76,6 +96,44 @@ Page({
|
|
|
userInfo: updateData
|
|
|
})
|
|
|
},
|
|
|
+ handleJoinSocket() {
|
|
|
+
|
|
|
+ this.socket = io(server.sokcet, {
|
|
|
+ path: "/ws-sync",
|
|
|
+ transport: ["websocket"],
|
|
|
+ parser: false
|
|
|
+ });
|
|
|
+ this.socket.on('connect', async () => {
|
|
|
+ const params = this.data.webviewParams
|
|
|
+ this.socket.emit('join', {
|
|
|
+ userId: params.vruserId,
|
|
|
+ roomId: params.roomId,
|
|
|
+ role: params.role,
|
|
|
+ isClient: true
|
|
|
+ })
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ this.socket.on('action', this.handleSocketAction)
|
|
|
+
|
|
|
+ },
|
|
|
+ async handleJoinRTC() {
|
|
|
+ const userId = wx.getStorageSync('wxUserId')
|
|
|
+ const sign = await getRTCSig(userId);
|
|
|
+
|
|
|
+ this.audioManger = audioManger({
|
|
|
+ roomId: this.data.webviewParams.roomId,
|
|
|
+ userId: userId,
|
|
|
+ sdkAppID: sign.sdkAppId,
|
|
|
+ sig: sign.sign,
|
|
|
+ noMute: false,
|
|
|
+ })
|
|
|
+ if (!this.audioManger.ring) {
|
|
|
+ this.audioManger.start()
|
|
|
+ }
|
|
|
+ this.audioManger.changeMute(false)
|
|
|
+
|
|
|
+ },
|
|
|
/**
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
*/
|
|
@@ -103,6 +161,7 @@ Page({
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
*/
|
|
|
onUnload() {
|
|
|
+ this.audioManger && this.audioManger.stop()
|
|
|
|
|
|
},
|
|
|
|
|
@@ -120,10 +179,46 @@ Page({
|
|
|
|
|
|
},
|
|
|
|
|
|
- /**
|
|
|
- * 用户点击右上角分享
|
|
|
- */
|
|
|
- onShareAppMessage() {
|
|
|
-
|
|
|
+ onShareAppMessage: function (res) {
|
|
|
+ const roomId = this.data.roomId
|
|
|
+ const isTour = this.data.isTour
|
|
|
+ const newPicUrl = this.data.roomDetail.cover || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
|
|
|
+ const base = {
|
|
|
+ imageUrl: newPicUrl,
|
|
|
+ path: `/pages/room/room?roomId=${roomId}&role=customer&isTour=${isTour}`
|
|
|
+ }
|
|
|
+ console.error('share', base)
|
|
|
+ if (res.from === 'button') {
|
|
|
+ // this.cancelShareMode()
|
|
|
+ return {
|
|
|
+ ...base,
|
|
|
+ title: '【好友邀请】一起来逛店吧!',
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return {
|
|
|
+ ...base,
|
|
|
+ title: '【好友邀请】一起来逛店吧!',
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ handleSocketAction(action: SocketAction) {
|
|
|
+ console.warn('action', action)
|
|
|
+ switch (action.type) {
|
|
|
+ case 'users-muted':
|
|
|
+ this.handleActionMuted(action.userId, action.muted)
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ handleActionMuted(userId: string | undefined, muted: boolean | undefined) {
|
|
|
+ if (userId && typeof muted !== "undefined") {
|
|
|
+ const f_userId = userId.replace('user_', '')
|
|
|
+ const app = getApp<IAppOption>();
|
|
|
+ if (app.globalData.userInfo?.wxUserId == f_userId) {
|
|
|
+ this.audioManger.changeMute(muted)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
})
|