12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616 |
- const {
- io
- } = require('./socket.io-v4.msgpack.js');
- var user = require('./services/user.js');
- const api = require('/config/api.js');
- const util = require('/utils/util.js');
- const UNLOGIN = 'NO_LOGIN'
- const btoa = require('./utils/btoa');
- const manyCount = 50
- import remote from './config.js'
- var app = getApp();
- var isIos = false
- wx.getSystemInfo({
- success: function (res) {
- isIos = res.platform == "ios"
- }
- })
- 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 obj = {},
- index = url.indexOf('?'), // 看url有没有参数
- params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2
- if (index != -1) { // 有参数时
- let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"]
- for (let i of parr) { // 遍历数组
- let arr = i.split('='); // 1) i id = 1 arr = [id, 1] 2)i type = 2 arr = [type, 2]
- obj[arr[0]] = arr[1]; // obj[arr[0]] = id, obj.id = 1 obj[arr[0]] = type, obj.type = 2
- }
- }
- return obj;
- }
- export default {
- joinUrl() {
- let url = this.data.url;
- //隐藏部分
- // if (this.data.url && /smobile.html/.test(this.data.url)) {
- // const newUrl = this.data.url.replace('smobile.html', 'shop.html')
- // url = newUrl
- // this.setData({
- // url: newUrl
- // });
- // }
- let options = {
- API_BASE_URL: api.API_BASE_URL,
- "url": url,
- // "url": 'http://192.168.0.112:8080',
- "reload": this.data.reload,
- "token": wx.getStorageSync('token'),
- "code": this.mcode,
- "brandId": this.options.id,
- "open": this.data.showCommodity,
- "pauseVideo": this.pauseVideo,
- "bottom": this.data.bottom || 0,
- socket: {
- socketHost: remote.socketHost,
- path: '/new-zfb',
- options: {
- ...this.data.socketOptions,
- // nickname: encodeURI(encodeURI(this.data.socketOptions.nickname))
- nickname: encodeURIComponent(encodeURIComponent(this.data.socketOptions.nickname))
- }
- }
- }
- // let base = 'http://127.0.0.1:5500/index.html'
- // let base = remote.viewHost + '/shop-container/shop.html'
- let sponsor = !!this.data.canShow
- if (this.data.join && !this.options.join) {
- sponsor = false
- }
- // 33是从我的房间出来的
- if (Number(this.data.type) === 33) {
- sponsor = true;
- }
- // debugger
- // remote.viewHost
- let hostUrl
- if (options.url.indexOf('www.4dkankan.com') != -1) {
- hostUrl = 'https://www.4dkankan.com/shop-container-zfb/'
- } else if (options.url.indexOf('test.4dkankan.com') != -1) {
- hostUrl = 'https://test.4dkankan.com/shop-container-zfb/'
- } else {
- // hostUrl = 'https://zfb.4dkankan.com/shop-container/'
- // hostUrl = remote.viewHost + '/shop-container/'
- hostUrl = remote.viewHost + '/shop-container-v4/'
- }
- // let base = remote.viewHost + '/shop-container/fashilong.html?env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
- let base = hostUrl + 'fashilong.html?time=' + Date.now() + '&env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
- // let base = remote.viewHost + '/shop.html'
- this.data.reload = false
- this.data.showCommodity = false
- //上线前隐藏Vlog
- // options.url = options.url + '&vlog';
- if (!this.data.webviewUrl) {
- console.log(base)
- this.setData({
- 'webviewUrl': base + '#' + JSON.stringify(options)
- })
- } else {
- this.socketSendMessage('clientSyncAction', {
- sender: 'h5',
- type: 'hashChange',
- data: options
- })
- }
- },
- onShow() {
- this.setData({
- isIos,
- showComtypesAllTab: false
- })
- // wx.showToast({
- // title: '测试--socket.connected' + this.socket.connected,
- // })
- const onlineAction = () => {
- this.pauseVideo = false
- this.joinUrl()
- // debugger
- this.socketSendMessage('changeOnlineStatus', {
- status: 1
- })
- }
- console.warn("socketInstance-1", this.socketInstance);
- if (this.socketInstance) {
- if (!this.socketInstance.connected) {
- // wx.showToast({
- // title: 'socketInstance-2',
- // icon: 'none',
- // duration: 5000
- // })
- // this.socketInstance.connect();
- // wx.showModal({
- // title: '提示',
- // content: '欧克',
- // showCancel: false,
- // confirmColor: '#0075DC',
- // success: function (res) {}
- // })
- setTimeout(onlineAction, 300)
- } else {
- // wx.showToast({
- // title: 'socketInstance-3',
- // icon: 'none',
- // duration: 5000
- // })
- onlineAction();
- }
- }
- // if (this.socketSendMessage) {
- // this.pauseVideo = false
- // this.joinUrl()
- // // debugger
- // this.socketSendMessage('changeOnlineStatus', {
- // status: 1
- // })
- // }
- },
- changeShowComtypesAllTab(ev) {
- this.setData({
- showCommodity: false
- })
- setTimeout(() => {
- this.setData({
- showComtypesAllTab: ev.currentTarget.dataset.show,
- showCommodity: true
- })
- }, 100)
- },
- async authorizeRecord() {
- let isAuth = await new Promise((r, j) => {
- wx.authorize({
- scope: 'scope.record',
- success: () => r(true),
- fail: () => r(false)
- })
- })
- if (isAuth) return true
- let res = await new Promise(r => {
- wx.showModal({
- title: '提示',
- content: '您未授权录音,说话功能将无法使用',
- showCancel: true,
- confirmText: "授权",
- confirmColor: "#52a2d8",
- success: res => r(res),
- fail: () => r(false)
- })
- })
- if (!res || res.cancel) return;
- isAuth = await new Promise((r) => {
- wx.openSetting({
- success: res => r(res.authSetting['scope.record']),
- fail: () => r(false)
- })
- })
- return isAuth
- },
- // 获取录音权限状态
- async getAuthorizeRecordStatus() {
- const isAuth = await new Promise((r, j) => {
- wx.authorize({
- scope: 'scope.record',
- success: () => r(true),
- fail: () => r(false)
- })
- })
- return Promise.resolve(isAuth)
- },
- async agetUserInfo() {
- const res = await util.request(api.UserInfo)
- if (res.errno === 401) {
- return {
- userId: UNLOGIN,
- avatar: ''
- }
- } else {
- const data = res.data
- data.region = data.city ? data.city.split(',') : []
- data.birthday = data.birthday || '1990-01-01'
- if (!data.nickname || !data.avatar || !data.mobile) {
- this.setData({
- isNotPersonalfullpack: true
- })
- }
- data.nickname = data.nickname ? data.nickname : data.username
- data.avatar = data.avatar ? data.avatar : 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
- return data
- }
- },
- async getUserInfo() {
- let userInfo = wx.getStorageSync('userInfo');
- let token = wx.getStorageSync('token');
- if (userInfo && userInfo.userId && token) {
- let info = await this.agetUserInfo()
- return {
- ...userInfo,
- ...info,
- avatarUrl: info.avatar
- };
- } else {
- return {
- userId: UNLOGIN,
- avatar: ''
- }
- }
- // let detail
- // let isAuth = await new Promise((r, j) => {
- // wx.authorize({
- // scope: 'scope.userInfo',
- // success: () => r(true),
- // fail: () => r(false)
- // })
- // })
- // if (!isAuth) {
- // this.setData({userAuth: true})
- // detail = await new Promise(r => {
- // this.bindGetUserInfo = (e) => {
- // if (e.detail.userInfo) {
- // this.setData({userAuth: false})
- // console.log('gei', e.detail)
- // r(e.detail)
- // }
- // }
- // })
- // } else {
- // detail = await new Promise(r => {
- // wx.getUserInfo({
- // success: res => r(res),
- // fail: () => r(false)
- // })
- // })
- // }
- // try {
- // let res = await user.loginByWeixin(detail)
- // app.globalData.userInfo = res.data.userInfo;
- // app.globalData.token = res.data.token;
- // return res.data.userInfo
- // } catch(e) {
- // return false
- // }
- },
- login() {
- getApp().setLoginProps(false)
- },
- async getSocketOptions(sceneId, roomId) {
- //TODO
- console.log('this.data.type', this.data.type)
- // debugger;
- let result
- if (Number(this.data.type) === 33) {
- result = await util.request(api.enterRoom, {
- businessId: roomId
- }, 'POST', 'application/json')
- if (result.code !== 200) {
- wx.showModal({
- content: result.error,
- complete: () => {
- if (result.message && result.message.isAnchor == 0) {
- wx.switchTab({
- url: '/pages/index/index',
- })
- } else {
- wx.redirectTo({
- url: '/pages/roomManger/roomManger',
- });
- }
- }
- })
- return
- }
- }
- const capacities = !!result ? result.message.capacities : 50 // 房间限制人数
- const {
- isAnchor,
- assistant,
- } = !!result ? result.message : {}
- let userInfo = await this.getUserInfo()
- // console.log('---', userInfo)
- // this.setData({
- // userInfoa: userInfo.nickname.split('').join(' ')
- // })
- userInfo.nickname = userInfo.nickname.replace(/[^\u4E00-\u9FA5A-Za-z0-9]/g, '')
- if (userInfo.nickname == "") {
- userInfo.nickname = "口"
- }
- // this.role !== 'leader'
- // let roomType
- // if ((!this.data.canShow && !this.data.join) || (this.data.join && !this.options.join)) {
- // // roomType = '1v1'
- // if (this.options.roomId) {
- // this.role = 'leader'
- // }
- // console.log('**************')
- // console.log(this.options)
- // }
- let isAllowMic // 真正MIC权, 房主与 授权一个 要在房间isAllowMic开启
- if (Number(isAnchor) === 1) {
- this.role = "leader"
- isAllowMic = 1
- } else {
- this.role = 'customer'
- isAllowMic = 0
- }
- // 助手改用isAssistant作为flag, role因为V3不能新增角色,只支持leader/customer。
- let isAssistant
- if (assistant && assistant.userId && assistant.userId == userInfo.userId) {
- isAssistant = true;
- } else {
- isAssistant = false
- }
- console.log('进入房间角色, 是否助手 %s', this.role, isAssistant);
- const isAuthMic = await this.getAuthorizeRecordStatus();
- // console.log('当前用户录音权限状态', isAuthMic)
- this.setData({
- isAllowMic,
- isAuthMic
- })
- const assistantId = (assistant && assistant.userId) ? assistant.userId : '';
- if (capacities) {
- this.setData({
- peopleCount: capacities
- })
- } else {
- this.setData({
- peopleCount: manyCount
- })
- }
- return {
- role: this.role,
- userId: userInfo.userId,
- // roomType,
- avatar: userInfo.avatarUrl,
- nickname: userInfo.nickname,
- voiceStatus: getApp().globalData.voiceProps.noMute ? 0 : 2,
- isAuthMic: isAuthMic ? 1 : 0,
- isAllowMic: isAllowMic,
- roomId: roomId,
- sceneNumber: sceneId,
- onlineStatus: 1,
- assistantId: assistantId,
- isAssistant: isAssistant ? 1 : 0,
- oid: userInfo.weixin_openid,
- userLimitNum: capacities || 50
- }
- },
- async socketStart({
- sceneId,
- roomId,
- options
- }) {
- if (!options) {
- options = await this.getSocketOptions(sceneId, roomId)
- }
- console.log('小程序参数', options)
- if (!options.roomId) {
- return Promise.resolve(false)
- }
- // 真正进入统计
- if (this.data.inviterId) {
- // wx.showToast({
- // title: '邀请人ID:' + this.data.inviterId,
- // })
- }
- if (options.roomId !== '888888') {
- util.request(api.trackRoom, {
- roomId: options.roomId,
- inviterId: this.data.inviterId || '',
- type: 0
- }, 'POST', 'application/json')
- }
- let userInfo = await this.getUserInfo()
- let socket = io(remote.socketHost, {
- path: '/new-zfb',
- transport: ['websocket'],
- extraHeaders: {
- "oid": userInfo.weixin_openid
- },
- query: {
- ...options,
- isClient: true,
- from: 2
- }
- });
- this.socketInstance = socket
- console.error('新建socket Room', options.roomId)
- this.setData({
- socketStatus: 0,
- })
- socket.on('connect', () => this.setData({
- socketStatus: 1
- }))
- socket.on('connect_error', () => this.setData({
- socketStatus: -1
- }))
- socket.on('connect_timeout', () => this.setData({
- socketStatus: -1
- }))
- socket.on('disconnect', () => this.setData({
- socketStatus: -1
- }))
- socket.on('reconnect', () => {
- // wx.showToast({
- // title: '重连',
- // })
- this.setData({
- socketStatus: this.data.socketStatus
- })
- let noMute = getApp().globalData.voiceProps.noMute
- this.socketSendMessage('changeVoiceStatus', {
- status: noMute ? 0 : 2
- })
- this.socketSendMessage('changeOnlineStatus', {
- status: 1
- })
- })
- socket.on('reconnect_failed', () => this.setData({
- socketStatus: -1
- }))
- socket.on('error', () => this.setData({
- socketStatus: -1
- }))
- socket.on('roomIn', config => {
- let enableTalk = config.roomsConfig.enableTalk !== false
- let noMute = getApp().globalData.voiceProps.noMute
- getApp().globalData.voiceProps.force = enableTalk
- if (!enableTalk && !noMute) {
- if (this.role !== 'leader') {
- // this.mic()
- }
- }
- })
- this.socketSendMessage = (event, obj) => {
- console.error('发送 socket Room', options.roomId, event, obj)
- socket && socket.emit(event, obj)
- }
- socket.on('clientSyncAction', (data) => {
- console.log('调用', data.type, '方法', data)
- if (this[data.type]) {
- this[data.type](data)
- } else if (data.type == 'wx-subscribe') {
- this.getUrlCode(data.data)
- } else {
- console.error('没有', data.type, '方法')
- }
- })
- socket.on('action', (data) => {
- if (data.type === 'navigateToGoods') {
- this.navigateToGoodsAction(data.data)
- }
- })
- socket.on('changeRoomEnableTalk', config => {
- if (this.role !== 'leader') {
- this.changeRoomEnableTalk(config)
- }
- })
- socket.on('startCall', this.startCall.bind(this))
- socket.on('stopCall', (data) => {
- console.log('on stopCall')
- this.stopCall(data)
- })
- 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) => {
- this.handleSomeOneLeave(user)
- })
- socket.on('roomClose', (data) => {
- console.log('on roomClose')
- this.stopCall(data)
- })
- socket.on('autoReJoin', (data) => {
- console.log('on autoReJoin')
- if ('roomId' in data) {
- options.roomId = Number(data.roomId)
- }
- })
- // 有MIC通知,主要是其他用户禁MIC 移到中间层处理。
- // socket.on('beHasMic', (data) => {
- // const socketOptions = this.data.socketOptions
- // if (data.user) {
- // const isOther = (socketOptions.role !== 'leader' && (Number(socketOptions.userId) !== Number(data.user.userId)));
- // if (isOther) {
- // this.closeMic();
- // // wx.showToast({
- // // title: '关mic' + isOther
- // // })
- // }
- // }
- // })
- socket.on("beKicked", data => {
- const that = this
- if (data.userId && data.roomId) {
- const socketOptions = this.data.socketOptions
- const userId = data.userId
- const roomId = data.roomId
- // debugger
- if (socketOptions.userId == userId && this.options.roomId == roomId) {
- this.exitRoom();
- wx.showToast({
- title: '您已被踢出房间!',
- icon: 'none',
- complete: () => {
- setTimeout(() => {
- that.socketStop();
- wx.switchTab({
- url: '/pages/index/index',
- })
- }, 1000)
- }
- })
- }
- }
- });
- socket.on("roomMaximum", () => {
- this.setData({
- roomMaximum: true
- })
- });
- //全员退出
- socket.on('roomDisMiss', () => {
- // wx.showToast({
- // title: '全员退出',
- // });
- this.setData({
- roomDisMiss: true
- })
- try {
- this.exitRoomApi();
- } catch (error) {
- console.log('error', error)
- }
- // this.exitRoom();
- })
- //服务器未知错误重进
- socket.on('unKnowError', () => {
- this.setData({
- unKnowError: true
- })
- })
- //被动通知开关MIC 要3秒后
- socket.on('serverOnMic', ({
- voiceStatus
- }) => {
- setTimeout(() => {
- if (Number(voiceStatus) === 2) {
- // wx.showToast({
- // title: '开MIC',
- // icon:'none'
- // })
- this.openMic();
- }
- if (Number(voiceStatus) === 0) {
- // wx.showToast({
- // title: '关MIC',
- // icon:'none'
- // })
- this.closeMic();
- }
- }, 3000)
- });
- this.socketStop = () => {
- if (socket) {
- socket.close()
- console.error('断开 并滞空 socket Room', options.roomId)
- this.setData({
- socketStatus: 2
- })
- socket = null
- }
- }
- return options
- },
- getUrlCode(url) {
- this.socketSendMessage('clientSyncAction', {
- sender: 'wx',
- type: 'wx-subscribe-result',
- data: 3020
- })
- // wx.request({
- // url: url, //仅为示例,并非真实的接口地址
- // method: 'get',
- // success: (res) => {
- // let code = -1
- // if (typeof res.data.code != 'undefined') {
- // code = res.data.code
- // }
- // this.socketSendMessage('clientSyncAction', {
- // sender: 'wx',
- // type: 'wx-subscribe-result',
- // data: code
- // })
- // },
- // fail: (err) => {
- // console.log(err)
- // }
- // })
- },
- changeRoomEnableTalk(data) {
- console.log(data)
- let noMute = getApp().globalData.voiceProps.noMute
- getApp().globalData.voiceProps.force = data.enableTalk
- // noMute true 静音
- // enableTalk false 静音
- if (!!data.enableTalk === !!noMute) {
- this.mic()
- }
- },
- navigateToGoods({
- data
- }) {
- // wx.showToast({
- // title: JSON.stringify(data).substr(40)
- // })
- this.navigateToGoodsAction(data)
- },
- navigateToGoodsAction(id) {
- wx.navigateTo({
- url: '/pages/goods/goods?id=' + id,
- })
- },
- getUrl(url, socketOptions, isJoin) {
- url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
- if (isJoin) {
- url += '&role=' + this.role + '&shopping'
- } else {
- url += '&role=' + this.role
- }
- console.error(url)
- console.log(isJoin)
- return url
- },
- navigateToMiniProgram(data) {
- wx.showModal({
- title: '温馨提示',
- content: '即将跳到其他小程序,是否继续?',
- showCancel: true, //是否显示取消按钮
- cancelText: "取消", //默认是“取消”
- confirmText: "确定", //默认是“确定”
- success: function (res) {
- if (res.cancel) {
- //点击取消,wx.navigateBack
- } else {
- wx.navigateToMiniProgram(data.data)
- }
- },
- fail: function (res) {
- //接口调用失败的回调函数,wx.navigateBack
- },
- complete: function (res) {
- //接口调用结束的回调函数(调用成功、失败都会执行)
- },
- })
- },
- async handleSomeOneInRoom(data) {
- if (data && data.user) {
- console.log('handleSomeOneInRoom', data)
- this.startCall(data)
- }
- },
- async startCall(data) {
- //TODO 触发三次
- console.log('startCall-data', data)
- // if( this.role =='leader'){
- this.setData({
- shareStatus: 1
- })
- if (!data) return;
- this.setData({
- surplus: this.data.peopleCount - data.roomsPerson.length
- })
- //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
- getApp().globalData.roomId = socketOptions.roomId
- const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
- if (!user) {
- return
- }
- //屏蔽有人进来才开麦克风
- // if (data.roomsPerson.length <= 1) {
- // return
- // }
- if (user && user.userId) {
- this.setData({
- userId: user.userId
- })
- }
- user.noMute = getApp().globalData.voiceProps.noMute
- getApp().setVoiceProps({
- ...user,
- action: 'startCall'
- })
- // this.socketSendMessage('changeVoiceStatus', {
- // status: getApp().globalData.voiceProps.noMute ? 0 : 2
- // })
- // }
- },
- stopCall() {
- console.error('stopCall')
- this.setData({
- shareStatus: 0
- })
- getApp().setVoiceProps({
- noMute: false,
- action: 'stopCall'
- })
- if (this.runManager) {
- // this.recorderManager.stop()
- this.runManager = false
- }
- },
- handleSomeOneLeave(data) {
- if (data.roomsPerson.length <= 1) {
- // this.stopCall()
- }
- this.setData({
- surplus: this.data.peopleCount - data.roomsPerson.length
- })
- },
- //deprecated
- async newRoomBk(data) {
- if (data.roomId) return;
- this.stopCall()
- getApp().globalData.rtcParams = []
- getApp().globalData.pusher = ''
- if (this.data.join && !this.options.join) {
- wx.switchTab({
- url: '/pages/index/index',
- })
- return;
- }
- this.role = this.data.canShow ? 'leader' : 'customer'
- let options = await this.getSocketOptions(this.mcode)
- this.socketSendMessage('clientSyncAction', {
- type: 'newRoom',
- data: options
- })
- setTimeout(async () => {
- this.wssSuccess = false
- this.socketStop && this.socketStop()
- this.data.many = !!this.data.canShow
- // this.setData({
- // // peopleCount: this.data.many ? manyCount : 5
- // peopleCount: manyCount
- // })
- let base = this.base
- let socketOptions = await this.socketStart({
- options
- })
- let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
- this.base = base
- this.setData({
- url,
- socketOptions,
- })
- this.joinUrl()
- this.setData({
- socketOptions
- })
- this.loadConponSuccess = true
- this.readySendCouponCtrl()
- }, 300)
- },
- // 真正退出房间
- async exitRoom() {
- const roomId = this.data.socketOptions.roomId;
- const role = this.role;
- const result = await util.request(api.exitRoom, {
- businessId: roomId
- }, 'POST', 'application/json');
- this.socketSendMessage('stopCall', {
- from: 2
- })
- this.stopCall();
- this.socketStop();
- if (role === 'leader') {
- wx.redirectTo({
- url: '/pages/roomManger/roomManger',
- });
- } else {
- wx.switchTab({
- url: '/pages/index/index'
- });
- }
- },
- // 一些情况下单调API退出
- exitRoomApi() {
- const roomId = this.data.socketOptions.roomId;
- const role = this.role;
- util.request(api.exitRoom, {
- businessId: roomId
- }, 'POST', 'application/json');
- },
- async exit() {
- // this.stopCall()
- getApp().globalData.rtcParams = []
- getApp().globalData.pusher = ''
- this.socketStop && this.socketStop()
- this.role = 'leader'
- let base = this.base
- let socketOptions = await this.socketStart({
- sceneId: this.mcode
- })
- let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
- this.base = base
- wx.nextTick(() => {
- setTimeout(() => {
- this.setData({
- url,
- loadUrl: true,
- socketOptions,
- showCommodityCtrl: false,
- hideWebView: false,
- reload: true
- })
- this.joinUrl()
- }, 500)
- })
- },
- clearDebuger() {
- this.setData({
- debugerInfo: ''
- })
- },
- async mic({
- data
- }) {
- if (Number(data.user.isAllowMic) === 1) {
- let noMute = getApp().globalData.voiceProps.noMute
- // debugger
- // noMute true 静音
- // enableTalk false 静音
- // if (!!getApp().globalData.voiceProps.force === !!noMute)
- // return
- // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
- if (!this.data.socketOptions.voiceStatus) {
- let voiceStatus = await this.authorizeRecord()
- if (voiceStatus) {
- this.data.socketOptions.voiceStatus = 1
- noMute = false
- } else {
- noMute = true
- }
- } else {
- noMute = !noMute
- }
- getApp().globalData.voiceProps.noMute = noMute
- this.socketSendMessage('changeVoiceStatus', {
- status: noMute ? 0 : 2,
- user: data.user
- })
- getApp().setVoiceProps({
- noMute
- })
- wx.showToast({
- title: `已${noMute ? '关闭' : '开启'}麦克风`,
- })
- }
- },
- closeMic() {
- getApp().globalData.voiceProps.noMute = true
- this.socketSendMessage('changeVoiceStatus', {
- status: 0,
- })
- getApp().setVoiceProps({
- noMute: true
- })
- },
- openMic() {
- getApp().globalData.voiceProps.noMute = false
- this.socketSendMessage('changeVoiceStatus', {
- status: 2,
- })
- getApp().setVoiceProps({
- noMute: false
- })
- },
- callPhone() {
- wx.makePhoneCall({
- phoneNumber: this.data.contractPhone,
- })
- this.setData({
- showContact: false
- })
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function (res) {
- let {
- id,
- newPicUrl
- } = this.data
- const socketOptions = this.data.socketOptions
- if (res.from === 'button') {
- this.setData({
- sendShare: false
- })
- const userId = socketOptions.userId
- const share = {
- title: '【好友推荐】一起来云逛吧',
- imageUrl: newPicUrl,
- path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}&inviterId=${userId}`,
- }
- console.log('分享', share);
- return share
- } else {
- return {
- imageUrl: newPicUrl,
- path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false&inviterId=${userId}`,
- }
- }
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- console.log('on onUnload')
- // this.socketSendMessage('stopCall', {})
- // this.stopCall()
- this.socketStop && this.socketStop()
- getApp().globalData.pusher = ''
- },
- cart(data) {
- this.setData({
- showCommodityCtrl: data.data
- })
- },
- share() {
- console.log('share-debug')
- const companyName = `指房宝(杭州)科技有限公司`
- const vrLink = `/pages/webview/index`
- const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
- const shareImg = img_url
- this.count = this.count || 0
- console.log('share-debug many:%s shareStatus: %s', !!this.data.many, this.data.shareStatus);
- if (this.data.many && this.data.shareStatus == 1) {
- //开启一起逛时候的分享
- console.log(`share-debug: /pages/shareRoom/shareRoom?img_url=${btoa(img_url)}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}&inviterId=${this.data.userId}`)
- console.log('share-debug', this.data.socketOptions)
- wx.navigateTo({
- url: `/pages/shareRoom/shareRoom?img_url=${btoa(img_url)}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}&inviterId=${this.data.userId}`,
- })
- } else {
- console.log(`share-debug: /pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}`);
- wx.navigateTo({
- url: `/pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}`,
- })
- }
- },
- back(data) {
- if (data.sender !== 'h5') return;
- wx.switchTab({
- url: '/pages/index/index'
- })
- this.setData({
- showCommodityCtrl: false
- })
- },
- service() {
- this.setData({
- showContact: true,
- showCommodity: false,
- showCoupon: false
- })
- },
- invite(data) {
- if (data.sender !== 'h5') return;
- // 分享房间进入统计
- if (this.data.socketOptions.roomId !== '888888') {
- util.request(api.trackRoom, {
- roomId: this.data.socketOptions.roomId,
- type: 1,
- }, 'POST', 'application/json')
- }
- this.setData({
- sendShare: true,
- count: ++this.data.count
- })
- },
- coupon(data) {
- if (data.sender !== 'h5') return;
- this.setData({
- showContact: false,
- showCommodity: false,
- showCoupon: true
- })
- },
- liveGotoGood(ev) {
- let id = ev.currentTarget.dataset.item.goodsId
- wx.navigateTo({
- url: '/pages/goods/goods?id=' + id,
- })
- },
- gotoGoodsDOM(event) {
- this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
- },
- gotoGoodsSocket(data) {
- this.gotoGoods(data.data)
- },
- gotoGoods(id) {
- console.log('---', id)
- this.socketSendMessage('clientSyncAction', {
- type: 'openTag',
- data: id
- })
- this.setData({
- showCommodity: false
- })
- this.joinUrl()
- },
- addCard(event) {
- wx.navigateTo({
- url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
- })
- },
- buyGoods(event) {
- wx.navigateTo({
- url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
- })
- },
- showCommodityFn() {
- this.setData({
- showCommodity: true,
- showContact: false,
- showCoupon: false
- })
- this.joinUrl()
- },
- hideComodity() {
- this.setData({
- showCommodity: false
- })
- this.joinUrl()
- },
- hideCoupon() {
- this.setData({
- showCoupon: !this.data.showCoupon
- })
- },
- async receive(ev) {
- let item = ev.target.dataset.item
- try {
- // wx.showToast({
- // title: '领取优惠卷',
- // })
- // return;
- if (item.hasReceived || item.number <= item.receiveNumber) return;
- let res = await util.request(api.CouponExchange, {
- couponId: item.id
- })
- if (res.code === 0) {
- wx.showToast({
- title: '已成功领取',
- success: () => {
- this.setData({
- showCoupon: false
- })
- wx.nextTick(() => {
- this.setData({
- coupons: this.data.coupons.map(citem => {
- return {
- ...citem,
- hasReceived: citem.id === item.id ? true : citem.hasReceived
- }
- }),
- showCoupon: true
- })
- })
- }
- })
- } else if (res.errno === 401) {
- getApp().setLoginProps(false)
- } else {
- wx.showToast({
- title: res.msg,
- })
- }
- } catch (e) {
- console.error(e)
- wx.showToast({
- icon: 'none',
- title: '领取失败',
- })
- }
- },
- async getCouponList(id) {
- const success = (res) => {
- this.setData({
- coupons: res.data.list.map(item => {
- item.typeMoney = item.typeMoney.toString()
- item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
- item.typeMoney.length === 4 ? '70rpx' : '130rpx'
- return item
- })
- })
- this.loadConponSuccess = true
- this.readySendCouponCtrl()
- }
- let res = await util.request(api.BrandCouponList, {
- brandId: id,
- pageNum: 1,
- pageSize: 10000
- }, 'GET')
- console.log(res)
- if (res.code === 0) {
- success(res)
- } else {
- let res = await util.request(api.UNBrandCouponList, {
- brandId: id,
- pageNum: 1,
- pageSize: 10000
- }, 'GET')
- success(res)
- }
- },
- ready() {
- this.wssSuccess = true
- this.readySendCouponCtrl()
- },
- readySendCouponCtrl() {
- if (this.wssSuccess && this.loadConponSuccess) {
- this.loadConponSuccess = false
- this.socketSendMessage('clientSyncAction', {
- type: 'showCoupon',
- data: this.data.coupons.length > 0
- })
- }
- },
- getBrand: function (id, code) {
- this.getGoodsCount(code, id)
- return;
- },
- getGoodsCount(code, id) {
- util.request(api.GoodsNumCount, {
- isDelete: 0,
- isOnSale: 1,
- brandId: id
- }, 'GET')
- .then(res => {
- if (res.errno === 0) {
- this.setData({
- goodsCount: res.data
- })
- }
- this.getCouponList(id)
- })
- },
- getGoodsList(id, category_id) {
- var that = this;
- if (!(this.data.navList && this.data.navList.length)) {
- that.navDatas = {}
- let navDatas = this.data.navList = this.data.comtypes
- // util.request(api.GoodsCategory, { id: category_id })
- // .then(function (res) {
- // if (res.errno == 0) {
- // let navDatas = res.data.brotherCategory
- // that.setData({
- // navList: navDatas,
- // currTypeId: category_id
- // });
- that.navDatas = {}
- navDatas.forEach(item => {
- util.request(api.GoodsList, {
- brandId: id,
- categoryId: item.category_id,
- page: that.data.page,
- size: that.data.size
- })
- .then(res => {
- if (res.errno === 0) {
- that.navDatas[item.category_id] = res.data.goodsList
- }
- })
- })
- // }
- // })
- }
- if (that.navDatas[category_id]) {
- if (!isIos) {
- let showCommodity = that.data.showCommodity
- that.setData({
- showCommodity: false
- })
- setTimeout(() => {
- wx.nextTick(() => {
- that.setData({
- goodsList: that.navDatas[category_id],
- currTypeId: category_id,
- showCommodity: showCommodity
- });
- })
- }, 500)
- } else {
- that.setData({
- goodsList: that.navDatas[category_id],
- currTypeId: category_id,
- });
- }
- } else {
- console.error('诱惑去啦')
- util.request(api.GoodsList, {
- brandId: id,
- categoryId: category_id,
- page: that.data.page,
- size: that.data.size
- })
- .then(function (res) {
- if (res.errno === 0) {
- that.setData({
- goodsList: res.data.goodsList,
- currTypeId: category_id
- });
- // this.data.navList
- }
- });
- }
- },
- getBrandDetail: function (id, type, cb) {
- console.log('getBrandDetail-params', id, type)
- util.request(api.BrandDetail, {
- id: id,
- type: type,
- }).then((res) => {
- console.log('getBrandDetail', res)
- let base = res.data.brand.sceneUrl
- // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
- if (res.errno === 0) {
- let url = base + "&sid=" + id
- // debugger
- this.setData({
- id: id,
- newPicUrl: res.data.brand.appListPicUrl,
- sceneNum: res.data.brand.sceneNum,
- canShow: res.data.brand.canShow,
- contractPhone: res.data.brand.contractPhone,
- contactInfo: {
- contactPhone: res.data.brand.contactPhone || '',
- contactHead: res.data.brand.contactHead || '',
- contactCompanyName: res.data.brand.contactCompanyName || '',
- contactNickName: res.data.brand.contactNickName || '',
- brands: res.data.brand.brands || []
- }
- })
- if (this.data.many === void 0) {
- this.data.many = !!res.data.brand.canShow
- }
- this.setData({
- // peopleCount: this.data.many ? manyCount : 5,
- peopleCount: manyCount
- })
- if (!res.data.brand.canShow) {
- this.role = 'customer'
- } else if (!this.options.join) {
- this.role = 'leader'
- }
- cb(url, urlToJson(url).m, )
- }
- });
- },
- sendContactInfo() {
- // this.socketSendMessage('getContactInfo', {
- // contactInfo: this.data.contactInfo,
- // })
- this.socketSendMessage('clientSyncAction', {
- type: 'getContactInfo',
- contactInfo: this.data.contactInfo
- })
- },
- goToCase({
- data
- }) {
- console.log('data', data)
- wx.showModal({
- title: '提示',
- content: `是否跳转到新的场景?`,
- showCancel: true,
- confirmColor: "#52a2d8",
- success: res => {
- if (res.confirm) {
- wx.navigateTo({
- url: `/pages/webview/index?id=${data.id}&type=32`,
- })
- }
- },
- fail: () => {
- }
- })
- },
- selectType(ev) {
- this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
- },
- hideCS() {
- this.setData({
- showCommodity: false,
- showCoupon: false,
- showContact: false
- })
- },
- hideContact() {
- this.setData({
- showContact: false
- })
- },
- calcShare() {
- // this.exit()
- this.setData({
- sendShare: false
- })
- },
- contactKf() {
- let keys = Object.keys(this.navDatas)
- let goodsId = this.navDatas[keys[0]][0].id
- let user = wx.getStorageSync('userinfoDetail')
- util.request(api.AddTalkCount, {
- goodsId,
- viewId: user && user.userId || '',
- sceneNum: this.data.sceneNum
- }, 'get')
- this.hideAlert && this.hideAlert()
- this.hideContact && this.hideContact()
- },
- onHide() {
- this.socketSendMessage('changeOnlineStatus', {
- status: 0
- })
- this.pauseVideo = true
- this.joinUrl()
- }
- }
|