import {useState, useEffect} from "react"; import {useRouter} from '@tarojs/taro' import {Button, ConfigProvider, Tag, Divider} from '@nutui/nutui-react-taro' import {ArrowLeft, Gift, Clock, CartCheck, Share} from '@nutui/icons-react-taro' import Taro from '@tarojs/taro' import {View, Text} from '@tarojs/components' import {ShopCoupon} from "@/api/shop/shopCoupon/model"; import {getShopCoupon} from "@/api/shop/shopCoupon"; import CouponShare from "@/components/CouponShare"; import dayjs from "dayjs"; const CouponDetail = () => { const router = useRouter() const [coupon, setCoupon] = useState(null) const [loading, setLoading] = useState(true) const [showShare, setShowShare] = useState(false) const couponId = router.params.id useEffect(() => { if (couponId) { loadCouponDetail() } }, [couponId]) const loadCouponDetail = async () => { try { setLoading(true) const data = await getShopCoupon(Number(couponId)) setCoupon(data) } catch (error) { console.error('获取优惠券详情失败:', error) Taro.showToast({ title: '获取优惠券详情失败', icon: 'error' }) } finally { setLoading(false) } } // 获取优惠券类型文本 const getCouponTypeText = (type?: number) => { switch (type) { case 10: return '满减券' case 20: return '折扣券' case 30: return '免费券' default: return '优惠券' } } // 获取优惠券金额显示 const getCouponAmountDisplay = () => { if (!coupon) return '' switch (coupon.type) { case 10: // 满减券 return `¥${coupon.reducePrice}` case 20: // 折扣券 return `${coupon.discount}折` case 30: // 免费券 return '免费' default: return `¥${coupon.reducePrice || 0}` } } // 获取使用条件文本 const getConditionText = () => { if (!coupon) return '' if (coupon.type === 30) return '无门槛使用' if (coupon.minPrice && parseFloat(coupon.minPrice) > 0) { return `满${coupon.minPrice}元可用` } return '无门槛使用' } // 获取有效期文本 const getValidityText = () => { if (!coupon) return '' if (coupon.expireType === 10) { return `领取后${coupon.expireDay}天内有效` } else { return `${dayjs(coupon.startTime).format('YYYY年MM月DD日')} 至 ${dayjs(coupon.endTime).format('YYYY年MM月DD日')}` } } // 获取适用范围文本 const getApplyRangeText = () => { if (!coupon) return '' switch (coupon.applyRange) { case 10: return '全部商品' case 20: return '指定商品' case 30: return '指定分类' default: return '全部商品' } } // 获取优惠券状态 const getCouponStatus = () => { if (!coupon) return { status: 0, text: '未知', color: 'default' } if (coupon.isExpire === 1) { return { status: 2, text: '已过期', color: 'danger' } } else if (coupon.status === 1) { return { status: 1, text: '已使用', color: 'warning' } } else { return { status: 0, text: '可使用', color: 'success' } } } // 使用优惠券 const handleUseCoupon = () => { if (!coupon) return Taro.showModal({ title: '使用优惠券', content: `确定要使用"${coupon.name}"吗?`, success: (res) => { if (res.confirm) { // 跳转到商品页面或购物车页面 Taro.navigateTo({ url: '/pages/index/index' }) } } }) } // 返回上一页 const handleBack = () => { Taro.navigateBack() } if (loading) { return ( 加载中... ) } if (!coupon) { return ( 优惠券不存在 ) } const statusInfo = getCouponStatus() return ( {/* 自定义导航栏 */} 优惠券详情 setShowShare(true)}> {statusInfo.text} {/* 优惠券卡片 */} {getCouponAmountDisplay()} {getCouponTypeText(coupon.type)} {coupon.name} {getConditionText()} {/* 详细信息 */} 使用说明 有效期 {getValidityText()} 适用范围 {getApplyRangeText()} {coupon.description && ( <> 使用说明 {coupon.description} )} {/* 底部操作按钮 */} {statusInfo.status === 0 && ( )} {/* 分享弹窗 */} {coupon && ( setShowShare(false)} /> )} ); }; export default CouponDetail;