"use client" import { useState, useEffect } from "react" import { useParams, useRouter } from "next/navigation" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { ChevronLeft, Smartphone, Battery, Wifi, MessageCircle, Users, Settings, History } from "lucide-react" import { Badge } from "@/components/ui/badge" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Switch } from "@/components/ui/switch" import { Label } from "@/components/ui/label" import { ScrollArea } from "@/components/ui/scroll-area" import { fetchDeviceDetail } from "@/api/devices" import { toast } from "sonner" interface WechatAccount { id: string avatar: string nickname: string wechatId: string gender: "male" | "female" status: "normal" | "abnormal" addFriendStatus: "enabled" | "disabled" friendCount: number lastActive: string } interface Device { id: string imei: string name: string status: "online" | "offline" battery: number lastActive: string historicalIds: string[] wechatAccounts: WechatAccount[] features: { autoAddFriend: boolean autoReply: boolean contentSync: boolean aiChat: boolean } history: { time: string action: string operator: string }[] totalFriend: number thirtyDayMsgCount: number } // 这个helper函数用于获取Badge变体类型 function getBadgeVariant(status: string): "default" | "destructive" | "outline" | "secondary" { if (status === "online" || status === "normal") { return "default" } else if (status === "abnormal") { return "destructive" } else if (status === "enabled") { return "outline" } else { return "secondary" } } export default function DeviceDetailPage() { const params = useParams() const router = useRouter() const [device, setDevice] = useState(null) const [activeTab, setActiveTab] = useState("info") const [loading, setLoading] = useState(true) useEffect(() => { if (!params.id) return const fetchDevice = async () => { try { setLoading(true) const response = await fetchDeviceDetail(params.id as string) if (response && response.code === 200 && response.data) { const serverData = response.data // 构建符合前端期望格式的设备对象 const formattedDevice: Device = { id: serverData.id?.toString() || "", imei: serverData.imei || "", name: serverData.memo || "未命名设备", status: serverData.alive === 1 ? "online" : "offline", battery: serverData.battery || 0, lastActive: serverData.lastUpdateTime || new Date().toISOString(), historicalIds: [], // 服务端暂无此数据 wechatAccounts: [], // 默认空数组 history: [], // 服务端暂无此数据 features: serverData.features || { autoAddFriend: false, autoReply: false, contentSync: false, aiChat: false }, totalFriend: serverData.totalFriend || 0, thirtyDayMsgCount: serverData.thirtyDayMsgCount || 0 } // 如果有微信账号信息,构建微信账号对象 if (serverData.wechatId) { formattedDevice.wechatAccounts = [ { id: serverData.wechatId?.toString() || "1", avatar: "/placeholder.svg", // 默认头像 nickname: serverData.memo || "微信账号", wechatId: serverData.imei || "", gender: "male", // 默认性别 status: serverData.alive === 1 ? "normal" : "abnormal", addFriendStatus: "enabled", friendCount: serverData.totalFriend || 0, lastActive: serverData.lastUpdateTime || new Date().toISOString() } ] } setDevice(formattedDevice) } else { // 如果API返回错误,则使用备用模拟数据 toast.error("获取设备信息失败,显示备用数据") fallbackToMockDevice() } } catch (error) { console.error("获取设备信息失败:", error) toast.error("获取设备信息出错,显示备用数据") fallbackToMockDevice() } finally { setLoading(false) } } const fallbackToMockDevice = () => { const mockDevice: Device = { id: params.id as string, imei: "sd123123", name: "设备 1", status: "online", battery: 85, lastActive: "2024-02-09 15:30:45", historicalIds: ["vx412321", "vfbadasd"], wechatAccounts: [ { id: "1", avatar: "https://hebbkx1anhila5yf.public.blob.vercel-storage.com/image-q2rVrFbfDdAbSnT3ZTNE7gfn3QCbvr.png", nickname: "老张", wechatId: "wxid_abc123", gender: "male", status: "normal", addFriendStatus: "enabled", friendCount: 523, lastActive: "2024-02-09 15:20:33", }, { id: "2", avatar: "https://hebbkx1anhila5yf.public.blob.vercel-storage.com/image-q2rVrFbfDdAbSnT3ZTNE7gfn3QCbvr.png", nickname: "老李", wechatId: "wxid_xyz789", gender: "male", status: "abnormal", addFriendStatus: "disabled", friendCount: 245, lastActive: "2024-02-09 14:15:22", }, ], features: { autoAddFriend: true, autoReply: true, contentSync: false, aiChat: true, }, history: [ { time: "2024-02-09 15:30:45", action: "开启自动加好友", operator: "系统", }, { time: "2024-02-09 14:20:33", action: "添加微信号", operator: "管理员", }, ], totalFriend: 768, thirtyDayMsgCount: 5678 } setDevice(mockDevice) } fetchDevice() }, [params.id]) if (loading || !device) { return
加载中...
} return (

设备详情

{device.name}

{device.status === "online" ? "在线" : "离线"}
IMEI: {device.imei}
{device.historicalIds && device.historicalIds.length > 0 && (
历史ID: {device.historicalIds.join(", ")}
)}
{device.battery}%
{device.status === "online" ? "已连接" : "未连接"}
最后活跃:{device.lastActive}
基本信息 关联账号 操作记录
自动通过好友验证
自动回复好友消息
自动同步朋友圈内容
启用AI智能对话
{device.wechatAccounts && device.wechatAccounts.length > 0 ? (
{device.wechatAccounts.map((account) => (
{account.nickname}
{account.nickname}
{account.status === "normal" ? "正常" : "异常"}
微信号: {account.wechatId}
性别: {account.gender === "male" ? "男" : "女"}
好友数: {account.friendCount} {account.addFriendStatus === "enabled" ? "可加友" : "已停用"}
))}
) : (

此设备暂无关联的微信账号

)}
{device.history && device.history.length > 0 ? (
{device.history.map((record, index) => (
{record.action}
操作人: {record.operator} · {record.time}
))}
) : (

暂无操作记录

)}
好友总数
{device.totalFriend || 0}
消息数量
{device.thirtyDayMsgCount || 0}
) }