2025-07-16 15:18:54 +08:00
|
|
|
|
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";
|
2025-07-17 09:53:34 +08:00
|
|
|
|
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
2025-07-16 15:18:54 +08:00
|
|
|
|
import { get } from "@/api/request";
|
2025-07-10 16:19:16 +08:00
|
|
|
|
|
|
|
|
|
|
// 微信好友接口类型
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-17 09:53:34 +08:00
|
|
|
|
// 获取好友列表API函数 - 添加 keyword 参数
|
2025-07-14 15:58:41 +08:00
|
|
|
|
const fetchFriendsList = async (params: {
|
|
|
|
|
|
page: number;
|
|
|
|
|
|
limit: number;
|
|
|
|
|
|
deviceIds?: string[];
|
2025-07-17 09:53:34 +08:00
|
|
|
|
keyword?: string;
|
2025-07-14 15:58:41 +08:00
|
|
|
|
}): Promise<FriendsResponse> => {
|
|
|
|
|
|
if (params.deviceIds && params.deviceIds.length === 0) {
|
2025-07-10 16:19:16 +08:00
|
|
|
|
return {
|
|
|
|
|
|
code: 200,
|
2025-07-16 15:18:54 +08:00
|
|
|
|
msg: "success",
|
2025-07-10 16:19:16 +08:00
|
|
|
|
data: {
|
|
|
|
|
|
list: [],
|
|
|
|
|
|
total: 0,
|
2025-07-14 15:58:41 +08:00
|
|
|
|
page: params.page,
|
2025-07-16 15:18:54 +08:00
|
|
|
|
limit: params.limit,
|
|
|
|
|
|
},
|
2025-07-10 16:19:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-07-16 15:18:54 +08:00
|
|
|
|
|
|
|
|
|
|
const deviceIdsParam = params?.deviceIds?.join(",") || "";
|
2025-07-17 09:53:34 +08:00
|
|
|
|
const keywordParam = params?.keyword
|
|
|
|
|
|
? `&keyword=${encodeURIComponent(params.keyword)}`
|
|
|
|
|
|
: "";
|
|
|
|
|
|
|
2025-07-16 15:18:54 +08:00
|
|
|
|
return get<FriendsResponse>(
|
2025-07-17 09:53:34 +08:00
|
|
|
|
`/v1/friend?page=${params.page}&limit=${params.limit}&deviceIds=${deviceIdsParam}${keywordParam}`
|
2025-07-16 15:18:54 +08:00
|
|
|
|
);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 组件属性接口
|
|
|
|
|
|
interface FriendSelectionProps {
|
|
|
|
|
|
selectedFriends: string[];
|
|
|
|
|
|
onSelect: (friends: string[]) => void;
|
2025-07-14 15:58:41 +08:00
|
|
|
|
onSelectDetail?: (friends: WechatFriend[]) => void; // 新增
|
|
|
|
|
|
deviceIds?: string[];
|
|
|
|
|
|
enableDeviceFilter?: boolean; // 新增开关,默认true
|
2025-07-10 16:19:16 +08:00
|
|
|
|
placeholder?: string;
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 15:18:54 +08:00
|
|
|
|
export default function FriendSelection({
|
|
|
|
|
|
selectedFriends,
|
|
|
|
|
|
onSelect,
|
2025-07-14 15:58:41 +08:00
|
|
|
|
onSelectDetail,
|
|
|
|
|
|
deviceIds = [],
|
|
|
|
|
|
enableDeviceFilter = true,
|
2025-07-10 16:19:16 +08:00
|
|
|
|
placeholder = "选择微信好友",
|
2025-07-16 15:18:54 +08:00
|
|
|
|
className = "",
|
2025-07-10 16:19:16 +08:00
|
|
|
|
}: FriendSelectionProps) {
|
|
|
|
|
|
const [dialogOpen, setDialogOpen] = useState(false);
|
|
|
|
|
|
const [friends, setFriends] = useState<WechatFriend[]>([]);
|
2025-07-16 15:18:54 +08:00
|
|
|
|
const [searchQuery, setSearchQuery] = useState("");
|
2025-07-10 16:19:16 +08:00
|
|
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
|
|
const [totalPages, setTotalPages] = useState(1);
|
|
|
|
|
|
const [totalFriends, setTotalFriends] = useState(0);
|
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
2025-07-14 15:58:41 +08:00
|
|
|
|
// 打开弹窗并请求第一页好友
|
|
|
|
|
|
const openDialog = () => {
|
|
|
|
|
|
setCurrentPage(1);
|
2025-07-17 09:53:34 +08:00
|
|
|
|
setSearchQuery(""); // 重置搜索关键词
|
2025-07-14 15:58:41 +08:00
|
|
|
|
setDialogOpen(true);
|
2025-07-17 09:53:34 +08:00
|
|
|
|
fetchFriends(1, "");
|
2025-07-14 15:58:41 +08:00
|
|
|
|
};
|
2025-07-10 16:19:16 +08:00
|
|
|
|
|
2025-07-14 15:58:41 +08:00
|
|
|
|
// 当页码变化时,拉取对应页数据(弹窗已打开时)
|
2025-07-10 16:19:16 +08:00
|
|
|
|
useEffect(() => {
|
2025-07-14 15:58:41 +08:00
|
|
|
|
if (dialogOpen && currentPage !== 1) {
|
2025-07-17 09:53:34 +08:00
|
|
|
|
fetchFriends(currentPage, searchQuery);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
}
|
2025-07-14 15:58:41 +08:00
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
|
}, [currentPage]);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
|
2025-07-17 09:53:34 +08:00
|
|
|
|
// 搜索防抖
|
|
|
|
|
|
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 = "") => {
|
2025-07-10 16:19:16 +08:00
|
|
|
|
setLoading(true);
|
|
|
|
|
|
try {
|
2025-07-14 15:58:41 +08:00
|
|
|
|
let res;
|
|
|
|
|
|
if (enableDeviceFilter) {
|
|
|
|
|
|
if (deviceIds.length === 0) {
|
|
|
|
|
|
setFriends([]);
|
|
|
|
|
|
setTotalFriends(0);
|
|
|
|
|
|
setTotalPages(1);
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-07-17 09:53:34 +08:00
|
|
|
|
res = await fetchFriendsList({
|
|
|
|
|
|
page,
|
|
|
|
|
|
limit: 20,
|
|
|
|
|
|
deviceIds: deviceIds,
|
|
|
|
|
|
keyword: keyword.trim() || undefined,
|
|
|
|
|
|
});
|
2025-07-14 15:58:41 +08:00
|
|
|
|
} else {
|
2025-07-17 09:53:34 +08:00
|
|
|
|
res = await fetchFriendsList({
|
|
|
|
|
|
page,
|
|
|
|
|
|
limit: 20,
|
|
|
|
|
|
keyword: keyword.trim() || undefined,
|
|
|
|
|
|
});
|
2025-07-14 15:58:41 +08:00
|
|
|
|
}
|
2025-07-16 15:18:54 +08:00
|
|
|
|
|
2025-07-10 16:19:16 +08:00
|
|
|
|
if (res && res.code === 200 && res.data) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
setFriends(
|
|
|
|
|
|
res.data.list.map((friend) => ({
|
|
|
|
|
|
id: friend.id?.toString() || "",
|
|
|
|
|
|
nickname: friend.nickname || "",
|
|
|
|
|
|
wechatId: friend.wechatId || "",
|
|
|
|
|
|
avatar: friend.avatar || "",
|
|
|
|
|
|
customer: friend.customer || "",
|
|
|
|
|
|
}))
|
|
|
|
|
|
);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
setTotalFriends(res.data.total || 0);
|
|
|
|
|
|
setTotalPages(Math.ceil((res.data.total || 0) / 20));
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
console.error("获取好友列表失败:", error);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 处理好友选择
|
|
|
|
|
|
const handleFriendToggle = (friendId: string) => {
|
2025-07-14 15:58:41 +08:00
|
|
|
|
let newIds: string[];
|
2025-07-10 16:19:16 +08:00
|
|
|
|
if (selectedFriends.includes(friendId)) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
newIds = selectedFriends.filter((id) => id !== friendId);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
} else {
|
2025-07-14 15:58:41 +08:00
|
|
|
|
newIds = [...selectedFriends, friendId];
|
|
|
|
|
|
}
|
|
|
|
|
|
onSelect(newIds);
|
|
|
|
|
|
if (onSelectDetail) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
const selectedObjs = friends.filter((f) => newIds.includes(f.id));
|
2025-07-14 15:58:41 +08:00
|
|
|
|
onSelectDetail(selectedObjs);
|
2025-07-10 16:19:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取显示文本
|
|
|
|
|
|
const getDisplayText = () => {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
if (selectedFriends.length === 0) return "";
|
2025-07-10 16:19:16 +08:00
|
|
|
|
return `已选择 ${selectedFriends.length} 个好友`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleConfirm = () => {
|
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-17 09:53:34 +08:00
|
|
|
|
// 清空搜索
|
|
|
|
|
|
const handleClearSearch = () => {
|
|
|
|
|
|
setSearchQuery("");
|
|
|
|
|
|
setCurrentPage(1);
|
|
|
|
|
|
fetchFriends(1, "");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-10 16:19:16 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{/* 输入框 */}
|
|
|
|
|
|
<div className={`relative ${className}`}>
|
|
|
|
|
|
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400">
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<svg
|
|
|
|
|
|
width="20"
|
|
|
|
|
|
height="20"
|
|
|
|
|
|
fill="none"
|
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
|
>
|
|
|
|
|
|
<path
|
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
|
strokeWidth="2"
|
|
|
|
|
|
d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
|
|
|
|
|
|
/>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
</svg>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
|
className="pl-10 h-12 rounded-xl border-gray-200 text-base"
|
|
|
|
|
|
readOnly
|
2025-07-14 15:58:41 +08:00
|
|
|
|
onClick={openDialog}
|
2025-07-10 16:19:16 +08:00
|
|
|
|
value={getDisplayText()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 微信好友选择弹窗 */}
|
|
|
|
|
|
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<DialogContent className="max-w-xl max-h-[90vh] flex flex-col p-0 gap-0 overflow-hidden bg-white">
|
2025-07-10 16:19:16 +08:00
|
|
|
|
<div className="p-6">
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<DialogTitle className="text-center text-xl font-medium mb-6">
|
|
|
|
|
|
选择微信好友
|
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
|
|
2025-07-10 16:19:16 +08:00
|
|
|
|
<div className="relative mb-4">
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="搜索好友"
|
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
|
|
className="pl-10 py-2 rounded-full border-gray-200"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
2025-07-17 09:53:34 +08:00
|
|
|
|
{searchQuery && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
className="absolute right-2 top-1/2 -translate-y-1/2 h-6 w-6 rounded-full"
|
|
|
|
|
|
onClick={handleClearSearch}
|
|
|
|
|
|
>
|
|
|
|
|
|
<X className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
2025-07-10 16:19:16 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-07-17 09:53:34 +08:00
|
|
|
|
<div className="flex-1 overflow-y-auto h-[50vh]">
|
2025-07-10 16:19:16 +08:00
|
|
|
|
{loading ? (
|
|
|
|
|
|
<div className="flex items-center justify-center h-full">
|
|
|
|
|
|
<div className="text-gray-500">加载中...</div>
|
|
|
|
|
|
</div>
|
2025-07-17 09:53:34 +08:00
|
|
|
|
) : friends.length > 0 ? (
|
2025-07-10 16:19:16 +08:00
|
|
|
|
<div className="divide-y">
|
2025-07-17 09:53:34 +08:00
|
|
|
|
{friends.map((friend) => (
|
2025-07-10 16:19:16 +08:00
|
|
|
|
<label
|
|
|
|
|
|
key={friend.id}
|
|
|
|
|
|
className="flex items-center px-6 py-4 hover:bg-gray-50 cursor-pointer"
|
|
|
|
|
|
onClick={() => handleFriendToggle(friend.id)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div className="mr-3 flex items-center justify-center">
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className={`w-5 h-5 rounded-full border ${
|
|
|
|
|
|
selectedFriends.includes(friend.id)
|
|
|
|
|
|
? "border-blue-600"
|
|
|
|
|
|
: "border-gray-300"
|
|
|
|
|
|
} flex items-center justify-center`}
|
|
|
|
|
|
>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
{selectedFriends.includes(friend.id) && (
|
|
|
|
|
|
<div className="w-3 h-3 rounded-full bg-blue-600"></div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex items-center space-x-3 flex-1">
|
|
|
|
|
|
<div className="w-10 h-10 rounded-full bg-gradient-to-r from-blue-400 to-purple-500 flex items-center justify-center text-white text-sm font-medium overflow-hidden">
|
|
|
|
|
|
{friend.avatar ? (
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<img
|
|
|
|
|
|
src={friend.avatar}
|
|
|
|
|
|
alt={friend.nickname}
|
|
|
|
|
|
className="w-full h-full object-cover"
|
|
|
|
|
|
/>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
friend.nickname.charAt(0)
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
|
<div className="font-medium">{friend.nickname}</div>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
|
|
微信ID: {friend.wechatId}
|
|
|
|
|
|
</div>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
{friend.customer && (
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<div className="text-sm text-gray-400">
|
|
|
|
|
|
归属客户: {friend.customer}
|
|
|
|
|
|
</div>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="flex items-center justify-center h-full">
|
|
|
|
|
|
<div className="text-gray-500">
|
2025-07-17 09:53:34 +08:00
|
|
|
|
{deviceIds.length === 0
|
|
|
|
|
|
? "请先选择设备"
|
|
|
|
|
|
: searchQuery
|
|
|
|
|
|
? `没有找到包含"${searchQuery}"的好友`
|
|
|
|
|
|
: "没有找到好友"}
|
2025-07-10 16:19:16 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-07-17 09:53:34 +08:00
|
|
|
|
</div>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="border-t p-4 flex items-center justify-between bg-white">
|
|
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
|
|
总计 {totalFriends} 个好友
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => setCurrentPage(Math.max(1, currentPage - 1))}
|
|
|
|
|
|
disabled={currentPage === 1 || loading}
|
|
|
|
|
|
className="px-2 py-0 h-8 min-w-0"
|
|
|
|
|
|
>
|
|
|
|
|
|
<
|
|
|
|
|
|
</Button>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<span className="text-sm">
|
|
|
|
|
|
{currentPage} / {totalPages}
|
|
|
|
|
|
</span>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
2025-07-16 15:18:54 +08:00
|
|
|
|
onClick={() =>
|
|
|
|
|
|
setCurrentPage(Math.min(totalPages, currentPage + 1))
|
|
|
|
|
|
}
|
2025-07-10 16:19:16 +08:00
|
|
|
|
disabled={currentPage === totalPages || loading}
|
|
|
|
|
|
className="px-2 py-0 h-8 min-w-0"
|
|
|
|
|
|
>
|
|
|
|
|
|
>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="border-t p-4 flex items-center justify-between bg-white">
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={() => setDialogOpen(false)}
|
|
|
|
|
|
className="px-6 rounded-full border-gray-300"
|
|
|
|
|
|
>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
onClick={handleConfirm}
|
|
|
|
|
|
className="px-6 bg-blue-600 hover:bg-blue-700 rounded-full"
|
|
|
|
|
|
>
|
2025-07-10 16:19:16 +08:00
|
|
|
|
确定 ({selectedFriends.length})
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
2025-07-16 15:18:54 +08:00
|
|
|
|
}
|