socket.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. const {
  2. io
  3. } = require('./socket.io-v4.msgpack.js');
  4. var user = require('./services/user.js');
  5. const api = require('/config/api.js');
  6. const util = require('/utils/util.js');
  7. const UNLOGIN = 'NO_LOGIN'
  8. const btoa = require('./utils/btoa');
  9. const manyCount = 50
  10. import remote from './config.js'
  11. var app = getApp();
  12. var isIos = false
  13. wx.getSystemInfo({
  14. success: function (res) {
  15. isIos = res.platform == "ios"
  16. }
  17. })
  18. const debounce = (fn, wait) => {
  19. let callback = fn;
  20. let timerId = null;
  21. function debounced() {
  22. let context = this;
  23. let args = arguments;
  24. clearTimeout(timerId);
  25. timerId = setTimeout(function () {
  26. callback.apply(context, args);
  27. }, wait);
  28. }
  29. return debounced;
  30. }
  31. let urlToJson = (url = window.location.href) => { // 箭头函数默认传值为当前页面url
  32. let obj = {},
  33. index = url.indexOf('?'), // 看url有没有参数
  34. params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2
  35. if (index != -1) { // 有参数时
  36. let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"]
  37. for (let i of parr) { // 遍历数组
  38. let arr = i.split('='); // 1) i id = 1 arr = [id, 1] 2)i type = 2 arr = [type, 2]
  39. obj[arr[0]] = arr[1]; // obj[arr[0]] = id, obj.id = 1 obj[arr[0]] = type, obj.type = 2
  40. }
  41. }
  42. return obj;
  43. }
  44. export default {
  45. joinUrl() {
  46. const url = true ? this.data.url.replace('shop.html', 'test-shop.html') : this.data.url;
  47. let options = {
  48. API_BASE_URL: api.API_BASE_URL,
  49. "url": url,
  50. // "url": 'http://192.168.0.112:8080',
  51. "reload": this.data.reload,
  52. "token": wx.getStorageSync('token'),
  53. "code": this.mcode,
  54. "brandId": this.options.id,
  55. "open": this.data.showCommodity,
  56. "pauseVideo": this.pauseVideo,
  57. "bottom": this.data.bottom || 0,
  58. socket: {
  59. socketHost: remote.socketHost,
  60. path: '/fsl-node',
  61. options: {
  62. ...this.data.socketOptions,
  63. // nickname: encodeURI(encodeURI(this.data.socketOptions.nickname))
  64. nickname: encodeURIComponent(encodeURIComponent(this.data.socketOptions.nickname))
  65. }
  66. }
  67. }
  68. // let base = 'http://127.0.0.1:5500/index.html'
  69. // let base = remote.viewHost + '/shop-container/shop.html'
  70. let sponsor = !!this.data.canShow
  71. if (this.data.join && !this.options.join) {
  72. sponsor = false
  73. }
  74. // 33是从我的房间出来的
  75. if (Number(this.data.type) === 33) {
  76. sponsor = true;
  77. }
  78. // debugger
  79. // remote.viewHost
  80. let hostUrl
  81. if (options.url.indexOf('www.4dkankan.com') != -1) {
  82. hostUrl = 'https://www.4dkankan.com/shop-container-zfb/'
  83. } else if (options.url.indexOf('test.4dkankan.com') != -1) {
  84. hostUrl = 'https://test.4dkankan.com/shop-container-zfb/'
  85. } else {
  86. // hostUrl = 'https://zfb.4dkankan.com/shop-container/'
  87. // hostUrl = remote.viewHost + '/shop-container/'
  88. hostUrl = remote.viewHost + '/shop-container-v4/'
  89. }
  90. // let base = remote.viewHost + '/shop-container/fashilong.html?env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  91. let base = hostUrl + 'fashilong.html?time=' + Date.now() + '&env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  92. // let base = remote.viewHost + '/shop.html'
  93. this.data.reload = false
  94. this.data.showCommodity = false
  95. options.url = options.url + '&vlog';
  96. if (!this.data.webviewUrl) {
  97. console.log(base)
  98. this.setData({
  99. 'webviewUrl': base + '#' + JSON.stringify(options)
  100. })
  101. } else {
  102. this.socketSendMessage('clientSyncAction', {
  103. sender: 'h5',
  104. type: 'hashChange',
  105. data: options
  106. })
  107. }
  108. },
  109. onShow() {
  110. this.setData({
  111. isIos,
  112. showComtypesAllTab: false
  113. })
  114. if (this.socketSendMessage) {
  115. this.pauseVideo = false
  116. this.joinUrl()
  117. this.socketSendMessage('changeOnlineStatus', {
  118. status: 1
  119. })
  120. }
  121. },
  122. changeShowComtypesAllTab(ev) {
  123. this.setData({
  124. showCommodity: false
  125. })
  126. setTimeout(() => {
  127. this.setData({
  128. showComtypesAllTab: ev.currentTarget.dataset.show,
  129. showCommodity: true
  130. })
  131. }, 100)
  132. },
  133. async authorizeRecord() {
  134. let isAuth = await new Promise((r, j) => {
  135. wx.authorize({
  136. scope: 'scope.record',
  137. success: () => r(true),
  138. fail: () => r(false)
  139. })
  140. })
  141. if (isAuth) return true
  142. let res = await new Promise(r => {
  143. wx.showModal({
  144. title: '提示',
  145. content: '您未授权录音,说话功能将无法使用',
  146. showCancel: true,
  147. confirmText: "授权",
  148. confirmColor: "#52a2d8",
  149. success: res => r(res),
  150. fail: () => r(false)
  151. })
  152. })
  153. if (!res || res.cancel) return;
  154. isAuth = await new Promise((r) => {
  155. wx.openSetting({
  156. success: res => r(res.authSetting['scope.record']),
  157. fail: () => r(false)
  158. })
  159. })
  160. return isAuth
  161. },
  162. // 获取录音权限状态
  163. async getAuthorizeRecordStatus() {
  164. const isAuth = await new Promise((r, j) => {
  165. wx.authorize({
  166. scope: 'scope.record',
  167. success: () => r(true),
  168. fail: () => r(false)
  169. })
  170. })
  171. return Promise.resolve(isAuth)
  172. },
  173. async agetUserInfo() {
  174. const res = await util.request(api.UserInfo)
  175. if (res.errno === 401) {
  176. return {
  177. userId: UNLOGIN,
  178. avatar: ''
  179. }
  180. } else {
  181. const data = res.data
  182. data.region = data.city ? data.city.split(',') : []
  183. data.birthday = data.birthday || '1990-01-01'
  184. return data
  185. }
  186. },
  187. async getUserInfo() {
  188. let userInfo = wx.getStorageSync('userInfo');
  189. let token = wx.getStorageSync('token');
  190. if (userInfo && userInfo.userId && token) {
  191. let info = await this.agetUserInfo()
  192. return {
  193. ...userInfo,
  194. ...info,
  195. avatarUrl: info.avatar
  196. };
  197. } else {
  198. return {
  199. userId: UNLOGIN,
  200. avatar: ''
  201. }
  202. }
  203. // let detail
  204. // let isAuth = await new Promise((r, j) => {
  205. // wx.authorize({
  206. // scope: 'scope.userInfo',
  207. // success: () => r(true),
  208. // fail: () => r(false)
  209. // })
  210. // })
  211. // if (!isAuth) {
  212. // this.setData({userAuth: true})
  213. // detail = await new Promise(r => {
  214. // this.bindGetUserInfo = (e) => {
  215. // if (e.detail.userInfo) {
  216. // this.setData({userAuth: false})
  217. // console.log('gei', e.detail)
  218. // r(e.detail)
  219. // }
  220. // }
  221. // })
  222. // } else {
  223. // detail = await new Promise(r => {
  224. // wx.getUserInfo({
  225. // success: res => r(res),
  226. // fail: () => r(false)
  227. // })
  228. // })
  229. // }
  230. // try {
  231. // let res = await user.loginByWeixin(detail)
  232. // app.globalData.userInfo = res.data.userInfo;
  233. // app.globalData.token = res.data.token;
  234. // return res.data.userInfo
  235. // } catch(e) {
  236. // return false
  237. // }
  238. },
  239. login() {
  240. getApp().setLoginProps(false)
  241. },
  242. async getSocketOptions(sceneId, roomId) {
  243. //TODO
  244. console.log('this.data.type', this.data.type)
  245. // debugger;
  246. let result
  247. if (Number(this.data.type) === 33) {
  248. result = await util.request(api.enterRoom, {
  249. businessId: roomId
  250. }, 'POST', 'application/json')
  251. if (result.code !== 200) {
  252. wx.showModal({
  253. content: result.error,
  254. complete: () => {
  255. wx.navigateBack({
  256. url: '/pages/roomManger/roomManger',
  257. })
  258. }
  259. })
  260. return
  261. }
  262. }
  263. const capacities = !!result ? result.message.capacities : 50 // 房间限制人数
  264. const {
  265. isAnchor,
  266. assistant,
  267. } = !!result ? result.message : {}
  268. let userInfo = await this.getUserInfo()
  269. // console.log('---', userInfo)
  270. // this.setData({
  271. // userInfoa: userInfo.nickname.split('').join(' ')
  272. // })
  273. userInfo.nickname = userInfo.nickname.replace(/[^\u4E00-\u9FA5A-Za-z0-9]/g, '')
  274. if (userInfo.nickname == "") {
  275. userInfo.nickname = "口"
  276. }
  277. // this.role !== 'leader'
  278. // let roomType
  279. // if ((!this.data.canShow && !this.data.join) || (this.data.join && !this.options.join)) {
  280. // // roomType = '1v1'
  281. // if (this.options.roomId) {
  282. // this.role = 'leader'
  283. // }
  284. // console.log('**************')
  285. // console.log(this.options)
  286. // }
  287. let isAllowMic // 真正MIC权, 房主与 授权一个 要在房间isAllowMic开启
  288. if (Number(isAnchor) === 1) {
  289. this.role = "leader"
  290. isAllowMic = 1
  291. } else {
  292. this.role = 'customer'
  293. isAllowMic = 0
  294. }
  295. if (assistant && assistant.userId && assistant.userId == userInfo.userId) {
  296. this.role = 'assistant'
  297. }
  298. console.log('进入房间角色,', this.role);
  299. const isAuthMic = await this.getAuthorizeRecordStatus();
  300. console.log('当前用户录音权限状态', isAuthMic)
  301. return {
  302. role: this.role,
  303. userId: userInfo.userId,
  304. // roomType,
  305. avatar: userInfo.avatarUrl,
  306. nickname: userInfo.nickname,
  307. voiceStatus: getApp().globalData.voiceProps.noMute ? 0 : 2,
  308. isAuthMic: isAuthMic ? 1 : 0,
  309. isAllowMic: isAllowMic,
  310. roomId: roomId,
  311. sceneNumber: sceneId,
  312. onlineStatus: 1,
  313. assistantId: assistant.userId || '',
  314. userLimitNum: capacities || 50
  315. }
  316. },
  317. async socketStart({
  318. sceneId,
  319. roomId,
  320. options
  321. }) {
  322. if (!options) {
  323. options = await this.getSocketOptions(sceneId, roomId)
  324. }
  325. console.log('小程序参数', options)
  326. if (!options.roomId) {
  327. return
  328. }
  329. let socket = io(remote.socketHost, {
  330. path: '/fsl-node',
  331. transport: ['websocket'],
  332. query: {
  333. ...options,
  334. isClient: true,
  335. from: 2
  336. }
  337. })
  338. console.error('新建socket Room', options.roomId)
  339. this.setData({
  340. socketStatus: 0
  341. })
  342. socket.on('connect', () => this.setData({
  343. socketStatus: 1
  344. }))
  345. socket.on('connect_error', () => this.setData({
  346. socketStatus: -1
  347. }))
  348. socket.on('connect_timeout', () => this.setData({
  349. socketStatus: -1
  350. }))
  351. socket.on('disconnect', () => this.setData({
  352. socketStatus: -1
  353. }))
  354. socket.on('reconnect', () => {
  355. // wx.showToast({
  356. // title: '重连',
  357. // })
  358. this.setData({
  359. socketStatus: this.data.socketStatus
  360. })
  361. let noMute = getApp().globalData.voiceProps.noMute
  362. this.socketSendMessage('changeVoiceStatus', {
  363. status: noMute ? 0 : 2
  364. })
  365. this.socketSendMessage('changeOnlineStatus', {
  366. status: 1
  367. })
  368. })
  369. socket.on('reconnect_failed', () => this.setData({
  370. socketStatus: -1
  371. }))
  372. socket.on('error', () => this.setData({
  373. socketStatus: -1
  374. }))
  375. socket.on('roomIn', config => {
  376. let enableTalk = config.roomsConfig.enableTalk !== false
  377. let noMute = getApp().globalData.voiceProps.noMute
  378. getApp().globalData.voiceProps.force = enableTalk
  379. if (!enableTalk && !noMute) {
  380. if (this.role !== 'leader') {
  381. // this.mic()
  382. }
  383. }
  384. })
  385. this.socketSendMessage = (event, obj) => {
  386. console.error('发送 socket Room', options.roomId, event, obj)
  387. socket.emit(event, obj)
  388. }
  389. socket.on('clientSyncAction', (data) => {
  390. console.log('调用', data.type, '方法', data)
  391. if (this[data.type]) {
  392. this[data.type](data)
  393. } else if (data.type == 'wx-subscribe') {
  394. this.getUrlCode(data.data)
  395. } else {
  396. console.error('没有', data.type, '方法')
  397. }
  398. })
  399. socket.on('action', (data) => {
  400. if (data.type === 'navigateToGoods') {
  401. this.navigateToGoodsAction(data.data)
  402. }
  403. })
  404. socket.on('changeRoomEnableTalk', config => {
  405. if (this.role !== 'leader') {
  406. this.changeRoomEnableTalk(config)
  407. }
  408. })
  409. socket.on('startCall', this.startCall.bind(this))
  410. socket.on('stopCall', (data) => {
  411. console.log('on stopCall')
  412. this.stopCall(data)
  413. })
  414. this.handleSomeOneInRoom = this.handleSomeOneInRoom.bind(this)
  415. // socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  416. socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  417. socket.on('someOneLeaveRoom', (user, data) => {
  418. this.handleSomeOneLeave(user)
  419. })
  420. socket.on('roomClose', (data) => {
  421. console.log('on roomClose')
  422. this.stopCall(data)
  423. })
  424. socket.on('autoReJoin', (data) => {
  425. console.log('on autoReJoin')
  426. if ('roomId' in data) {
  427. options.roomId = Number(data.roomId)
  428. }
  429. })
  430. socket.on("beKicked", data => {
  431. if (data.userId && data.roomId) {
  432. const socketOptions = this.data.socketOptions
  433. const userId = data.userId
  434. const roomId = data.roomId
  435. // debugger
  436. if (socketOptions.userId == userId && this.options.roomId == roomId) {
  437. wx.showToast({
  438. title: '您已被踢出房间!',
  439. icon: 'none',
  440. complete: () => {
  441. setTimeout(() => {
  442. this.socketStop();
  443. wx.redirectTo({
  444. url: '/pages/roomManger/roomManger',
  445. });
  446. }, 1000)
  447. }
  448. })
  449. }
  450. }
  451. });
  452. this.socketStop = () => {
  453. if (socket) {
  454. socket.close()
  455. console.error('断开 并滞空 socket Room', options.roomId)
  456. this.setData({
  457. socketStatus: 2
  458. })
  459. socket = null
  460. }
  461. }
  462. return options
  463. },
  464. getUrlCode(url) {
  465. this.socketSendMessage('clientSyncAction', {
  466. sender: 'wx',
  467. type: 'wx-subscribe-result',
  468. data: 3020
  469. })
  470. // wx.request({
  471. // url: url, //仅为示例,并非真实的接口地址
  472. // method: 'get',
  473. // success: (res) => {
  474. // let code = -1
  475. // if (typeof res.data.code != 'undefined') {
  476. // code = res.data.code
  477. // }
  478. // this.socketSendMessage('clientSyncAction', {
  479. // sender: 'wx',
  480. // type: 'wx-subscribe-result',
  481. // data: code
  482. // })
  483. // },
  484. // fail: (err) => {
  485. // console.log(err)
  486. // }
  487. // })
  488. },
  489. changeRoomEnableTalk(data) {
  490. console.log(data)
  491. let noMute = getApp().globalData.voiceProps.noMute
  492. getApp().globalData.voiceProps.force = data.enableTalk
  493. // noMute true 静音
  494. // enableTalk false 静音
  495. if (!!data.enableTalk === !!noMute) {
  496. this.mic()
  497. }
  498. },
  499. navigateToGoods({
  500. data
  501. }) {
  502. // wx.showToast({
  503. // title: JSON.stringify(data).substr(40)
  504. // })
  505. this.navigateToGoodsAction(data)
  506. },
  507. navigateToGoodsAction(id) {
  508. wx.navigateTo({
  509. url: '/pages/goods/goods?id=' + id,
  510. })
  511. },
  512. getUrl(url, socketOptions, isJoin) {
  513. url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
  514. if (isJoin) {
  515. url += '&role=' + this.role + '&shopping'
  516. } else {
  517. url += '&role=' + this.role
  518. }
  519. console.error(url)
  520. console.log(isJoin)
  521. return url
  522. },
  523. navigateToMiniProgram(data) {
  524. wx.showModal({
  525. title: '温馨提示',
  526. content: '即将跳到其他小程序,是否继续?',
  527. showCancel: true, //是否显示取消按钮
  528. cancelText: "取消", //默认是“取消”
  529. confirmText: "确定", //默认是“确定”
  530. success: function (res) {
  531. if (res.cancel) {
  532. //点击取消,wx.navigateBack
  533. } else {
  534. wx.navigateToMiniProgram(data.data)
  535. }
  536. },
  537. fail: function (res) {
  538. //接口调用失败的回调函数,wx.navigateBack
  539. },
  540. complete: function (res) {
  541. //接口调用结束的回调函数(调用成功、失败都会执行)
  542. },
  543. })
  544. },
  545. async handleSomeOneInRoom(data) {
  546. if (data && data.user) {
  547. console.log('handleSomeOneInRoom', data)
  548. this.startCall(data)
  549. }
  550. },
  551. async startCall(data) {
  552. //TODO 触发三次
  553. console.log('startCall-data', data)
  554. // if( this.role =='leader'){
  555. this.setData({
  556. shareStatus: 1
  557. })
  558. if (!data) return;
  559. this.setData({
  560. surplus: this.data.peopleCount - data.roomsPerson.length
  561. })
  562. //undefined是未授权,状态为3
  563. let voiceStatus
  564. if (!this.isAuthorizeRecord) {
  565. const unAuth = await this.authorizeRecord();
  566. if (typeof unAuth === 'undefined') {
  567. // debugger
  568. voiceStatus = 3
  569. } else {
  570. voiceStatus = Number(unAuth)
  571. }
  572. }
  573. //限制只有主持人才可以开麦
  574. // if (this.role == 'leader') {
  575. // if (!this.isAuthorizeRecord) {
  576. // const voiceStatus = Number(await this.authorizeRecord())
  577. // this.isAuthorizeRecord = true
  578. // // getApp().setVoiceProps({
  579. // // noMute: !voiceStatus
  580. // // })
  581. // // console.log(getApp().globalData.voiceProps.noMute)
  582. // // this.socketSendMessage('changeVoiceStatus', {
  583. // // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  584. // // })
  585. // // this.data.socketOptions.voiceStatus = 1
  586. // // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
  587. // }
  588. // }
  589. const socketOptions = this.data.socketOptions
  590. getApp().globalData.roomId = socketOptions.roomId
  591. const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
  592. if (!user) {
  593. return
  594. }
  595. //屏蔽有人进来才开麦克风
  596. // if (data.roomsPerson.length <= 1) {
  597. // return
  598. // }
  599. user.noMute = getApp().globalData.voiceProps.noMute
  600. getApp().setVoiceProps({
  601. ...user,
  602. action: 'startCall'
  603. })
  604. // this.socketSendMessage('changeVoiceStatus', {
  605. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  606. // })
  607. // }
  608. },
  609. stopCall() {
  610. console.error('stopCall')
  611. this.setData({
  612. shareStatus: 0
  613. })
  614. getApp().setVoiceProps({
  615. noMute: false,
  616. action: 'stopCall'
  617. })
  618. if (this.runManager) {
  619. // this.recorderManager.stop()
  620. this.runManager = false
  621. }
  622. },
  623. handleSomeOneLeave(data) {
  624. if (data.roomsPerson.length <= 1) {
  625. // this.stopCall()
  626. }
  627. this.setData({
  628. surplus: this.data.peopleCount - data.roomsPerson.length
  629. })
  630. },
  631. //deprecated
  632. async newRoomBk(data) {
  633. if (data.roomId) return;
  634. this.stopCall()
  635. getApp().globalData.rtcParams = []
  636. getApp().globalData.pusher = ''
  637. if (this.data.join && !this.options.join) {
  638. wx.switchTab({
  639. url: '/pages/index/index',
  640. })
  641. return;
  642. }
  643. this.role = this.data.canShow ? 'leader' : 'customer'
  644. let options = await this.getSocketOptions(this.mcode)
  645. this.socketSendMessage('clientSyncAction', {
  646. type: 'newRoom',
  647. data: options
  648. })
  649. setTimeout(async () => {
  650. this.wssSuccess = false
  651. this.socketStop && this.socketStop()
  652. this.data.many = !!this.data.canShow
  653. this.setData({
  654. // peopleCount: this.data.many ? manyCount : 5
  655. peopleCount: manyCount
  656. })
  657. let base = this.base
  658. let socketOptions = await this.socketStart({
  659. options
  660. })
  661. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  662. this.base = base
  663. this.setData({
  664. url,
  665. socketOptions,
  666. })
  667. this.joinUrl()
  668. this.setData({
  669. socketOptions
  670. })
  671. this.loadConponSuccess = true
  672. this.readySendCouponCtrl()
  673. }, 300)
  674. },
  675. // 真正退出房间
  676. async exitRoom() {
  677. const roomId = this.data.socketOptions.roomId;
  678. const role = this.role;
  679. const result = await util.request(api.exitRoom, {
  680. businessId: roomId
  681. }, 'POST', 'application/json');
  682. this.socketSendMessage('stopCall', {
  683. from: 2
  684. })
  685. this.stopCall();
  686. this.socketStop();
  687. if (role === 'leader') {
  688. wx.redirectTo({
  689. url: '/pages/roomManger/roomManger',
  690. });
  691. } else {
  692. wx.switchTab({
  693. url: '/pages/index/index'
  694. });
  695. }
  696. },
  697. async exit() {
  698. // this.stopCall()
  699. getApp().globalData.rtcParams = []
  700. getApp().globalData.pusher = ''
  701. this.socketStop && this.socketStop()
  702. this.role = 'leader'
  703. let base = this.base
  704. let socketOptions = await this.socketStart({
  705. sceneId: this.mcode
  706. })
  707. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  708. this.base = base
  709. wx.nextTick(() => {
  710. setTimeout(() => {
  711. this.setData({
  712. url,
  713. loadUrl: true,
  714. socketOptions,
  715. showCommodityCtrl: false,
  716. hideWebView: false,
  717. reload: true
  718. })
  719. this.joinUrl()
  720. }, 500)
  721. })
  722. },
  723. clearDebuger() {
  724. this.setData({
  725. debugerInfo: ''
  726. })
  727. },
  728. async mic({
  729. data
  730. }) {
  731. if (Number(data.user.isAllowMic) === 1) {
  732. let noMute = getApp().globalData.voiceProps.noMute
  733. // debugger
  734. // noMute true 静音
  735. // enableTalk false 静音
  736. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  737. // return
  738. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  739. if (!this.data.socketOptions.voiceStatus) {
  740. let voiceStatus = await this.authorizeRecord()
  741. if (voiceStatus) {
  742. this.data.socketOptions.voiceStatus = 1
  743. noMute = false
  744. } else {
  745. noMute = true
  746. }
  747. } else {
  748. noMute = !noMute
  749. }
  750. getApp().globalData.voiceProps.noMute = noMute
  751. this.socketSendMessage('changeVoiceStatus', {
  752. status: noMute ? 0 : 2,
  753. user: data.user
  754. })
  755. getApp().setVoiceProps({
  756. noMute
  757. })
  758. wx.showToast({
  759. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  760. })
  761. }
  762. },
  763. closeMic() {
  764. getApp().globalData.voiceProps.noMute = true
  765. this.socketSendMessage('changeVoiceStatus', {
  766. status: 0,
  767. })
  768. getApp().setVoiceProps({
  769. noMute: true
  770. })
  771. },
  772. openMic() {
  773. getApp().globalData.voiceProps.noMute = false
  774. this.socketSendMessage('changeVoiceStatus', {
  775. status: 2,
  776. })
  777. getApp().setVoiceProps({
  778. noMute: false
  779. })
  780. },
  781. callPhone() {
  782. wx.makePhoneCall({
  783. phoneNumber: this.data.contractPhone,
  784. })
  785. this.setData({
  786. showContact: false
  787. })
  788. },
  789. /**
  790. * 用户点击右上角分享
  791. */
  792. onShareAppMessage: function (res) {
  793. let {
  794. id,
  795. newPicUrl
  796. } = this.data
  797. if (res.from === 'button') {
  798. this.setData({
  799. sendShare: false
  800. })
  801. return {
  802. title: '【好友推荐】一起来云逛吧',
  803. imageUrl: newPicUrl,
  804. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  805. }
  806. } else {
  807. return {
  808. imageUrl: newPicUrl,
  809. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  810. }
  811. }
  812. },
  813. /**
  814. * 生命周期函数--监听页面卸载
  815. */
  816. onUnload: function () {
  817. console.log('on onUnload')
  818. // this.socketSendMessage('stopCall', {})
  819. // this.stopCall()
  820. this.socketStop && this.socketStop()
  821. getApp().globalData.pusher = ''
  822. },
  823. cart(data) {
  824. this.setData({
  825. showCommodityCtrl: data.data
  826. })
  827. },
  828. share() {
  829. console.log('**********')
  830. // console.log(!!this.data.mamy)
  831. const companyName = `指房宝(杭州)科技有限公司`
  832. const vrLink = `/pages/webview/index`
  833. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  834. const shareImg = img_url
  835. this.count = this.count || 0
  836. if (this.data.many && this.data.shareStatus == 1) {
  837. //开启一起逛时候的分享
  838. console.log(`/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}`)
  839. console.log(this.data.socketOptions)
  840. wx.navigateTo({
  841. 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}`,
  842. })
  843. } else {
  844. console.log(`/pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}`);
  845. wx.navigateTo({
  846. 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}`,
  847. })
  848. }
  849. },
  850. back(data) {
  851. if (data.sender !== 'h5') return;
  852. wx.switchTab({
  853. url: '/pages/index/index'
  854. })
  855. this.setData({
  856. showCommodityCtrl: false
  857. })
  858. },
  859. service() {
  860. this.setData({
  861. showContact: true,
  862. showCommodity: false,
  863. showCoupon: false
  864. })
  865. },
  866. invite(data) {
  867. if (data.sender !== 'h5') return;
  868. this.setData({
  869. sendShare: true,
  870. count: ++this.data.count
  871. })
  872. },
  873. coupon(data) {
  874. if (data.sender !== 'h5') return;
  875. this.setData({
  876. showContact: false,
  877. showCommodity: false,
  878. showCoupon: true
  879. })
  880. },
  881. liveGotoGood(ev) {
  882. let id = ev.currentTarget.dataset.item.goodsId
  883. wx.navigateTo({
  884. url: '/pages/goods/goods?id=' + id,
  885. })
  886. },
  887. gotoGoodsDOM(event) {
  888. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  889. },
  890. gotoGoodsSocket(data) {
  891. this.gotoGoods(data.data)
  892. },
  893. gotoGoods(id) {
  894. console.log('---', id)
  895. this.socketSendMessage('clientSyncAction', {
  896. type: 'openTag',
  897. data: id
  898. })
  899. this.setData({
  900. showCommodity: false
  901. })
  902. this.joinUrl()
  903. },
  904. addCard(event) {
  905. wx.navigateTo({
  906. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  907. })
  908. },
  909. buyGoods(event) {
  910. wx.navigateTo({
  911. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  912. })
  913. },
  914. showCommodityFn() {
  915. this.setData({
  916. showCommodity: true,
  917. showContact: false,
  918. showCoupon: false
  919. })
  920. this.joinUrl()
  921. },
  922. hideComodity() {
  923. this.setData({
  924. showCommodity: false
  925. })
  926. this.joinUrl()
  927. },
  928. hideCoupon() {
  929. this.setData({
  930. showCoupon: !this.data.showCoupon
  931. })
  932. },
  933. async receive(ev) {
  934. let item = ev.target.dataset.item
  935. try {
  936. // wx.showToast({
  937. // title: '领取优惠卷',
  938. // })
  939. // return;
  940. if (item.hasReceived || item.number <= item.receiveNumber) return;
  941. let res = await util.request(api.CouponExchange, {
  942. couponId: item.id
  943. })
  944. if (res.code === 0) {
  945. wx.showToast({
  946. title: '已成功领取',
  947. success: () => {
  948. this.setData({
  949. showCoupon: false
  950. })
  951. wx.nextTick(() => {
  952. this.setData({
  953. coupons: this.data.coupons.map(citem => {
  954. return {
  955. ...citem,
  956. hasReceived: citem.id === item.id ? true : citem.hasReceived
  957. }
  958. }),
  959. showCoupon: true
  960. })
  961. })
  962. }
  963. })
  964. } else if (res.errno === 401) {
  965. getApp().setLoginProps(false)
  966. } else {
  967. wx.showToast({
  968. title: res.msg,
  969. })
  970. }
  971. } catch (e) {
  972. console.error(e)
  973. wx.showToast({
  974. icon: 'none',
  975. title: '领取失败',
  976. })
  977. }
  978. },
  979. async getCouponList(id) {
  980. const success = (res) => {
  981. this.setData({
  982. coupons: res.data.list.map(item => {
  983. item.typeMoney = item.typeMoney.toString()
  984. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  985. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  986. return item
  987. })
  988. })
  989. this.loadConponSuccess = true
  990. this.readySendCouponCtrl()
  991. }
  992. let res = await util.request(api.BrandCouponList, {
  993. brandId: id,
  994. pageNum: 1,
  995. pageSize: 10000
  996. }, 'GET')
  997. console.log(res)
  998. if (res.code === 0) {
  999. success(res)
  1000. } else {
  1001. let res = await util.request(api.UNBrandCouponList, {
  1002. brandId: id,
  1003. pageNum: 1,
  1004. pageSize: 10000
  1005. }, 'GET')
  1006. success(res)
  1007. }
  1008. },
  1009. ready() {
  1010. this.wssSuccess = true
  1011. this.readySendCouponCtrl()
  1012. },
  1013. readySendCouponCtrl() {
  1014. if (this.wssSuccess && this.loadConponSuccess) {
  1015. this.loadConponSuccess = false
  1016. this.socketSendMessage('clientSyncAction', {
  1017. type: 'showCoupon',
  1018. data: this.data.coupons.length > 0
  1019. })
  1020. }
  1021. },
  1022. getBrand: function (id, code) {
  1023. this.getGoodsCount(code, id)
  1024. return;
  1025. },
  1026. getGoodsCount(code, id) {
  1027. util.request(api.GoodsNumCount, {
  1028. isDelete: 0,
  1029. isOnSale: 1,
  1030. brandId: id
  1031. }, 'GET')
  1032. .then(res => {
  1033. if (res.errno === 0) {
  1034. this.setData({
  1035. goodsCount: res.data
  1036. })
  1037. }
  1038. this.getCouponList(id)
  1039. })
  1040. },
  1041. getGoodsList(id, category_id) {
  1042. var that = this;
  1043. if (!(this.data.navList && this.data.navList.length)) {
  1044. that.navDatas = {}
  1045. let navDatas = this.data.navList = this.data.comtypes
  1046. // util.request(api.GoodsCategory, { id: category_id })
  1047. // .then(function (res) {
  1048. // if (res.errno == 0) {
  1049. // let navDatas = res.data.brotherCategory
  1050. // that.setData({
  1051. // navList: navDatas,
  1052. // currTypeId: category_id
  1053. // });
  1054. that.navDatas = {}
  1055. navDatas.forEach(item => {
  1056. util.request(api.GoodsList, {
  1057. brandId: id,
  1058. categoryId: item.category_id,
  1059. page: that.data.page,
  1060. size: that.data.size
  1061. })
  1062. .then(res => {
  1063. if (res.errno === 0) {
  1064. that.navDatas[item.category_id] = res.data.goodsList
  1065. }
  1066. })
  1067. })
  1068. // }
  1069. // })
  1070. }
  1071. if (that.navDatas[category_id]) {
  1072. if (!isIos) {
  1073. let showCommodity = that.data.showCommodity
  1074. that.setData({
  1075. showCommodity: false
  1076. })
  1077. setTimeout(() => {
  1078. wx.nextTick(() => {
  1079. that.setData({
  1080. goodsList: that.navDatas[category_id],
  1081. currTypeId: category_id,
  1082. showCommodity: showCommodity
  1083. });
  1084. })
  1085. }, 500)
  1086. } else {
  1087. that.setData({
  1088. goodsList: that.navDatas[category_id],
  1089. currTypeId: category_id,
  1090. });
  1091. }
  1092. } else {
  1093. console.error('诱惑去啦')
  1094. util.request(api.GoodsList, {
  1095. brandId: id,
  1096. categoryId: category_id,
  1097. page: that.data.page,
  1098. size: that.data.size
  1099. })
  1100. .then(function (res) {
  1101. if (res.errno === 0) {
  1102. that.setData({
  1103. goodsList: res.data.goodsList,
  1104. currTypeId: category_id
  1105. });
  1106. // this.data.navList
  1107. }
  1108. });
  1109. }
  1110. },
  1111. getBrandDetail: function (id, type, cb) {
  1112. util.request(api.BrandDetail, {
  1113. id: id,
  1114. type: type,
  1115. }).then((res) => {
  1116. let base = res.data.brand.sceneUrl
  1117. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  1118. if (res.errno === 0) {
  1119. let url = base + "&sid=" + id
  1120. this.setData({
  1121. id: id,
  1122. newPicUrl: res.data.brand.appListPicUrl,
  1123. sceneNum: res.data.brand.sceneNum,
  1124. canShow: res.data.brand.canShow,
  1125. contractPhone: res.data.brand.contractPhone
  1126. })
  1127. if (this.data.many === void 0) {
  1128. this.data.many = !!res.data.brand.canShow
  1129. }
  1130. this.setData({
  1131. // peopleCount: this.data.many ? manyCount : 5,
  1132. peopleCount: manyCount
  1133. })
  1134. if (!res.data.brand.canShow) {
  1135. this.role = 'customer'
  1136. } else if (!this.options.join) {
  1137. this.role = 'leader'
  1138. }
  1139. cb(url, urlToJson(url).m, )
  1140. }
  1141. });
  1142. },
  1143. selectType(ev) {
  1144. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  1145. },
  1146. hideCS() {
  1147. this.setData({
  1148. showCommodity: false,
  1149. showCoupon: false,
  1150. showContact: false
  1151. })
  1152. },
  1153. hideContact() {
  1154. this.setData({
  1155. showContact: false
  1156. })
  1157. },
  1158. calcShare() {
  1159. // this.exit()
  1160. this.setData({
  1161. sendShare: false
  1162. })
  1163. },
  1164. contactKf() {
  1165. let keys = Object.keys(this.navDatas)
  1166. let goodsId = this.navDatas[keys[0]][0].id
  1167. let user = wx.getStorageSync('userinfoDetail')
  1168. util.request(api.AddTalkCount, {
  1169. goodsId,
  1170. viewId: user && user.userId || '',
  1171. sceneNum: this.data.sceneNum
  1172. }, 'get')
  1173. this.hideAlert && this.hideAlert()
  1174. this.hideContact && this.hideContact()
  1175. },
  1176. onHide() {
  1177. this.socketSendMessage('changeOnlineStatus', {
  1178. status: 0
  1179. })
  1180. this.pauseVideo = true
  1181. this.joinUrl()
  1182. }
  1183. }