home.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const pages = require('../../mixin/pages')
  2. const { getProductCategoryList } = require('../../api/common')
  3. const { getLunboList } = require('./api/index')
  4. let leftHeight = 0
  5. let rightHeight = 0
  6. let query = null
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. ...pages.data(),
  13. listUrl: '/api/user/home/product/list',
  14. searchForm: {
  15. key_words: '',
  16. category_id: ''
  17. },
  18. txtlist: [],
  19. headerNav: [
  20. {
  21. icon: 'partner@2x.png',
  22. name: '供应商',
  23. path: 'business'
  24. },
  25. {
  26. icon: 'guide@2x.png',
  27. name: '采购指南',
  28. path: 'guide'
  29. },
  30. {
  31. icon: 'collection@2x.png',
  32. name: '收藏',
  33. path: 'collection'
  34. },
  35. {
  36. icon: 'news.png',
  37. name: '农事天地',
  38. path: 'news'
  39. }
  40. ],
  41. categoryList: [],
  42. leftList: [],
  43. rightList: []
  44. },
  45. ...pages.methods,
  46. /**
  47. * 生命周期函数--监听页面加载
  48. */
  49. async onLoad(options) {
  50. this.fetchLunboList()
  51. await this.fetchProductCategoryList()
  52. this.fetchOrderList()
  53. },
  54. /**
  55. * 生命周期函数--监听页面初次渲染完成
  56. */
  57. onReady() {
  58. },
  59. /**
  60. * 生命周期函数--监听页面显示
  61. */
  62. onShow() {
  63. this.getTabBar().init()
  64. },
  65. /**
  66. * 生命周期函数--监听页面隐藏
  67. */
  68. onHide() {
  69. },
  70. /**
  71. * 生命周期函数--监听页面卸载
  72. */
  73. onUnload() {
  74. },
  75. /**
  76. * 页面相关事件处理函数--监听用户下拉动作
  77. */
  78. onPullDownRefresh() {
  79. if (this.data.freshing) {
  80. return
  81. }
  82. this.setData({
  83. freshing: true
  84. })
  85. this.bindCallBack()
  86. },
  87. /**
  88. * 页面上拉触底事件的处理函数
  89. */
  90. onReachBottom() {
  91. this.fetchOrderList()
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage() {
  97. },
  98. bindCallBack() {
  99. this.refreshOrderList()
  100. this.fetchLunboList()
  101. },
  102. async fetchLunboList() {
  103. try {
  104. const { status, data, msg } = await getLunboList()
  105. if (status) {
  106. const { lunbos } = data
  107. this.setData({
  108. lunbos: lunbos
  109. })
  110. } else {
  111. wx.showToast({
  112. title: msg,
  113. icon: 'none'
  114. })
  115. }
  116. } catch (err) {}
  117. },
  118. async fetchProductCategoryList() {
  119. try {
  120. const { status, data, msg } = await getProductCategoryList()
  121. if (status && Array.isArray(data) && data.length > 0) {
  122. this.setData({
  123. categoryList: data.map(item => {
  124. return {
  125. ...item,
  126. text: item.category_name
  127. }
  128. }),
  129. 'searchForm.category_id': data[0].id
  130. })
  131. } else {
  132. wx.showToast({
  133. title: msg,
  134. icon: 'none'
  135. })
  136. }
  137. } catch (err) {}
  138. },
  139. handleTab(event) {
  140. const { name } = event.detail
  141. this.setData({
  142. 'searchForm.category_id': name
  143. }, () => {
  144. this.refreshOrderList()
  145. })
  146. },
  147. async isLeft(list) {
  148. const {
  149. leftList,
  150. rightList
  151. } = this.data
  152. query = wx.createSelectorQuery()
  153. for (const item of list) {
  154. leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item)
  155. await this.getBoxHeight(leftList, rightList)
  156. }
  157. },
  158. getBoxHeight(leftList, rightList) {
  159. return new Promise((resolve, reject) => {
  160. this.setData({
  161. leftList,
  162. rightList
  163. }, () => {
  164. query.select('.waterfall-left').boundingClientRect()
  165. query.select('.waterfall-right').boundingClientRect()
  166. query.exec((res) => {
  167. leftHeight = res[0].height
  168. rightHeight = res[1].height
  169. resolve()
  170. })
  171. })
  172. })
  173. },
  174. jumpLeavingAMessage() {
  175. wx.navigateTo({
  176. url: '/pages/leavingAMessage/leavingAMessage'
  177. })
  178. },
  179. handleNav(e) {
  180. const { item } = e.currentTarget.dataset
  181. if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
  182. const path = item.path
  183. if (path === 'news' || path === 'partner') {
  184. wx.switchTab({
  185. url: `/pages/${path}/${path}`
  186. })
  187. return
  188. }
  189. wx.navigateTo({
  190. url: `/pages/${path}/${path}`
  191. })
  192. }
  193. },
  194. openMarketing(e) {
  195. const { page } = e.currentTarget.dataset
  196. wx.navigateTo({
  197. url: `/pages/${page}/${page}`
  198. })
  199. }
  200. })