linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
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
// [z-paging]通用布局相关模块
 
// #ifdef APP-NVUE
const weexDom = weex.requireModule('dom');
// #endif
 
export default {
    data() {
        return {
            systemInfo: null,
            cssSafeAreaInsetBottom: -1,
        }
    },
    computed: {
        windowTop() {
            if (!this.systemInfo) return 0;
            //暂时修复vue3中隐藏系统导航栏后windowTop获取不正确的问题,具体bug详见https://ask.dcloud.net.cn/question/141634
            //感谢litangyu!!https://github.com/SmileZXLee/uni-z-paging/issues/25
            // #ifdef VUE3 && H5
            const pageHeadNode = document.getElementsByTagName("uni-page-head");
            if (!pageHeadNode.length) return 0;
            // #endif
            return this.systemInfo.windowTop || 0;
        },
        safeAreaBottom() {
            if (!this.systemInfo) return 0;
            let safeAreaBottom = 0;
            // #ifdef APP-PLUS
            safeAreaBottom = this.systemInfo.safeAreaInsets.bottom || 0 ;
            // #endif
            // #ifndef APP-PLUS
            safeAreaBottom = Math.max(this.cssSafeAreaInsetBottom, 0);
            // #endif
            return safeAreaBottom;
        },
        isOldWebView() {
            // #ifndef APP-NVUE || MP-KUAISHOU
            try {
                const systemInfos = uni.getSystemInfoSync().system.split(' ');
                const deviceType = systemInfos[0];
                const version = parseInt(systemInfos[1]);
                if ((deviceType === 'iOS' && version <= 10) || (deviceType === 'Android' && version <= 6)) {
                    return true;
                }
            } catch(e) {
                return false;
            }
            // #endif
            return false;
        },
        zSlots() {
            // #ifdef VUE2
            
            // #ifdef MP-ALIPAY
            return this.$slots;
            // #endif
            
            return this.$scopedSlots || this.$slots;
            // #endif
            
            return this.$slots;
        }
    },
    methods: {
        //获取节点尺寸
        _getNodeClientRect(select, inDom = true, scrollOffset = false) {
            // #ifdef APP-NVUE
            select = select.replace(/[.|#]/g, '');
            const ref = this.$refs[select];
            return new Promise((resolve, reject) => {
                if (ref) {
                    weexDom.getComponentRect(ref, option => {
                        resolve(option && option.result ? [option.size] : false);
                    })
                } else {
                    resolve(false);
                }
            });
            return;
            // #endif
            //#ifdef MP-ALIPAY
            inDom = false;
            //#endif
            let res = !!inDom ? uni.createSelectorQuery().in(inDom === true ? this : inDom) : uni.createSelectorQuery();
            scrollOffset ? res.select(select).scrollOffset() : res.select(select).boundingClientRect();
            return new Promise((resolve, reject) => {
                res.exec(data => {
                    resolve((data && data != '' && data != undefined && data.length) ? data : false);
                });
            });
        },
        //获取slot="left"和slot="right"宽度并且更新布局
        _updateLeftAndRightWidth(targetStyle, parentNodePrefix) {
            this.$nextTick(() => {
                let delayTime = 0;
                // #ifdef MP-BAIDU
                delayTime = 10;
                // #endif
                setTimeout(() => {
                    ['left','right'].map(position => {
                        this._getNodeClientRect(`.${parentNodePrefix}-${position}`).then(res => {
                            this.$set(targetStyle, position, res ? res[0].width + 'px' : '0px');
                        });
                    })
                }, delayTime)
            })
        },
        //通过获取css设置的底部安全区域占位view高度设置bottom距离
        _getCssSafeAreaInsetBottom(success) {
            this._getNodeClientRect('.zp-safe-area-inset-bottom').then(res => {
                this.cssSafeAreaInsetBottom = res ? res[0].height : -1;
                res && success && success();
            });
        }
    }
}