pages.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }, 2000),
  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 (Array.isArray(list)) {
  52. const _list = isRefresh ? [].concat(list) : that.data.listData.concat(list)
  53. that.setData({
  54. listData: _list,
  55. finished: list.length < that.data.pagesize,
  56. isRefresh: false,
  57. isFetchLock: false,
  58. pagenum: that.data.pagenum + 1
  59. })
  60. }
  61. } else {
  62. wx.showToast({
  63. title: msg,
  64. icon: 'none'
  65. })
  66. }
  67. } catch (e) {}
  68. if (that.data.freshing) {
  69. that.setData({
  70. freshing: false
  71. })
  72. }
  73. if (isRefresh && wx.stopPullDownRefresh) {
  74. wx.stopPullDownRefresh()
  75. }
  76. },
  77. refreshOrderList() {
  78. this.setData({
  79. pagenum: 1,
  80. pagesize: 20,
  81. finished: false,
  82. isRefresh: true,
  83. isFetchLock: false
  84. }, () => {
  85. this.fetchOrderList()
  86. })
  87. }
  88. }
  89. }