"use client"; import { useState } from "react"; import { Lock, ArrowRightLeft, Copy, Eraser } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { toast } from "sonner"; export default function Base64Page() { const [input, setInput] = useState(""); const [output, setOutput] = useState(""); const [mode, setMode] = useState<"encode" | "decode">("encode"); const handleEncode = () => { if (!input) { toast.warning("请输入要编码的内容"); return; } try { const encoded = btoa(unescape(encodeURIComponent(input))); setOutput(encoded); toast.success("编码成功"); } catch { toast.error("编码失败"); } }; const handleDecode = () => { if (!input) { toast.warning("请输入要解码的内容"); return; } try { const decoded = decodeURIComponent(escape(atob(input))); setOutput(decoded); toast.success("解码成功"); } catch { toast.error("解码失败,请检查输入是否为有效的 Base64 字符串"); } }; const handleConvert = () => { if (mode === "encode") { handleEncode(); } else { handleDecode(); } }; const copyToClipboard = async () => { if (!output) { toast.warning("没有可复制的内容"); return; } try { await navigator.clipboard.writeText(output); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const clearAll = () => { setInput(""); setOutput(""); }; const swapInputOutput = () => { setInput(output); setOutput(""); }; return (
Base64 编码与解码转换工具,支持中文