Liuyi
2024-11-26 53dc2bac460d2ff210aa9523717753ae1fd2c159
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
// #ifndef APP-NVUE
// 计算版本
export function compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)
    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }
    for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i], 10)
        const num2 = parseInt(v2[i], 10)
 
        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }
    return 0
}
const systemInfo = uni.getSystemInfoSync();
 
function gte(version) {
    // 截止 2023-03-22 mac pc小程序不支持 canvas 2d
    let {
        SDKVersion,
        platform
    } = systemInfo;
    // #ifdef MP-ALIPAY
    SDKVersion = my.SDKVersion
    // #endif
    // #ifdef MP-WEIXIN
    return platform !== 'mac' && compareVersion(SDKVersion, version) >= 0;
    // #endif
    return compareVersion(SDKVersion, version) >= 0;
}
 
 
export function canIUseCanvas2d() {
    // #ifdef MP-WEIXIN
    return gte('2.9.0');
    // #endif
    // #ifdef MP-ALIPAY
    return gte('2.7.0');
    // #endif
    // #ifdef MP-TOUTIAO
    return gte('1.78.0');
    // #endif
    return false
}
 
export function convertTouchesToArray(touches) {
    // 如果 touches 是一个数组,则直接返回它
    if (Array.isArray(touches)) {
        return touches;
    }
    // 如果touches是一个对象,则转换为数组
    if (typeof touches === 'object' && touches !== null) {
        return Object.values(touches);
    }
    // 对于其他类型,直接返回它
    return touches;
}
 
export function wrapTouch(event) {
    for (let i = 0; i < event.touches.length; ++i) {
        const touch = event.touches[i];
        touch.offsetX = touch.x;
        touch.offsetY = touch.y;
    }
    return event;
}
export const devicePixelRatio = uni.getSystemInfoSync().pixelRatio
// #endif
// #ifdef APP-NVUE
export function base64ToPath(base64) {
    return new Promise((resolve, reject) => {
        const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
        const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
        bitmap.loadBase64Data(base64, () => {
            if (!format) {
                reject(new Error('ERROR_BASE64SRC_PARSE'))
            }
            const time = new Date().getTime();
            const filePath = `_doc/uniapp_temp/${time}.${format}`
 
            bitmap.save(filePath, {},
                () => {
                    bitmap.clear()
                    resolve(filePath)
                },
                (error) => {
                    bitmap.clear()
                    console.error(`${JSON.stringify(error)}`)
                    reject(error)
                })
        }, (error) => {
            bitmap.clear()
            console.error(`${JSON.stringify(error)}`)
            reject(error)
        })
    })
}
// #endif
 
 
export function sleep(time) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(true)
        }, time)
    })
}
 
 
export function getRect(selector, options = {}) {
    const typeDefault = 'boundingClientRect'
    const {
        context,
        type = typeDefault
    } = options
    return new Promise((resolve, reject) => {
        const dom = uni.createSelectorQuery().in(context).select(selector);
        const result = (rect) => {
            if (rect) {
                resolve(rect)
            } else {
                reject()
            }
        }
        if (type == typeDefault) {
            dom[type](result).exec()
        } else {
            dom[type]({
                node: true,
                size: true,
                rect: true
            }, result).exec()
        }
    });
};