socket.js 24 KB

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