catalog.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. Page({
  4. data: {
  5. navList: [],
  6. categoryList: [],
  7. currentCategory: {},
  8. categorys: [],
  9. scrollLeft: 0,
  10. scrollTop: 0,
  11. goodsCount: 0,
  12. scrollHeight: 0,
  13. top: 0,
  14. navtop: 0
  15. },
  16. onLoad: function (options) {
  17. this.setData({
  18. imgServer: util.imgServer,
  19. })
  20. getApp().checkNetStatu();
  21. this.getCatalog();
  22. },
  23. onPullDownRefresh() {
  24. this.getCatalog();
  25. getApp().onPullDownRefresh()
  26. },
  27. gotoSearch() {
  28. wx.navigateTo({
  29. url: '/pages/search/search',
  30. })
  31. },
  32. getCatalog: function () {
  33. //CatalogList
  34. let that = this;
  35. wx.showLoading({
  36. title: '加载中...',
  37. });
  38. util.request(api.CatalogList).then(async function (res) {
  39. let navList = res.data.categoryList
  40. let ccs = []
  41. await Promise.all(navList.map((nav, i) => {
  42. return util.request(api.CatalogCurrent, { id: nav.id })
  43. .then(function (res) {
  44. ccs[i] = res.data.currentCategory
  45. })
  46. }))
  47. that.setData({
  48. navList,
  49. currentCategory: ccs[0],
  50. categorys: ccs
  51. });
  52. wx.nextTick(() => {
  53. that.getCatesTop()
  54. wx.createSelectorQuery().select('#cate-layout').boundingClientRect().exec((rect) => {
  55. that.catgHeight = rect[0].height
  56. })
  57. })
  58. wx.hideLoading();
  59. });
  60. // util.request(api.GoodsCount).then(function (res) {
  61. // that.setData({
  62. // goodsCount: res.data.goodsCount
  63. // });
  64. // });
  65. },
  66. onReady: function () {
  67. // 页面渲染完成
  68. },
  69. onShow: function () {
  70. // 页面显示
  71. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  72. this.getTabBar().setData({
  73. selected: 1
  74. })
  75. }
  76. getApp().updateCardCount()
  77. },
  78. onHide: function () {
  79. // 页面隐藏
  80. },
  81. onUnload: function () {
  82. // 页面关闭
  83. },
  84. async getCatesTop(eid) {
  85. const index = eid ? this.data.categorys.findIndex(({id}) => id === eid) : this.data.categorys.length
  86. if (this.heights) {
  87. return [...this.heights].splice(0, index)
  88. }
  89. const firstCategorys = [...this.data.categorys].splice(0, index)
  90. const heights = []
  91. await Promise.all(
  92. firstCategorys.map(({id}) =>
  93. new Promise(r =>
  94. wx.createSelectorQuery().select('#index-nav-' + id).boundingClientRect().exec((rect) => {
  95. heights.push(rect[0].height)
  96. r()
  97. })
  98. )
  99. )
  100. )
  101. this.heights = heights
  102. return heights
  103. },
  104. async scroll(ev) {
  105. let heights = await this.getCatesTop()
  106. let mtop = 0
  107. let tages = heights.map((hei) => {
  108. let sstart = ev.detail.scrollTop
  109. let eend = sstart + this.catgHeight
  110. let start = mtop
  111. let end = mtop + hei
  112. let s = start > sstart ? start : sstart
  113. let e = end > eend ? eend : end
  114. mtop += hei
  115. if (s > e) {
  116. return 0
  117. } else {
  118. return (e - s) / hei
  119. }
  120. })
  121. let index = tages.indexOf([...tages].sort((b, a) => a - b)[0])
  122. this.setData({
  123. currentCategory: this.data.categorys[index],
  124. navtop: index * 100 + 'rpx'
  125. })
  126. },
  127. switchCate: async function (event) {
  128. const eid = event.currentTarget.dataset.id
  129. const top = (await this.getCatesTop(eid)).reduce((t, c) => t + c, 0)
  130. this.setData({
  131. currentCategory: this.data.categorys.find(({id}) => id === eid),
  132. top: top
  133. })
  134. }
  135. })