fei.wang
2025-04-30 722da005a5ec126bedf752ac6bd5c5c7f6172155
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<template>
    <view
        class="data-list-components safe-area-inset-bottom"
        :class="{ 'safe-area-inset-bottom': safeAreaInsetBottom }"
        :style="[dataListStyle]"
        @touchmove.stop.prevent
    >
    <u-search class="searchbg" shape="true" bg-color="rgba(255, 255, 255, 0.5)" :placeholder="place" :show-action="false" v-model="keyword" @search="searchxinxi"
                    :style="{top: topheight+'px',position: 'absolute',width: width,right: '5%'}"></u-search>
        <scroll-view
            class="scroll-view"
            :scroll-top="scrollTop"
            :scroll-y="scrollingEnabled"
            :lower-threshold="lowerThreshold"
            :enable-back-to-top="enableBackToTop"
            @scroll="handleScroll"
            @scrolltolower="handleScrolltolower"
            @touchstart="handleTouchStart"
            @touchmove="handleTouchMove"
            @touchend="handleTouchEnd"
        >
            <view class="scroll-content" :style="[scrollContentStyle]">
                <view class="pull-down-view">
                    <slot name="refresh" v-if="$slots.refresh"></slot>
                    <view class="refresh-view" v-else>
                        <view class="loading-animation"></view>
                        <text class="pull-down-text">{{ refreshText[refreshStatus] }}</text>
                    </view>
                </view>
                <view class="scroll-list"><slot></slot></view>
                <view class="loading-view" v-if="loadingMask">
                    <view class="loading-animation"></view>
                    <text class="loading-text">加载中...</text>
                </view>
                <view class="empty-view" v-if="showEmpty">
                    <slot name="empty" v-if="$slots.empty"></slot>
                    <view class="empty-content" v-else>
                        <image class="empty-image" :src="emptyImage || defaultEmptyImage" mode=""></image>
                        <text class="empty-text">{{ emptyText }}</text>
                    </view>
                </view>
                <view class="pull-up-view" v-else>
                    <slot name="load" v-if="$slots.load"></slot>
                    <view class="load-view" v-else>
                        <view class="loading-animation" v-if="loadStatus != 'nomore'"></view>
                        <text class="loading-text">{{ loadText[loadStatus] }}</text>
                    </view>
                </view>
            </view>
            <!-- <page-pagination :total="pagep.totalp" layout="total,first,prev,page,next,last,limit,jumper"></page-pagination> -->
        </scroll-view>
        <page-pagination style="position:absolute; zIndex: 9999 " :total="pagep.totalp" layout="total,first,prev,page,next,last,limit,jumper" @change="change" @sizeChange="sizeChange"></page-pagination> 
    </view>
</template>
 
<script>
/**
 * data-list 数据列表
 * @description 数据列表展示,上拉加载更多数据,下拉刷新数据
 * @tips 如果父组件有设置padding,可能造成组件高度不正确的问题
 * @property {Number, String} page 默认分页
 * @property {Number, String} size 默认分页大小
 * @property {Boolean} loading 是否显示首次加载时的loading遮罩层
 * @property {Boolean} safeAreaInsetBottom 是否开启底部安全区域适配
 * @property {String} emptyImage 空数据提示图片
 * @property {String} emptyText 空数据提示文字
 * @property {Boolean} enableBackToTop iOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部
 * @property {Number, String} refresherThreshold 下拉刷新阈值(单位px),触发 refresherrefresh 事件
 * @property {Number, String} lowerThreshold 距底部多远时(单位px),触发 scrolltolower 事件
 * @methods {Function} loadData 加载数据方法
 * @methods {Function} refreshData 刷新数据方法
 * @methods {Function} computedHeight 重新计算高度方法(有时候页面某些地方布局被改变了,会造成组件高度不能撑满的问题,就调用此方法)
 * @event {Function} load 加载 事件(参数一:分页请求参数,参数二:回调方法,需要将列表和数据总数回调回去计算 {list:数据列表对象,total:后端数据总数})
 * @event {Function} refresh 刷新 事件(参数一:分页请求参数,参数二:回调方法,需要将列表和数据总数回调回去计算 {list:数据列表对象,total:后端数据总数})
 * @event {Function} scroll 滚动 事件
 * @event {Function} scrolltolower 上拉触底 事件
 * @event {Function} refresherpulling 正在下拉 事件
 * @event {Function} refresherrestore 下拉刷新被复位 事件
 * @event {Function} refresherabort 下拉刷新被终止 事件
 * @event {Function} refresherrefresh 下拉刷新被触发 事件
 * @example <data-list class="data-list" ref="list" @load="handleLoad" @refresh="handleRefresh" @scroll="handleScroll"></data-list>
 */
    import {
        findwarning
    } from '@/config/api.js';
