四零语境前端代码仓库
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.

137 lines
3.7 KiB

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