import {useState, useEffect} from "react"; import {useRouter} from '@tarojs/taro' import {Button, ConfigProvider, Input, Divider} from '@nutui/nutui-react-taro' import {ArrowLeft, QrCode, Gift, Voucher} from '@nutui/icons-react-taro' import Taro from '@tarojs/taro' import {View, Text} from '@tarojs/components' import dayjs from 'dayjs' import {ShopGift} from "@/api/shop/shopGift/model"; import {pageShopGift, updateShopGift} from "@/api/shop/shopGift"; import GiftCard from "@/components/GiftCard"; const GiftCardRedeem = () => { const router = useRouter() const [code, setCode] = useState('') const [loading, setLoading] = useState(false) const [validating, setValidating] = useState(false) const [validGift, setValidGift] = useState(null) const [redeemSuccess, setRedeemSuccess] = useState(false) // 从路由参数获取扫码结果 useEffect(() => { if (router.params.code) { const scannedCode = decodeURIComponent(router.params.code) setCode(scannedCode) handleValidateCode(scannedCode) } }, [router.params.code]) // 验证兑换码 const handleValidateCode = async (inputCode?: string) => { const codeToValidate = inputCode || code if (!codeToValidate.trim()) { Taro.showToast({ title: '请输入兑换码', icon: 'none' }) return } setValidating(true) try { const gifts = await pageShopGift({code: codeToValidate,status: 0}) if(gifts?.count == 0){ Taro.showToast({ title: '兑换码无效或已使用', icon: 'none' }) return } const item = gifts?.list[0]; if(item){ setValidGift(item) Taro.showToast({ title: '验证成功', icon: 'success' }) } } catch (error) { console.error('验证兑换码失败:', error) setValidGift(null) Taro.showToast({ title: '兑换码无效或已使用', icon: 'error' }) } finally { setValidating(false) } } // 兑换礼品卡 const handleRedeem = async () => { if (!validGift || !code.trim()) return setLoading(true) try { await updateShopGift({ ...validGift, userId: Taro.getStorageSync('UserId'), takeTime: dayjs.unix(Date.now() / 1000).format('YYYY-MM-DD HH:mm:ss') }) setRedeemSuccess(true) Taro.showToast({ title: '兑换成功', icon: 'success' }) } catch (error) { console.error('兑换礼品卡失败:', error) Taro.showToast({ title: '兑换失败', icon: 'error' }) } finally { setLoading(false) } } // 扫码兑换 const handleScanCode = () => { Taro.scanCode({ success: (res) => { const scannedCode = res.result if (scannedCode) { setCode(scannedCode) handleValidateCode(scannedCode) } }, fail: () => { Taro.showToast({ title: '扫码失败', icon: 'error' }) } }) } // 返回上一页 const handleBack = () => { Taro.navigateBack() } // 查看我的礼品卡 const handleViewMyGifts = () => { Taro.navigateTo({ url: '/user/gift/index' }) } // 转换礼品卡数据 const transformGiftData = (gift: ShopGift) => { return { id: gift.id || 0, name: gift.name || '礼品卡', description: gift.description, code: gift.code, goodsImage: gift.goodsImage, faceValue: gift.faceValue, type: gift.type, expireTime: gift.expireTime, takeTime: gift.takeTime, useLocation: gift.useLocation, contactInfo: gift.contactInfo, showCode: false, // 兑换页面不显示兑换码 showUseBtn: false, // 兑换页面不显示使用按钮 showDetailBtn: false, // 兑换页面不显示详情按钮 theme: 'gold' as const } } return ( {/* 自定义导航栏 */} 兑换礼品卡 {!redeemSuccess ? ( <> {/* 兑换说明 */} 兑换说明 请输入或扫描礼品卡兑换码,验证成功后即可兑换到您的账户中。每个兑换码只能使用一次。 {/* 兑换码输入 */} 输入兑换码 {/* 验证结果 */} {validGift && ( <> 验证成功 )} ) : ( /* 兑换成功页面 */ 兑换成功! 礼品卡已添加到您的账户中 {validGift && ( )} )} ); }; export default GiftCardRedeem;