import {useState} from "react"; import Taro, {useDidShow} from '@tarojs/taro' import {Button, Cell, CellGroup, Empty, ConfigProvider, SearchBar, Tag, InfiniteLoading, Loading, PullToRefresh} from '@nutui/nutui-react-taro' import {Edit, Del, Eye} from '@nutui/icons-react-taro' import {View} from '@tarojs/components' import {CmsArticle} from "@/api/cms/cmsArticle/model"; import {pageCmsArticle, removeCmsArticle} from "@/api/cms/cmsArticle"; import FixedButton from "@/components/FixedButton"; import dayjs from "dayjs"; const ArticleArticleManage = () => { const [list, setList] = useState([]) const [loading, setLoading] = useState(false) // const [refreshing, setRefreshing] = useState(false) const [hasMore, setHasMore] = useState(true) const [searchValue, setSearchValue] = useState('') const [page, setPage] = useState(1) const [total, setTotal] = useState(0) const reload = async (isRefresh = false) => { if (isRefresh) { setPage(1) setList([]) setHasMore(true) } setLoading(true) try { const currentPage = isRefresh ? 1 : page const res = await pageCmsArticle({ page: currentPage, limit: 10, keywords: searchValue }) if (res && res.list) { const newList = isRefresh ? res.list : [...list, ...res.list] setList(newList) setTotal(res.count || 0) // 判断是否还有更多数据 setHasMore(res.list.length === 10) // 如果返回的数据等于limit,说明可能还有更多 if (!isRefresh) { setPage(currentPage + 1) } else { setPage(2) // 刷新后下一页是第2页 } } else { setHasMore(false) setTotal(0) } } catch (error) { console.error('获取文章失败:', error) Taro.showToast({ title: '获取文章失败', icon: 'error' }); } finally { setLoading(false) } } // 搜索功能 const handleSearch = (value: string) => { setSearchValue(value) reload(true) } // 下拉刷新 const handleRefresh = async () => { // setRefreshing(true) await reload(true) // setRefreshing(false) } // 删除文章 const handleDelete = async (id?: number) => { Taro.showModal({ title: '确认删除', content: '确定要删除这篇文章吗?', success: async (res) => { if (res.confirm) { try { await removeCmsArticle(id) Taro.showToast({ title: '删除成功', icon: 'success' }); reload(true); } catch (error) { Taro.showToast({ title: '删除失败', icon: 'error' }); } } } }); } // 编辑文章 const handleEdit = (item: CmsArticle) => { Taro.navigateTo({ url: `/shop/shopArticle/add?id=${item.articleId}` }); } // 查看文章详情 const handleView = (item: CmsArticle) => { // 这里可以跳转到文章详情页面 Taro.navigateTo({ url: `/cms/detail/index?id=${item.articleId}` }) } // 获取状态标签 const getStatusTag = (status?: number) => { switch (status) { case 0: return 已发布 case 1: return 待审核 case 2: return 已驳回 case 3: return 违规内容 default: return 未知 } } // 加载更多 const loadMore = async () => { if (!loading && hasMore) { await reload(false) // 不刷新,追加数据 } } useDidShow(() => { reload(true).then() }); return ( {/* 搜索栏 */} {/* 统计信息 */} {total > 0 && ( 共找到 {total} 篇文章 )} {/* 文章列表 */} {list.length === 0 && !loading ? ( ) : ( 加载中... } loadMoreText={ {list.length === 0 ? "暂无数据" : "没有更多了"} } > {list.map((item, index) => ( {/* 文章标题和状态 */} {item.title} {getStatusTag(item.status)} {/* 文章概述 */} {item.overview && ( {item.overview} )} {/* 文章信息 */} 阅读: {item.actualViews || 0} {item.price && 价格: ¥{item.price}} 创建: {dayjs(item.createTime).format('MM-DD HH:mm')} {/* 操作按钮 */} ))} )} {/* 底部浮动按钮 */} } onClick={() => Taro.navigateTo({url: '/shop/shopArticle/add'})} /> ); }; export default ArticleArticleManage;