Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

93 lines
2.4KB

  1. // components/List/index.js
  2. import { getDataByPath } from "../../utils/util"
  3. import { get } from "../../utils/api"
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. url: {
  10. type: String
  11. },
  12. dataPath: {
  13. type: String
  14. },
  15. list: {
  16. type: Array
  17. },
  18. params: {
  19. type: Object,
  20. },
  21. limit: {
  22. type: Number,
  23. value: 10
  24. },
  25. scrolling:{
  26. type:Boolean,
  27. value:true,
  28. },
  29. emptyText:{
  30. type:String,
  31. value:"暂无数据"
  32. }
  33. },
  34. /**
  35. * 组件的初始数据
  36. */
  37. data: {
  38. page: 1,
  39. limit: 10,
  40. more: true,
  41. loading: false
  42. },
  43. // 数据监听器
  44. observers: {
  45. "url,params"(url) {
  46. if (!url) return
  47. this.resetPage();
  48. },
  49. },
  50. /**
  51. * 组件的方法列表
  52. */
  53. methods: {
  54. loadMore() {
  55. let page = this.data.page + 1;
  56. this.setData({ page })
  57. this.getData();
  58. },
  59. getData() {
  60. if (!this.data.more) return
  61. if (!this.data.loading && (this.data.params || Object.keys(this.data.params).length > 0)) {
  62. this.setData({ loading: true })
  63. get(this.data.url, Object.assign({}, { page: this.data.page, limit: this.data.limit }, this.data.params || {})).then(res => {
  64. let records;
  65. if (res.code === 0) {
  66. records = getDataByPath(res, this.data.dataPath || "data.records")
  67. } else {
  68. records = [];
  69. }
  70. if (records.length < this.data.limit) {
  71. this.setData({ more: false })
  72. }
  73. this.triggerEvent("update-list", records)
  74. }).catch(e => {
  75. wx.showToast({
  76. title: '系统错误',
  77. icon: 'error',
  78. duration: 1500,
  79. });
  80. }).finally(() => {
  81. this.setData({ loading: false })
  82. })
  83. }
  84. },
  85. resetPage() {
  86. this.setData({ more: true, page: 1 })
  87. this.triggerEvent('reset-list')
  88. this.getData();
  89. }
  90. }
  91. })