木邻有你前端代码仓库
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.

121 lines
3.0 KiB

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