"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent } from "@/components/ui/card" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Search, Plus, Trash2 } from "lucide-react" import type { WechatGroup } from "@/types/group-push" import { api } from "@/lib/api" import { showToast } from "@/lib/toast" interface GroupSelectorProps { selectedGroups: WechatGroup[] onGroupsChange: (groups: WechatGroup[]) => void onPrevious: () => void onNext: () => void onSave: () => void onCancel: () => void } export function GroupSelector({ selectedGroups = [], onGroupsChange, onPrevious, onNext, onSave, onCancel, }: GroupSelectorProps) { const [isDialogOpen, setIsDialogOpen] = useState(false) const [searchTerm, setSearchTerm] = useState("") const [serviceFilter, setServiceFilter] = useState("") const [groups, setGroups] = useState([]) const [total, setTotal] = useState(0) const [loading, setLoading] = useState(false) const [currentPage, setCurrentPage] = useState(1) const pageSize = 10 // 拉取群列表 const fetchGroups = async (page = 1, keyword = "") => { setLoading(true) try { const params = new URLSearchParams({ page: page.toString(), limit: pageSize.toString(), keyword: keyword.trim(), }) const res = await api.get(`/v1/workbench/group-list?${params.toString()}`) as any if (res.code === 200 && Array.isArray(res.data.list)) { const mappedList = (res.data.list || []).map((item: any) => ({ ...item, // 保留所有原始字段,方便渲染 id: String(item.id), name: item.groupName, avatar: item.groupAvatar, serviceAccount: { id: item.ownerWechatId, name: item.nickName, avatar: item.avatar, // 可补充 }, })) setGroups(mappedList) setTotal(res.data.total || mappedList.length) } else { setGroups([]) setTotal(0) showToast(res.msg || "获取群列表失败", "error") } } catch (e) { setGroups([]) setTotal(0) showToast((e as any)?.message || "网络错误", "error") } finally { setLoading(false) } } // 弹窗打开/搜索/翻页时拉取 useEffect(() => { if (isDialogOpen) { fetchGroups(currentPage, searchTerm) } // eslint-disable-next-line }, [isDialogOpen, currentPage]) // 搜索时重置页码 const handleSearch = () => { setCurrentPage(1) fetchGroups(1, searchTerm) } const handleAddGroup = (group: WechatGroup) => { if (!selectedGroups.some((g) => g.id === group.id)) { onGroupsChange([...selectedGroups, group]) } setIsDialogOpen(false) } const handleRemoveGroup = (groupId: string) => { onGroupsChange(selectedGroups.filter((group) => group.id !== groupId)) } // 过滤客服(本地过滤) const filteredGroups = groups.filter((group) => { const matchesService = !serviceFilter || group.serviceAccount?.name?.includes(serviceFilter) return matchesService }) return (
* 推送社群:
{selectedGroups.length > 0 ? ( 序号 群信息 推送客服 操作 {selectedGroups.map((group, index) => ( {index + 1}
{group.name}
{group.name}
群主:{group.serviceAccount?.name}
群主:{group.serviceAccount?.name}
))}
) : (
请点击"选择微信聊天群"按钮添加群组
)}
选择微信聊天群
setSearchTerm(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleSearch()} />
setServiceFilter(e.target.value)} />
{loading ? (
加载中...
) : filteredGroups.length === 0 ? (
暂无群聊
) : ( 序号 群信息 推送客服 操作 {filteredGroups.map((group, index) => ( {(currentPage - 1) * pageSize + index + 1}
{group.name}
{group.name}
群主:{group.serviceAccount?.name}
群主:{group.serviceAccount?.name}
))}
)}
{/* 分页 */} {total > pageSize && (
第 {currentPage} / {Math.ceil(total / pageSize)} 页
)}
) }