home.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = wx.createSelectorQuery()
  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. for (const item of list) {
  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. })