import React, { useState } from 'react' import { View, Text, Input, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { bindGiftCard } from '@/api/shop/shopGiftCard' import request from '@/utils/request' import { useScrollHeight } from '@/hooks/useScrollHeight' definePageConfig({ navigationBarTitleText: '兑换礼品卡', }) const GiftCardExchangePage: React.FC = () => { const scrollHeight = useScrollHeight(44) const [code, setCode] = useState('') const [step, setStep] = useState(1) // 1:输入兑换码, 2:确认兑换 const [cardInfo, setCardInfo] = useState({ code: '', amount: 200, fromUser: '张三', expireDate: '2029-05-12', }) // 查询礼品卡 const handleQuery = async () => { if (!code.trim()) { Taro.showToast({ title: '请输入兑换码', icon: 'none' }) return } if (code.length < 8) { Taro.showToast({ title: '兑换码格式错误', icon: 'none' }) return } Taro.showLoading({ title: '查询中...' }) try { // 调用 API 查询礼品卡信息 const res = await request.get<{ code: number; data: any }>(`/shop/shop-gift/by-code/${code}`) Taro.hideLoading() if (res.code === 0 && res.data) { setCardInfo({ code: res.data.code || code, amount: res.data.faceValue || 0, fromUser: res.data.nickName || '未知用户', expireDate: res.data.takeTime || '无限制', }) setStep(2) Taro.showToast({ title: '查询成功', icon: 'success' }) } else { Taro.showToast({ title: '礼品卡不存在', icon: 'none' }) } } catch (err: any) { Taro.hideLoading() Taro.showToast({ title: err.message || '查询失败', icon: 'none' }) } } // 确认兑换 const handleExchange = async () => { Taro.showModal({ title: '确认兑换', content: `确定兑换该礼品卡吗?\n面值:¥${cardInfo.amount}`, confirmText: '确认兑换', confirmColor: '#0e932e', success: async (res) => { if (res.confirm) { Taro.showLoading({ title: '兑换中...' }) try { const result = await bindGiftCard(cardInfo.code) Taro.hideLoading() if (result.code === 0) { Taro.showToast({ title: '兑换成功', icon: 'success' }) setTimeout(() => { Taro.navigateBack() }, 1500) } else { Taro.showToast({ title: result.message || '兑换失败', icon: 'none' }) } } catch (err: any) { Taro.hideLoading() Taro.showToast({ title: err.message || '兑换失败', icon: 'none' }) } } }, }) } // 重新输入 const handleReset = () => { setCode('') setStep(1) } return ( {step === 1 ? ( <> {/* 输入兑换码 */} 输入兑换码 setCode(e.detail.value.toUpperCase())} placeholder='请输入礼品卡兑换码' className='text-center text-lg font-bold tracking-widest' maxlength={16} /> • 兑换码通常为 12-16 位字符 • 可在购买记录中查看兑换码 = 8 ? 'bg-purple-500' : 'bg-gray-300' }`} onClick={code.length >= 8 ? handleQuery : undefined} > 查询礼品卡 {/* 兑换说明 */} 兑换说明 1. 兑换后金额将存入您的账户余额 2. 礼品卡兑换后不可撤销 3. 如有问题,请联系客服 ) : ( <> {/* 确认兑换 */} 🎁 确认兑换 {/* 礼品卡信息 */} 礼品卡面值 ¥{cardInfo.amount} 兑换码 {cardInfo.code} 赠送人 {cardInfo.fromUser} 有效期至 {cardInfo.expireDate} 兑换后金额将存入您的账户余额 重新输入 确认兑换 )} ) } export default GiftCardExchangePage