socket.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. userLimitNum: capacities
  314. }
  315. },
  316. async socketStart({
  317. sceneId,
  318. roomId,
  319. options
  320. }) {
  321. if (!options) {
  322. options = await this.getSocketOptions(sceneId, roomId)
  323. }
  324. console.log('小程序参数', options)
  325. if (!options.roomId) {
  326. return
  327. }
  328. let socket = io(remote.socketHost, {
  329. path: '/fsl-node',
  330. transport: ['websocket'],
  331. query: {
  332. ...options,
  333. isClient: true,
  334. from: 2
  335. }
  336. })
  337. console.error('新建socket Room', options.roomId)
  338. this.setData({
  339. socketStatus: 0
  340. })
  341. socket.on('connect', () => this.setData({
  342. socketStatus: 1
  343. }))
  344. socket.on('connect_error', () => this.setData({
  345. socketStatus: -1
  346. }))
  347. socket.on('connect_timeout', () => this.setData({
  348. socketStatus: -1
  349. }))
  350. socket.on('disconnect', () => this.setData({
  351. socketStatus: -1
  352. }))
  353. socket.on('reconnect', () => {
  354. // wx.showToast({
  355. // title: '重连',
  356. // })
  357. this.setData({
  358. socketStatus: this.data.socketStatus
  359. })
  360. let noMute = getApp().globalData.voiceProps.noMute
  361. this.socketSendMessage('changeVoiceStatus', {
  362. status: noMute ? 0 : 2
  363. })
  364. this.socketSendMessage('changeOnlineStatus', {
  365. status: 1
  366. })
  367. })
  368. socket.on('reconnect_failed', () => this.setData({
  369. socketStatus: -1
  370. }))
  371. socket.on('error', () => this.setData({
  372. socketStatus: -1
  373. }))
  374. socket.on('roomIn', config => {
  375. let enableTalk = config.roomsConfig.enableTalk !== false
  376. let noMute = getApp().globalData.voiceProps.noMute
  377. getApp().globalData.voiceProps.force = enableTalk
  378. if (!enableTalk && !noMute) {
  379. if (this.role !== 'leader') {
  380. // this.mic()
  381. }
  382. }
  383. })
  384. this.socketSendMessage = (event, obj) => {
  385. console.error('发送 socket Room', options.roomId, event, obj)
  386. socket.emit(event, obj)
  387. }
  388. socket.on('clientSyncAction', (data) => {
  389. console.log('调用', data.type, '方法', data)
  390. if (this[data.type]) {
  391. this[data.type](data)
  392. } else if (data.type == 'wx-subscribe') {
  393. this.getUrlCode(data.data)
  394. } else {
  395. console.error('没有', data.type, '方法')
  396. }
  397. })
  398. socket.on('action', (data) => {
  399. if (data.type === 'navigateToGoods') {
  400. this.navigateToGoodsAction(data.data)
  401. }
  402. })
  403. socket.on('changeRoomEnableTalk', config => {
  404. if (this.role !== 'leader') {
  405. this.changeRoomEnableTalk(config)
  406. }
  407. })
  408. socket.on('startCall', this.startCall.bind(this))
  409. socket.on('stopCall', (data) => {
  410. console.log('on stopCall')
  411. this.stopCall(data)
  412. })
  413. this.handleSomeOneInRoom = this.handleSomeOneInRoom.bind(this)
  414. // socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  415. socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  416. socket.on('someOneLeaveRoom', (user, data) => {
  417. this.handleSomeOneLeave(user)
  418. })
  419. socket.on('roomClose', (data) => {
  420. console.log('on roomClose')
  421. this.stopCall(data)
  422. })
  423. socket.on('autoReJoin', (data) => {
  424. console.log('on autoReJoin')
  425. if ('roomId' in data) {
  426. options.roomId = Number(data.roomId)
  427. }
  428. })
  429. socket.on("beKicked", data => {
  430. if (data.userId && data.roomId) {
  431. const socketOptions = this.data.socketOptions
  432. const userId = data.userId
  433. const roomId = data.roomId
  434. // debugger
  435. if (socketOptions.userId == userId && this.options.roomId == roomId) {
  436. wx.showToast({
  437. title: '您已被踢出房间!',
  438. icon: 'none',
  439. complete: () => {
  440. setTimeout(() => {
  441. this.socketStop();
  442. wx.redirectTo({
  443. url: '/pages/roomManger/roomManger',
  444. });
  445. }, 1000)
  446. }
  447. })
  448. }
  449. }
  450. });
  451. this.socketStop = () => {
  452. if (socket) {
  453. socket.close()
  454. console.error('断开 并滞空 socket Room', options.roomId)
  455. this.setData({
  456. socketStatus: 2
  457. })
  458. socket = null
  459. }
  460. }
  461. return options
  462. },
  463. getUrlCode(url) {
  464. this.socketSendMessage('clientSyncAction', {
  465. sender: 'wx',
  466. type: 'wx-subscribe-result',
  467. data: 3020
  468. })
  469. // wx.request({
  470. // url: url, //仅为示例,并非真实的接口地址
  471. // method: 'get',
  472. // success: (res) => {
  473. // let code = -1
  474. // if (typeof res.data.code != 'undefined') {
  475. // code = res.data.code
  476. // }
  477. // this.socketSendMessage('clientSyncAction', {
  478. // sender: 'wx',
  479. // type: 'wx-subscribe-result',
  480. // data: code
  481. // })
  482. // },
  483. // fail: (err) => {
  484. // console.log(err)
  485. // }
  486. // })
  487. },
  488. changeRoomEnableTalk(data) {
  489. console.log(data)
  490. let noMute = getApp().globalData.voiceProps.noMute
  491. getApp().globalData.voiceProps.force = data.enableTalk
  492. // noMute true 静音
  493. // enableTalk false 静音
  494. if (!!data.enableTalk === !!noMute) {
  495. this.mic()
  496. }
  497. },
  498. navigateToGoods({
  499. data
  500. }) {
  501. // wx.showToast({
  502. // title: JSON.stringify(data).substr(40)
  503. // })
  504. this.navigateToGoodsAction(data)
  505. },
  506. navigateToGoodsAction(id) {
  507. wx.navigateTo({
  508. url: '/pages/goods/goods?id=' + id,
  509. })
  510. },
  511. getUrl(url, socketOptions, isJoin) {
  512. url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
  513. if (isJoin) {
  514. url += '&role=' + this.role + '&shopping'
  515. } else {
  516. url += '&role=' + this.role
  517. }
  518. console.error(url)
  519. console.log(isJoin)
  520. return url
  521. },
  522. navigateToMiniProgram(data) {
  523. wx.showModal({
  524. title: '温馨提示',
  525. content: '即将跳到其他小程序,是否继续?',
  526. showCancel: true, //是否显示取消按钮
  527. cancelText: "取消", //默认是“取消”
  528. confirmText: "确定", //默认是“确定”
  529. success: function (res) {
  530. if (res.cancel) {
  531. //点击取消,wx.navigateBack
  532. } else {
  533. wx.navigateToMiniProgram(data.data)
  534. }
  535. },
  536. fail: function (res) {
  537. //接口调用失败的回调函数,wx.navigateBack
  538. },
  539. complete: function (res) {
  540. //接口调用结束的回调函数(调用成功、失败都会执行)
  541. },
  542. })
  543. },
  544. async handleSomeOneInRoom(data) {
  545. if (data && data.user) {
  546. console.log('handleSomeOneInRoom', data)
  547. this.startCall(data)
  548. }
  549. },
  550. async startCall(data) {
  551. //TODO 触发三次
  552. console.log('startCall-data', data)
  553. // if( this.role =='leader'){
  554. this.setData({
  555. shareStatus: 1
  556. })
  557. if (!data) return;
  558. this.setData({
  559. surplus: this.data.peopleCount - data.roomsPerson.length
  560. })
  561. //undefined是未授权,状态为3
  562. let voiceStatus
  563. if (!this.isAuthorizeRecord) {
  564. const unAuth = await this.authorizeRecord();
  565. if (typeof unAuth === 'undefined') {
  566. // debugger
  567. voiceStatus = 3
  568. } else {
  569. voiceStatus = Number(unAuth)
  570. }
  571. }
  572. //限制只有主持人才可以开麦
  573. // if (this.role == 'leader') {
  574. // if (!this.isAuthorizeRecord) {
  575. // const voiceStatus = Number(await this.authorizeRecord())
  576. // this.isAuthorizeRecord = true
  577. // // getApp().setVoiceProps({
  578. // // noMute: !voiceStatus
  579. // // })
  580. // // console.log(getApp().globalData.voiceProps.noMute)
  581. // // this.socketSendMessage('changeVoiceStatus', {
  582. // // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  583. // // })
  584. // // this.data.socketOptions.voiceStatus = 1
  585. // // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
  586. // }
  587. // }
  588. const socketOptions = this.data.socketOptions
  589. getApp().globalData.roomId = socketOptions.roomId
  590. const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
  591. if (!user) {
  592. return
  593. }
  594. //屏蔽有人进来才开麦克风
  595. // if (data.roomsPerson.length <= 1) {
  596. // return
  597. // }
  598. user.noMute = getApp().globalData.voiceProps.noMute
  599. getApp().setVoiceProps({
  600. ...user,
  601. action: 'startCall'
  602. })
  603. // this.socketSendMessage('changeVoiceStatus', {
  604. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  605. // })
  606. // }
  607. },
  608. stopCall() {
  609. console.error('stopCall')
  610. this.setData({
  611. shareStatus: 0
  612. })
  613. getApp().setVoiceProps({
  614. noMute: false,
  615. action: 'stopCall'
  616. })
  617. if (this.runManager) {
  618. // this.recorderManager.stop()
  619. this.runManager = false
  620. }
  621. },
  622. handleSomeOneLeave(data) {
  623. if (data.roomsPerson.length <= 1) {
  624. // this.stopCall()
  625. }
  626. this.setData({
  627. surplus: this.data.peopleCount - data.roomsPerson.length
  628. })
  629. },
  630. //deprecated
  631. async newRoom(data) {
  632. if (data.roomId) return;
  633. this.stopCall()
  634. getApp().globalData.rtcParams = []
  635. getApp().globalData.pusher = ''
  636. if (this.data.join && !this.options.join) {
  637. wx.switchTab({
  638. url: '/pages/index/index',
  639. })
  640. return;
  641. }
  642. this.role = this.data.canShow ? 'leader' : 'customer'
  643. let options = await this.getSocketOptions(this.mcode)
  644. this.socketSendMessage('clientSyncAction', {
  645. type: 'newRoom',
  646. data: options
  647. })
  648. setTimeout(async () => {
  649. this.wssSuccess = false
  650. this.socketStop && this.socketStop()
  651. this.data.many = !!this.data.canShow
  652. this.setData({
  653. // peopleCount: this.data.many ? manyCount : 5
  654. peopleCount: manyCount
  655. })
  656. let base = this.base
  657. let socketOptions = await this.socketStart({
  658. options
  659. })
  660. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  661. this.base = base
  662. this.setData({
  663. url,
  664. socketOptions,
  665. })
  666. this.joinUrl()
  667. this.setData({
  668. socketOptions
  669. })
  670. this.loadConponSuccess = true
  671. this.readySendCouponCtrl()
  672. }, 300)
  673. },
  674. // 真正退出房间
  675. async exitRoom() {
  676. const roomId = this.data.socketOptions.roomId;
  677. const result = await util.request(api.exitRoom, {
  678. businessId: roomId
  679. }, 'POST', 'application/json');
  680. this.socketSendMessage('stopCall', {
  681. from: 2
  682. })
  683. this.stopCall();
  684. this.socketStop();
  685. wx.redirectTo({
  686. url: '/pages/roomManger/roomManger',
  687. });
  688. },
  689. async exit() {
  690. // this.stopCall()
  691. getApp().globalData.rtcParams = []
  692. getApp().globalData.pusher = ''
  693. this.socketStop && this.socketStop()
  694. this.role = 'leader'
  695. let base = this.base
  696. let socketOptions = await this.socketStart({
  697. sceneId: this.mcode
  698. })
  699. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  700. this.base = base
  701. wx.nextTick(() => {
  702. setTimeout(() => {
  703. this.setData({
  704. url,
  705. loadUrl: true,
  706. socketOptions,
  707. showCommodityCtrl: false,
  708. hideWebView: false,
  709. reload: true
  710. })
  711. this.joinUrl()
  712. }, 500)
  713. })
  714. },
  715. clearDebuger() {
  716. this.setData({
  717. debugerInfo: ''
  718. })
  719. },
  720. async mic({
  721. data
  722. }) {
  723. if (Number(data.user.isAllowMic) === 1) {
  724. let noMute = getApp().globalData.voiceProps.noMute
  725. // debugger
  726. // noMute true 静音
  727. // enableTalk false 静音
  728. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  729. // return
  730. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  731. if (!this.data.socketOptions.voiceStatus) {
  732. let voiceStatus = await this.authorizeRecord()
  733. if (voiceStatus) {
  734. this.data.socketOptions.voiceStatus = 1
  735. noMute = false
  736. } else {
  737. noMute = true
  738. }
  739. } else {
  740. noMute = !noMute
  741. }
  742. getApp().globalData.voiceProps.noMute = noMute
  743. this.socketSendMessage('changeVoiceStatus', {
  744. status: noMute ? 0 : 2,
  745. user: data.user
  746. })
  747. getApp().setVoiceProps({
  748. noMute
  749. })
  750. wx.showToast({
  751. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  752. })
  753. }
  754. },
  755. closeMic(data) {
  756. getApp().globalData.voiceProps.noMute = false
  757. this.socketSendMessage('changeVoiceStatus', {
  758. status: 0,
  759. user: data.user
  760. })
  761. getApp().setVoiceProps({
  762. noMute: false
  763. })
  764. },
  765. openMic() {
  766. getApp().globalData.voiceProps.noMute = true
  767. this.socketSendMessage('changeVoiceStatus', {
  768. status: 2,
  769. user: data.user
  770. })
  771. getApp().setVoiceProps({
  772. noMute: true
  773. })
  774. },
  775. callPhone() {
  776. wx.makePhoneCall({
  777. phoneNumber: this.data.contractPhone,
  778. })
  779. this.setData({
  780. showContact: false
  781. })
  782. },
  783. /**
  784. * 用户点击右上角分享
  785. */
  786. onShareAppMessage: function (res) {
  787. let {
  788. id,
  789. newPicUrl
  790. } = this.data
  791. if (res.from === 'button') {
  792. this.setData({
  793. sendShare: false
  794. })
  795. return {
  796. title: '【好友推荐】一起来云逛吧',
  797. imageUrl: newPicUrl,
  798. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  799. }
  800. } else {
  801. return {
  802. imageUrl: newPicUrl,
  803. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  804. }
  805. }
  806. },
  807. /**
  808. * 生命周期函数--监听页面卸载
  809. */
  810. onUnload: function () {
  811. console.log('on onUnload')
  812. // this.socketSendMessage('stopCall', {})
  813. // this.stopCall()
  814. this.socketStop()
  815. getApp().globalData.pusher = ''
  816. },
  817. cart(data) {
  818. this.setData({
  819. showCommodityCtrl: data.data
  820. })
  821. },
  822. share() {
  823. console.log('**********')
  824. // console.log(!!this.data.mamy)
  825. const companyName = `指房宝(杭州)科技有限公司`
  826. const vrLink = `/pages/webview/index`
  827. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  828. const shareImg = img_url
  829. this.count = this.count || 0
  830. if (this.data.many && this.data.shareStatus == 1) {
  831. //开启一起逛时候的分享
  832. 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}`)
  833. console.log(this.data.socketOptions)
  834. wx.navigateTo({
  835. 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}`,
  836. })
  837. } else {
  838. 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}`);
  839. wx.navigateTo({
  840. 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}`,
  841. })
  842. }
  843. },
  844. back(data) {
  845. if (data.sender !== 'h5') return;
  846. wx.switchTab({
  847. url: '/pages/index/index'
  848. })
  849. this.setData({
  850. showCommodityCtrl: false
  851. })
  852. },
  853. service() {
  854. this.setData({
  855. showContact: true,
  856. showCommodity: false,
  857. showCoupon: false
  858. })
  859. },
  860. invite(data) {
  861. if (data.sender !== 'h5') return;
  862. this.setData({
  863. sendShare: true,
  864. count: ++this.data.count
  865. })
  866. },
  867. coupon(data) {
  868. if (data.sender !== 'h5') return;
  869. this.setData({
  870. showContact: false,
  871. showCommodity: false,
  872. showCoupon: true
  873. })
  874. },
  875. liveGotoGood(ev) {
  876. let id = ev.currentTarget.dataset.item.goodsId
  877. wx.navigateTo({
  878. url: '/pages/goods/goods?id=' + id,
  879. })
  880. },
  881. gotoGoodsDOM(event) {
  882. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  883. },
  884. gotoGoodsSocket(data) {
  885. this.gotoGoods(data.data)
  886. },
  887. gotoGoods(id) {
  888. console.log('---', id)
  889. this.socketSendMessage('clientSyncAction', {
  890. type: 'openTag',
  891. data: id
  892. })
  893. this.setData({
  894. showCommodity: false
  895. })
  896. this.joinUrl()
  897. },
  898. addCard(event) {
  899. wx.navigateTo({
  900. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  901. })
  902. },
  903. buyGoods(event) {
  904. wx.navigateTo({
  905. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  906. })
  907. },
  908. showCommodityFn() {
  909. this.setData({
  910. showCommodity: true,
  911. showContact: false,
  912. showCoupon: false
  913. })
  914. this.joinUrl()
  915. },
  916. hideComodity() {
  917. this.setData({
  918. showCommodity: false
  919. })
  920. this.joinUrl()
  921. },
  922. hideCoupon() {
  923. this.setData({
  924. showCoupon: !this.data.showCoupon
  925. })
  926. },
  927. async receive(ev) {
  928. let item = ev.target.dataset.item
  929. try {
  930. // wx.showToast({
  931. // title: '领取优惠卷',
  932. // })
  933. // return;
  934. if (item.hasReceived || item.number <= item.receiveNumber) return;
  935. let res = await util.request(api.CouponExchange, {
  936. couponId: item.id
  937. })
  938. if (res.code === 0) {
  939. wx.showToast({
  940. title: '已成功领取',
  941. success: () => {
  942. this.setData({
  943. showCoupon: false
  944. })
  945. wx.nextTick(() => {
  946. this.setData({
  947. coupons: this.data.coupons.map(citem => {
  948. return {
  949. ...citem,
  950. hasReceived: citem.id === item.id ? true : citem.hasReceived
  951. }
  952. }),
  953. showCoupon: true
  954. })
  955. })
  956. }
  957. })
  958. } else if (res.errno === 401) {
  959. getApp().setLoginProps(false)
  960. } else {
  961. wx.showToast({
  962. title: res.msg,
  963. })
  964. }
  965. } catch (e) {
  966. console.error(e)
  967. wx.showToast({
  968. icon: 'none',
  969. title: '领取失败',
  970. })
  971. }
  972. },
  973. async getCouponList(id) {
  974. const success = (res) => {
  975. this.setData({
  976. coupons: res.data.list.map(item => {
  977. item.typeMoney = item.typeMoney.toString()
  978. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  979. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  980. return item
  981. })
  982. })
  983. this.loadConponSuccess = true
  984. this.readySendCouponCtrl()
  985. }
  986. let res = await util.request(api.BrandCouponList, {
  987. brandId: id,
  988. pageNum: 1,
  989. pageSize: 10000
  990. }, 'GET')
  991. console.log(res)
  992. if (res.code === 0) {
  993. success(res)
  994. } else {
  995. let res = await util.request(api.UNBrandCouponList, {
  996. brandId: id,
  997. pageNum: 1,
  998. pageSize: 10000
  999. }, 'GET')
  1000. success(res)
  1001. }
  1002. },
  1003. ready() {
  1004. this.wssSuccess = true
  1005. this.readySendCouponCtrl()
  1006. },
  1007. readySendCouponCtrl() {
  1008. if (this.wssSuccess && this.loadConponSuccess) {
  1009. this.loadConponSuccess = false
  1010. this.socketSendMessage('clientSyncAction', {
  1011. type: 'showCoupon',
  1012. data: this.data.coupons.length > 0
  1013. })
  1014. }
  1015. },
  1016. getBrand: function (id, code) {
  1017. this.getGoodsCount(code, id)
  1018. return;
  1019. },
  1020. getGoodsCount(code, id) {
  1021. util.request(api.GoodsNumCount, {
  1022. isDelete: 0,
  1023. isOnSale: 1,
  1024. brandId: id
  1025. }, 'GET')
  1026. .then(res => {
  1027. if (res.errno === 0) {
  1028. this.setData({
  1029. goodsCount: res.data
  1030. })
  1031. }
  1032. this.getCouponList(id)
  1033. })
  1034. },
  1035. getGoodsList(id, category_id) {
  1036. var that = this;
  1037. if (!(this.data.navList && this.data.navList.length)) {
  1038. that.navDatas = {}
  1039. let navDatas = this.data.navList = this.data.comtypes
  1040. // util.request(api.GoodsCategory, { id: category_id })
  1041. // .then(function (res) {
  1042. // if (res.errno == 0) {
  1043. // let navDatas = res.data.brotherCategory
  1044. // that.setData({
  1045. // navList: navDatas,
  1046. // currTypeId: category_id
  1047. // });
  1048. that.navDatas = {}
  1049. navDatas.forEach(item => {
  1050. util.request(api.GoodsList, {
  1051. brandId: id,
  1052. categoryId: item.category_id,
  1053. page: that.data.page,
  1054. size: that.data.size
  1055. })
  1056. .then(res => {
  1057. if (res.errno === 0) {
  1058. that.navDatas[item.category_id] = res.data.goodsList
  1059. }
  1060. })
  1061. })
  1062. // }
  1063. // })
  1064. }
  1065. if (that.navDatas[category_id]) {
  1066. if (!isIos) {
  1067. let showCommodity = that.data.showCommodity
  1068. that.setData({
  1069. showCommodity: false
  1070. })
  1071. setTimeout(() => {
  1072. wx.nextTick(() => {
  1073. that.setData({
  1074. goodsList: that.navDatas[category_id],
  1075. currTypeId: category_id,
  1076. showCommodity: showCommodity
  1077. });
  1078. })
  1079. }, 500)
  1080. } else {
  1081. that.setData({
  1082. goodsList: that.navDatas[category_id],
  1083. currTypeId: category_id,
  1084. });
  1085. }
  1086. } else {
  1087. console.error('诱惑去啦')
  1088. util.request(api.GoodsList, {
  1089. brandId: id,
  1090. categoryId: category_id,
  1091. page: that.data.page,
  1092. size: that.data.size
  1093. })
  1094. .then(function (res) {
  1095. if (res.errno === 0) {
  1096. that.setData({
  1097. goodsList: res.data.goodsList,
  1098. currTypeId: category_id
  1099. });
  1100. // this.data.navList
  1101. }
  1102. });
  1103. }
  1104. },
  1105. getBrandDetail: function (id, type, cb) {
  1106. util.request(api.BrandDetail, {
  1107. id: id,
  1108. type: type,
  1109. }).then((res) => {
  1110. let base = res.data.brand.sceneUrl
  1111. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  1112. if (res.errno === 0) {
  1113. let url = base + "&sid=" + id
  1114. this.setData({
  1115. id: id,
  1116. newPicUrl: res.data.brand.appListPicUrl,
  1117. sceneNum: res.data.brand.sceneNum,
  1118. canShow: res.data.brand.canShow,
  1119. contractPhone: res.data.brand.contractPhone
  1120. })
  1121. if (this.data.many === void 0) {
  1122. this.data.many = !!res.data.brand.canShow
  1123. }
  1124. this.setData({
  1125. // peopleCount: this.data.many ? manyCount : 5,
  1126. peopleCount: manyCount
  1127. })
  1128. if (!res.data.brand.canShow) {
  1129. this.role = 'customer'
  1130. } else if (!this.options.join) {
  1131. this.role = 'leader'
  1132. }
  1133. cb(url, urlToJson(url).m, )
  1134. }
  1135. });
  1136. },
  1137. selectType(ev) {
  1138. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  1139. },
  1140. hideCS() {
  1141. this.setData({
  1142. showCommodity: false,
  1143. showCoupon: false,
  1144. showContact: false
  1145. })
  1146. },
  1147. hideContact() {
  1148. this.setData({
  1149. showContact: false
  1150. })
  1151. },
  1152. calcShare() {
  1153. // this.exit()
  1154. this.setData({
  1155. sendShare: false
  1156. })
  1157. },
  1158. contactKf() {
  1159. let keys = Object.keys(this.navDatas)
  1160. let goodsId = this.navDatas[keys[0]][0].id
  1161. let user = wx.getStorageSync('userinfoDetail')
  1162. util.request(api.AddTalkCount, {
  1163. goodsId,
  1164. viewId: user && user.userId || '',
  1165. sceneNum: this.data.sceneNum
  1166. }, 'get')
  1167. this.hideAlert && this.hideAlert()
  1168. this.hideContact && this.hideContact()
  1169. },
  1170. onHide() {
  1171. this.socketSendMessage('changeOnlineStatus', {
  1172. status: 0
  1173. })
  1174. this.pauseVideo = true
  1175. this.joinUrl()
  1176. }
  1177. }