goods.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const pages = require('../../mixin/pages')
  2. const { getProductCategoryList } = require('../../api/common')
  3. let leftHeight = 0
  4. let rightHeight = 0
  5. let query = null
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. ...pages.data(),
  12. listUrl: '/api/user/caigou/product/list',
  13. searchForm: {
  14. key_words: '',
  15. category_id: ''
  16. },
  17. categoryList: [],
  18. navScrollLeft: -1,
  19. leftList: [],
  20. rightList: []
  21. },
  22. ...pages.methods,
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. async onLoad(options) {
  27. await this.fetchProductCategoryList()
  28. this.fetchOrderList()
  29. },
  30. /**
  31. * 生命周期函数--监听页面初次渲染完成
  32. */
  33. onReady() {
  34. },
  35. /**
  36. * 生命周期函数--监听页面显示
  37. */
  38. onShow() {
  39. },
  40. /**
  41. * 生命周期函数--监听页面隐藏
  42. */
  43. onHide() {
  44. },
  45. /**
  46. * 生命周期函数--监听页面卸载
  47. */
  48. onUnload() {
  49. },
  50. /**
  51. * 页面相关事件处理函数--监听用户下拉动作
  52. */
  53. onPullDownRefresh() {
  54. if (this.data.freshing) {
  55. return
  56. }
  57. this.setData({
  58. freshing: true
  59. })
  60. this.bindCallBack()
  61. },
  62. /**
  63. * 页面上拉触底事件的处理函数
  64. */
  65. onReachBottom() {
  66. this.fetchOrderList()
  67. },
  68. bindCallBack() {
  69. this.refreshOrderList()
  70. },
  71. async fetchProductCategoryList() {
  72. try {
  73. const { status, data, msg } = await getProductCategoryList()
  74. if (status && Array.isArray(data) && data.length > 0) {
  75. const temp = data.map(item => {
  76. return {
  77. ...item,
  78. text: item.category_name
  79. }
  80. })
  81. temp.unshift({
  82. category_img_url: 'https://lanman-shining.oss-cn-hangzhou.aliyuncs.com/images/user/bashi635cd28a12481.png',
  83. category_name: '全部',
  84. id: 0
  85. })
  86. this.setData({
  87. categoryList: temp,
  88. 'searchForm.category_id': temp[0].id
  89. })
  90. } else {
  91. wx.showToast({
  92. title: msg,
  93. icon: 'none'
  94. })
  95. }
  96. } catch (err) {}
  97. },
  98. handleNav(e) {
  99. const { item } = e.currentTarget.dataset
  100. if (this.data.searchForm.category_id !== item.id) {
  101. this.setData({
  102. 'searchForm.category_id': item.id,
  103. navScrollLeft: 0
  104. }, () => {
  105. this.refreshOrderList()
  106. })
  107. }
  108. },
  109. async isLeft(list) {
  110. const {
  111. leftList,
  112. rightList
  113. } = this.data
  114. query = wx.createSelectorQuery().in(this)
  115. for (const item of list) {
  116. if (leftHeight < rightHeight) {
  117. leftList.push(item)
  118. } else if (leftHeight === rightHeight) {
  119. leftList.length <= rightList ? leftList.push(item) : rightList.push(item)
  120. } else {
  121. rightList.push(item)
  122. }
  123. await this.getBoxHeight(leftList, rightList)
  124. }
  125. },
  126. getBoxHeight(leftList, rightList) {
  127. return new Promise((resolve, reject) => {
  128. this.setData({
  129. leftList,
  130. rightList
  131. }, () => {
  132. query.select('.waterfall-left').boundingClientRect()
  133. query.select('.waterfall-right').boundingClientRect()
  134. query.exec((res) => {
  135. leftHeight = res[0].height
  136. rightHeight = res[1].height
  137. resolve()
  138. })
  139. })
  140. })
  141. }
  142. })