list.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const fetch = require('../../utils/fetch')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. category: null,
  8. shops: [],
  9. pageIndex: 0,
  10. pageSize: 20,
  11. totalCount: 0,
  12. hasMore: true
  13. },
  14. loadMore () {
  15. let { pageIndex, pageSize, searchText } = this.data
  16. const params = { _page: ++pageIndex, _limit: pageSize }
  17. if (searchText) params.q = searchText
  18. return fetch(`/?${this.data.category.id}`, params)
  19. .then(res => {
  20. res = '[{"id":1,"score":"c","name":"2018大梅时代歌舞节","phone":"400-8123-321","address":"光华路36号","businessHours":"8:00 - 16:00","images":["/assets/icons/basketball.png"]},{"id":2,"score":"c","name":"2018大梅时代体育节","phone":"400-8123-321","address":"光华路36号","businessHours":"8:00 - 16:00","images":["/assets/icons/basketball.png"]},{"id":3,"score":"c","name":"2018大梅时代演讲比赛","phone":"400-8123-321","address":"光华路36号","businessHours":"8:00 - 16:00","images":["/assets/icons/basketball.png"]},{"id":4,"score":"c","name":"2018大梅时代新歌声大赛","phone":"400-8123-321","address":"光华路36号","businessHours":"8:00 - 16:00","images":["/assets/icons/basketball.png"]}]';
  21. res = JSON.parse(res);
  22. const totalCount = 4; //parseInt(res.header['X-Total-Count'])
  23. const hasMore = this.data.pageIndex * this.data.pageSize < totalCount
  24. const shops = this.data.shops.concat(res);
  25. this.setData({ shops:shops, totalCount, pageIndex, hasMore })
  26. })
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad (options) {
  32. fetch(`/?catogories${options.cat}`)
  33. .then(res => {
  34. res = { data: {name:"球类运动"} };
  35. this.setData({ category: res.data })
  36. wx.setNavigationBarTitle({ title: res.data.name })
  37. this.loadMore()
  38. })
  39. },
  40. /**
  41. * 页面相关事件处理函数--监听用户下拉动作
  42. */
  43. onPullDownRefresh () {
  44. this.setData({ shops: [], pageIndex: 0, hasMore: true })
  45. this.loadMore().then(() => wx.stopPullDownRefresh())
  46. },
  47. /**
  48. * 页面上拉触底事件的处理函数
  49. */
  50. onReachBottom () {
  51. // TODO:节流
  52. this.loadMore()
  53. },
  54. searchHandle () {
  55. // console.log(this.data.searchText)
  56. this.setData({ shops: [], pageIndex: 0, hasMore: true })
  57. this.loadMore()
  58. },
  59. showSearchHandle () {
  60. this.setData({ searchShowed: true })
  61. },
  62. hideSearchHandle () {
  63. this.setData({ searchText: '', searchShowed: false })
  64. },
  65. clearSearchHandle () {
  66. this.setData({ searchText: '' })
  67. },
  68. searchChangeHandle (e) {
  69. this.setData({ searchText: e.detail.value })
  70. }
  71. })