buddhism/uni_modules/lime-echart/components/l-echart/utils.js

147 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-12-05 16:45:28 +08:00
// #ifndef APP-NVUE
// 计算版本
export function compareVersion(v1, v2) {
2025-08-14 11:22:53 +08:00
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)
2023-12-05 16:45:28 +08:00
2025-08-14 11:22:53 +08:00
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
}
}
return 0
2023-12-05 16:45:28 +08:00
}
2025-08-14 11:22:53 +08:00
const systemInfo = uni.getSystemInfoSync()
2023-12-05 16:45:28 +08:00
function gte(version) {
2025-08-14 11:22:53 +08:00
// 截止 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
2023-12-05 16:45:28 +08:00
}
export function canIUseCanvas2d() {
2025-08-14 11:22:53 +08:00
// #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
2023-12-05 16:45:28 +08:00
}
export function convertTouchesToArray(touches) {
2025-08-14 11:22:53 +08:00
// 如果 touches 是一个数组,则直接返回它
if (Array.isArray(touches)) {
return touches
}
// 如果touches是一个对象则转换为数组
if (typeof touches === 'object' && touches !== null) {
return Object.values(touches)
}
// 对于其他类型,直接返回它
return touches
2023-12-05 16:45:28 +08:00
}
export function wrapTouch(event) {
2025-08-14 11:22:53 +08:00
for (let i = 0; i < event.touches.length; ++i) {
const touch = event.touches[i]
touch.offsetX = touch.x
touch.offsetY = touch.y
}
return event
2023-12-05 16:45:28 +08:00
}
export const devicePixelRatio = uni.getSystemInfoSync().pixelRatio
// #endif
// #ifdef APP-NVUE
export function base64ToPath(base64) {
2025-08-14 11:22:53 +08:00
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}`
2023-12-05 16:45:28 +08:00
2025-08-14 11:22:53 +08:00
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)
}
)
})
2023-12-05 16:45:28 +08:00
}
// #endif
export function sleep(time) {
2025-08-14 11:22:53 +08:00
return new Promise(resolve => {
setTimeout(() => {
resolve(true)
}, time)
})
2023-12-05 16:45:28 +08:00
}
export function getRect(selector, options = {}) {
2025-08-14 11:22:53 +08:00
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()
}
})
}