"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { useToast } from "@/components/ui/use-toast"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Save, Trash, CheckCircle2, XCircle, Pencil } from "lucide-react"; import Link from "next/link"; interface SavedConfig { id: string; name: string; secretId: string; secretKey: string; zoneId: string; } interface EOApiResponse { Response: { RequestId: string; JobId: string; FailedList: string[]; Error?: { Message: string; Code: string; }; }; } export function CleanCacheForm() { const [secretId, setSecretId] = useState(""); const [secretKey, setSecretKey] = useState(""); const [zoneId, setZoneId] = useState(""); const [loading, setLoading] = useState(false); const [dialogOpen, setDialogOpen] = useState(false); const [result, setResult] = useState(null); const [savedConfigs, setSavedConfigs] = useState([]); const [saveDialogOpen, setSaveDialogOpen] = useState(false); const [configName, setConfigName] = useState(""); const [editingConfigId, setEditingConfigId] = useState(null); const [urls, setUrls] = useState(""); const [prefixes, setPrefixes] = useState(""); const [hosts, setHosts] = useState(""); const [tags, setTags] = useState(""); const { toast } = useToast(); useEffect(() => { // 加载保存的配置列表 const configs = localStorage.getItem("savedConfigs"); if (configs) { setSavedConfigs(JSON.parse(configs)); } }, []); const saveConfig = () => { if (!configName.trim()) { toast({ variant: "destructive", title: "错误", description: "请输入配置名称", }); return; } if (!secretId || !secretKey || !zoneId) { toast({ variant: "destructive", title: "错误", description: "请填写完整的配置信息!", }); return; } let updatedConfigs: SavedConfig[]; if (editingConfigId) { // 编辑现有配置 updatedConfigs = savedConfigs.map(config => { if (config.id === editingConfigId) { return { ...config, name: configName, secretId, secretKey, zoneId }; } return config; }); toast({ title: "成功", description: "配置已更新", }); } else { // 添加新配置 const newConfig: SavedConfig = { id: Date.now().toString(), name: configName, secretId, secretKey, zoneId, }; updatedConfigs = [...savedConfigs, newConfig]; toast({ title: "成功", description: "配置已保存", }); } setSavedConfigs(updatedConfigs); localStorage.setItem("savedConfigs", JSON.stringify(updatedConfigs)); setSaveDialogOpen(false); setConfigName(""); setEditingConfigId(null); }; const editConfig = (config: SavedConfig) => { setConfigName(config.name); setSecretId(config.secretId); setSecretKey(config.secretKey); setZoneId(config.zoneId); setEditingConfigId(config.id); setSaveDialogOpen(true); }; const loadConfig = (config: SavedConfig) => { setSecretId(config.secretId); setSecretKey(config.secretKey); setZoneId(config.zoneId); toast({ title: "成功", description: "配置已加载", }); }; const deleteConfig = (id: string) => { const updatedConfigs = savedConfigs.filter(config => config.id !== id); setSavedConfigs(updatedConfigs); localStorage.setItem("savedConfigs", JSON.stringify(updatedConfigs)); toast({ title: "成功", description: "配置已删除", }); }; const purgeUrls = async () => { if (!urls.trim()) { toast({ variant: "destructive", title: "错误", description: "请输入需要刷新的URL", }); return; } await callApi("purge_url", urls.split("\n").map(t => t.trim()).filter(t => t)); }; const purgePrefixes = async () => { if (!prefixes.trim()) { toast({ variant: "destructive", title: "错误", description: "请输入需要刷新的目录", }); return; } await callApi("purge_prefix", prefixes.split("\n").map(t => t.trim()).filter(t => t)); }; const purgeHosts = async () => { if (!hosts.trim()) { toast({ variant: "destructive", title: "错误", description: "请输入需要刷新的主机", }); return; } await callApi("purge_host", hosts.split("\n").map(t => t.trim()).filter(t => t)); }; const purgeTags = async () => { if (!tags.trim()) { toast({ variant: "destructive", title: "错误", description: "请输入需要刷新的缓存标签", }); return; } await callApi("purge_cache_tag", tags.split("\n").map(t => t.trim()).filter(t => t)); }; const purgeAll = async () => { await callApi("purge_all", []); }; const callApi = async (type: string, targets: string[], method = "invalidate") => { if (!secretId || !secretKey || !zoneId) { toast({ variant: "destructive", title: "错误", description: "请填写完整的配置信息!", }); return; } setLoading(true); try { const response = await fetch("/api/eo-cleancache", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ secretId, secretKey, zoneId, type, targets, method, }), }); const result = await response.json(); setResult(result); setDialogOpen(true); if (result.Response && !result.Response.Error) { toast({ title: "成功", description: "操作成功!", }); } else { toast({ variant: "destructive", title: "失败", description: `操作失败:${ result.Response?.Error?.Message || "未知错误" }`, }); } } catch (error) { toast({ variant: "destructive", title: "错误", description: `请求失败:${(error as Error).message}`, }); setResult({ Response: { RequestId: "", JobId: "", FailedList: [], Error: { Message: (error as Error).message, Code: "RequestError" } } }); setDialogOpen(true); } finally { setLoading(false); } }; return ( 腾讯云EdgeOne缓存刷新工具 数据保存在浏览器本地,不会上传到任何服务器。通用国内站和国际站。
已保存的配置 点击配置名称可快速加载
{savedConfigs.length === 0 ? (

暂无保存的配置

) : (
{savedConfigs.map((config) => (
))}
)}
setSecretId(e.target.value)} placeholder="请输入 SecretId" />
国内站: https://console.cloud.tencent.com/cam/capi 国际站: https://console.intl.cloud.tencent.com/cam/capi
setSecretKey(e.target.value)} placeholder="请输入 SecretKey" />
setZoneId(e.target.value)} placeholder="请输入 ZoneId" />
国内站: https://console.cloud.tencent.com/edgeone/zones 国际站: https://console.intl.cloud.tencent.com/edgeone/zones
刷新全部 URL刷新 目录刷新 Host刷新 Cache Tag
刷新全部缓存 将清理该域名下的所有缓存文件 按URL刷新 输入需要刷新的URL地址,每行一个