refactor: 前端界面微调

This commit is contained in:
mofeng-git
2026-06-15 23:36:17 +08:00
parent e2c19d550c
commit f1e362a820
14 changed files with 141 additions and 95 deletions

View File

@@ -7,17 +7,34 @@ export function cn(...inputs: ClassValue[]) {
/**
* Generate a UUID v4 with fallback for older browsers
* Uses crypto.randomUUID() if available, otherwise falls back to manual generation
* Uses crypto.randomUUID() in secure contexts and crypto.getRandomValues()
* where randomUUID is unavailable, such as HTTP LAN access.
*/
export function generateUUID(): string {
// Use native API if available (modern browsers)
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID()
const webCrypto = globalThis.crypto
if (typeof webCrypto?.randomUUID === 'function') {
return webCrypto.randomUUID()
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
const bytes = new Uint8Array(16)
if (typeof webCrypto?.getRandomValues === 'function') {
webCrypto.getRandomValues(bytes)
} else {
for (let i = 0; i < bytes.length; i++) {
bytes[i] = Math.floor(Math.random() * 256)
}
}
bytes[6] = (bytes[6]! & 0x0f) | 0x40
bytes[8] = (bytes[8]! & 0x3f) | 0x80
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0'))
return [
hex.slice(0, 4).join(''),
hex.slice(4, 6).join(''),
hex.slice(6, 8).join(''),
hex.slice(8, 10).join(''),
hex.slice(10, 16).join(''),
].join('-')
}