2026-01-22 17:55:45 +08:00
|
|
|
import path from 'path-browserify'
|
2026-01-22 17:55:35 +08:00
|
|
|
|
|
|
|
|
export function getCurrentYear(): Date {
|
2026-01-22 17:55:45 +08:00
|
|
|
return new Date()
|
2026-01-22 17:55:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 得到随机数
|
|
|
|
|
* @param min 最小值
|
|
|
|
|
* @param max 最大值
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export function getTrueRandomInt(min: number, max: number) {
|
2026-01-22 17:55:45 +08:00
|
|
|
const range = max - min + 1
|
|
|
|
|
const maxSafe = 0xFFFFFFFF // 32位最大无符号整数 (2^32 - 1)
|
|
|
|
|
let randomValue = 0
|
2026-01-22 17:55:35 +08:00
|
|
|
|
|
|
|
|
do {
|
2026-01-22 17:55:45 +08:00
|
|
|
const buffer = new Uint32Array(1)
|
|
|
|
|
window.crypto.getRandomValues(buffer)
|
|
|
|
|
randomValue = (buffer[0]! / (maxSafe + 1)) * range // 转换为[0, range)的浮点数
|
|
|
|
|
} while (randomValue >= range) // 拒绝采样避免偏差
|
2026-01-22 17:55:35 +08:00
|
|
|
|
2026-01-22 17:55:45 +08:00
|
|
|
return Math.floor(randomValue) + min
|
2026-01-22 17:55:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 路径拼接
|
|
|
|
|
*/
|
|
|
|
|
export function browserPathJoin(base: string, ...paths: string[]) {
|
2026-01-22 17:55:45 +08:00
|
|
|
const [protocol, ...rest] = base.split('://')
|
2026-01-22 17:55:35 +08:00
|
|
|
if (rest.length > 0) {
|
2026-01-22 17:55:45 +08:00
|
|
|
const pathPart = rest.join('://').replace(/\/+/g, '/')
|
|
|
|
|
return `${protocol}://${path.join(pathPart, ...paths)}`
|
2026-01-22 17:55:35 +08:00
|
|
|
}
|
2026-01-22 17:55:45 +08:00
|
|
|
return path.join(base, ...paths)
|
2026-01-22 17:55:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下一个tick后执行
|
|
|
|
|
*/
|
|
|
|
|
export function nextTickSleep() {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
nextTick(() => {
|
2026-01-22 17:55:45 +08:00
|
|
|
resolve(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-01-22 17:55:35 +08:00
|
|
|
}
|