// pages/activity/detail.js
|
const app = getApp()
|
const utils = require('../../lib/utils.js')
|
|
Page({
|
data: {
|
activityId: null,
|
activity: null,
|
myApplication: null,
|
buttonDisabled: false,
|
buttonText: '我要报名',
|
loading: true,
|
error: null
|
},
|
|
onLoad(options) {
|
if (options.id) {
|
this.setData({ activityId: options.id });
|
this.loadActivityDetail(options.id);
|
} else {
|
this.setData({ loading: false, error: '无效的比赛ID' });
|
}
|
},
|
|
onPullDownRefresh() {
|
if (this.data.activityId) {
|
this.loadActivityDetail(this.data.activityId).finally(() => {
|
wx.stopPullDownRefresh();
|
});
|
} else {
|
wx.stopPullDownRefresh();
|
}
|
},
|
|
loadActivityDetail(id) {
|
this.setData({ loading: true, error: null });
|
const query = `
|
query GetActivityDetailAndStatus($id: ID!) {
|
activity(id: $id) {
|
id
|
name
|
description
|
signupDeadline
|
matchTime
|
address
|
state
|
stateName
|
playerCount
|
playerMax
|
coverImage { fullUrl }
|
images { fullUrl }
|
videos { fullUrl, fullThumbUrl }
|
stages {
|
id
|
name
|
matchTime
|
description
|
}
|
}
|
myActivityPlayer(activityId: $id) {
|
id
|
state
|
}
|
}
|
`;
|
|
return app.graphqlRequest(query, { id: id })
|
.then(res => {
|
if (res.activity) {
|
let buttonDisabled = false;
|
let buttonText = '我要报名';
|
const myApplication = res.myActivityPlayer;
|
|
if (myApplication) {
|
if (myApplication.state === 0) {
|
buttonDisabled = true;
|
buttonText = '等待审核';
|
} else if (myApplication.state === 1) {
|
buttonDisabled = true;
|
buttonText = '已报名';
|
}
|
}
|
|
if (!buttonDisabled) {
|
if (new Date() > new Date(res.activity.signupDeadline)) {
|
buttonDisabled = true;
|
buttonText = '报名已截止';
|
}
|
}
|
|
this.setData({
|
activity: res.activity,
|
myApplication: myApplication,
|
loading: false,
|
buttonDisabled: buttonDisabled,
|
buttonText: buttonText
|
});
|
} else {
|
throw new Error('未找到比赛信息');
|
}
|
})
|
.catch(err => {
|
this.setData({
|
loading: false,
|
error: '加载失败,请稍后重试'
|
});
|
console.error('加载比赛详情或报名状态失败:', err);
|
});
|
},
|
|
handleRegister() {
|
if (!this.data.activityId) {
|
wx.showToast({
|
title: '无效的活动ID',
|
icon: 'none'
|
});
|
return;
|
}
|
wx.navigateTo({
|
url: `/pages/registration/registration?id=${this.data.activityId}`
|
});
|
},
|
|
// 格式化日期和时间
|
formatDateTime(dateStr) {
|
return utils.formatDate(dateStr, 'YYYY-MM-DD HH:mm');
|
},
|
|
// 格式化日期范围
|
formatDateRange(start, end) {
|
if (!start && !end) return '待定';
|
const startDate = start ? utils.formatDate(start, 'YYYY-MM-DD') : '';
|
const endDate = end ? utils.formatDate(end, 'YYYY-MM-DD') : '';
|
if (startDate && endDate && startDate !== endDate) {
|
return `${startDate} - ${endDate}`;
|
}
|
return startDate || endDate;
|
}
|
});
|