pages.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const { request } = require('../api/request')
  2. const { debounce } = require('../utils/util')
  3. module.exports = {
  4. data() {
  5. return {
  6. pagenum: 1,
  7. pagesize: 20,
  8. finished: false, // 所有数据是否加载完
  9. isRefresh: false, // 是否下拉刷新
  10. isFetchLock: false, // 接口调用加锁
  11. listData: [],
  12. freshing: false
  13. }
  14. },
  15. methods: {
  16. handleKeyWords: debounce(function (event) {
  17. const { formkey } = event.target.dataset
  18. if (formkey) {
  19. this.setData({
  20. [`searchForm.${formkey}`]: event.detail.value
  21. }, () => {
  22. this.refreshOrderList()
  23. })
  24. }
  25. }, 1000),
  26. async fetchOrderList() {
  27. const that = this
  28. const isRefresh = that.data.isRefresh
  29. if (that.data.finished) {
  30. return
  31. }
  32. if (that.data.isFetchLock) {
  33. return
  34. }
  35. that.setData({
  36. isFetchLock: true
  37. })
  38. try {
  39. const { status, data, msg } = await request({
  40. url: that.data.listUrl,
  41. method: 'POST',
  42. data: {
  43. 'page': that.data.pagenum,
  44. 'page_size': that.data.pagesize,
  45. ...that.data.searchForm
  46. },
  47. showLoading: false
  48. })
  49. if (status) {
  50. const { list } = data
  51. if (that.data.hasOwnProperty('fans_num')) {
  52. that.setData({
  53. fans_num: data.fans_num,
  54. follow_num: data.follow_num
  55. })
  56. }
  57. if (Array.isArray(list)) {
  58. const listFormat = list.map(item => {
  59. if (that.data.listUrl === '/api/user/goods/shop/list') {
  60. return {
  61. ...item,
  62. shop_address: item.shop_address ? JSON.parse(item.shop_address) : {}
  63. }
  64. }
  65. if (that.data.listUrl === '/api/user/track/list') {
  66. return {
  67. ...item,
  68. track_img_url: item.track_img_url ? JSON.parse(item.track_img_url) : {}
  69. }
  70. }
  71. return {
  72. ...item
  73. }
  74. })
  75. const _list = isRefresh ? [].concat(listFormat) : that.data.listData.concat(listFormat)
  76. const temp = {
  77. listData: _list,
  78. finished: list.length < that.data.pagesize,
  79. isRefresh: false,
  80. isFetchLock: false,
  81. pagenum: that.data.pagenum + 1
  82. }
  83. // VR看菜园:轮播图只取前三个
  84. if (that.data.listUrl === '/api/user/vr/list') {
  85. temp['topSwiperData'] = _list.filter(item => item.top === 1)
  86. temp['listData'] = _list.filter(item => item.top !== 1)
  87. }
  88. if (isRefresh && this.data.hasOwnProperty('leftList')) {
  89. temp['leftList'] = []
  90. temp['rightList'] = []
  91. }
  92. that.setData(temp, () => {
  93. if (that.isLeft) {
  94. const timer = setTimeout(() => {
  95. clearTimeout(timer)
  96. that.isLeft(list)
  97. }, 500)
  98. }
  99. })
  100. }
  101. } else {
  102. wx.showToast({
  103. title: msg,
  104. icon: 'none'
  105. })
  106. }
  107. } catch (e) {}
  108. if (that.data.freshing) {
  109. that.setData({
  110. freshing: false
  111. })
  112. }
  113. if (isRefresh && wx.stopPullDownRefresh) {
  114. wx.stopPullDownRefresh()
  115. }
  116. },
  117. refreshOrderList() {
  118. this.setData({
  119. pagenum: 1,
  120. pagesize: 20,
  121. finished: false,
  122. isRefresh: true,
  123. isFetchLock: false
  124. }, () => {
  125. this.fetchOrderList()
  126. })
  127. }
  128. }
  129. }