home.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. const temp = data.map(item => {
  123. return {
  124. ...item,
  125. text: item.category_name
  126. }
  127. })
  128. temp.unshift({
  129. category_img_url: '',
  130. category_name: '全部',
  131. id: 0
  132. })
  133. this.setData({
  134. categoryList: temp,
  135. 'searchForm.category_id': temp[0].id
  136. })
  137. } else {
  138. wx.showToast({
  139. title: msg,
  140. icon: 'none'
  141. })
  142. }
  143. } catch (err) {}
  144. },
  145. handleTab(event) {
  146. const { name } = event.detail
  147. this.setData({
  148. 'searchForm.category_id': name
  149. }, () => {
  150. this.refreshOrderList()
  151. })
  152. },
  153. async isLeft(list) {
  154. const {
  155. leftList,
  156. rightList
  157. } = this.data
  158. query = wx.createSelectorQuery()
  159. for (const item of list) {
  160. // leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item)
  161. if (leftHeight < rightHeight) {
  162. leftList.push(item)
  163. } else if (leftHeight === rightHeight) {
  164. leftList.length <= rightList ? leftList.push(item) : rightList.push(item)
  165. } else {
  166. rightList.push(item)
  167. }
  168. await this.getBoxHeight(leftList, rightList)
  169. }
  170. },
  171. getBoxHeight(leftList, rightList) {
  172. return new Promise((resolve, reject) => {
  173. this.setData({
  174. leftList,
  175. rightList
  176. }, () => {
  177. query.select('.waterfall-left').boundingClientRect()
  178. query.select('.waterfall-right').boundingClientRect()
  179. query.exec((res) => {
  180. leftHeight = res[0].height
  181. rightHeight = res[1].height
  182. resolve()
  183. })
  184. })
  185. })
  186. },
  187. jumpLeavingAMessage() {
  188. wx.navigateTo({
  189. url: '/pages/leavingAMessage/leavingAMessage'
  190. })
  191. },
  192. handleSwiperClick(e) {
  193. const { item } = e.currentTarget.dataset
  194. if (Object.prototype.toString.call(item) === '[object Object]') {
  195. const { lunbo_link_url } = item
  196. if (/^http/.test(lunbo_link_url)) {
  197. wx.navigateTo({
  198. url: '/pages/h5/h5?url=' + lunbo_link_url
  199. })
  200. } else if (/^\/pages/.test(lunbo_link_url)) {
  201. wx.navigateTo({
  202. url: lunbo_link_url
  203. })
  204. }
  205. }
  206. },
  207. handleNav(e) {
  208. const { item } = e.currentTarget.dataset
  209. if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
  210. const path = item.path
  211. if (path === 'news' || path === 'partner') {
  212. wx.switchTab({
  213. url: `/pages/${path}/${path}`
  214. })
  215. return
  216. }
  217. wx.navigateTo({
  218. url: `/pages/${path}/${path}`
  219. })
  220. }
  221. },
  222. openMarketing(e) {
  223. const { page } = e.currentTarget.dataset
  224. wx.navigateTo({
  225. url: `/pages/${page}/${page}`
  226. })
  227. }
  228. })