import React, { useState, useEffect } from "react"; import { Search, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { get } from "@/api/request"; // 微信好友接口类型 interface WechatFriend { id: string; nickname: string; wechatId: string; avatar: string; customer: string; } // 好友列表API响应类型 interface FriendsResponse { code: number; msg: string; data: { list: Array<{ id: number; nickname: string; wechatId: string; avatar?: string; customer?: string; }>; total: number; page: number; limit: number; }; } // 获取好友列表API函数 const fetchFriendsList = async (params: { page: number; limit: number; deviceIds?: string[]; }): Promise => { if (params.deviceIds && params.deviceIds.length === 0) { return { code: 200, msg: "success", data: { list: [], total: 0, page: params.page, limit: params.limit, }, }; } const deviceIdsParam = params?.deviceIds?.join(",") || ""; return get( `/v1/friend?page=${params.page}&limit=${params.limit}&deviceIds=${deviceIdsParam}` ); }; // 组件属性接口 interface FriendSelectionProps { selectedFriends: string[]; onSelect: (friends: string[]) => void; onSelectDetail?: (friends: WechatFriend[]) => void; // 新增 deviceIds?: string[]; enableDeviceFilter?: boolean; // 新增开关,默认true placeholder?: string; className?: string; } export default function FriendSelection({ selectedFriends, onSelect, onSelectDetail, deviceIds = [], enableDeviceFilter = true, placeholder = "选择微信好友", className = "", }: FriendSelectionProps) { const [dialogOpen, setDialogOpen] = useState(false); const [friends, setFriends] = useState([]); const [searchQuery, setSearchQuery] = useState(""); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [totalFriends, setTotalFriends] = useState(0); const [loading, setLoading] = useState(false); // 打开弹窗并请求第一页好友 const openDialog = () => { setCurrentPage(1); setDialogOpen(true); fetchFriends(1); }; // 当页码变化时,拉取对应页数据(弹窗已打开时) useEffect(() => { if (dialogOpen && currentPage !== 1) { fetchFriends(currentPage); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentPage]); // 获取好友列表API const fetchFriends = async (page: number) => { setLoading(true); try { let res; if (enableDeviceFilter) { if (deviceIds.length === 0) { setFriends([]); setTotalFriends(0); setTotalPages(1); setLoading(false); return; } res = await fetchFriendsList({ page, limit: 20, deviceIds: deviceIds }); } else { res = await fetchFriendsList({ page, limit: 20 }); } if (res && res.code === 200 && res.data) { setFriends( res.data.list.map((friend) => ({ id: friend.id?.toString() || "", nickname: friend.nickname || "", wechatId: friend.wechatId || "", avatar: friend.avatar || "", customer: friend.customer || "", })) ); setTotalFriends(res.data.total || 0); setTotalPages(Math.ceil((res.data.total || 0) / 20)); } } catch (error) { console.error("获取好友列表失败:", error); } finally { setLoading(false); } }; // 过滤好友 const filteredFriends = friends.filter( (friend) => friend.nickname.toLowerCase().includes(searchQuery.toLowerCase()) || friend.wechatId.toLowerCase().includes(searchQuery.toLowerCase()) ); // 处理好友选择 const handleFriendToggle = (friendId: string) => { let newIds: string[]; if (selectedFriends.includes(friendId)) { newIds = selectedFriends.filter((id) => id !== friendId); } else { newIds = [...selectedFriends, friendId]; } onSelect(newIds); if (onSelectDetail) { const selectedObjs = friends.filter((f) => newIds.includes(f.id)); onSelectDetail(selectedObjs); } }; // 获取显示文本 const getDisplayText = () => { if (selectedFriends.length === 0) return ""; return `已选择 ${selectedFriends.length} 个好友`; }; const handleConfirm = () => { setDialogOpen(false); }; return ( <> {/* 输入框 */}
{/* 微信好友选择弹窗 */}
选择微信好友
setSearchQuery(e.target.value)} className="pl-10 py-2 rounded-full border-gray-200" />
{loading ? (
加载中...
) : filteredFriends.length > 0 ? (
{filteredFriends.map((friend) => ( ))}
) : (
{deviceIds.length === 0 ? "请先选择设备" : "没有找到好友"}
)}
总计 {totalFriends} 个好友
{currentPage} / {totalPages}
); }