index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <!-- 列表 -->
  3. <scroll-view class="scroll-view" scroll-y refresher-enabled :refresher-triggered="isRefreshing"
  4. @refresherrefresh="onRefresh" @scrolltolower="onLoadMore">
  5. <!-- 空状态 -->
  6. <view v-if="!loading && !dataList.length" class="empty-state">
  7. <view class="flex-d flex-a-c" style="padding-top: 25vh;" v-if="slotEmpty">
  8. <image src="@/pages-home/static/no-data.png" style="width:100%;height:260rpx" mode="heightFix"></image>
  9. <view class="common-title" style="padding:33rpx 0 20rpx 0">暂无内容</view>
  10. <view class="common-text" v-if="emptyText">{{ emptyText }}</view>
  11. </view>
  12. <u-empty v-else mode="list" text="暂无扫描记录" margin-top="200"></u-empty>
  13. </view>
  14. <slot></slot>
  15. <!-- 加载更多 -->
  16. <view class="load-more" v-if="dataList.length > 0">
  17. <u-divider bg-color="#F5F5F5">{{ hasMore ? '加载中...' : '我是有底线的' }}</u-divider>
  18. </view>
  19. </scroll-view>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. isRefreshing: false,
  26. loading: false,
  27. page: 1,
  28. hasMore: true,
  29. dataList: [],
  30. }
  31. },
  32. props: {
  33. url: {
  34. type: String,
  35. required: true,
  36. default: '/token/order/scanLogs'
  37. },
  38. pageSize: {
  39. type: Number,
  40. default: 10
  41. },
  42. slotEmpty: {
  43. type: Boolean,
  44. default: false
  45. },
  46. emptyText: {
  47. type: String,
  48. }
  49. },
  50. onLoad() {
  51. this.loadData()
  52. },
  53. methods: {
  54. // 加载数据
  55. async loadData(isRefresh = false) {
  56. if (isRefresh) {
  57. this.page = 1
  58. this.hasMore = true
  59. }
  60. if (!this.hasMore || this.loading) return
  61. this.loading = true
  62. try {
  63. const res = await this.fetchBookList()
  64. if (isRefresh) {
  65. this.dataList = res.list
  66. } else {
  67. this.dataList = [...this.dataList, ...res.list]
  68. }
  69. this.$emit('updateList', this.dataList)
  70. this.hasMore = res.hasMore
  71. this.page++
  72. } catch (error) {
  73. uni.showToast({
  74. title: '加载失败',
  75. icon: 'none'
  76. })
  77. } finally {
  78. this.loading = false
  79. if (isRefresh) {
  80. this.isRefreshing = false
  81. }
  82. }
  83. },
  84. // 模拟获取数据
  85. fetchBookList() {
  86. return new Promise((resolve) => {
  87. uni.showLoading({
  88. title: '加载中...',
  89. })
  90. uni.$u.http.get(this.url, {
  91. pageSize: this.pageSize,
  92. pageNum: this.page
  93. }).then(res => {
  94. resolve({
  95. list: res.rows,
  96. hasMore: res.rows ? res.rows.length < res.total - this.dataList
  97. .length : false
  98. })
  99. }).finally(() => {
  100. uni.hideLoading()
  101. })
  102. })
  103. },
  104. reloadData() {
  105. this.page = 1
  106. this.fetchBookList()
  107. },
  108. // 下拉刷新
  109. async onRefresh() {
  110. if (this.loading) return
  111. this.isRefreshing = true
  112. await this.loadData(true)
  113. },
  114. // 上拉加载更多
  115. async onLoadMore() {
  116. if (this.loading || !this.hasMore) return
  117. await this.loadData()
  118. },
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .scroll-view {
  124. height: calc(100vh - 88rpx);
  125. }
  126. .load-more {
  127. width: 100%;
  128. display: flex;
  129. color: #999999;
  130. font-size: 24rpx;
  131. padding: 30rpx 0;
  132. justify-content: center;
  133. padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  134. }
  135. </style>