socket.js 23 KB

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