import { enroll } from '../../api/other'; const app = getApp(); Page({ data: { formData: { phone: '', captcha: '', nickname: '', gender: '', // '女' 或 '男' race_number: '', // 身份证号 competition_no: '', // 比赛ID competition_image: '' // 上传的图片路径 }, phoneError: '', genders: ['女', '男'], showPicker: false, pickerValue: '', fileList: [], checked: false, showRegistrationSuccess: false, canSubmit: false, // 按钮是否可点击 showAgreementModal:false, agreementContent:'', agreementTitle:'' }, onShowPicker() { this.setData({ showPicker: true }); }, onCancel() { this.setData({ showPicker: false }); }, onPickerChange(event) { const { value } = event.detail; this.setData({ pickerValue: value }); }, onConfirm() { this.setData({ formData: { ...this.data.formData, gender: this.data.pickerValue }, showPicker: false }, () => this.checkFormValid()); }, // 上传文件 afterRead(event) { const { file } = event.detail; wx.uploadFile({ url: 'https://example.weixin.qq.com/upload', // 替换成真实接口 filePath: file.url, name: 'file', formData: { user: 'test' }, success: (res) => { const { fileList } = this.data; const path = res.data; // 假设返回路径 fileList.push({ ...file, url: path }); this.setData({ fileList, formData: { ...this.data.formData, competition_image: path } }); } }); }, // 输入框数据绑定 onInput(e) { const field = e.currentTarget.dataset.field; const value = e.detail; // 手机号单独做校验 if (field === 'phone') { const regPhone = /^1[3-9]\d{9}$/; if (value && !regPhone.test(value)) { this.setData({ phoneError: '手机号格式不正确' }); }else{ this.setData({ phoneError: '' }); } } this.setData({ formData: { ...this.data.formData, [field]: value } }, () => this.checkFormValid()); }, // 勾选协议 onCheckbox(event) { this.setData({ checked: event.detail }, () => this.checkFormValid()); }, // 校验表单是否可提交 checkFormValid() { const { phone, captcha, nickname, gender, race_number } = this.data.formData; const valid = phone && captcha && nickname && gender && race_number && this.data.checked; this.setData({ canSubmit: valid }); }, // 提交报名 async onSubmit() { if (!this.data.canSubmit) return; const payload = { ...this.data.formData, gender: this.data.formData.gender === '女' ? 0 : 1 }; const res = await enroll(payload); if (res.code === 200) { wx.showToast({ title: res.data, icon: 'none', duration: 2000 }); this.setData({ showRegistrationSuccess: true }); } else { wx.showToast({ title: res.message || '报名失败', icon: 'none', duration: 2000 }); } }, // 点击协议文字显示弹窗 showAgreement(e) { const type = e.currentTarget.dataset.type; const dataInfo = app.globalData.programConfig.marathon_event; this.setData({ showAgreementModal: true, agreementContent: type == 'rules'?dataInfo.rules : type == 'liability'?dataInfo.disclaimer : dataInfo.privacy_policy, agreementTitle:type == 'rules'?'活动规则': type == 'liability'?'免责协议' : '隐私政策' }); }, closeAgreementModal() { this.setData({ showAgreementModal: false }); }, onReceive() { this.setData({ showRegistrationSuccess: false }); wx.navigateBack({ delta: 1 }); } });