index.vue 5.6 KB

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