import {useState} from "react"; import Taro, {useDidShow} from '@tarojs/taro' import {Button, Empty, ConfigProvider, SearchBar, InfiniteLoading, Loading, PullToRefresh} from '@nutui/nutui-react-taro' import {Gift} from '@nutui/icons-react-taro' import {View} from '@tarojs/components' import {ShopCoupon} from "@/api/shop/shopCoupon/model"; import {pageShopCoupon} from "@/api/shop/shopCoupon"; import CouponList from "@/components/CouponList"; import {CouponCardProps} from "@/components/CouponCard"; const CouponReceive = () => { const [list, setList] = useState([]) const [loading, setLoading] = 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 pageShopCoupon({ page: currentPage, limit: 10, keywords: searchValue, enabled: 1, // 启用状态 isExpire: 0 // 未过期 }) if (res && res.list) { const newList = isRefresh ? res.list : [...list, ...res.list] setList(newList) setTotal(res.count || 0) setHasMore(res.list.length === 10) if (!isRefresh) { setPage(currentPage + 1) } else { setPage(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 () => { await reload(true) } // 转换优惠券数据为CouponCard组件所需格式 const transformCouponData = (coupon: ShopCoupon): CouponCardProps => { let amount = 0 let type: 10 | 20 | 30 = 10 // 使用新的类型值 if (coupon.type === 10) { // 满减券 type = 10 amount = parseFloat(coupon.reducePrice || '0') } else if (coupon.type === 20) { // 折扣券 type = 20 amount = coupon.discount || 0 } else if (coupon.type === 30) { // 免费券 type = 30 amount = 0 } return { amount, type, status: 0, // 可领取状态 minAmount: parseFloat(coupon.minPrice || '0'), title: coupon.name || '优惠券', startTime: coupon.startTime, endTime: coupon.endTime, showReceiveBtn: true, // 显示领取按钮 onReceive: () => handleReceiveCoupon(coupon), theme: getThemeByType(coupon.type) } } // 根据优惠券类型获取主题色 const getThemeByType = (type?: number): 'red' | 'orange' | 'blue' | 'purple' | 'green' => { switch (type) { case 10: return 'red' // 满减券 case 20: return 'orange' // 折扣券 case 30: return 'green' // 免费券 default: return 'blue' } } // 领取优惠券 const handleReceiveCoupon = async (_coupon: ShopCoupon) => { try { // 这里应该调用领取优惠券的API // await receiveCoupon(coupon.id) Taro.showToast({ title: '领取成功', icon: 'success' }) // 刷新列表 reload(true) } catch (error) { console.error('领取优惠券失败:', error) Taro.showToast({ title: '领取失败', icon: 'error' }) } } // 优惠券点击事件 const handleCouponClick = (_coupon: CouponCardProps, index: number) => { const originalCoupon = list[index] if (originalCoupon) { // 显示优惠券详情 showCouponDetail(originalCoupon) } } // 显示优惠券详情 const showCouponDetail = (coupon: ShopCoupon) => { // 跳转到优惠券详情页 Taro.navigateTo({ url: `/user/coupon/detail?id=${coupon.id}` }) } // 加载更多 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.length === 0 && !loading && ( 暂无可领取优惠券 请关注商家活动获取更多优惠券 )} ); }; export default CouponReceive;