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
// 测试注册页面修复
// 模拟小程序环境和registration.js的关键逻辑
 
// 模拟微信小程序API
const wx = {
  showToast: (options) => console.log('Toast:', options.title),
  showModal: (options) => {
    console.log('Modal:', options.title, '-', options.content)
    if (options.success) {
      options.success({ confirm: true })
    }
  },
  navigateBack: () => console.log('Navigate back')
}
 
// 模拟GraphQL请求
const graphqlRequest = async (query, variables) => {
  console.log('GraphQL Request:', { query: query.substring(0, 50) + '...', variables })
  
  // 添加延迟模拟网络请求
  await new Promise(resolve => setTimeout(resolve, 100))
  
  // 模拟不同的响应情况
  if (variables.id === 'invalid') {
    return { data: null }
  } else if (variables.id === 'error') {
    throw new Error('Network error')
  } else {
    return {
      data: {
        activity: {
          id: variables.id,
          title: '测试活动',
          description: '测试活动描述',
          registrationRequirements: '测试要求',
          isTeamActivity: false,
          maxTeamSize: 1,
          requiredFields: ['name', 'phone'],
          allowedFileTypes: ['jpg', 'png'],
          maxFileSize: 10485760
        }
      }
    }
  }
}
 
// 模拟页面数据
class RegistrationPage {
  constructor() {
    this.data = {
      activityId: null,
      activity: null,
      loading: false,
      isSubmitting: false,
      formData: {
        name: '测试用户',
        phone: '13800138000',
        gender: '男',
        regionId: '1'
      }
    }
  }
 
  setData(newData) {
    Object.assign(this.data, newData)
    console.log('Data updated:', Object.keys(newData))
  }
 
  // 加载活动信息(修复后的版本)
  async loadActivityInfo() {
    try {
      this.setData({ loading: true })
      
      const query = `
        query GetActivity($id: ID!) {
          activity(id: $id) {
            id
            title
            description
            registrationRequirements
            isTeamActivity
            maxTeamSize
            requiredFields
            allowedFileTypes
            maxFileSize
          }
        }
      `
      
      const result = await graphqlRequest(query, { id: this.data.activityId })
      
      if (result.data && result.data.activity) {
        this.setData({
          activity: result.data.activity
        })
        
        console.log('Activity loaded successfully:', result.data.activity.title)
      } else {
        // 活动信息不存在或加载失败
        wx.showModal({
          title: '提示',
          content: '活动信息不存在或已失效',
          showCancel: false,
          success: () => {
            wx.navigateBack()
          }
        })
      }
    } catch (error) {
      console.error('加载活动信息失败:', error)
      wx.showModal({
        title: '加载失败',
        content: '网络连接异常,请检查网络后重试',
        showCancel: true,
        cancelText: '返回',
        confirmText: '重试',
        success: (res) => {
          if (res.confirm) {
            console.log('用户选择重试')
          } else {
            wx.navigateBack()
          }
        }
      })
    } finally {
      this.setData({ loading: false })
    }
  }
 
  validateForm() {
    return true // 简化验证
  }
 
  // 提交方法(修复后的版本)
  async onSubmit() {
    if (this.data.isSubmitting) return
    
    // 检查活动信息是否加载完成
    if (!this.data.activity || !this.data.activity.id) {
      wx.showToast({
        title: '活动信息加载中,请稍后重试',
        icon: 'none'
      })
      return
    }
    
    // 表单验证
    if (!this.validateForm()) {
      wx.showToast({
        title: '请检查表单信息',
        icon: 'none'
      })
      return
    }
    
    this.setData({ isSubmitting: true })
    
    try {
      const { formData } = this.data
      const activityId = this.data.activity.id
      
      console.log('Submitting registration for activity:', activityId)
      
      // 模拟提交成功
      wx.showToast({
        title: '报名成功',
        icon: 'success'
      })
      
    } catch (error) {
      console.error('提交失败:', error)
      wx.showToast({
        title: error.message || '提交失败,请重试',
        icon: 'none'
      })
    } finally {
      this.setData({ isSubmitting: false })
    }
  }
}
 
// 测试场景
async function runTests() {
  console.log('=== 测试注册页面修复 ===\n')
  
  // 测试1: 正常情况
  console.log('测试1: 正常加载活动信息')
  const page1 = new RegistrationPage()
  page1.data.activityId = '123'
  await page1.loadActivityInfo()
  await page1.onSubmit()
  console.log('')
  
  // 测试2: 活动不存在
  console.log('测试2: 活动信息不存在')
  const page2 = new RegistrationPage()
  page2.data.activityId = 'invalid'
  await page2.loadActivityInfo()
  await page2.onSubmit()
  console.log('')
  
  // 测试3: 网络错误
  console.log('测试3: 网络连接错误')
  const page3 = new RegistrationPage()
  page3.data.activityId = 'error'
  await page3.loadActivityInfo()
  await page3.onSubmit()
  console.log('')
  
  // 测试4: 活动未加载时提交
  console.log('测试4: 活动信息未加载时提交')
  const page4 = new RegistrationPage()
  await page4.onSubmit()
  console.log('')
  
  console.log('=== 测试完成 ===')
}
 
runTests().catch(console.error)