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

94 lines
2.2 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. // 简化版列表的混入
  2. export default {
  3. data() {
  4. return {
  5. list: [],
  6. pageNo : 1,
  7. pageSize : 10,
  8. // 此处为拓展参数
  9. extraParams: { },
  10. mixinListApi: '',
  11. isLoading: false,
  12. mixinListCofig: {
  13. // 数据返回的直接路径
  14. responsePath: 'result.records',
  15. // 列表是否需要下拉刷新
  16. isPullDownRefresh: true,
  17. // 列表是否需要上拉加载
  18. isReachBottomLoad: true
  19. }
  20. }
  21. },
  22. methods: {
  23. // 解析分路径获取嵌套值
  24. resolvePath(obj, path) {
  25. return path.split('.').reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : null), obj)
  26. },
  27. // 刷新时保留的请求参数
  28. setParams() {
  29. return { }
  30. },
  31. // 初始化数据
  32. initData() {
  33. this.list = [ ]
  34. },
  35. // 初始化请求参数
  36. initParams() {
  37. this.extraParams = { ...this.setParams() }
  38. },
  39. // 初始化分页
  40. initPage(){
  41. this.pageNo = 1
  42. },
  43. // 全部初始化
  44. initAll() {
  45. this.initData()
  46. // this.initParams()
  47. this.initPage()
  48. },
  49. // 获取列表
  50. async getList() {
  51. this.isLoading = true
  52. const apiMethod = this.resolvePath(this.$api, this.mixinListApi)
  53. if (typeof apiMethod !== 'function') {
  54. console.log('mixinApi不存在', this.mixinListApi);
  55. return
  56. }
  57. const res = await apiMethod({
  58. ...this.extraParams,
  59. pageNo: this.pageNo,
  60. pageSize: this.pageSize
  61. })
  62. const resData = this.resolvePath(res, this.mixinListCofig.responsePath) || []
  63. if (res.code === 200 && resData.length) {
  64. this.list = [...this.list, ...resData]
  65. this.pageNo++
  66. }else {
  67. uni.showToast({
  68. title: '暂无数据',
  69. icon: 'none'
  70. })
  71. }
  72. // 如果有在加载中
  73. if (this.isLoading) {
  74. this.isLoading = false
  75. }
  76. // 有过有在下拉加载
  77. uni.stopPullDownRefresh()
  78. },
  79. },
  80. async onShow() {
  81. if (!this.list.length) {
  82. await this.getList()
  83. }
  84. },
  85. onHide() {
  86. this.initAll()
  87. },
  88. async onPullDownRefresh() {
  89. this.initAll()
  90. await this.getList()
  91. },
  92. async onReachBottom() {
  93. await this.getList()
  94. }
  95. }