home.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. this.fetchOrderList()
  54. },
  55. /**
  56. * 生命周期函数--监听页面初次渲染完成
  57. */
  58. onReady() {
  59. },
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow() {
  64. this.getTabBar().init()
  65. },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide() {
  70. },
  71. /**
  72. * 生命周期函数--监听页面卸载
  73. */
  74. onUnload() {
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh() {
  80. if (this.data.freshing) {
  81. return
  82. }
  83. this.setData({
  84. freshing: true
  85. })
  86. this.bindCallBack()
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom() {
  92. this.fetchOrderList()
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage() {
  98. },
  99. bindCallBack() {
  100. this.refreshOrderList()
  101. this.fetchLunboList()
  102. },
  103. async fetchLunboList() {
  104. try {
  105. const { status, data, msg } = await getLunboList()
  106. if (status) {
  107. const { lunbos } = data
  108. this.setData({
  109. lunbos: lunbos
  110. })
  111. } else {
  112. wx.showToast({
  113. title: msg,
  114. icon: 'none'
  115. })
  116. }
  117. } catch (err) {}
  118. },
  119. async fetchProductCategoryList() {
  120. try {
  121. const { status, data, msg } = await getProductCategoryList()
  122. if (status && Array.isArray(data) && data.length > 0) {
  123. const temp = data.map(item => {
  124. return {
  125. ...item,
  126. text: item.category_name
  127. }
  128. })
  129. temp.unshift({
  130. category_img_url: '',
  131. category_name: '全部',
  132. id: 0
  133. })
  134. this.setData({
  135. categoryList: temp,
  136. 'searchForm.category_id': temp[0].id
  137. })
  138. } else {
  139. wx.showToast({
  140. title: msg,
  141. icon: 'none'
  142. })
  143. }
  144. } catch (err) {}
  145. },
  146. handleTab(event) {
  147. const { name } = event.detail
  148. this.setData({
  149. 'searchForm.category_id': name
  150. }, () => {
  151. this.refreshOrderList()
  152. })
  153. },
  154. async isLeft(list) {
  155. const {
  156. leftList,
  157. rightList
  158. } = this.data
  159. query = wx.createSelectorQuery()
  160. for (const item of list) {
  161. // leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item)
  162. if (leftHeight < rightHeight) {
  163. leftList.push(item)
  164. } else if (leftHeight === rightHeight) {
  165. leftList.length <= rightList ? leftList.push(item) : rightList.push(item)
  166. } else {
  167. rightList.push(item)
  168. }
  169. await this.getBoxHeight(leftList, rightList)
  170. }
  171. },
  172. getBoxHeight(leftList, rightList) {
  173. return new Promise((resolve, reject) => {
  174. this.setData({
  175. leftList,
  176. rightList
  177. }, () => {
  178. query.select('.waterfall-left').boundingClientRect()
  179. query.select('.waterfall-right').boundingClientRect()
  180. query.exec((res) => {
  181. leftHeight = res[0].height
  182. rightHeight = res[1].height
  183. resolve()
  184. })
  185. })
  186. })
  187. },
  188. jumpLeavingAMessage() {
  189. wx.navigateTo({
  190. url: '/pages/leavingAMessage/leavingAMessage'
  191. })
  192. },
  193. handleSwiperClick(e) {
  194. const isTab = app.globalData.tabBarList.map(item => item.pagePath)
  195. const { item } = e.currentTarget.dataset
  196. if (Object.prototype.toString.call(item) === '[object Object]') {
  197. const { lunbo_link_url } = item
  198. if (/^http/.test(lunbo_link_url)) {
  199. wx.navigateTo({
  200. url: '/pages/h5/h5?url=' + lunbo_link_url
  201. })
  202. } else if (/^\/pages/.test(lunbo_link_url)) {
  203. if (isTab.findIndex(item => lunbo_link_url.indexOf(item) > -1) > -1) {
  204. wx.switchTab({
  205. url: lunbo_link_url
  206. })
  207. } else {
  208. wx.navigateTo({
  209. url: lunbo_link_url
  210. })
  211. }
  212. }
  213. }
  214. },
  215. handleNav(e) {
  216. const { item } = e.currentTarget.dataset
  217. if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
  218. const path = item.path
  219. if (path === 'news' || path === 'partner') {
  220. wx.switchTab({
  221. url: `/pages/${path}/${path}`
  222. })
  223. return
  224. }
  225. wx.navigateTo({
  226. url: `/pages/${path}/${path}`
  227. })
  228. }
  229. },
  230. openMarketing(e) {
  231. const { page } = e.currentTarget.dataset
  232. wx.navigateTo({
  233. url: `/pages/${page}/${page}`
  234. })
  235. }
  236. })