木邻有你前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.5 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. // 简化版列表的混入
  2. export default {
  3. data() {
  4. return {
  5. list: [],
  6. pageNo : 1,
  7. pageSize : 10,
  8. mixinListApi: '',
  9. isLoading: false,
  10. hasMore: true,
  11. // 额外返回出去的数据
  12. extraData: null,
  13. // 每次更新数据后执行的函数 可以进行数据处理
  14. afterUpdateDataFn: function() {},
  15. // 每次更新数据前执行的函数,
  16. beforeUpdateDataFn: function() {},
  17. // 混入配置
  18. mixinListConfig: {
  19. // 数据返回的直接路径
  20. responsePath: 'result.records',
  21. // 列表是否需要下拉刷新
  22. isPullDownRefresh: true,
  23. // 列表是否需要上拉加载
  24. isReachBottomLoad: true,
  25. // 额外返回出去的数据的路径
  26. extraDataPath: ''
  27. }
  28. }
  29. },
  30. computed: {
  31. // 自定义onShow前会执行的函数
  32. mixinFnBeforePageShow() {
  33. return function() {}
  34. }
  35. },
  36. methods: {
  37. // 获取文件的自定义传参 -- 可以在页面中重写
  38. mixinSetParams() {
  39. return {}
  40. },
  41. // 解析分路径获取嵌套值
  42. resolvePath(obj, path) {
  43. if (path){
  44. return path.split('.').reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : null), obj)
  45. }else {
  46. return obj
  47. }
  48. },
  49. // 初始化分页
  50. initPage(){
  51. this.pageNo = 1,
  52. this.hasMore = true
  53. },
  54. // 获取列表
  55. async getList(isRefresh = false) {
  56. if (!this.hasMore) {
  57. return
  58. }
  59. this.isLoading = true
  60. const apiMethod = this.resolvePath(this.$api, this.mixinListApi)
  61. if (typeof apiMethod !== 'function') {
  62. console.log('mixinApi不存在', this.mixinListApi);
  63. return
  64. }
  65. // 每次更新数据前执行的函数
  66. if (this.beforeUpdateDataFn) {
  67. this.beforeUpdateDataFn(this.list)
  68. }
  69. const res = await apiMethod({
  70. pageNo: this.pageNo,
  71. pageSize: this.pageSize,
  72. ...this.mixinSetParams()
  73. })
  74. const resData = this.resolvePath(res, this.mixinListConfig.responsePath) || []
  75. if (res.code === 200) {
  76. // 如果没有值了
  77. if (!resData.length) {
  78. this.hasMore = false
  79. uni.showToast({
  80. title: '暂无更多数据',
  81. icon: 'none'
  82. })
  83. }else {
  84. this.pageNo++
  85. }
  86. if (isRefresh ) {
  87. // 如果是刷新,直接覆盖
  88. this.list = resData
  89. } else {
  90. this.list = [...this.list, ...resData]
  91. }
  92. // 如果有额外数据的路径,刷新后,需要将额外数据也刷新
  93. if (this.mixinListConfig.extraDataPath !== '') {
  94. this.extraData = this.resolvePath(res, this.mixinListConfig.extraDataPath)
  95. }
  96. }
  97. // 每次更新数据后执行的函数
  98. if (this.afterUpdateDataFn) {
  99. this.afterUpdateDataFn(this.list)
  100. }
  101. // 如果有在加载中
  102. if (this.isLoading) {
  103. this.isLoading = false
  104. }
  105. // 有过有在下拉加载
  106. uni.stopPullDownRefresh()
  107. },
  108. },
  109. async onShow() {
  110. // if (!this.list.length) {
  111. if (this.mixinFnBeforePageShow) this.mixinFnBeforePageShow()
  112. this.initPage()
  113. await this.getList(true)
  114. // }
  115. },
  116. async onPullDownRefresh() {
  117. // 在下拉还没结束前 不做任何操作
  118. if (this.isLoading) {
  119. return
  120. }
  121. this.initPage()
  122. await this.getList(true)
  123. },
  124. async onReachBottom() {
  125. if (this.isLoading) {
  126. return
  127. }
  128. await this.getList()
  129. }
  130. }