UnisKB/ui/src/views/mindmap/workbench/utils/uuid.ts

11 lines
583 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// crypto.randomUUID 仅在安全上下文(HTTPS/localhost)可用;管理平台可能以 HTTP 访问,需要降级。
export function uuid(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') return crypto.randomUUID()
const buf = new Uint8Array(16)
crypto.getRandomValues(buf)
buf[6] = (buf[6] & 0x0f) | 0x40
buf[8] = (buf[8] & 0x3f) | 0x80
const hex = Array.from(buf, b => b.toString(16).padStart(2, '0')).join('')
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
}