index.vue 4.9 KB

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