socket.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. // 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 ||'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png',
  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', () => {
  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
  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. console.error('-----aaaaa-----', getApp().globalData.voiceProps.noMute)
  372. debugger
  373. this.socketSendMessage('changeVoiceStatus', {
  374. status: getApp().globalData.voiceProps.noMute ? 0 : 2
  375. })
  376. },
  377. stopCall() {
  378. console.error('stopCall')
  379. getApp().setVoiceProps({
  380. noMute: false,
  381. action: 'stopCall'
  382. })
  383. if (this.runManager) {
  384. // this.recorderManager.stop()
  385. this.runManager = false
  386. }
  387. },
  388. handleSomeOneLeave(data) {
  389. if (data.roomsPerson.length <= 1) {
  390. // this.stopCall()
  391. }
  392. },
  393. async newRoom(data) {
  394. if (data.roomId) return;
  395. this.stopCall()
  396. getApp().globalData.rtcParams = []
  397. getApp().globalData.pusher = ''
  398. this.role = 'leader'
  399. let options = await this.getSocketOptions(this.mcode)
  400. console.error('发送H5 newRoom', options)
  401. this.socketSendMessage('clientSyncAction', {
  402. type: 'newRoom',
  403. data: options
  404. })
  405. setTimeout(async () => {
  406. this.wssSuccess = false
  407. this.socketStop && this.socketStop()
  408. let base = this.base
  409. let socketOptions = await this.socketStart({
  410. options
  411. })
  412. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  413. this.base = base
  414. this.setData({
  415. url,
  416. socketOptions,
  417. })
  418. this.joinUrl()
  419. this.setData({ socketOptions })
  420. this.loadConponSuccess = true
  421. this.readySendCouponCtrl()
  422. }, 300)
  423. },
  424. async exit() {
  425. this.stopCall()
  426. getApp().globalData.rtcParams = []
  427. getApp().globalData.pusher = ''
  428. this.socketStop && this.socketStop()
  429. this.role = 'leader'
  430. let base = this.base
  431. let socketOptions = await this.socketStart({
  432. sceneId: this.mcode
  433. })
  434. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  435. this.base = base
  436. wx.nextTick(() => {
  437. setTimeout(() => {
  438. this.setData({
  439. url,
  440. loadUrl: true,
  441. socketOptions,
  442. showCommodityCtrl: false,
  443. hideWebView: false,
  444. reload: true
  445. })
  446. this.joinUrl()
  447. }, 500)
  448. })
  449. },
  450. async mic() {
  451. let noMute = getApp().globalData.voiceProps.noMute
  452. if (!this.data.socketOptions.voiceStatus) {
  453. let voiceStatus = await this.authorizeRecord()
  454. if (voiceStatus) {
  455. this.data.socketOptions.voiceStatus = 1
  456. noMute = false
  457. } else {
  458. noMute = true
  459. }
  460. } else {
  461. noMute = !noMute
  462. }
  463. getApp().globalData.voiceProps.noMute = noMute
  464. this.socketSendMessage('changeVoiceStatus', {
  465. status: noMute ? 0 : 2
  466. })
  467. getApp().setVoiceProps({
  468. noMute
  469. })
  470. wx.showToast({
  471. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  472. })
  473. },
  474. callPhone() {
  475. wx.makePhoneCall({
  476. phoneNumber: '400-2586366',
  477. })
  478. this.setData({
  479. showContact: false
  480. })
  481. },
  482. /**
  483. * 用户点击右上角分享
  484. */
  485. onShareAppMessage: function (res) {
  486. let {
  487. id,
  488. newPicUrl
  489. } = this.data
  490. if (res.from === 'button') {
  491. this.setData({
  492. sendShare: false
  493. })
  494. return {
  495. title: '【好友邀请】一起来逛店吧!',
  496. imageUrl: newPicUrl,
  497. path: `/pages/webview/index?id=${id}&join=true&roomId=${this.data.socketOptions.roomId}`,
  498. }
  499. } else {
  500. return {
  501. imageUrl: newPicUrl,
  502. // path: `/pages/webview/index?id=${id}&join=false`, //保留
  503. path: `/pages/webview/index?id=${id}`,
  504. }
  505. }
  506. },
  507. /**
  508. * 生命周期函数--监听页面卸载
  509. */
  510. onUnload: function () {
  511. console.log('on onUnload')
  512. this.socketSendMessage('stopCall', {})
  513. this.stopCall()
  514. this.socketStop()
  515. getApp().globalData.pusher = ''
  516. },
  517. cart(data) {
  518. this.setData({
  519. showCommodityCtrl: data.data
  520. })
  521. },
  522. share(data) {
  523. const companyName = `看店4DKanKan`
  524. const vrLink = `/pages/webview/index`
  525. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  526. const shareImg = img_url
  527. let id = data.data || this.data.id
  528. this.count = this.count || 0
  529. wx.navigateTo({
  530. url: `/pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${id}`,
  531. })
  532. },
  533. back(data) {
  534. if (data.sender !== 'h5') return;
  535. wx.switchTab({
  536. url: '/pages/index/index'
  537. })
  538. this.setData({
  539. showCommodityCtrl: false
  540. })
  541. },
  542. service() {
  543. this.setData({
  544. showContact: true,
  545. showCommodity: false,
  546. showCoupon: false
  547. })
  548. },
  549. invite(data) {
  550. console.log('---', data)
  551. if (data.sender !== 'h5') return;
  552. this.setData({
  553. sendShare: true,
  554. count: ++this.data.count
  555. })
  556. },
  557. coupon(data) {
  558. if (data.sender !== 'h5') return;
  559. this.setData({
  560. showContact: false,
  561. showCommodity: false,
  562. showCoupon: true
  563. })
  564. },
  565. liveGotoGood(ev) {
  566. let id = ev.currentTarget.dataset.item.goodsId
  567. wx.navigateTo({
  568. url: '/pages/goods/goods?id=' + id,
  569. })
  570. },
  571. gotoGoodsDOM(event) {
  572. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  573. },
  574. gotoGoodsSocket(data) {
  575. this.gotoGoods(data.data)
  576. },
  577. gotoGoods(id) {
  578. console.log('---', id)
  579. this.socketSendMessage('clientSyncAction', {
  580. type: 'openTag',
  581. data: id
  582. })
  583. this.setData({
  584. showCommodity: false
  585. })
  586. this.joinUrl()
  587. },
  588. // 加入购物车
  589. addCard(data) {
  590. wx.navigateTo({
  591. url: '/pages/goods/goods?id=' + data.data + '&oper=addCard',
  592. })
  593. },
  594. // 立即购买
  595. buyGoods(data) {
  596. wx.navigateTo({
  597. url: '/pages/goods/goods?id=' + data.data + '&oper=buyGoods',
  598. })
  599. },
  600. // addCard(event) {
  601. // wx.navigateTo({
  602. // url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  603. // })
  604. // },
  605. // buyGoods(event) {
  606. // wx.navigateTo({
  607. // url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  608. // })
  609. // },
  610. showCommodityFn() {
  611. this.setData({
  612. showCommodity: true,
  613. showContact: false,
  614. showCoupon: false
  615. })
  616. this.joinUrl()
  617. },
  618. hideComodity() {
  619. this.setData({
  620. showCommodity: false
  621. })
  622. this.joinUrl()
  623. },
  624. hideCoupon() {
  625. this.setData({
  626. showCoupon: !this.data.showCoupon
  627. })
  628. },
  629. async receive(ev) {
  630. let item = ev.target.dataset.item
  631. try {
  632. // wx.showToast({
  633. // title: '领取优惠卷',
  634. // })
  635. // return;
  636. if (item.hasReceived || item.number <= item.receiveNumber) return;
  637. let res = await util.request(api.CouponExchange, {
  638. couponId: item.id
  639. })
  640. if (res.code === 0) {
  641. wx.showToast({
  642. title: '已成功领取',
  643. success: () => {
  644. this.setData({
  645. showCoupon: false
  646. })
  647. wx.nextTick(() => {
  648. this.setData({
  649. coupons: this.data.coupons.map(citem => {
  650. return {
  651. ...citem,
  652. hasReceived: citem.id === item.id ? true : citem.hasReceived
  653. }
  654. }),
  655. showCoupon: true
  656. })
  657. })
  658. }
  659. })
  660. } else if (res.errno === 401) {
  661. getApp().setLoginProps(false)
  662. } else {
  663. wx.showToast({
  664. title: res.msg,
  665. })
  666. }
  667. } catch (e) {
  668. console.error(e)
  669. wx.showToast({
  670. icon: 'none',
  671. title: '领取失败',
  672. })
  673. }
  674. },
  675. async getCouponList(id) {
  676. const success = (res) => {
  677. this.setData({
  678. coupons: res.data.list.map(item => {
  679. item.typeMoney = item.typeMoney.toString()
  680. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  681. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  682. return item
  683. })
  684. })
  685. this.loadConponSuccess = true
  686. this.readySendCouponCtrl()
  687. }
  688. let res = await util.request(api.BrandCouponList, {
  689. brandId: id,
  690. pageNum: 1,
  691. pageSize: 10000
  692. }, 'GET')
  693. console.log(res)
  694. if (res.code === 0) {
  695. success(res)
  696. } else {
  697. let res = await util.request(api.UNBrandCouponList, {
  698. brandId: id,
  699. pageNum: 1,
  700. pageSize: 10000
  701. }, 'GET')
  702. success(res)
  703. }
  704. },
  705. ready() {
  706. this.wssSuccess = true
  707. this.readySendCouponCtrl()
  708. },
  709. readySendCouponCtrl() {
  710. if (this.wssSuccess && this.loadConponSuccess) {
  711. this.loadConponSuccess = false
  712. this.socketSendMessage('clientSyncAction', {
  713. type: 'showCoupon',
  714. data: this.data.coupons.length > 0
  715. })
  716. }
  717. },
  718. getBrand: function (id, code) {
  719. this.getGoodsCount(code, id)
  720. return;
  721. let that = this;
  722. util.request(api.SueneCategory, {
  723. sceneNum: code
  724. }, 'GET').then(function (res) {
  725. if (res.code === 0) {
  726. const comtypes = res.list.map(item => {
  727. item.width = (item.name.length + (item.num.toString().length / 2) + 2) * 16
  728. return {
  729. ...item
  730. }
  731. })
  732. that.setData({
  733. comWidth: comtypes.reduce((a, b) => a + b.width + 10, 0),
  734. comtypes,
  735. thumComtypes: (!isIos && comtypes.length > 3) ? comtypes.slice(0, 3) : null,
  736. currTypeId: comtypes.length > 0 && comtypes[0].category_id
  737. });
  738. wx.showToast({
  739. title: 'currTypeId' + that.data.currTypeId.length,
  740. })
  741. that.data.currTypeId && that.getGoodsList(id, that.data.currTypeId);
  742. }
  743. });
  744. },
  745. getGoodsCount(code, id) {
  746. util.request(api.GoodsNumCount, {
  747. isDelete: 0,
  748. isOnSale: 1,
  749. brandId: id
  750. }, 'GET')
  751. .then(res => {
  752. if (res.code === 0) {
  753. this.setData({
  754. goodsCount: res.data
  755. })
  756. }
  757. this.getCouponList(id)
  758. })
  759. },
  760. getGoodsList(id, category_id) {
  761. var that = this;
  762. if (!(this.data.navList && this.data.navList.length)) {
  763. that.navDatas = {}
  764. let navDatas = this.data.navList = this.data.comtypes
  765. // util.request(api.GoodsCategory, { id: category_id })
  766. // .then(function (res) {
  767. // if (res.errno == 0) {
  768. // let navDatas = res.data.brotherCategory
  769. // that.setData({
  770. // navList: navDatas,
  771. // currTypeId: category_id
  772. // });
  773. that.navDatas = {}
  774. navDatas.forEach(item => {
  775. util.request(api.GoodsList, {
  776. brandId: id,
  777. categoryId: item.category_id,
  778. page: that.data.page,
  779. size: that.data.size
  780. })
  781. .then(res => {
  782. if (res.errno === 0) {
  783. that.navDatas[item.category_id] = res.data.goodsList
  784. }
  785. })
  786. })
  787. // }
  788. // })
  789. }
  790. if (that.navDatas[category_id]) {
  791. if (!isIos) {
  792. let showCommodity = that.data.showCommodity
  793. that.setData({
  794. showCommodity: false
  795. })
  796. setTimeout(() => {
  797. wx.nextTick(() => {
  798. that.setData({
  799. goodsList: that.navDatas[category_id],
  800. currTypeId: category_id,
  801. showCommodity: showCommodity
  802. });
  803. })
  804. }, 500)
  805. } else {
  806. that.setData({
  807. goodsList: that.navDatas[category_id],
  808. currTypeId: category_id,
  809. });
  810. }
  811. } else {
  812. console.error('诱惑去啦')
  813. util.request(api.GoodsList, {
  814. brandId: id,
  815. categoryId: category_id,
  816. page: that.data.page,
  817. size: that.data.size
  818. })
  819. .then(function (res) {
  820. if (res.errno === 0) {
  821. that.setData({
  822. goodsList: res.data.goodsList,
  823. currTypeId: category_id
  824. });
  825. // this.data.navList
  826. }
  827. });
  828. }
  829. },
  830. getBrandDetail: function (id, cb) {
  831. util.request(api.BrandDetail, {
  832. id: id
  833. }).then((res) => {
  834. let base = res.data.brand.sceneUrl
  835. // let base = 'http://192.168.0.112:8080/shop.html?m=t-sfytakg&origin=4Dplaza"'
  836. if (res.errno === 0) {
  837. let url = base + "&sid=" + id
  838. this.setData({
  839. id: id,
  840. newPicUrl: res.data.brand.appListPicUrl,
  841. sceneNum: res.data.brand.sceneNum
  842. })
  843. cb(url, urlToJson(url).m, )
  844. }
  845. });
  846. },
  847. selectType(ev) {
  848. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  849. },
  850. hideCS() {
  851. this.setData({
  852. showCommodity: false,
  853. showCoupon: false,
  854. showContact: false
  855. })
  856. },
  857. hideContact() {
  858. this.setData({
  859. showContact: false
  860. })
  861. },
  862. calcShare() {
  863. // this.exit()
  864. this.setData({
  865. sendShare: false
  866. })
  867. },
  868. contactKf() {
  869. let keys = Object.keys(this.navDatas)
  870. let goodsId = this.navDatas[keys[0]][0].id
  871. let user = wx.getStorageSync('userinfoDetail')
  872. util.request(api.AddTalkCount, {
  873. goodsId,
  874. viewId: user && user.userId || '',
  875. sceneNum: this.data.sceneNum
  876. }, 'get')
  877. this.hideAlert && this.hideAlert()
  878. this.hideContact && this.hideContact()
  879. },
  880. onHide() {
  881. this.socketSendMessage('changeOnlineStatus', { status: false })
  882. this.pauseVideo = true
  883. this.joinUrl()
  884. }
  885. }