register.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { enroll } from '../../api/other';
  2. const app = getApp();
  3. Page({
  4. data: {
  5. formData: {
  6. phone: '',
  7. captcha: '',
  8. nickname: '',
  9. gender: '', // '女' 或 '男'
  10. race_number: '', // 身份证号
  11. competition_no: '', // 比赛ID
  12. competition_image: '' // 上传的图片路径
  13. },
  14. phoneError: '',
  15. genders: ['女', '男'],
  16. showPicker: false,
  17. pickerValue: '',
  18. fileList: [],
  19. checked: false,
  20. showRegistrationSuccess: false,
  21. canSubmit: false, // 按钮是否可点击
  22. showAgreementModal:false,
  23. agreementContent:'',
  24. agreementTitle:''
  25. },
  26. onShowPicker() {
  27. this.setData({ showPicker: true });
  28. },
  29. onCancel() {
  30. this.setData({ showPicker: false });
  31. },
  32. onPickerChange(event) {
  33. const { value } = event.detail;
  34. this.setData({ pickerValue: value });
  35. },
  36. onConfirm() {
  37. this.setData({
  38. formData: {
  39. ...this.data.formData,
  40. gender: this.data.pickerValue
  41. },
  42. showPicker: false
  43. }, () => this.checkFormValid());
  44. },
  45. // 上传文件
  46. afterRead(event) {
  47. const { file } = event.detail;
  48. wx.uploadFile({
  49. url: 'https://example.weixin.qq.com/upload', // 替换成真实接口
  50. filePath: file.url,
  51. name: 'file',
  52. formData: { user: 'test' },
  53. success: (res) => {
  54. const { fileList } = this.data;
  55. const path = res.data; // 假设返回路径
  56. fileList.push({ ...file, url: path });
  57. this.setData({
  58. fileList,
  59. formData: { ...this.data.formData, competition_image: path }
  60. });
  61. }
  62. });
  63. },
  64. // 输入框数据绑定
  65. onInput(e) {
  66. const field = e.currentTarget.dataset.field;
  67. const value = e.detail;
  68. // 手机号单独做校验
  69. if (field === 'phone') {
  70. const regPhone = /^1[3-9]\d{9}$/;
  71. if (value && !regPhone.test(value)) {
  72. this.setData({ phoneError: '手机号格式不正确' });
  73. }else{
  74. this.setData({ phoneError: '' });
  75. }
  76. }
  77. this.setData({
  78. formData: { ...this.data.formData, [field]: value }
  79. }, () => this.checkFormValid());
  80. },
  81. // 勾选协议
  82. onCheckbox(event) {
  83. this.setData({ checked: event.detail }, () => this.checkFormValid());
  84. },
  85. // 校验表单是否可提交
  86. checkFormValid() {
  87. const { phone, captcha, nickname, gender, race_number } = this.data.formData;
  88. const valid = phone && captcha && nickname && gender && race_number && this.data.checked;
  89. this.setData({ canSubmit: valid });
  90. },
  91. // 提交报名
  92. async onSubmit() {
  93. if (!this.data.canSubmit) return;
  94. const payload = {
  95. ...this.data.formData,
  96. gender: this.data.formData.gender === '女' ? 0 : 1
  97. };
  98. const res = await enroll(payload);
  99. if (res.code === 200) {
  100. wx.showToast({ title: res.data, icon: 'none', duration: 2000 });
  101. this.setData({ showRegistrationSuccess: true });
  102. } else {
  103. wx.showToast({ title: res.message || '报名失败', icon: 'none', duration: 2000 });
  104. }
  105. },
  106. // 点击协议文字显示弹窗
  107. showAgreement(e) {
  108. const type = e.currentTarget.dataset.type;
  109. const dataInfo = app.globalData.programConfig.marathon_event;
  110. this.setData({
  111. showAgreementModal: true,
  112. agreementContent: type == 'rules'?dataInfo.rules : type == 'liability'?dataInfo.disclaimer : dataInfo.privacy_policy,
  113. agreementTitle:type == 'rules'?'活动规则': type == 'liability'?'免责协议' : '隐私政策'
  114. });
  115. },
  116. closeAgreementModal() {
  117. this.setData({ showAgreementModal: false });
  118. },
  119. onReceive() {
  120. this.setData({ showRegistrationSuccess: false });
  121. wx.navigateBack({ delta: 1 });
  122. }
  123. });