"use client"; import { useState } from "react"; import { Copy, Eraser, RefreshCw, FileText } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { toast } from "sonner"; export default function UuidPage() { const [uuids, setUuids] = useState([]); const [count, setCount] = useState(1); const [format, setFormat] = useState<"default" | "uppercase" | "nohyphen">( "default" ); const generateUUID = (): string => { // 生成符合 RFC 4122 版本 4 的 UUID const template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; return template.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === "x" ? r : (r & 0x3) | 0x8; return v.toString(16); }); }; const formatUUID = (uuid: string): string => { switch (format) { case "uppercase": return uuid.toUpperCase(); case "nohyphen": return uuid.replace(/-/g, ""); default: return uuid; } }; const handleGenerate = () => { const newUuids = Array.from({ length: count }, () => formatUUID(generateUUID()) ); setUuids(newUuids); toast.success(`已生成 ${count} 个 UUID`); }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const copyAll = async () => { if (uuids.length === 0) { toast.warning("没有可复制的内容"); return; } try { await navigator.clipboard.writeText(uuids.join("\n")); toast.success("已复制所有 UUID"); } catch { toast.error("复制失败"); } }; const clearAll = () => { setUuids([]); }; return (
{/* Page Header */}

UUID 生成

生成符合 RFC 4122 标准的 UUID v4 通用唯一标识符

{/* Action Bar */}
setCount(parseInt(e.target.value) || 1)} className="w-20" />
{/* Result Panel */} 生成结果 共 {uuids.length} 个
{uuids.length > 0 ? ( uuids.map((uuid, index) => (
{uuid}
)) ) : (

点击生成按钮创建 UUID

)}
{/* Info Card */} 💡 使用说明

什么是 UUID

UUID(Universally Unique Identifier)是一种 128 位的标识符,用于在分布式系统中唯一标识信息。

UUID v4 特点

  • 基于随机数生成
  • 122 位有效随机位
  • 重复概率极低

常见用途

  • 数据库主键
  • 会话标识
  • 分布式系统 ID
); }