362 lines
11 KiB
TypeScript
362 lines
11 KiB
TypeScript
import React, { useState, useEffect } from "react";
|
|
import { SearchOutlined, DeleteOutlined } from "@ant-design/icons";
|
|
import { Popup } from "antd-mobile";
|
|
import { Button, Input } from "antd";
|
|
import { getFriendList } from "./api";
|
|
import style from "./index.module.scss";
|
|
import Layout from "@/components/Layout/Layout";
|
|
import PopupHeader from "@/components/PopuLayout/header";
|
|
import PopupFooter from "@/components/PopuLayout/footer";
|
|
|
|
// 微信好友接口类型
|
|
interface WechatFriend {
|
|
id: string;
|
|
nickname: string;
|
|
wechatId: string;
|
|
avatar: string;
|
|
customer: string;
|
|
}
|
|
|
|
// 组件属性接口
|
|
interface FriendSelectionProps {
|
|
selectedFriends: string[];
|
|
onSelect: (friends: string[]) => void;
|
|
onSelectDetail?: (friends: WechatFriend[]) => void;
|
|
deviceIds?: string[];
|
|
enableDeviceFilter?: boolean;
|
|
placeholder?: string;
|
|
className?: string;
|
|
visible?: boolean; // 新增
|
|
onVisibleChange?: (visible: boolean) => void; // 新增
|
|
selectedListMaxHeight?: number;
|
|
showInput?: boolean;
|
|
showSelectedList?: boolean;
|
|
readonly?: boolean;
|
|
onConfirm?: (selectedIds: string[], selectedItems: WechatFriend[]) => void; // 新增
|
|
}
|
|
|
|
export default function FriendSelection({
|
|
selectedFriends,
|
|
onSelect,
|
|
onSelectDetail,
|
|
deviceIds = [],
|
|
enableDeviceFilter = true,
|
|
placeholder = "选择微信好友",
|
|
className = "",
|
|
visible,
|
|
onVisibleChange,
|
|
selectedListMaxHeight = 300,
|
|
showInput = true,
|
|
showSelectedList = true,
|
|
readonly = false,
|
|
onConfirm,
|
|
}: FriendSelectionProps) {
|
|
const [popupVisible, setPopupVisible] = useState(false);
|
|
const [friends, setFriends] = useState<WechatFriend[]>([]);
|
|
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 realVisible = visible !== undefined ? visible : popupVisible;
|
|
const setRealVisible = (v: boolean) => {
|
|
if (onVisibleChange) onVisibleChange(v);
|
|
if (visible === undefined) setPopupVisible(v);
|
|
};
|
|
|
|
// 打开弹窗并请求第一页好友
|
|
const openPopup = () => {
|
|
if (readonly) return;
|
|
setCurrentPage(1);
|
|
setSearchQuery("");
|
|
setRealVisible(true);
|
|
fetchFriends(1, "");
|
|
};
|
|
|
|
// 当页码变化时,拉取对应页数据(弹窗已打开时)
|
|
useEffect(() => {
|
|
if (realVisible && currentPage !== 1) {
|
|
fetchFriends(currentPage, searchQuery);
|
|
}
|
|
}, [currentPage, realVisible, searchQuery]);
|
|
|
|
// 搜索防抖
|
|
useEffect(() => {
|
|
if (!realVisible) return;
|
|
|
|
const timer = setTimeout(() => {
|
|
setCurrentPage(1);
|
|
fetchFriends(1, searchQuery);
|
|
}, 500);
|
|
|
|
return () => clearTimeout(timer);
|
|
}, [searchQuery, realVisible]);
|
|
|
|
// 获取好友列表API - 添加 keyword 参数
|
|
const fetchFriends = async (page: number, keyword: string = "") => {
|
|
setLoading(true);
|
|
try {
|
|
const params: any = {
|
|
page,
|
|
limit: 20,
|
|
};
|
|
|
|
if (keyword.trim()) {
|
|
params.keyword = keyword.trim();
|
|
}
|
|
|
|
if (enableDeviceFilter && deviceIds.length > 0) {
|
|
params.deviceIds = deviceIds.join(",");
|
|
}
|
|
|
|
const response = await getFriendList(params);
|
|
if (response && response.list) {
|
|
setFriends(response.list);
|
|
setTotalFriends(response.total || 0);
|
|
setTotalPages(Math.ceil((response.total || 0) / 20));
|
|
}
|
|
} catch (error) {
|
|
console.error("获取好友列表失败:", error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
// 处理好友选择
|
|
const handleFriendToggle = (friendId: string) => {
|
|
if (readonly) return;
|
|
|
|
const newSelectedFriends = selectedFriends.includes(friendId)
|
|
? selectedFriends.filter(id => id !== friendId)
|
|
: [...selectedFriends, friendId];
|
|
|
|
onSelect(newSelectedFriends);
|
|
|
|
// 如果有 onSelectDetail 回调,传递完整的好友对象
|
|
if (onSelectDetail) {
|
|
const selectedFriendObjs = friends.filter(friend =>
|
|
newSelectedFriends.includes(friend.id),
|
|
);
|
|
onSelectDetail(selectedFriendObjs);
|
|
}
|
|
};
|
|
|
|
// 获取显示文本
|
|
const getDisplayText = () => {
|
|
if (selectedFriends.length === 0) return "";
|
|
return `已选择 ${selectedFriends.length} 个好友`;
|
|
};
|
|
|
|
// 获取已选好友详细信息
|
|
const selectedFriendObjs = [
|
|
...friends.filter(friend => selectedFriends.includes(friend.id)),
|
|
...selectedFriends
|
|
.filter(id => !friends.some(friend => friend.id === id))
|
|
.map(id => ({
|
|
id,
|
|
nickname: id,
|
|
wechatId: id,
|
|
avatar: "",
|
|
customer: "",
|
|
})),
|
|
];
|
|
|
|
// 删除已选好友
|
|
const handleRemoveFriend = (id: string) => {
|
|
if (readonly) return;
|
|
onSelect(selectedFriends.filter(d => d !== id));
|
|
};
|
|
|
|
// 确认选择
|
|
const handleConfirm = () => {
|
|
if (onConfirm) {
|
|
onConfirm(selectedFriends, selectedFriendObjs);
|
|
}
|
|
setRealVisible(false);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{/* 输入框 */}
|
|
{showInput && (
|
|
<div className={`${style.inputWrapper} ${className}`}>
|
|
<Input
|
|
placeholder={placeholder}
|
|
value={getDisplayText()}
|
|
onClick={openPopup}
|
|
prefix={<SearchOutlined />}
|
|
allowClear={!readonly}
|
|
size="large"
|
|
readOnly={readonly}
|
|
disabled={readonly}
|
|
style={
|
|
readonly ? { background: "#f5f5f5", cursor: "not-allowed" } : {}
|
|
}
|
|
/>
|
|
</div>
|
|
)}
|
|
{/* 已选好友列表窗口 */}
|
|
{showSelectedList && selectedFriendObjs.length > 0 && (
|
|
<div
|
|
className={style.selectedListWindow}
|
|
style={{
|
|
maxHeight: selectedListMaxHeight,
|
|
overflowY: "auto",
|
|
marginTop: 8,
|
|
border: "1px solid #e5e6eb",
|
|
borderRadius: 8,
|
|
background: "#fff",
|
|
}}
|
|
>
|
|
{selectedFriendObjs.map(friend => (
|
|
<div
|
|
key={friend.id}
|
|
className={style.selectedListRow}
|
|
style={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
padding: "4px 8px",
|
|
borderBottom: "1px solid #f0f0f0",
|
|
fontSize: 14,
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
flex: 1,
|
|
minWidth: 0,
|
|
whiteSpace: "nowrap",
|
|
overflow: "hidden",
|
|
textOverflow: "ellipsis",
|
|
}}
|
|
>
|
|
{friend.nickname || friend.wechatId || friend.id}
|
|
</div>
|
|
{!readonly && (
|
|
<Button
|
|
type="text"
|
|
icon={<DeleteOutlined />}
|
|
size="small"
|
|
style={{
|
|
marginLeft: 4,
|
|
color: "#ff4d4f",
|
|
border: "none",
|
|
background: "none",
|
|
minWidth: 24,
|
|
height: 24,
|
|
display: "flex",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
}}
|
|
onClick={() => handleRemoveFriend(friend.id)}
|
|
/>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
{/* 弹窗 */}
|
|
<Popup
|
|
visible={realVisible && !readonly}
|
|
onMaskClick={() => setRealVisible(false)}
|
|
position="bottom"
|
|
bodyStyle={{ height: "100vh" }}
|
|
>
|
|
<Layout
|
|
header={
|
|
<PopupHeader
|
|
title="选择微信好友"
|
|
searchQuery={searchQuery}
|
|
setSearchQuery={setSearchQuery}
|
|
searchPlaceholder="搜索好友"
|
|
loading={loading}
|
|
onRefresh={() => fetchFriends(currentPage, searchQuery)}
|
|
/>
|
|
}
|
|
footer={
|
|
<PopupFooter
|
|
total={totalFriends}
|
|
currentPage={currentPage}
|
|
totalPages={totalPages}
|
|
loading={loading}
|
|
selectedCount={selectedFriends.length}
|
|
onPageChange={setCurrentPage}
|
|
onCancel={() => setRealVisible(false)}
|
|
onConfirm={handleConfirm}
|
|
/>
|
|
}
|
|
>
|
|
<div className={style.friendList}>
|
|
{loading ? (
|
|
<div className={style.loadingBox}>
|
|
<div className={style.loadingText}>加载中...</div>
|
|
</div>
|
|
) : friends.length > 0 ? (
|
|
<div className={style.friendListInner}>
|
|
{friends.map(friend => (
|
|
<label
|
|
key={friend.id}
|
|
className={style.friendItem}
|
|
onClick={() => !readonly && handleFriendToggle(friend.id)}
|
|
>
|
|
<div className={style.radioWrapper}>
|
|
<div
|
|
className={
|
|
selectedFriends.includes(friend.id)
|
|
? style.radioSelected
|
|
: style.radioUnselected
|
|
}
|
|
>
|
|
{selectedFriends.includes(friend.id) && (
|
|
<div className={style.radioDot}></div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className={style.friendInfo}>
|
|
<div className={style.friendAvatar}>
|
|
{friend.avatar ? (
|
|
<img
|
|
src={friend.avatar}
|
|
alt={friend.nickname}
|
|
className={style.avatarImg}
|
|
/>
|
|
) : (
|
|
friend.nickname.charAt(0)
|
|
)}
|
|
</div>
|
|
<div className={style.friendDetail}>
|
|
<div className={style.friendName}>
|
|
{friend.nickname}
|
|
</div>
|
|
<div className={style.friendId}>
|
|
微信ID: {friend.wechatId}
|
|
</div>
|
|
{friend.customer && (
|
|
<div className={style.friendCustomer}>
|
|
归属客户: {friend.customer}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</label>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className={style.emptyBox}>
|
|
<div className={style.emptyText}>
|
|
{deviceIds.length === 0
|
|
? "请先选择设备"
|
|
: searchQuery
|
|
? `没有找到包含"${searchQuery}"的好友`
|
|
: "没有找到好友"}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Layout>
|
|
</Popup>
|
|
</>
|
|
);
|
|
}
|