wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
import { isArray, isEmpty, isNumber } from "lodash-es";
import { computed, getCurrentInstance, nextTick, ref } from "vue";
 
// 获取父组件
export function getParent(name: string, k1: string[], k2?: string[]) {
    const { proxy }: any = getCurrentInstance();
 
    const d = ref();
    let n = 10;
 
    const next = () => {
        let parent = proxy.$parent;
 
        while (parent) {
            if (parent.$options.name !== name) {
                parent = parent.$parent;
            } else {
                if (isArray(k2)) {
                    nextTick(() => {
                        const child: any = {};
 
                        (k2 || []).map((key: string) => {
                            if (proxy[key]) {
                                child[key] = proxy[key];
                            }
                        });
 
                        if (!parent.__children) {
                            parent.__children = [];
                        }
 
                        if (!isEmpty(child)) {
                            parent.__children.push(child);
                        }
                    });
                }
 
                return (k1 || []).reduce((res: any, key: string) => {
                    res[key] = parent[key];
 
                    return res;
                }, {});
            }
        }
 
        return parent || d.value;
    };
 
    return computed(() => next());
}
 
// 获取元素位置信息
export async function getRect(selector: string): Promise<any> {
    return new Promise((resolve) => {
        uni.createSelectorQuery()
            .select(selector)
            .boundingClientRect((res) => {
                resolve(res);
            })
            .exec();
    });
}
 
// 解析rpx
export function parseRpx(val: any): string {
    return isArray(val) ? val.map(parseRpx).join(" ") : isNumber(val) ? `${val}rpx` : val;
}
 
// px 转 rpx
export function px2Rpx(px: number) {
    return px / (uni.upx2px(100) / 100);
}