// 测试注册页面修复
|
// 模拟小程序环境和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)
|