import {useState, useEffect} from "react"; import {useRouter} from '@tarojs/taro' import {Button, ConfigProvider, Input, TextArea} from '@nutui/nutui-react-taro' import {ArrowLeft, Location} from '@nutui/icons-react-taro' import Taro from '@tarojs/taro' import {View, Text} from '@tarojs/components' import {ShopGift} from "@/api/shop/shopGift/model"; import {getShopGift, useGift} from "@/api/shop/shopGift"; import GiftCard from "@/components/GiftCard"; const GiftCardUse = () => { const router = useRouter() const [gift, setGift] = useState(null) const [loading, setLoading] = useState(true) const [submitting, setSubmitting] = useState(false) const [useLocation, setUseLocation] = useState('') const [useNote, setUseNote] = useState('') const [useSuccess, setUseSuccess] = useState(false) const giftId = router.params.id useEffect(() => { if (giftId) { loadGiftDetail() } }, [giftId]) // 加载礼品卡详情 const loadGiftDetail = async () => { try { setLoading(true) const data = await getShopGift(Number(giftId)) setGift(data) // 如果礼品卡有预设使用地址,自动填入 if (data.useLocation) { setUseLocation(data.useLocation) } } catch (error) { console.error('获取礼品卡详情失败:', error) Taro.showToast({ title: '获取礼品卡详情失败', icon: 'error' }) } finally { setLoading(false) } } // 使用礼品卡 const handleUseGift = async () => { if (!gift) return // 根据礼品卡类型进行不同的验证 if (gift.type === 10 && !useLocation.trim()) { // 实物礼品卡需要地址 Taro.showToast({ title: '请填写使用地址', icon: 'none' }) return } setSubmitting(true) try { await useGift({ giftId: gift.id!, useLocation: useLocation.trim(), useNote: useNote.trim() }) setUseSuccess(true) Taro.showToast({ title: '使用成功', icon: 'success' }) } catch (error) { console.error('使用礼品卡失败:', error) Taro.showToast({ title: '使用失败', icon: 'error' }) } finally { setSubmitting(false) } } // 获取当前位置 const handleGetLocation = () => { Taro.getLocation({ type: 'gcj02', success: (res) => { // 这里可以调用地理编码API将坐标转换为地址 // 暂时使用坐标信息 setUseLocation(`经度:${res.longitude}, 纬度:${res.latitude}`) Taro.showToast({ title: '位置获取成功', icon: 'success' }) }, 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, useTime: gift.useTime, useLocation: gift.useLocation, contactInfo: gift.contactInfo, showCode: false, showUseBtn: false, showDetailBtn: false, theme: 'gold' as const } } if (loading) { return ( 加载中... ) } if (!gift) { return ( 礼品卡不存在 ) } return ( {/* 自定义导航栏 */} 使用礼品卡 {!useSuccess ? ( <> {/* 礼品卡信息 */} {/* 使用表单 */} 使用信息 {/* 使用地址(实物礼品卡必填) */} {gift.type === 10 && ( 使用地址 * )} {/* 虚拟礼品卡和服务礼品卡的地址选填 */} {gift.type !== 10 && ( 使用地址(选填) )} {/* 使用备注 */} 使用备注(选填)