index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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="bgColor">{{ 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. bgColor: {
  34. type: String,
  35. default: '#F5F5F5'
  36. },
  37. url: {
  38. type: String,
  39. required: true,
  40. default: '/token/order/scanLogs'
  41. },
  42. pageSize: {
  43. type: Number,
  44. default: 10
  45. },
  46. slotEmpty: {
  47. type: Boolean,
  48. default: false
  49. },
  50. emptyText: {
  51. type: String,
  52. },
  53. params: {
  54. type: Object,
  55. default: {}
  56. },
  57. immediate: {
  58. type: Boolean,
  59. default: true
  60. }
  61. },
  62. onLoad() {
  63. if (this.immediate) {
  64. this.loadData()
  65. }
  66. },
  67. methods: {
  68. // 加载数据
  69. async loadData(isRefresh = false, params = {}) {
  70. if (isRefresh) {
  71. this.page = 1
  72. this.hasMore = true
  73. }
  74. if (!this.hasMore || this.loading) return
  75. this.loading = true
  76. try {
  77. const res = await this.fetchBookList(params)
  78. if (isRefresh) {
  79. this.dataList = res.list
  80. } else {
  81. this.dataList = [...this.dataList, ...res.list]
  82. }
  83. this.$emit('updateList', this.dataList)
  84. this.hasMore = res.hasMore
  85. this.page++
  86. } catch (error) {
  87. uni.showToast({
  88. title: '加载失败',
  89. icon: 'none'
  90. })
  91. } finally {
  92. this.loading = false
  93. if (isRefresh) {
  94. this.isRefreshing = false
  95. }
  96. }
  97. },
  98. // 模拟获取数据
  99. fetchBookList(params = {}) {
  100. return new Promise((resolve) => {
  101. uni.showLoading({
  102. title: '加载中...',
  103. })
  104. uni.$u.http.get(this.url, {
  105. pageSize: this.pageSize,
  106. pageNum: this.page,
  107. ...params
  108. }).then(res => {
  109. resolve({
  110. list: res.rows,
  111. hasMore: res.rows ? res.rows.length < res.total - this.dataList
  112. .length : false
  113. })
  114. }).finally(() => {
  115. uni.hideLoading()
  116. })
  117. })
  118. },
  119. reloadData() {
  120. this.page = 1
  121. this.fetchBookList()
  122. },
  123. // 下拉刷新
  124. async onRefresh() {
  125. if (this.loading) return
  126. this.isRefreshing = true
  127. await this.loadData(true, this.params)
  128. },
  129. // 上拉加载更多
  130. async onLoadMore() {
  131. if (this.loading || !this.hasMore) return
  132. await this.loadData(false, this.params)
  133. },
  134. }
  135. }
  136. </script>
  137. <style lang="scss">
  138. .scroll-view {
  139. height: calc(100vh - 88rpx);
  140. }
  141. .load-more {
  142. width: 100%;
  143. display: flex;
  144. color: #999999;
  145. font-size: 24rpx;
  146. padding: 30rpx 0;
  147. justify-content: center;
  148. padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  149. }
  150. </style>