const request = require('../../utils/request') Page({ data: { tabs: [ { label: '全部', value: '', className: 'tab-item-active' }, { label: '户外', value: 'outdoor', className: '' }, { label: '餐饮', value: 'dining', className: '' }, { label: '文化', value: 'culture', className: '' }, { label: '体育', value: 'sport', className: '' } ], currentCategory: '', allActivities: [], activities: [], loading: false }, onShow() { const app = getApp() app.ensureLogin() .catch((err) => { console.error('Ensure login before activities failed:', err) }) .finally(() => { this.loadActivities() }) }, async loadActivities() { this.setData({ loading: true }) try { const activities = await request({ url: '/activities' }) const normalized = (activities || []).map((item) => ({ ...item, locationText: item.location || '地点待定', statusText: item.status === 1 ? '报名中' : item.status === 4 ? '已结束' : '其他', statusClassName: item.status === 1 ? 'status-active' : item.status === 4 ? 'status-finished' : 'status-default', registerText: item.is_registered ? '已报名' : item.can_register ? '可报名' : '不可报名', remainPercent: Math.min( Math.round((((item.registered_male || 0) + (item.registered_female || 0)) / Math.max((item.capacity_male || 0) + (item.capacity_female || 0), 1)) * 100), 100 ), remainSummary: `男 ${item.registered_male || 0}/${item.capacity_male || 0} · 女 ${item.registered_female || 0}/${item.capacity_female || 0}`, matchSummary: item.match_limit ? `已匹配 ${item.match_count || 0}/${item.match_limit} · 剩余 ${item.match_remaining || 0}` : '' })) this.setData({ allActivities: normalized }) this.applyCategoryFilter() } catch (err) { console.error('Load activities failed:', err) wx.showToast({ title: '活动加载失败,请稍后重试', icon: 'none' }) this.setData({ activities: [], allActivities: [] }) } finally { this.setData({ loading: false }) } }, applyCategoryFilter() { const currentCategory = this.data.currentCategory const activities = !currentCategory ? this.data.allActivities : this.data.allActivities.filter((item) => item.category === currentCategory) this.setData({ activities }) }, switchTab(e) { const currentCategory = e.currentTarget.dataset.value this.setData({ currentCategory, tabs: this.data.tabs.map((item) => ({ ...item, className: item.value === currentCategory ? 'tab-item-active' : '' })) }) this.applyCategoryFilter() }, goDetail(e) { const id = e.currentTarget.dataset.id wx.navigateTo({ url: `/pages/activity-detail/activity-detail?id=${id}` }) } })