| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <template>
- <!-- 列表 -->
- <scroll-view
- class="scroll-view"
- scroll-y
- refresher-enabled
- :refresher-triggered="isRefreshing"
- @refresherrefresh="onRefresh"
- @scrolltolower="onLoadMore"
- :style="{ height: height }"
- >
- <!-- 空状态 -->
- <view v-if="!loading && !dataList.length" class="empty-state">
- <view class="flex-d flex-a-c" style="padding-top: 25vh" v-if="slotEmpty">
- <image src="https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/no-data.png" style="width: 100%; height: 260rpx" mode="heightFix"></image>
- <view class="common-title" style="padding: 33rpx 0 20rpx 0">暂无内容</view>
- <view class="common-text" v-if="emptyText">{{ emptyText }}</view>
- </view>
- <u-empty v-else mode="list" text="暂无扫描记录" margin-top="200"></u-empty>
- </view>
- <slot></slot>
- <!-- 加载更多 -->
- <view class="load-more" v-if="dataList.length > 0">
- <u-divider :bg-color="bgColor">{{ hasMore ? "加载中..." : "我是有底线的" }}</u-divider>
- </view>
- </scroll-view>
- </template>
- <script>
- export default {
- data() {
- return {
- isRefreshing: false,
- loading: false,
- page: 1,
- hasMore: true,
- dataList: [],
- };
- },
- props: {
- height: {
- type: String,
- default: "calc(100vh - 88rpx)",
- },
- bgColor: {
- type: String,
- default: "#F5F5F5",
- },
- url: {
- type: String,
- required: true,
- default: "/token/order/scanLogs",
- },
- pageSize: {
- type: Number,
- default: 10,
- },
- slotEmpty: {
- type: Boolean,
- default: false,
- },
- emptyText: {
- type: String,
- },
- params: {
- type: Object,
- default: {},
- },
- immediate: {
- type: Boolean,
- default: true,
- },
- },
- mounted() {
- console.log(this.immediate, "immediate");
- if (this.immediate) {
- this.loadData(true);
- }
- },
- methods: {
- // 加载数据
- async loadData(isRefresh = false, params = {}) {
- if (isRefresh) {
- this.page = 1;
- this.hasMore = true;
- }
- if (!this.hasMore || this.loading) return;
- this.loading = true;
- try {
- const res = await this.fetchBookList(params);
- if (isRefresh) {
- this.dataList = res.list;
- } else {
- this.dataList = [...this.dataList, ...res.list];
- }
- this.$emit("updateList", this.dataList);
- this.hasMore = res.hasMore;
- this.page++;
- } catch (error) {
- uni.showToast({
- title: "加载失败",
- icon: "none",
- });
- } finally {
- this.loading = false;
- if (isRefresh) {
- this.isRefreshing = false;
- }
- // #ifdef MP-ALIPAY
- my.stopPullDownRefresh()
- // #endif
- }
- },
- // 模拟获取数据
- fetchBookList(params = {}) {
- return new Promise((resolve) => {
- uni.showLoading({
- title: "加载中...",
- });
- uni.$u.http
- .get(this.url, {
- pageSize: this.pageSize,
- pageNum: this.page,
- ...params,
- })
- .then((res) => {
- resolve({
- list: res.rows,
- hasMore: res.rows ? res.rows.length < res.total - this.dataList.length : false,
- });
- })
- .finally(() => {
- uni.hideLoading();
- });
- });
- },
- reloadData() {
- this.page = 1;
- this.fetchBookList();
- },
- // 下拉刷新
- async onRefresh() {
- if (this.loading) return;
- this.isRefreshing = true;
- await this.loadData(true, this.params);
- },
- // 上拉加载更多
- async onLoadMore() {
- if (this.loading || !this.hasMore) return;
- await this.loadData(false, this.params);
- },
- },
- };
- </script>
- <style lang="scss">
- .scroll-view {
- height: calc(100vh - 88rpx);
- }
- .load-more {
- width: 100%;
- display: flex;
- color: #999999;
- font-size: 24rpx;
- padding: 30rpx 0;
- justify-content: center;
- padding-bottom: 40rpx;
- }
- </style>
|