import React, { useState, useEffect } from "react"; import { SearchOutlined, DeleteOutlined } from "@ant-design/icons"; import { Button, Input } from "antd"; import { Popup, Checkbox } from "antd-mobile"; import style from "./index.module.scss"; import Layout from "@/components/Layout/Layout"; import PopupHeader from "@/components/PopuLayout/header"; import PopupFooter from "@/components/PopuLayout/footer"; import { getContentLibraryList } from "./api"; // 内容库接口类型 interface ContentLibraryItem { id: string; name: string; description?: string; sourceType?: number; // 1=文本 2=图片 3=视频 creatorName?: string; updateTime?: string; [key: string]: any; } // 类型标签文本 const getTypeText = (type?: number) => { if (type === 1) return "文本"; if (type === 2) return "图片"; if (type === 3) return "视频"; return "未知"; }; // 时间格式化 const formatDate = (dateStr?: string) => { if (!dateStr) return "-"; const d = new Date(dateStr); if (isNaN(d.getTime())) return "-"; return `${d.getFullYear()}/${(d.getMonth() + 1) .toString() .padStart(2, "0")}/${d.getDate().toString().padStart(2, "0")} ${d .getHours() .toString() .padStart(2, "0")}:${d.getMinutes().toString().padStart(2, "0")}:${d .getSeconds() .toString() .padStart(2, "0")}`; }; // 组件属性接口 interface ContentLibrarySelectionProps { selectedLibraries: string[]; onSelect: (libraries: string[]) => void; onSelectDetail?: (libraries: ContentLibraryItem[]) => void; placeholder?: string; className?: string; visible?: boolean; onVisibleChange?: (visible: boolean) => void; selectedListMaxHeight?: number; showInput?: boolean; showSelectedList?: boolean; readonly?: boolean; onConfirm?: ( selectedIds: string[], selectedItems: ContentLibraryItem[] ) => void; } export default function ContentLibrarySelection({ selectedLibraries, onSelect, onSelectDetail, placeholder = "选择内容库", className = "", visible, onVisibleChange, selectedListMaxHeight = 300, showInput = true, showSelectedList = true, readonly = false, onConfirm, }: ContentLibrarySelectionProps) { const [popupVisible, setPopupVisible] = useState(false); const [libraries, setLibraries] = useState([]); const [searchQuery, setSearchQuery] = useState(""); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [totalLibraries, setTotalLibraries] = useState(0); const [loading, setLoading] = useState(false); // 获取已选内容库详细信息 const selectedLibraryObjs = libraries.filter((item) => selectedLibraries.includes(item.id) ); // 删除已选内容库 const handleRemoveLibrary = (id: string) => { if (readonly) return; onSelect(selectedLibraries.filter((g) => g !== id)); }; // 受控弹窗逻辑 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); fetchLibraries(1, ""); }; // 当页码变化时,拉取对应页数据(弹窗已打开时) useEffect(() => { if (realVisible && currentPage !== 1) { fetchLibraries(currentPage, searchQuery); } }, [currentPage, realVisible, searchQuery]); // 搜索防抖 useEffect(() => { if (!realVisible) return; const timer = setTimeout(() => { setCurrentPage(1); fetchLibraries(1, searchQuery); }, 500); return () => clearTimeout(timer); }, [searchQuery, realVisible]); // 获取内容库列表API const fetchLibraries = async (page: number, keyword: string = "") => { setLoading(true); try { let params: any = { page, limit: 20, }; if (keyword.trim()) { params.keyword = keyword.trim(); } const response = await getContentLibraryList(params); if (response && response.list) { setLibraries(response.list); setTotalLibraries(response.total || 0); setTotalPages(Math.ceil((response.total || 0) / 20)); } } catch (error) { console.error("获取内容库列表失败:", error); } finally { setLoading(false); } }; // 处理内容库选择 const handleLibraryToggle = (libraryId: string) => { if (readonly) return; const newSelected = selectedLibraries.includes(libraryId) ? selectedLibraries.filter((id) => id !== libraryId) : [...selectedLibraries, libraryId]; onSelect(newSelected); if (onSelectDetail) { const selectedObjs = libraries.filter((item) => newSelected.includes(item.id) ); onSelectDetail(selectedObjs); } }; // 获取显示文本 const getDisplayText = () => { if (selectedLibraries.length === 0) return ""; return `已选择 ${selectedLibraries.length} 个内容库`; }; // 确认选择 const handleConfirm = () => { if (onConfirm) { onConfirm(selectedLibraries, selectedLibraryObjs); } setRealVisible(false); }; return ( <> {/* 输入框 */} {showInput && (
} allowClear={!readonly} size="large" readOnly={readonly} disabled={readonly} style={ readonly ? { background: "#f5f5f5", cursor: "not-allowed" } : {} } />
)} {/* 已选内容库列表窗口 */} {showSelectedList && selectedLibraryObjs.length > 0 && (
{selectedLibraryObjs.map((item) => (
{item.name || item.id}
{!readonly && (
))}
)} {/* 弹窗 */} setRealVisible(false)} position="bottom" bodyStyle={{ height: "100vh" }} > fetchLibraries(currentPage, searchQuery)} /> } footer={ setRealVisible(false)} onConfirm={handleConfirm} /> } >
{loading ? (
加载中...
) : libraries.length > 0 ? (
{libraries.map((item) => ( ))}
) : (
{searchQuery ? `没有找到包含"${searchQuery}"的内容库` : "没有找到内容库"}
)}
); }