lrj
昨天 9f8395fab13ca4b230a0f7d62636e209745c91d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// pages/activity/detail.js
const app = getApp()
const utils = require('../../lib/utils.js')
 
Page({
  data: {
    // 活动ID
    activityId: null,
    // 活动详情
    activity: null,
    // 加载状态
    loading: true,
    // 用户报名状态
    registrationStatus: null,
    // 是否已报名
    isRegistered: false,
    // 媒体文件列表
    mediaFiles: [],
    // 当前媒体索引
    currentMediaIndex: 0,
    // 是否显示媒体预览
    showMediaPreview: false,
    // 报名人数(实时更新)
    currentParticipants: 0,
    // 是否可以报名
    canRegisterStatus: false,
    // 报名按钮文本
    registerButtonText: '立即报名',
    // 报名按钮是否禁用
    registerButtonDisabled: false
  },
 
  onLoad(options) {
    console.log('活动详情页加载', options)
    
    if (options.id) {
      this.setData({ activityId: options.id })
      this.loadActivityDetail()
      this.checkRegistrationStatus()
    } else {
      utils.showError('参数错误')
      utils.navigateBack()
    }
  },
 
  onShow() {
    console.log('活动详情页显示')
    // 刷新报名人数
    if (this.data.activityId) {
      this.refreshParticipantCount()
    }
  },
 
  onShareAppMessage() {
    const activity = this.data.activity
    return {
      title: activity ? activity.name : '精彩赛事',
      path: `/pages/activity/detail?id=${this.data.activityId}`,
      imageUrl: activity && activity.coverImage ? activity.coverImage.fullUrl : ''
    }
  },
 
  // 加载赛事详情
  loadActivityDetail() {
    this.setData({ loading: true })
    
    app.graphqlRequest(`
      query getActivityDetail($id: ID!) {
        activity(id: $id) {
          id
          name
          description
          signupDeadline
          matchTime
          address
          playerMax
          state
          createTime
          updateTime
          playerCount
          stateName
          coverImage {
            id
            name
            path
            fullUrl
            fullThumbUrl
            mediaType
          }
          images {
            id
            name
            path
            fullUrl
            fullThumbUrl
            mediaType
          }
          videos {
            id
            name
            path
            fullUrl
            fullThumbUrl
            mediaType
          }
        }
      }
    `, {
      id: this.data.activityId
    }).then(data => {
      if (data.activity) {
        const activity = data.activity
        
        // 处理媒体文件
        const mediaFiles = []
        
        // 添加封面图
        if (activity.coverImage) {
          mediaFiles.push({
            type: 'image',
            url: activity.coverImage.fullUrl,
            title: '封面图片'
          })
        }
        
        // 添加其他图片
        if (activity.images && activity.images.length > 0) {
          activity.images.forEach((image, index) => {
            mediaFiles.push({
              type: 'image',
              url: image.fullUrl,
              title: `图片 ${index + 1}`
            })
          })
        }
        
        // 添加视频
        if (activity.videos && activity.videos.length > 0) {
          activity.videos.forEach((video, index) => {
            mediaFiles.push({
              type: 'video',
              url: video.fullUrl,
              title: `视频 ${index + 1}`
            })
          })
        }
        
        // 预处理时间字段
        if (activity.signupDeadline) {
          activity.formattedSignupDeadline = this.formatDate(activity.signupDeadline)
        }
        if (activity.matchTime) {
          activity.formattedMatchTime = this.formatDate(activity.matchTime)
        }
 
        this.setData({
          activity: activity,
          mediaFiles: mediaFiles,
          currentParticipants: activity.playerCount,
          loading: false
        }, () => {
          // 在数据设置完成后,检查报名状态
          this.checkRegistrationStatus()
          this.updateCanRegisterStatus()
        })
      } else {
        utils.showError('赛事不存在')
        utils.navigateBack()
      }
    }).catch(err => {
      console.error('加载赛事详情失败:', err)
      utils.showError('加载失败,请重试')
      this.setData({ loading: false })
    })
  },
 
  // 检查报名状态
  checkRegistrationStatus() {
    if (!app.globalData.token) {
      return
    }
    
    app.graphqlRequest(`
      query checkRegistrationStatus($activityId: ID!) {
        playerRegistration(activityId: $activityId) {
          id
          status
          registrationTime
          reviewStatus
          reviewComment
        }
      }
    `, {
      activityId: this.data.activityId
    }).then(data => {
      if (data.playerRegistration) {
        this.setData({
          registrationStatus: data.playerRegistration,
          isRegistered: true
        }, () => {
          this.updateCanRegisterStatus()
        })
      } else {
        this.setData({
          isRegistered: false
        }, () => {
          this.updateCanRegisterStatus()
        })
      }
    }).catch(err => {
      console.error('检查报名状态失败:', err)
    })
  },
 
  // 刷新报名人数
  refreshParticipantCount() {
    app.graphqlRequest(`
      query getParticipantCount($id: ID!) {
        activity(id: $id) {
          playerCount
        }
      }
    `, {
      id: this.data.activityId
    }).then(data => {
      if (data.activity) {
        this.setData({
          currentParticipants: data.activity.playerCount
        }, () => {
          this.updateCanRegisterStatus()
        })
      }
    }).catch(err => {
      console.error('刷新报名人数失败:', err)
    })
  },
 
  // 媒体文件点击
  onMediaTap(e) {
    const index = e.currentTarget.dataset.index
    const media = this.data.mediaFiles[index]
    
    if (media.type === 'image') {
      // 预览图片
      const imageUrls = this.data.mediaFiles
        .filter(item => item.type === 'image')
        .map(item => item.url)
      
      wx.previewImage({
        current: media.url,
        urls: imageUrls
      })
    } else if (media.type === 'video') {
      // 播放视频
      this.setData({
        currentMediaIndex: index,
        showMediaPreview: true
      })
    }
  },
 
  // 关闭媒体预览
  closeMediaPreview() {
    this.setData({
      showMediaPreview: false
    })
  },
 
  // 报名按钮点击
  onRegisterTap() {
    // 如果按钮被禁用,不执行任何操作
    if (this.data.registerButtonDisabled) {
      return
    }
 
    // 如果已报名,查看报名状态
    if (this.data.isRegistered) {
      this.onViewRegistrationStatus()
      return
    }
 
    // 检查登录状态
    if (!app.globalData.token) {
      utils.showToast('请先登录')
      app.login()
      return
    }
    
    const activity = this.data.activity
    
    // 检查报名时间
    const now = new Date()
    const regEnd = new Date(activity.signupDeadline)
    
    if (now > regEnd) {
      utils.showToast('报名已结束')
      return
    }
    
    // 检查报名人数
    if (activity.playerMax && this.data.currentParticipants >= activity.playerMax) {
      utils.showToast('报名人数已满')
      return
    }
    
    // 跳转到报名页面
    utils.navigateTo('/pages/registration/registration', {
      activityId: this.data.activityId
    })
  },
 
  // 查看报名状态
  onViewRegistrationStatus() {
    const status = this.data.registrationStatus
    let statusText = ''
    
    switch (status.status) {
      case 'PENDING':
        statusText = '待审核'
        break
      case 'APPROVED':
        statusText = '已通过'
        break
      case 'REJECTED':
        statusText = '已拒绝'
        break
      case 'CANCELLED':
        statusText = '已取消'
        break
      default:
        statusText = status.status
    }
    
    let content = `报名状态:${statusText}\n报名时间:${utils.formatDate(status.registrationTime)}`
    
    if (status.reviewComment) {
      content += `\n审核意见:${status.reviewComment}`
    }
    
    wx.showModal({
      title: '报名状态',
      content: content,
      showCancel: false
    })
  },
 
  // 联系主办方
  onContactTap() {
    const activity = this.data.activity
    
    if (activity.contactPhone) {
      wx.showActionSheet({
        itemList: ['拨打电话', '复制号码'],
        success: (res) => {
          if (res.tapIndex === 0) {
            wx.makePhoneCall({
              phoneNumber: activity.contactPhone
            })
          } else if (res.tapIndex === 1) {
            wx.setClipboardData({
              data: activity.contactPhone,
              success: () => {
                utils.showSuccess('号码已复制')
              }
            })
          }
        }
      })
    } else {
      utils.showToast('暂无联系方式')
    }
  },
 
  // 分享赛事
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
 
  // 格式化日期
  formatDate(date) {
    return utils.formatDate(date, 'YYYY-MM-DD HH:mm')
  },
 
  // 获取状态文本
  getStatusText(state) {
    const statusMap = {
      0: '草稿',
      1: '已发布',
      2: '报名结束',
      3: '进行中',
      4: '已结束',
      5: '已取消'
    }
    return statusMap[state] || '未知状态'
  },
 
  // 获取状态样式类
  getStatusClass(state) {
    const classMap = {
      0: 'text-muted',
      1: 'text-success',
      2: 'text-warning',
      3: 'text-primary',
      4: 'text-muted',
      5: 'text-danger'
    }
    return classMap[state] || 'text-muted'
  },
 
  // 获取报名进度百分比
  getRegistrationProgress() {
    const activity = this.data.activity
    if (!activity || !activity.playerMax || activity.playerMax <= 0) {
      return 0
    }
    return Math.min((this.data.currentParticipants / activity.playerMax) * 100, 100)
  },
 
  // 判断是否可以报名
  canRegister() {
    const activity = this.data.activity
    if (!activity) return false
    
    const now = new Date()
    const regEnd = new Date(activity.signupDeadline)
    
    return now <= regEnd && 
           activity.state === 1 &&
           (!activity.playerMax || this.data.currentParticipants < activity.playerMax) &&
           !this.data.isRegistered
  },
 
  // 获取报名按钮状态和文本
  getRegisterButtonStatus() {
    const activity = this.data.activity
    if (!activity) {
      return { canRegister: false, buttonText: '暂不可报名', disabled: true }
    }
 
    // 如果已报名
    if (this.data.isRegistered) {
      return { canRegister: false, buttonText: '查看报名状态', disabled: false }
    }
 
    const now = new Date()
    const regEnd = new Date(activity.signupDeadline)
 
    // 检查报名截止时间
    if (now > regEnd) {
      return { canRegister: false, buttonText: '报名已结束', disabled: true }
    }
 
    // 检查报名人数是否已满
    if (activity.playerMax && this.data.currentParticipants >= activity.playerMax) {
      return { canRegister: false, buttonText: '人数已满', disabled: true }
    }
 
    // 检查活动状态
    if (activity.state !== 1) {
      return { canRegister: false, buttonText: '暂不可报名', disabled: true }
    }
 
    // 可以报名
    return { canRegister: true, buttonText: '立即报名', disabled: false }
  },
 
  // 更新报名状态
  updateCanRegisterStatus() {
    const buttonStatus = this.getRegisterButtonStatus()
    this.setData({
      canRegisterStatus: buttonStatus.canRegister,
      registerButtonText: buttonStatus.buttonText,
      registerButtonDisabled: buttonStatus.disabled
    })
  }
})