import { View, Text, Image, ScrollView } from '@tarojs/components' import Taro, { useLoad, useShareAppMessage, useShareTimeline } from '@tarojs/taro' import { useState } from 'react' import { getCompetitions } from '../../api' import './index.scss' export default function CompetitionList() { const [competitions, setCompetitions] = useState([]) const [loading, setLoading] = useState(false) const [debugMsg, setDebugMsg] = useState('') useLoad(() => { fetchCompetitions() }) /** * 配置并监听分享给朋友的功能 */ useShareAppMessage(() => { return { title: '赛事中心', path: '/pages/competition/index' } }) /** * 配置并监听分享到朋友圈的功能 */ useShareTimeline(() => { return { title: '赛事中心', query: '' } }) const fetchCompetitions = async () => { setLoading(true) setDebugMsg('开始加载...') try { console.log('Fetching competitions...') const res = await getCompetitions() console.log('Competitions res:', res) setDebugMsg(`请求成功: 数量 ${res?.results?.length}`) if (res && res.results) { setCompetitions(res.results) } else { setDebugMsg(`数据格式异常: ${JSON.stringify(res)}`) } } catch (e) { console.error('Fetch failed:', e) setDebugMsg(`请求失败: ${e.errMsg || JSON.stringify(e)}`) Taro.showToast({ title: '加载失败', icon: 'none' }) } finally { setLoading(false) } } const goDetail = (id) => { Taro.navigateTo({ url: `/pages/competition/detail?id=${id}` }) } const getStatusText = (status) => { const map = { 'published': '即将开始', 'registration': '报名中', 'submission': '作品提交中', 'judging': '评审中', 'ended': '已结束', 'draft': '草稿' } return map[status] || status } return ( {competitions.map(item => ( goDetail(item.id)}> {item.title} {getStatusText(item.status)} {item.description} {item.start_time?.split('T')[0]} ~ {item.end_time?.split('T')[0]} ))} {!loading && competitions.length === 0 && ( 暂无比赛 调试信息: {debugMsg} )} ) }