"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { ChevronLeft, Plus, MoreHorizontal, Play, Pause, Edit, Trash2, Eye, TrendingUp, Users, Database, Clock, Search, Filter, RefreshCw, } from "lucide-react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Input } from "@/components/ui/input" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import Link from "next/link" import BottomNav from "@/app/components/BottomNav" import { api } from "@/lib/api" import { showToast } from "@/lib/toast" import { Switch } from "@/components/ui/switch" interface DistributionPlan { id: string | number companyId?: number name: string type?: number status: number // 1: 进行中, 0: 已暂停 autoStart?: number userId?: number createTime: string updateTime?: string config: { id?: number workbenchId?: number distributeType?: number maxPerDay?: number timeType?: number startTime?: string endTime?: string account?: string[] devices: string[] pools: string[] createTime?: string updateTime?: string lastUpdated?: string total: { dailyAverage: number totalAccounts: number deviceCount: number poolCount: number totalUsers: number } } creatorName?: string auto_like?: any moments_sync?: any group_push?: any } interface ApiResponse { code: number msg: string data: { list: DistributionPlan[] total: number } } export default function TrafficDistributionPage() { const router = useRouter() const [loading, setLoading] = useState(false) const [searchTerm, setSearchTerm] = useState("") const [plans, setPlans] = useState([]) const [currentPage, setCurrentPage] = useState(1) const [total, setTotal] = useState(0) const pageSize = 10 // 加载分发计划数据 const fetchPlans = async (page: number, searchTerm?: string) => { const loadingToast = showToast("正在加载分发计划...", "loading", true); try { setLoading(true) const queryParams = new URLSearchParams({ type: "5", page: page.toString(), limit: pageSize.toString() }) if (searchTerm) { queryParams.append('keyword', searchTerm) } const response = await api.get(`/v1/workbench/list?${queryParams.toString()}`) if (response.code === 200) { setPlans(response.data.list) setTotal(response.data.total) } else { showToast(response.msg || "获取分发计划失败", "error") } } catch (error: any) { console.error("获取分发计划失败:", error) showToast(error?.message || "请检查网络连接", "error") } finally { loadingToast.remove(); setLoading(false) } } useEffect(() => { fetchPlans(currentPage, searchTerm) }, [currentPage]) const handleSearch = () => { setCurrentPage(1) fetchPlans(1, searchTerm) } const handleRefresh = () => { fetchPlans(currentPage, searchTerm) } const handleDelete = async (planId: string) => { const loadingToast = showToast("正在删除计划...", "loading", true); try { const response = await api.delete(`/v1/workbench/delete?id=${planId}`) if (response.code === 200) { loadingToast.remove(); fetchPlans(currentPage, searchTerm) showToast(response.msg || "已成功删除分发计划", "success") } else { loadingToast.remove(); showToast(response.msg || "请稍后再试", "error") } } catch (error: any) { console.error("删除计划失败:", error) loadingToast.remove(); showToast(error?.message || "请检查网络连接", "error") } } const handleEdit = (planId: string) => { router.push(`/workspace/traffic-distribution/${planId}/edit`) } const handleView = (planId: string) => { router.push(`/workspace/traffic-distribution/${planId}`) } const togglePlanStatus = async (planId: string, currentStatus: number) => { const loadingToast = showToast("正在更新计划状态...", "loading", true); try { const response = await api.post('/v1/workbench/update-status', { id: planId, status: currentStatus === 1 ? 0 : 1 }) if (response.code === 200) { setPlans(plans.map(plan => plan.id === planId ? { ...plan, status: currentStatus === 1 ? 0 : 1 } : plan )) const newStatus = currentStatus === 1 ? 0 : 1 loadingToast.remove(); showToast(response.msg || `计划${newStatus === 1 ? "已启动" : "已暂停"}`, "success") } else { loadingToast.remove(); showToast(response.msg || "请稍后再试", "error") } } catch (error: any) { console.error("更新计划状态失败:", error) loadingToast.remove(); showToast(error?.message || "请检查网络连接", "error") } } return (

流量分发

setSearchTerm(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSearch()} />
{loading ? (
{[...Array(3)].map((_, index) => (
))}
) : plans.length === 0 ? (
暂无数据
) : (
{plans.map((plan) => ( {/* 卡片头部:全部元素一行排列,间距紧凑 */}
{plan.name} {plan.status === 1 ? "进行中" : "已暂停"} togglePlanStatus(plan.id.toString(), Number(plan.status))} className="ml-2" />
{/* handleView(plan.id.toString())}> 查看详情 */} handleEdit(plan.id.toString())}> 编辑计划 togglePlanStatus(plan.id.toString(), Number(plan.status))}> {plan.status === 1 ? ( <> 暂停计划 ) : ( <> 启动计划 )} handleDelete(plan.id.toString())} className="text-red-600"> 删除计划
{/* 卡片内容 - 上3下2布局,图标在文字左侧 */}
{plan.config.total.totalAccounts}
分发账号
{plan.config.total.deviceCount}
分发设备
{plan.config.total.poolCount}
流量池
{/* 横向分隔线 */}
{plan.config.total.dailyAverage}
日均分发量
{plan.config.total.totalAccounts}
总流量池数量
{/* 底部信息 */}
上次执行: {plan.config.lastUpdated}
创建人: {plan.creatorName}
))}
)} {/* 分页 */} {!loading && total > pageSize && (
第 {currentPage} 页 共 {Math.ceil(total / pageSize)} 页
)}
) }