3
tj
2025-05-22 14764dd7679369a650ad1d60be62aacc863755a1
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
<template>
    <view class="waterfall-item">
        <slot name="default"></slot>
    </view>
</template>
 
<script>
    import { wait } from './js/utils.js';
    export default {
        name:"helangWaterfallItem",
        options:{
            virtualHost: true
        },
        props:{
            colIndex:{
                type:Number,
                default:0
            },
            reportHeightTime:{
                type:Number | String | undefined | null,
                default:''
            }
        },
        watch:{
            "$props.reportHeightTime"(newValue, oldValue){
                if(newValue !== oldValue){                    
                    this.postHeight({
                        reportHeightTimeChange:true
                    });
                }
            }
        },
        data() {
            return {
                
            };
        },
        mounted() {
            this.postHeight();
        },
        methods:{
            async postHeight(params){
                const { reportHeightTimeChange = false } = params ?? {};
                // 添加一个异步的宏任务,部分低性能的移动设备会因为渲染问题导致上报的高度存在错误问题
                await wait(10);
                // 当前高度
                const currentHeight = await this.getHeight();
                // 显示高度
                const displayHeight = this.displayHeight || 0;
                // 上报高度
                let height = currentHeight - displayHeight;
                
                // 上报高度为0时,取消上报。防止用户调用 update 函数时错误的reportHeight参数情况 
                if(height === 0){
                    return;
                }
                
                this.$emit("height",{
                    colIndex:this.$props.colIndex,
                    height,
                    reportHeightTimeChange
                });
                // 缓存渲染高度
                this.displayHeight = currentHeight;
            },
            getHeight(){
                const query = uni.createSelectorQuery().in(this);
                return new Promise((resolve,reject)=>{
                    query.select('.waterfall-item').boundingClientRect((data) => {
                        // 节点高度
                        const height = Math.floor(data.height) || 0;
                        resolve(height);
                    }).exec();
                })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .waterfall-item{
        & + .waterfall-item{
            margin-top:20rpx;
        }
    }
</style>