home.js 5.4 KB

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