import React, { useState, useEffect } from 'react' import { View, Text, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { useUser } from '@/hooks/useUser' import { useScrollHeight } from '@/hooks/useScrollHeight' import { listShopCoupon } from '@/api/shop/shopCoupon' import type { ShopCoupon } from '@/api/shop/shopCoupon/model' import EmptyState from '@/components/common/EmptyState' definePageConfig({ navigationBarTitleText: '领券中心', }) const CouponCenterPage: React.FC = () => { const { isLoggedIn } = useUser() const [coupons, setCoupons] = useState([]) const [loading, setLoading] = useState(true) const scrollHeight = useScrollHeight(44) useEffect(() => { if (!isLoggedIn) { Taro.navigateTo({ url: '/passport/login' }) return } fetchCoupons() }, [isLoggedIn]) const fetchCoupons = async () => { try { const data = await listShopCoupon({ status: 1 }) setCoupons(data || []) } catch (e) { console.error('获取优惠券失败:', e) } finally { setLoading(false) } } const handleClaimCoupon = (id: number) => { Taro.showModal({ title: '领取优惠券', content: '确定要领取这张优惠券吗?', success: (res) => { if (res.confirm) { Taro.showToast({ title: '领取成功', icon: 'success' }) fetchCoupons() } }, }) } const getCouponTypeText = (type: number) => { const typeMap: Record = { 1: '无门槛券', 2: '满减券', 3: '折扣券', 4: '运费券', } return typeMap[type] || '优惠券' } if (!isLoggedIn) { return null } return ( {loading ? ( 加载中... ) : coupons.length === 0 ? ( ) : ( {coupons.map((item) => ( {item.discountType === 3 ? `${item.discountValue}折` : `¥${item.amount}`} {item.minAmount ? `满${item.minAmount}可用` : '无门槛'} {getCouponTypeText(item.couponType)} {item.name} 有效期至:{item.expireTime} handleClaimCoupon(item.id!)} > 领取 ))} )} ) } export default CouponCenterPage