index.vue 7.63 KB
<!--
 * @Date: 2022-09-27 17:13:05
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-10-20 21:56:24
 * @FilePath: /swx/src/pages/joinActivity/index.vue
 * @Description: 活动报名
-->
<template>
  <view class="join-activity-page">
    <view class="activity-title">
      <view class="box">
        <text class="bg-gradient" style="font-size: 1.25rem;">请填写报名信息</text>
      </view>
    </view>
    <AtInput required :border="true" title="姓名" type='text' placeholder='请输入姓名' v-model:value="username" />
    <AtInput required :border="true" title="手机号" type='phone' placeholder='请输入手机号' v-model:value="phone" />
    <view class="at-input">
      <view class="at-input__container">
        <label class="h5-label at-input__title at-input__title--required">年龄段</label>
        <input @tap="show_popup=true" class="h5-input at-input__input" placeholder-class="placeholder" :value="age_group" placeholder="请选择年龄范围" :disabled="true"/>
        <view style="margin-right: 1rem; margin-top: 0.5rem;"><van-icon :name="icon_sel" color="" /></view>
      </view>
    </view>
    <view class="at-input">
      <view class="at-input__container">
        <label class="h5-label at-input__title at-input__title--required">性别</label>
        <van-radio-group
          :value="user_sex"
          @change="onSexChange"
          direction="horizontal"
        >
          <van-radio name="man" checked-color="#199A74">男士</van-radio>
          <van-radio name="woman" checked-color="#199A74">女士</van-radio>
        </van-radio-group>
      </view>
    </view>
    <view v-for="(item, index) in fields" :key="index">
      <AtInput
      :required="item.is_require"
      :border="true"
      :title="item.field"
      type='text'
      :placeholder="'请输入' + item.field"
      v-model:value="item.value" />
    </view>
  </view>

  <bottom-button @on-submit="onSubmit">确认报名</bottom-button>

  <!-- 年龄段弹出框 -->
  <!-- <van-popup :show="show_popup" position="bottom" custom-style="height: 30%;" :lock-scroll="true">
    <view class="limit-wrapper">
      <view class="form-item border">
        <view class="form-item-title fix">年龄最小值</view>
        <view style="float: right;">
          <van-field :value="min_number" @change="changeMinNumber" label="" placeholder="请输入年龄最小值"
            placeholder-style="color: #999; font-size: 1rem;" input-align="right" customStyle="" rightIcon=""
            maxlength="3" :border="false" />
        </view>
      </view>
      <view class="form-item border">
        <view class="form-item-title fix">年龄最大值</view>
        <view style="float: right;">
          <van-field :value="max_number" @change="changeMaxNumber" label="" placeholder="请输入年龄最大值"
            placeholder-style="color: #999; font-size: 1rem;" input-align="right" customStyle="" rightIcon=""
            maxlength="3" :border="false" />
        </view>
      </view>
    </view>
    <van-row>
      <van-col span="12">
        <view class="limit-button-wrapper">
          <view class="button cancel" @tap="closeEditLimit">取消</view>
        </view>
      </van-col>
      <van-col span="12">
        <view class="limit-button-wrapper">
          <view class="button confirm" @tap="confirmEditLimit">确定</view>
        </view>
      </van-col>
    </van-row>
  </van-popup> -->
  <van-popup :show="show_popup" position="bottom" custom-style="height: 40%;" :lock-scroll="true">
    <van-picker :show-toolbar="true" title="" confirm-button-text="确定" :columns="age_columns"
      :default-index="defaultIndex"
      toolbar-class="picker-toolbar" @confirm="onAgeConfirm" @cancel="onAgeCancel" @change="onAgeChange" />
  </van-popup>
  <van-toast id="van-toast" />
</template>

<script setup>
import { ref, onMounted } from "vue";
import { AtInput } from 'taro-ui-vue3'
import "taro-ui-vue3/dist/style/components/input.scss";
import icon_sel from '@/images/icon/sel@2x.png'
import bottomButton from "@/components/bottom-button";
import Taro from '@tarojs/taro'
import { getCurrentPageParam } from "@/utils/weapp";
import { activityInfoAPI } from '@/api/Host/index';
import { addRegAPI, editRegAPI } from '@/api/Reg/index';
import mixin from '@/utils/mixin';
import Toast from '@/components/vant-weapp/toast/toast';
import { myInfoAPI } from '@/api/Reg/index';

const username = ref('');
const phone = ref('');
const user_sex = ref('');
const fields = ref([]);

onMounted(async () => {
  const { code, data } = await activityInfoAPI({ i: getCurrentPageParam().id });
  if (code) {
    // 额外的报名信息
    fields.value = data.activity.fields;
    fields.value.forEach(item => {
      item.value = '';
    });
  }
  // 编辑模式
  if (getCurrentPageParam().type === 'edit') {
    // 动态修改标题
    wx.setNavigationBarTitle({
      title: '修改活动报名',
    });
    const { code, data } = await myInfoAPI({ i: getCurrentPageParam().reg_id });
    if (code) {
      username.value = data.name;
      phone.value = data.phone;
      age_group.value = data.age_group;
      user_sex.value = data.gender;
      // 额外信息
      if (data.extend) {
        for (const key in data.extend) {
          fields.value.forEach(item => {
            if (item.field === key) {
              item.value = data.extend[key];
            }
          });
        }
      }
    }
  }
})

const onSexChange = ({ detail }) => {
  user_sex.value = detail;
}

const show_popup = ref(false);
const age_group = ref('');
const age_columns = ['10-19岁', '20-29岁', '30-39岁', '40-49岁', '50-59岁', '60-69岁', '70-79岁', '80岁以上']
const defaultIndex = ref(0);
const onAgeChange = (event) => {
}
const onAgeConfirm = (event) => {
  const detail = event.detail;
  age_group.value = detail.value;
  show_popup.value = false;
}
const onAgeCancel = (event) => {
  show_popup.value = false;
}

const validForm = () => {
  if (!username.value) {
    Toast('姓名不能为空');
    return false;
  }
  if (!phone.value) {
    Toast('手机号不能为空');
    return false;
  }
  const pattern = /^\d{11}$/;
  if (!pattern.test(phone.value)) {
    Toast('请输入正确手机号');
    return false;
  }
  if (!age_group.value) {
    Toast('年龄段不能为空');
    return false;
  }
  if (!user_sex.value) {
    Toast('性别不能为空');
    return false;
  }
  fields.value.length && fields.value.forEach(item => {
    if (item.is_require && !item.value) {
      Toast(item.field + '不能为空');
      return false;
    }
  });
  return true;
}

const onSubmit = async (val) => {
  if (validForm()) {
    // 额外信息枚举字段
    const extend = {};
    fields.value.length && fields.value.forEach(item => {
      extend[item.field] = item.value;
    });
    const params = {
      activity_id: getCurrentPageParam().id,
      type: 'player',
      name: username.value,
      phone: phone.value,
      gender: user_sex.value,
      age_group: age_group.value,
      post: '',
      extend: JSON.stringify(extend)
    }
    if (getCurrentPageParam().type === 'edit') {
      params.i = getCurrentPageParam().reg_id;
      const { code } = await editRegAPI(params);
      if (code) {
        Taro.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 3000,
          success: function () {
            Taro.navigateBack()
          }
        });
      }
    } else {
      const { code } = await addRegAPI(params);
      if (code) {
        Taro.redirectTo({
          url: '../joinSuccess/index?id=' + getCurrentPageParam().id
        })
      }
    }
  }
}
</script>

<script>
import "./index.less";

export default {
  name: "demoPage",
  mixins: [mixin.init],
};
</script>