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, 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函数 - 添加 keyword 参数 const fetchFriendsList = async (params: { page: number; limit: number; deviceIds?: string[]; keyword?: 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(",") || ""; const keywordParam = params?.keyword ? `&keyword=${encodeURIComponent(params.keyword)}` : ""; return get( `/v1/friend?page=${params.page}&limit=${params.limit}&deviceIds=${deviceIdsParam}${keywordParam}` ); }; // 组件属性接口 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); setSearchQuery(""); // 重置搜索关键词 setDialogOpen(true); fetchFriends(1, ""); }; // 当页码变化时,拉取对应页数据(弹窗已打开时) useEffect(() => { if (dialogOpen && currentPage !== 1) { fetchFriends(currentPage, searchQuery); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentPage]); // 搜索防抖 useEffect(() => { if (!dialogOpen) return; const timer = setTimeout(() => { setCurrentPage(1); // 重置到第一页 fetchFriends(1, searchQuery); }, 500); // 500 防抖 return () => clearTimeout(timer); }, [searchQuery, dialogOpen]); // 获取好友列表API - 添加 keyword 参数 const fetchFriends = async (page: number, keyword: string = "") => { 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, keyword: keyword.trim() || undefined, }); } else { res = await fetchFriendsList({ page, limit: 20, keyword: keyword.trim() || undefined, }); } 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 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); }; // 清空搜索 const handleClearSearch = () => { setSearchQuery(""); setCurrentPage(1); fetchFriends(1, ""); }; return ( <> {/* 输入框 */}
{/* 微信好友选择弹窗 */}
选择微信好友
setSearchQuery(e.target.value)} className="pl-10 py-2 rounded-full border-gray-200" /> {searchQuery && ( )}
{loading ? (
加载中...
) : friends.length > 0 ? (
{friends.map((friend) => ( ))}
) : (
{deviceIds.length === 0 ? "请先选择设备" : searchQuery ? `没有找到包含"${searchQuery}"的好友` : "没有找到好友"}
)}
总计 {totalFriends} 个好友
{currentPage} / {totalPages}
); }