socket.js 23 KB

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