"use client" import { useState } from "react" import { ChevronLeft, Copy, Plus, Trash2 } from "lucide-react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { useRouter } from "next/navigation" import { toast } from "@/components/ui/use-toast" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/components/ui/dialog" import { Switch } from "@/components/ui/switch" // 获取渠道中文名称 const getChannelName = (channel: string) => { const channelMap: Record = { douyin: "抖音", kuaishou: "快手", xiaohongshu: "小红书", weibo: "微博", haibao: "海报", phone: "电话", } return channelMap[channel] || channel } interface ApiKey { id: string name: string key: string createdAt: string lastUsed: string | null status: "active" | "inactive" } interface Webhook { id: string name: string url: string events: string[] createdAt: string lastTriggered: string | null status: "active" | "inactive" } export default function ApiManagementPage({ params }: { params: { channel: string } }) { const router = useRouter() const channel = params.channel const channelName = getChannelName(channel) // 模拟API密钥数据 const [apiKeys, setApiKeys] = useState([ { id: "1", name: `${channelName}获客API密钥`, key: `api_${channel}_${Math.random().toString(36).substring(2, 10)}`, createdAt: "2024-03-20 14:30:00", lastUsed: "2024-03-21 09:15:22", status: "active", }, ]) // 模拟Webhook数据 const [webhooks, setWebhooks] = useState([ { id: "1", name: `${channelName}获客回调`, url: `https://api.example.com/webhooks/${channel}`, events: ["customer.created", "customer.updated", "tag.added"], createdAt: "2024-03-20 14:35:00", lastTriggered: "2024-03-21 09:16:45", status: "active", }, ]) // 对话框状态 const [showNewApiKeyDialog, setShowNewApiKeyDialog] = useState(false) const [showNewWebhookDialog, setShowNewWebhookDialog] = useState(false) const [newApiKeyName, setNewApiKeyName] = useState("") const [newWebhookData, setNewWebhookData] = useState({ name: "", url: "", events: ["customer.created", "customer.updated", "tag.added"], }) // 创建新API密钥 const handleCreateApiKey = () => { if (!newApiKeyName.trim()) { toast({ title: "错误", description: "请输入API密钥名称", variant: "destructive", }) return } const newKey: ApiKey = { id: `${Date.now()}`, name: newApiKeyName, key: `api_${channel}_${Math.random().toString(36).substring(2, 15)}`, createdAt: new Date().toLocaleString(), lastUsed: null, status: "active", } setApiKeys([...apiKeys, newKey]) setNewApiKeyName("") setShowNewApiKeyDialog(false) toast({ title: "创建成功", description: "新的API密钥已创建", variant: "success", }) } // 创建新Webhook const handleCreateWebhook = () => { if (!newWebhookData.name.trim() || !newWebhookData.url.trim()) { toast({ title: "错误", description: "请填写所有必填字段", variant: "destructive", }) return } const newWebhook: Webhook = { id: `${Date.now()}`, name: newWebhookData.name, url: newWebhookData.url, events: newWebhookData.events, createdAt: new Date().toLocaleString(), lastTriggered: null, status: "active", } setWebhooks([...webhooks, newWebhook]) setNewWebhookData({ name: "", url: "", events: ["customer.created", "customer.updated", "tag.added"], }) setShowNewWebhookDialog(false) toast({ title: "创建成功", description: "新的Webhook已创建", variant: "success", }) } // 删除API密钥 const handleDeleteApiKey = (id: string) => { setApiKeys(apiKeys.filter((key) => key.id !== id)) toast({ title: "删除成功", description: "API密钥已删除", variant: "success", }) } // 删除Webhook const handleDeleteWebhook = (id: string) => { setWebhooks(webhooks.filter((webhook) => webhook.id !== id)) toast({ title: "删除成功", description: "Webhook已删除", variant: "success", }) } // 切换API密钥状态 const toggleApiKeyStatus = (id: string) => { setApiKeys( apiKeys.map((key) => (key.id === id ? { ...key, status: key.status === "active" ? "inactive" : "active" } : key)), ) } // 切换Webhook状态 const toggleWebhookStatus = (id: string) => { setWebhooks( webhooks.map((webhook) => webhook.id === id ? { ...webhook, status: webhook.status === "active" ? "inactive" : "active" } : webhook, ), ) } return (

{channelName}获客接口管理

API密钥 Webhook

API密钥管理

{apiKeys.map((apiKey) => (
{apiKey.name} 创建于 {apiKey.createdAt}
toggleApiKeyStatus(apiKey.id)} /> {apiKey.status === "active" ? "启用" : "禁用"}
{apiKey.lastUsed &&

上次使用: {apiKey.lastUsed}

}
))} {apiKeys.length === 0 && (

暂无API密钥

)}

Webhook管理

{webhooks.map((webhook) => (
{webhook.name} 创建于 {webhook.createdAt}
toggleWebhookStatus(webhook.id)} /> {webhook.status === "active" ? "启用" : "禁用"}
{webhook.events.map((event) => ( {event} ))}
{webhook.lastTriggered && (

上次触发: {webhook.lastTriggered}

)}
))} {webhooks.length === 0 && (

暂无Webhook

)}
{/* 创建API密钥对话框 */} 创建新API密钥 创建一个新的API密钥用于访问{channelName}获客接口
setNewApiKeyName(e.target.value)} />
{/* 创建Webhook对话框 */} 创建新Webhook 创建一个新的Webhook用于接收{channelName}获客事件通知
setNewWebhookData({ ...newWebhookData, name: e.target.value })} />
setNewWebhookData({ ...newWebhookData, url: e.target.value })} />
{["customer.created", "customer.updated", "tag.added", "tag.removed"].map((event) => (
{ if (checked) { setNewWebhookData({ ...newWebhookData, events: [...newWebhookData.events, event], }) } else { setNewWebhookData({ ...newWebhookData, events: newWebhookData.events.filter((e) => e !== event), }) } }} /> {event}
))}
) }