// pages/activity/detail.js const app = getApp() const utils = require('../../lib/utils.js') Page({ data: { activityId: null, activity: null, myApplication: null, buttonDisabled: false, buttonText: '我要报名', // 导出相关 isEmployee: false, exportingActivity: false, exportingStageId: null, 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 = '报名已截止'; } } // 角色:是否员工(主办方/管理员) const isEmployee = !!(app.globalData?.userInfo?.employee && app.globalData.userInfo.employee.id) this.setData({ activity: res.activity, myApplication: myApplication, loading: false, buttonDisabled: buttonDisabled, buttonText: buttonText, isEmployee: isEmployee }); } 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; }, // 复制链接到剪贴板 copyLink(e) { const url = e.currentTarget.dataset.url if (!url) { wx.showToast({ title: '暂无链接', icon: 'none' }) return } wx.setClipboardData({ data: url, success: () => { wx.showToast({ title: '已复制下载链接', icon: 'success' }) } }) }, // 在WebView内打开链接(用于预览/下载) openWebView(e) { const url = e.currentTarget.dataset.url if (!url) { wx.showToast({ title: '暂无链接', icon: 'none' }) return } wx.navigateTo({ url: `/pages/webview/webview?url=${encodeURIComponent(url)}&title=${encodeURIComponent('评审导出ZIP')}` }) }, // 触发导出(主活动) async handleExportActivity() { if (!this.data.isEmployee) { wx.showToast({ title: '无权限执行导出', icon: 'none' }) return } if (!this.data.activityId) { wx.showToast({ title: '无效的活动ID', icon: 'none' }) return } this.setData({ exportingActivity: true }) const mutation = ` mutation ExportReviewZip($activityId: ID!, $stageIds: [ID]) { exportReviewZip(activityId: $activityId, stageIds: $stageIds) { success url message } } ` try { const res = await app.graphqlRequest(mutation, { activityId: this.data.activityId, stageIds: null }) const result = res && res.exportReviewZip if (result && result.success) { wx.showToast({ title: '导出成功', icon: 'success' }) // 刷新以显示最新链接 await this.loadActivityDetail(this.data.activityId) } else { wx.showToast({ title: result?.message || '导出失败', icon: 'none' }) } } catch (err) { console.error('导出失败:', err) wx.showToast({ title: '导出失败', icon: 'none' }) } finally { this.setData({ exportingActivity: false }) } }, // 触发导出(阶段) async handleExportStage(e) { if (!this.data.isEmployee) { wx.showToast({ title: '无权限执行导出', icon: 'none' }) return } const stageId = e.currentTarget.dataset.stageId if (!stageId) { wx.showToast({ title: '无效的阶段ID', icon: 'none' }) return } this.setData({ exportingStageId: stageId }) const mutation = ` mutation ExportReviewZip($activityId: ID!, $stageIds: [ID]) { exportReviewZip(activityId: $activityId, stageIds: $stageIds) { success url message } } ` try { const res = await app.graphqlRequest(mutation, { activityId: this.data.activityId, stageIds: [stageId] }) const result = res && res.exportReviewZip if (result && result.success) { wx.showToast({ title: '导出成功', icon: 'success' }) await this.loadActivityDetail(this.data.activityId) } else { wx.showToast({ title: result?.message || '导出失败', icon: 'none' }) } } catch (err) { console.error('导出阶段失败:', err) wx.showToast({ title: '导出失败', icon: 'none' }) } finally { this.setData({ exportingStageId: null }) } } });