import emptyImages from './emptyImage.js';
export default {
    name: 'data-list',
    props: {
        width: {
            type: [Number, String],
            default: 1
        },
        page: { 
            type: [Number, String],
            default: 1
        },
        size: {
            type: [Number, String],
            default: 10
        },
        loading: {
            type: Boolean,
            default: true
        },
        safeAreaInsetBottom: {
            type: Boolean,
            default: true
        },
        emptyImage: {
            type: String
        },
        emptyText: {
            type: String,
            default: '暂无数据~'
        },
        enableBackToTop: {
            type: Boolean,
            default: false
        },
        refresherThreshold: {
            type: [Number, String],
            default: 80
        },
        lowerThreshold: {
            type: [Number, String],
            default: 40
        }
    },
    data() {
        return {
            pagep: {
                totalp: 0,
                pageSizep: 10,
                currentPagep: 2
            },
            place:'请输入编号',
            keyword:'',
            topheight:55,
            list: [], // 数据列表
            total: -1, // 数据总数
            currentPage: 1, // 当前分页页码
            currentSize: 10, // 当前分页大小
            showEmpty: false, // 是否显示空数据
            waiting: false, // 是否等待加载中
            loadingMask: this.loading, // 是否显示加载中遮罩
            autoLoad: true, // 是否自动加载
            scrollTop: 0, // 设置竖向滚动条位置
            currentScrollTop: 0, // 当前竖向滚动条位置
            scrollingEnabled: true, // 是否启用竖向滚动
            pullingDown: false, // 是否正在下拉
            pullDownHeight: 0, // 下拉高度
            pullDownTimeStamp: 0, // 下拉时的时间戳
            loadStatus: 'loading',
            loadText: {
                loading: '加载中~',
                nomore: '没有更多啦~'
            },
            refreshStatus: 'soon',
            refreshText: {
                soon: '继续下拉即可刷新~',
                sure: '松开即可刷新~',
                refreshing: '正在刷新中~'
            },
            defaultEmptyImage: emptyImages.data,
            componentsHeight: 'auto' // 组件高度属性
        };
    },
    computed: {
        dataListStyle() {
            const { componentsHeight } = this;
            return {
                height: componentsHeight
            };
        },
        scrollContentStyle() {
            const style = {};
            const { pullDownHeight, pullingDown } = this;
            style.transform = pullingDown ? `translateY(${pullDownHeight}px)` : `translateY(0px)`;
            style.transition = pullingDown ? `transform .1s linear` : `transform 0.3s cubic-bezier(0.19,1.64,0.42,0.72)`;
            return style;
        }
    },
    mounted() {
        if (this.width=='90%') {
            this.place = '请输入名称'
        }else{
            this.place = '请输入编号'
        }
         this.topheight=this.vuex_custom_bar_height+15
        this.handleInit();
    },
    methods: {
        change(currentPage, type) { // 当前页改变
            this.pagep.currentPagep = currentPage;
            console.log("点击了" + type + ",当前页:" + currentPage);
        uni.showLoading({
            title: '加载中'
        });
            this.waiting = true;
            this.$emit('load', { page: this.pagep.currentPagep, size: this.pagep.pageSizep }, res => {
                
                setTimeout(function () {
                    uni.hideLoading();
                }, 1500);
                this.waiting = false;
                this.loadingMask = false;
                this.list = res.list;
                this.total = res.total;
                this.pagep.totalp = res.total;
                this.currentPage++;
                this.handleComputeData();
                this.computedHeight();
            });
        },
        sizeChange(pageSize) { // 每页条数改变
            this.pagep.pageSizep = pageSize;
            console.log("每页", pageSize, "条");
        },
        changeTotal() { // 更改总条数
            this.pagep.totalp = 555
        },
        changeCurrentPage() { // 更改当前页
            this.pagep.currentPagep = 3
        },
        // 数据加载
        loadData() {
            this.waiting = true;
            this.$emit('load', { page: this.currentPage, size: this.currentSize }, res => {
                this.waiting = false;
                this.loadingMask = false;
                this.list = res.list;
                this.total = res.total;
                this.pagep.totalp = res.total;
                this.currentPage++;
                this.handleComputeData();
                this.computedHeight();
            });
        },
        // 刷新数据
        searchxinxi() {
            const params = {
                current: this.page,
                size: this.size,
                objectid:this.keyword,
            }
            
            // findwarning(params).then((res) => {
            //     console.log(res);
            //     this.list=[]
            //     if (res.code == 0) {
                    
            //         let list = [];
            //         setTimeout(() => {
            //             let list = [];
            //             for (var i = 0; i < res.data.records.length; i++) {
            //                 this.list.push(res.data.records[i]);
            //             }
            //         //     console.log(list);
            //         //     this.list = [...this.list, ...list];
            //         //     callback({ list: this.list, total: res.data.total });
            //         }, 1500);
            //         this.autoLoad = true;
            //         this.pullingDown = false;
            //         this.pullDownHeight = 0;
            //         // this.list = list
            //         this.total = res.data.total;
            //         this.currentPage++;
            //         console.log(this.list);
            //         this.handleComputeData();
            //         this.computedHeight();
            //         this.$emit('refresherrestore');
            //     }
            // })
            this.currentPage = this.page;
            this.currentSize = this.size;
            this.$emit('refresh', { page: this.currentPage, size: this.currentSize, objectid:this.keyword}, res => {
                
                this.autoLoad = true;
                this.pullingDown = false;
                this.pullDownHeight = 0;
                this.list = res.list;
                this.total = res.total;
                this.pagep.totalp = res.total;
                this.currentPage++;
                this.handleComputeData();
                this.computedHeight();
                this.$emit('refresherrestore');
            });
        },
        // 刷新数据
        refreshData() {
            this.currentPage = this.page;
            this.currentSize = this.size;
            this.$emit('refresh', { page: this.currentPage, size: this.currentSize }, res => {
                this.autoLoad = true;
                this.pullingDown = false;
                this.pullDownHeight = 0;
                this.list = res.list;
                this.total = res.total;
                this.pagep.totalp = res.total;
                this.currentPage++;
                this.handleComputeData();
                this.computedHeight();
                this.$emit('refresherrestore');
            });
        },
        // 计算高度
        computedHeight() {
            this.getRect('.data-list-components').then(res => {
                
                // #ifdef H5
                this.componentsHeight = `calc(100vh - ${res.top-200}px - var(--window-top))`; 
                // #endif
                // #ifndef H5
                this.componentsHeight = `${this.vuex_custom_index_height-168}px`;
                // #endif
                this.autoLoad = true;
                this.$nextTick(() => {
                    this.handleAutoLoadData();  
                });
            });
        },
        // 自动加载数据
        async handleAutoLoadData() {
            if (this.autoLoad && this.loadStatus != 'nomore') {
                const components = await this.getRect('.data-list-components');
                const scrolList = await this.getRect('.scroll-list');
                if (scrolList.height <= components.height) {
                
                    this.loadData();
                } else {
                    this.autoLoad = false;
                }
            }
        },
        // 计算数据
        handleComputeData() {
            const len = this.list.length;
            const total = this.total;
            if (len == 0 || total == 0) {
                this.autoLoad = false;
                this.showEmpty = true;
            } else if (len >= this.total) {
                this.autoLoad = false;
                this.showEmpty = false;
                this.loadStatus = 'nomore';
            } else if (len < this.total) {
                this.showEmpty = false;
                this.loadStatus = 'loading';
            }
        },
        // 上拉触底事件
        handleScrolltolower(e) {
            this.$emit('scrolltolower', e);
            if (this.loadStatus == 'nomore' || this.waiting) return;
            this.loadData();
        },
        // 滚动事件
        handleScroll(e) {
            this.currentScrollTop = e.detail.scrollTop;
            this.$emit('scroll', e);
        },
        // 触摸按下处理
        handleTouchStart(e) {
            this.pullingDown = true;
            this.currentTouchStartY = e.touches[0].clientY;
        },
        // 触摸按下滑动处理
        handleTouchMove(e) {
            this.$emit('refresherpulling', e);
            if (e.touches[0].clientY < this.currentTouchStartY) return;
            const shakeInterval = 1000 / 60;
            const currentTimeStamp = new Date().getTime();
            if (this.pullDownTimeStamp && currentTimeStamp - this.pullDownTimeStamp <= shakeInterval) return;
            this.pullDownTimeStamp = currentTimeStamp;
            const currentTouchMoveY = e.touches[0].clientY;
            const movingDistance = (currentTouchMoveY - this.currentTouchStartY) * 0.7;
            const moreDistance = movingDistance > this.refresherThreshold ? (movingDistance - this.refresherThreshold) * 0.3 : 0;
            const computeDistance =
                movingDistance > this.refresherThreshold ? this.refresherThreshold + moreDistance : movingDistance + moreDistance;
            if (computeDistance >= this.refresherThreshold) {
                this.refreshStatus = 'sure';
            } else {
                this.refreshStatus = 'soon';
            }
            this.pullDownHeight = computeDistance;
        },
        // 触摸松开处理
        handleTouchEnd(e) {
            if (this.pullDownHeight < this.refresherThreshold) {
                this.pullingDown = false;
                this.pullDownHeight = 0;
                this.refreshStatus = 'soon';
                this.$emit('refresherabort', e);
            } else {
                this.pullDownHeight = this.refresherThreshold;
                this.refreshStatus = 'refreshing';
                this.$emit('refresherrefresh', e);
                this.refreshData();
            }
        },
        // 初始化
        handleInit() {
            this.currentPage = this.page;
            this.currentSize = this.size;
            this.computedHeight();
        },
        // 查询dom节点信息
        getRect(selector, all) {
            return new Promise(resolve => {
                uni.createSelectorQuery()
                    .in(this)
                    [all ? 'selectAll' : 'select'](selector)
                    .boundingClientRect(rect => {
                        if (all && Array.isArray(rect) && rect.length) {
                            resolve(rect);
                        }
                        if (!all && rect) {
                            resolve(rect);
                        }
                    })
                    .exec();
            });
        }
    }
};
</script>
 
