11 lines
583 B
TypeScript
11 lines
583 B
TypeScript
// 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)}`
|
||
}
|