socket.js 28 KB

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