<style scoped lang="scss">
.data-list-components {
    height: 100%;
    .scroll-view {
        height: 100%;
        .scroll-content {
            width: 100%;
            height: 100%;
            position: relative;
            .empty-view {
                width: 100%;
                height: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                .empty-content {
                    margin: auto;
                    display: flex;
                    align-items: center;
                    flex-direction: column;
                    .empty-image {
                        width: 400rpx;
                        height: 400rpx;
                    }
                    .empty-text {
                        color: #606266;
                        margin-top: 20rpx;
                    }
                }
            }
            .pull-down-view {
                top: 0;
                left: 0;
                width: 100%;
                padding: 30rpx 0;
                display: flex;
                position: absolute;
                align-items: center;
                flex-direction: column;
                justify-content: center;
                transform: translateY(-100%);
                .refresh-view {
                    display: flex;
                    align-items: center;
                    flex-direction: column;
                    .loading-animation {
                        width: 50rpx;
                        height: 50rpx;
                    }
                    .pull-down-text {
                        color: #606266;
                        font-size: 24rpx;
                        margin-top: 10rpx;
                    }
                }
            }
            .scroll-list {
                display: flex;
                flex-direction: column;
            }
            .loading-view {
                width: 100%;
                height: 100%;
                display: flex;
                align-items: center;
                flex-direction: column;
                justify-content: center;
                .loading-animation {
                    width: 40rpx;
                    height: 40rpx;
                }
                .loading-text {
                    color: #606266;
                    font-size: 24rpx;
                    margin-top: 10rpx;
                }
            }
            .pull-up-view {
                width: 100%;
                padding: 15rpx 0;
                display: flex;
                align-items: center;
                justify-content: center;
                .load-view {
                    display: flex;
                    align-items: center;
                    .loading-animation {
                        width: 30rpx;
                        height: 30rpx;
                    }
                    .loading-text {
                        color: #606266;
                        font-size: 24rpx;
                        margin-left: 20rpx;
                    }
                }
            }
            .loading-animation {
                position: relative;
                &::after,
                &::before {
                    left: 0;
                    width: 100%;
                    position: absolute;
                    border: 0 solid currentColor;
                    background-color: #b7b7b7;
                }
                &::after {
                    content: '';
                    top: -25%;
                    z-index: 1;
                    height: 100%;
                    border-radius: 10%;
                    animation: spin 0.6s -0.1s linear infinite;
                }
                &::before {
                    content: '';
                    bottom: -9%;
                    height: 10%;
                    background: #000;
                    border-radius: 50%;
                    opacity: 0.2;
                    animation: shadow 0.6s -0.1s linear infinite;
                }
                @keyframes spin {
                    17% {
                        border-bottom-right-radius: 10%;
                    }
                    25% {
                        transform: translateY(25%) rotate(22.5deg);
                    }
                    50% {
                        border-bottom-right-radius: 100%;
                        transform: translateY(50%) scale(1, 0.9) rotate(45deg);
                    }
                    75% {
                        transform: translateY(25%) rotate(67.5deg);
                    }
                    100% {
                        transform: translateY(0) rotate(90deg);
                    }
                }
                @keyframes shadow {
                    50% {
                        transform: scale(1.25, 1);
                    }
                }
            }
        }
    }
}
</style>