socket.js 31 KB

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