import React, { useState, useEffect } from 'react' import { View, Text, ScrollView, Image } from '@tarojs/components' import Taro from '@tarojs/taro' import { getShopActivity, signUpActivity, cancelSignUpActivity, getSignUpStatus } from '@/api/shop/shopActivity' import type { ShopActivity, ActivityStatus } from '@/api/shop/shopActivity/model' definePageConfig({ navigationBarTitleText: '活动详情', }) const ActivityDetailPage: React.FC = () => { const { id } = Taro.getCurrentInstance().router?.params || {} const [activity, setActivity] = useState(null) const [loading, setLoading] = useState(true) const [signedUp, setSignedUp] = useState(false) const [submitting, setSubmitting] = useState(false) useEffect(() => { if (id) { fetchDetail(Number(id)) fetchSignUpStatus(Number(id)) } }, [id]) const fetchDetail = async (activityId: number) => { try { const data = await getShopActivity(activityId) setActivity(data) } catch (err: any) { Taro.showToast({ title: err.message || '加载失败', icon: 'none' }) } finally { setLoading(false) } } const fetchSignUpStatus = async (activityId: number) => { try { const res = await getSignUpStatus(activityId) setSignedUp(res.signedUp) } catch { // 未登录或接口不可用时忽略 } } // 报名活动 const handleSignUp = () => { if (!activity) return Taro.showModal({ title: '确认报名', content: '确定报名参加该活动吗?', confirmColor: '#0e932e', success: async (res) => { if (res.confirm) { setSubmitting(true) try { await signUpActivity({ activityId: activity.id, userId: 0 }) setSignedUp(true) setActivity(prev => prev ? { ...prev, participants: prev.participants + 1 } : prev) Taro.showToast({ title: '报名成功', icon: 'success' }) } catch (err: any) { Taro.showToast({ title: err.message || '报名失败', icon: 'none' }) } finally { setSubmitting(false) } } } }) } // 取消报名 const handleCancelSignUp = () => { if (!activity) return Taro.showModal({ title: '取消报名', content: '确定取消报名吗?', confirmColor: '#ef4444', success: async (res) => { if (res.confirm) { setSubmitting(true) try { await cancelSignUpActivity(activity.id) setSignedUp(false) setActivity(prev => prev ? { ...prev, participants: Math.max(0, prev.participants - 1) } : prev) Taro.showToast({ title: '已取消报名', icon: 'success' }) } catch (err: any) { Taro.showToast({ title: err.message || '取消失败', icon: 'none' }) } finally { setSubmitting(false) } } } }) } // 分享活动 const handleShare = () => { Taro.showToast({ title: '分享功能开发中', icon: 'none' }) } // 获取状态标签 const getStatusLabel = (status: ActivityStatus) => { const map: Record = { upcoming: { label: '未开始', color: 'text-blue-500' }, ongoing: { label: '进行中', color: 'text-green-500' }, ended: { label: '已结束', color: 'text-gray-400' }, cancelled: { label: '已取消', color: 'text-gray-400' }, } return map[status] || { label: '未知', color: 'text-gray-400' } } // 格式化时间 const formatTime = (timeStr: string) => { const date = new Date(timeStr) return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}` } if (loading) { return ( 加载中... ) } if (!activity) { return ( 活动不存在 ) } const statusInfo = getStatusLabel(activity.status) return ( {/* 活动头图 */} {activity.image ? ( ) : ( 🎉 )} {/* 活动信息 */} {activity.name} {statusInfo.label} 📅 {formatTime(activity.startTime)} 👥 {activity.participants}人参与 {activity.maxParticipants && ( 🔢 限{activity.maxParticipants}人 )} {activity.typeName || activity.type} {activity.description.split('\n')[0]} {/* 活动详情 */} 活动详情 {activity.description} {/* 活动规则 */} {activity.rules && ( 活动规则 {activity.rules} )} {/* 底部操作栏 */} 分享 {activity.status === 'ongoing' && !signedUp && ( {submitting ? '处理中...' : '立即报名'} )} {activity.status === 'ongoing' && signedUp && ( {submitting ? '处理中...' : '取消报名'} )} {activity.status === 'upcoming' && ( 即将开始 )} {(activity.status === 'ended' || activity.status === 'cancelled') && ( 已结束 )} ) } export default ActivityDetailPage