socket.js 28 KB

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