import React, { useState } from 'react' import { View, Text, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { useRequest } from '@/hooks/useRequest' import { listShopGoods, type ShopGoods } from '@/api/shop/shopGoods' definePageConfig({ navigationBarTitleText: '积分兑换', }) // 兑换分类 const categories = [ { label: '全部', value: 0 }, { label: '优惠券', value: 1 }, { label: '实物商品', value: 2 }, { label: '虚拟商品', value: 3 }, ] const ExchangePage: React.FC = () => { const [activeCat, setActiveCat] = useState(0) const [userPoints] = useState(1280) // 模拟用户积分 // 获取可兑换商品 const { data, loading } = useRequest(listShopGoods, { defaultParams: [{ page: 1, limit: 20, exchangeType: 1 }], // exchangeType=1 表示积分兑换商品 onError: (err) => { Taro.showToast({ title: err.message || '加载失败', icon: 'none' }) } }) // 模拟兑换商品数据(实际应该从API获取) const exchangeProducts = [ { id: 1, name: '满100减10优惠券', points: 500, image: '', stock: 100, type: 1, description: '积分兑换专属优惠券', }, { id: 2, name: '满200减25优惠券', points: 1000, image: '', stock: 50, type: 1, description: '积分兑换专属优惠券', }, { id: 3, name: '品牌定制水杯', points: 2000, image: '', stock: 30, type: 2, description: '高品质保温水杯,限量兑换', }, { id: 4, name: '精美帆布袋', points: 1500, image: '', stock: 50, type: 2, description: '环保帆布袋,时尚实用', }, { id: 5, name: '视频会员月卡', points: 3000, image: '', stock: 20, type: 3, description: '主流视频平台会员月卡', }, ] // 筛选商品 const filteredProducts = activeCat === 0 ? exchangeProducts : exchangeProducts.filter(p => p.type === activeCat) // 处理兑换 const handleExchange = (product: typeof exchangeProducts[0]) => { if (userPoints < product.points) { Taro.showToast({ title: '积分不足', icon: 'none' }) return } if (product.stock <= 0) { Taro.showToast({ title: '库存不足', icon: 'none' }) return } Taro.showModal({ title: '确认兑换', content: `确定使用 ${product.points} 积分兑换「${product.name}」吗?`, confirmText: '确认兑换', confirmColor: '#0e932e', success: (res) => { if (res.confirm) { Taro.showLoading({ title: '兑换中...' }) // 这里应该调用兑换API setTimeout(() => { Taro.hideLoading() Taro.showToast({ title: '兑换成功', icon: 'success' }) }, 1500) } } }) } // 获取类型标签 const getTypeLabel = (type: number) => { const map: Record = { 1: '优惠券', 2: '实物商品', 3: '虚拟商品' } return map[type] || '其他' } return ( {/* 用户积分信息 */} 我的积分 {userPoints} 积分 已兑换 3 件 兑换记录 查看 {/* 分类标签 */} {categories.map(cat => ( setActiveCat(cat.value)} > {cat.label} {activeCat === cat.value && ( )} ))} {/* 商品列表 */} {filteredProducts.length === 0 ? ( 🎁 暂无兑换商品 ) : ( {filteredProducts.map(product => { const canExchange = userPoints >= product.points && product.stock > 0 return ( {/* 商品信息 */} {/* 商品图片 */} 🎁 {/* 商品详情 */} {product.name} {product.description} {product.points} 积分 {getTypeLabel(product.type)} 库存: {product.stock} canExchange && handleExchange(product)} > {!canExchange ? userPoints < product.points ? '积分不足' : '库存不足' : '立即兑换' } ) })} )} ) } export default ExchangePage