import {useState, useEffect, useCallback} from 'react' import {View, Text} from '@tarojs/components' import Taro, {useDidShow} from '@tarojs/taro' import {Loading, InfiniteLoading, Empty, Space, SearchBar, Button} from '@nutui/nutui-react-taro' import { pageClinicPrescription } from "@/api/clinic/clinicPrescription"; import {ClinicPrescription} from "@/api/clinic/clinicPrescription/model"; const SelectPrescription = () => { const [list, setList] = useState([]) const [loading, setLoading] = useState(false) const [searchValue, setSearchValue] = useState('') const [displaySearchValue, setDisplaySearchValue] = useState('') const [page, setPage] = useState(1) const [hasMore, setHasMore] = useState(true) // 获取处方数据 const fetchPrescriptionData = useCallback(async (resetPage = false, targetPage?: number) => { setLoading(true); try { const currentPage = resetPage ? 1 : (targetPage || page); // 构建API参数 const params: any = { page: currentPage }; // 添加搜索关键词 if (displaySearchValue.trim()) { params.keywords = displaySearchValue.trim(); } const res = await pageClinicPrescription(params); if (res?.list && res.list.length > 0) { // 如果是重置页面或第一页,直接设置新数据;否则追加数据 if (resetPage || currentPage === 1) { setList(res.list); } else { setList(prevList => prevList.concat(res.list)); } // 正确判断是否还有更多数据 const hasMoreData = res.list.length >= 10; // 假设每页10条数据 setHasMore(hasMoreData); } else { if (resetPage || currentPage === 1) { setList([]); } setHasMore(false); } setPage(currentPage); } catch (error) { console.error('获取处方数据失败:', error); Taro.showToast({ title: '加载失败,请重试', icon: 'none' }); } finally { setLoading(false); } }, [page, displaySearchValue]); const reloadMore = async () => { if (loading || !hasMore) return; // 防止重复加载 const nextPage = page + 1; await fetchPrescriptionData(false, nextPage); } // 防抖搜索功能 useEffect(() => { const timer = setTimeout(() => { setDisplaySearchValue(searchValue); }, 300); // 300ms 防抖 return () => clearTimeout(timer); }, [searchValue]); // 初始化数据 useEffect(() => { fetchPrescriptionData(true).then(); }, [displaySearchValue]); // 监听页面显示,当从其他页面返回时刷新数据 useDidShow(() => { // 刷新数据 setList([]); setPage(1); setHasMore(true); fetchPrescriptionData(true); }); // 选择处方 const selectPrescription = (prescription: ClinicPrescription) => { // 将选中的处方信息传递回上一个页面 const pages = Taro.getCurrentPages(); if (pages.length > 1) { const prevPage = pages[pages.length - 2]; // @ts-ignore if (prevPage && typeof prevPage.setSelectedPrescription === 'function') { // @ts-ignore prevPage.setSelectedPrescription(prescription); } } Taro.navigateBack(); }; // 渲染处方项 const renderPrescriptionItem = (prescription: ClinicPrescription) => ( 处方编号: {prescription.orderNo || '无编号'} 处方类型: {prescription.prescriptionType === 0 ? '中药' : prescription.prescriptionType === 1 ? '西药' : '未知'} 诊断结果: {prescription.diagnosis || '无'} 创建时间: {prescription.createTime || '未知'} {/* 显示备注字段 */} 备注: {prescription.comments || '暂无'} {/* 选择按钮 */} ); // 渲染处方列表 const renderPrescriptionList = () => { const isSearching = displaySearchValue.trim().length > 0; return ( {/* 搜索结果统计 */} {isSearching && ( 搜索 "{displaySearchValue}" 的结果,共找到 {list.length} 条记录 )} { // 滚动事件处理 }} onScrollToUpper={() => { // 滚动到顶部事件处理 }} loadingText={ <> 加载中... } loadMoreText={ list.length === 0 ? ( ) : ( 没有更多了 ) } > {loading && list.length === 0 ? ( 加载中... ) : ( list.map(renderPrescriptionItem) )} ); }; return ( {/* 搜索栏 */} setSearchValue(value)} onClear={() => { setSearchValue(''); setDisplaySearchValue(''); }} clearable /> {/* 处方列表 */} {renderPrescriptionList()} ); }; export default SelectPrescription;