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 (page: number = 1, limit: number = 20, deviceIds: string[]): Promise => { if (deviceIds.length === 0) { return { code: 200, msg: 'success', data: { list: [], total: 0, page, limit } }; } const deviceIdsParam = deviceIds.join(','); return get(`/v1/friend?page=${page}&limit=${limit}&deviceIds=${deviceIdsParam}`); }; // 组件属性接口 interface FriendSelectionProps { selectedFriends: string[]; onSelect: (friends: string[]) => void; deviceIds: string[]; placeholder?: string; className?: string; } export default function FriendSelection({ selectedFriends, onSelect, deviceIds, 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); // 当弹窗打开时获取好友列表 useEffect(() => { if (dialogOpen && deviceIds.length > 0) { fetchFriends(currentPage); } }, [dialogOpen, currentPage, deviceIds]); // 当设备ID变化时,重置页码 useEffect(() => { if (deviceIds.length > 0) { setCurrentPage(1); } }, [deviceIds]); // 获取好友列表API const fetchFriends = async (page: number) => { if (deviceIds.length === 0) return; setLoading(true); try { const res = await fetchFriendsList(page, 20, deviceIds); 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) => { if (selectedFriends.includes(friendId)) { onSelect(selectedFriends.filter(id => id !== friendId)); } else { onSelect([...selectedFriends, friendId]); } }; // 获取显示文本 const getDisplayText = () => { if (selectedFriends.length === 0) return ''; return `已选择 ${selectedFriends.length} 个好友`; }; const handleConfirm = () => { setDialogOpen(false); }; return ( <> {/* 输入框 */}
setDialogOpen(true)} value={getDisplayText()} />
{/* 微信好友选择弹窗 */}
选择微信好友
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}
); }