展品维保小程序前端代码接口
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.

135 lines
3.6 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks 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. }
  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. // console.log('本次请求的pageNo和pageSize', this.pageNo, this.pageSize)
  57. if (!this.hasMore) {
  58. return
  59. }
  60. this.isLoading = true
  61. const apiMethod = this.resolvePath(this.$api, this.mixinListApi)
  62. if (typeof apiMethod !== 'function') {
  63. console.log('mixinApi不存在', this.mixinListApi);
  64. return
  65. }
  66. // 每次更新数据前执行的函数
  67. if (this.beforeUpdateDataFn) {
  68. this.beforeUpdateDataFn(this.list)
  69. }
  70. const res = await apiMethod({
  71. pageNo: this.pageNo,
  72. pageSize: this.pageSize,
  73. ...this.mixinSetParams()
  74. })
  75. const resData = this.resolvePath(res, this.mixinListConfig.responsePath) || []
  76. if (res.code === 200) {
  77. // 如果没有值了
  78. if (!resData.length) {
  79. this.hasMore = false
  80. uni.showToast({
  81. title: '暂无更多数据',
  82. icon: 'none'
  83. })
  84. }else {
  85. this.pageNo++
  86. }
  87. if (isRefresh ) {
  88. // 如果是刷新,直接覆盖
  89. this.list = resData
  90. } else {
  91. this.list = [...this.list, ...resData]
  92. }
  93. // 如果有额外数据的路径,刷新后,需要将额外数据也刷新
  94. if (this.mixinListConfig.extraDataPath !== '') {
  95. this.extraData = this.resolvePath(res, this.mixinListConfig.extraDataPath)
  96. }
  97. }
  98. // 每次更新数据后执行的函数
  99. if (this.afterUpdateDataFn) {
  100. this.afterUpdateDataFn(this.list)
  101. }
  102. // 如果有在加载中
  103. if (this.isLoading) {
  104. this.isLoading = false
  105. }
  106. // 有过有在下拉加载
  107. uni.stopPullDownRefresh()
  108. },
  109. },
  110. async onShow() {
  111. // if (!this.list.length) {
  112. if (this.mixinFnBeforePageShow) this.mixinFnBeforePageShow()
  113. this.initPage()
  114. await this.getList(true)
  115. // }
  116. },
  117. async onPullDownRefresh() {
  118. // 在下拉还没结束前 不做任何操作
  119. if (this.isLoading) {
  120. return
  121. }
  122. this.initPage()
  123. await this.getList(true)
  124. },
  125. async onReachBottom() {
  126. if (this.isLoading) {
  127. return
  128. }
  129. await this.getList()
  130. }
  131. }