index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <scroll-view
  3. class="c_scroll_paging"
  4. v-bind="__attrs"
  5. @scrolltolower="scrolltolower"
  6. @refresherrefresh="onRefresh"
  7. >
  8. <slot></slot>
  9. <view class="footer" :class="{ 'footer-empty-box': imageName }">
  10. <!-- 数据为空/加载失败显示 -->
  11. <statusView
  12. :imageName="imageName"
  13. :emptyImgStyle="emptyImgStyle"
  14. v-if="imageName"
  15. />
  16. <u-loading-icon
  17. v-else-if="loading"
  18. size="16"
  19. text="加载中"
  20. ></u-loading-icon>
  21. <u-divider v-else-if="noMore" text="没有更多"></u-divider>
  22. </view>
  23. </scroll-view>
  24. </template>
  25. <script>
  26. import statusView from '@/components/statusView';
  27. export default {
  28. name: 'CScrollPaging',
  29. model: {
  30. prop: 'value',
  31. event: 'change',
  32. },
  33. components: {
  34. statusView,
  35. },
  36. props: {
  37. value: {
  38. default: () => [],
  39. type: Array,
  40. },
  41. queryList: {
  42. // return接口的res
  43. type: Function,
  44. default: () => () => {},
  45. }, // function 加载数据,并且将分页作为参数返回
  46. pageSize: {
  47. type: Number,
  48. default: 10, // 每页显示的条数
  49. },
  50. },
  51. data() {
  52. return {
  53. imageName: '', // 暂无数据 或 数据加载失败
  54. loading: false,
  55. freshing: false,
  56. pageNum: 0, // 页数
  57. total: 0, // 总条数
  58. noMore: false, // 是否没有更多
  59. };
  60. },
  61. computed: {
  62. emptyImgStyle() {
  63. let __style = {};
  64. if (this.imageName === 'empty') {
  65. __style = {
  66. width: '207px',
  67. height: '208px',
  68. };
  69. }
  70. if (this.imageName === 'load-fail') {
  71. __style = {
  72. width: '200px',
  73. height: '141px',
  74. };
  75. }
  76. return __style;
  77. },
  78. __attrs() {
  79. return {
  80. 'scroll-y': true,
  81. 'refresher-enabled': true, // 开启自定义下拉刷新
  82. 'refresher-triggered': this.freshing, // 设置当前下拉刷新状态,true 表示下拉刷新已经被触发
  83. ...this.$attrs,
  84. };
  85. },
  86. },
  87. created() {
  88. this.scrolltolower();
  89. },
  90. methods: {
  91. async onRefresh() {
  92. // 下拉刷新被触发
  93. if (this.freshing) return;
  94. try {
  95. this.freshing = true; // 下拉的状态
  96. this.pageNum = 0;
  97. await this.getList(true);
  98. this.freshing = false;
  99. } catch (e) {
  100. this.freshing = false;
  101. console.error(e);
  102. }
  103. },
  104. async getList(isReload) {
  105. // Boolean 是否下拉刷新
  106. try {
  107. this.pageNum += 1;
  108. const params = {
  109. pageNum: this.pageNum,
  110. pageSize: this.pageSize, // 每页显示的条数
  111. };
  112. this.loading = true;
  113. const res = await this.queryList(params);
  114. this.loading = false;
  115. const { rows = [] } = res || {};
  116. const data = isReload ? rows : [...this.value, ...rows];
  117. this.$emit('change', data);
  118. this.imageName =
  119. !rows.length && this.pageNum === 1 ? 'empty' : ''; // 数据为空
  120. // 没有更多
  121. this.noMore = rows.length < this.pageSize;
  122. } catch (e) {
  123. this.imageName = 'load-fail'; // 数据加载失败
  124. this.$emit('change', []);
  125. this.loading = false;
  126. console.error(e);
  127. }
  128. },
  129. scrolltolower() {
  130. // 滚动到底部触发
  131. if (this.noMore) return;
  132. this.getList();
  133. },
  134. },
  135. };
  136. </script>
  137. <style scoped lang="scss">
  138. .c_scroll_paging {
  139. height: 100%;
  140. box-sizing: border-box;
  141. /deep/ .uni-scroll-view {
  142. position: relative;
  143. .uni-scroll-view-content {
  144. position: absolute;
  145. top: 0;
  146. left: 0;
  147. }
  148. }
  149. .footer {
  150. display: flex;
  151. justify-content: center;
  152. /deep/ .u-divider {
  153. width: 200px;
  154. margin: 0;
  155. }
  156. }
  157. .footer-empty-box {
  158. align-items: center;
  159. height: 100%;
  160. background-color: #fff;
  161. border-radius: $bd-radius;
  162. }
  163. }
  164. </style>