import React, { useEffect, useState } from 'react' import { View, Text, Image, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { pageShopSeckill } from '@/api/shop/shopSeckill' import type { ShopSeckill } from '@/api/shop/shopSeckill/model' import EmptyState from '@/components/common/EmptyState' import { getCompressedImageUrl } from '@/utils/image' definePageConfig({ navigationBarTitleText: '限时秒杀', }) const SeckillListPage: React.FC = () => { const [seckillList, setSeckillList] = useState([]) const [loading, setLoading] = useState(true) const [currentTime, setCurrentTime] = useState(Date.now()) useEffect(() => { fetchSeckillList() // 每秒更新当前时间(用于倒计时) const timer = setInterval(() => { setCurrentTime(Date.now()) }, 1000) return () => clearInterval(timer) }, []) const fetchSeckillList = async () => { try { setLoading(true) const res = await pageShopSeckill({ page: 1, pageSize: 20, status: 1 }) if (res.code === 0 && res.data) { setSeckillList(res.data.items || []) } } catch (err) { console.error('获取秒杀列表失败', err) Taro.showToast({ title: '加载失败', icon: 'none' }) } finally { setLoading(false) } } const handleSeckillClick = (item: ShopSeckill) => { Taro.navigateTo({ url: `/pages/shop/seckill-detail?seckillId=${item.id}` }) } const getCountdown = (endTime: string) => { const end = new Date(endTime).getTime() const diff = end - currentTime if (diff <= 0) return '已结束' const hours = Math.floor(diff / (1000 * 60 * 60)) const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)) const seconds = Math.floor((diff % (1000 * 60)) / 1000) return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}` } const getProgress = (item: ShopSeckill) => { if (item.stock === 0) return 100 return Math.round((item.soldCount / (item.soldCount + item.stock)) * 100) } return ( {/* 顶部倒计时横幅 */} 限时秒杀 距结束 --:--:-- {loading ? ( 加载中... ) : seckillList.length === 0 ? ( ) : ( {seckillList.map(item => ( handleSeckillClick(item)} > {item.goodsName || item.product?.name || '商品名称'} {/* 进度条 */} 已售{getProgress(item)}% {'\u00A5'}{item.seckillPrice} {'\u00A5'}{item.product?.price || 0} 0 ? '#ff4d4f' : '#999' }} > {item.stock > 0 ? '立即抢购' : '已抢光'} ))} )} ) } export default SeckillListPage