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

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