verifyUser.vue 6.17 KB
<template>
  <div class="verify-user-page">
    <p class="title" style="">请录入真实的信息</p>
    <div class="input-content">
      <van-config-provider :theme-vars="themeVars">
        <van-form ref="form" @submit="onSubmit">
          <div class="van-hairline--bottom">
            <van-field v-model="user_name" name="user_name" label="家长姓名" placeholder="请输入你真实的姓名"
              :rules="[{ required: true, message: '请填写家长姓名' }]" />
          </div>
          <div class="van-hairline--bottom">
            <van-field v-if="use_widget" v-model="phone" name="phone" label="手机号" placeholder="手机号" readonly clickable
              :rules="[{ validator, message: '请输入正确手机号' }]"
              @touchstart.stop="showKeyboard" />
            <van-field v-else v-model="phone" name="phone" label="手机号" placeholder="手机号"
              :rules="[{ validator, message: '请输入正确手机号' }]" />
          </div>
          <div class="van-hairline--bottom">
            <van-field v-model="pin" center clearable name="pin" type="digit" label="短信验证码" placeholder="请输入短信验证码"
              :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]">
              <template #button>
                <van-button v-if="countDown.current.value.total === limit" size="small" type="primary" :disabled="disabled" @click="sendCode">
                  <span>发送验证码</span>
                </van-button>
                <van-button v-else size="small" type="primary" :disabled="disabled">
                  <span>{{ countDown.current.value.seconds }} 秒重新发送</span>
                </van-button>
              </template>
            </van-field>
          </div>
        </van-form>
      </van-config-provider>
    </div>
  </div>

  <div style="padding: 2rem;">
    <my-button @on-click="submit" type="primary">保存</my-button>
  </div>

  <van-number-keyboard v-model="phone" :show="keyboard_show" :maxlength="11" @blur="onBlur" />
</template>

<script setup>
import MyButton from '@/components/MyButton/index.vue'

import { wxInfo } from '@/utils/tools';

import { ref, onMounted, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from '@/utils/axios';
import { showSuccessToast, showFailToast, showToast } from 'vant';
import { styleColor } from '@/constant.js';
import { smsAPI } from '@/api/common'
import { useCountDown } from '@vant/use';

const $route = useRoute();
const $router = useRouter();

const themeVars = {
  buttonPrimaryBackground: styleColor.baseColor,
  buttonPrimaryBorderColor: styleColor.baseColor,
  buttonPrimaryColor: styleColor.baseFontColor,
};

/******************** 输入框控件 START ********************/

/**
 * 判断微信环境看是否弹出控件框
 * 桌面微信直接输入
 * 其他环境弹出输入框
 */
let use_widget = ref(true);
if (wxInfo().isiOS || wxInfo().isAndroid) {
  use_widget.value = true;
} else {
  use_widget.value = false;
}

// 手机号输入控件
const keyboard_show = ref(false);
const showKeyboard = () => { // 弹出数字弹框
  keyboard_show.value = true;
};
const onBlur = () => { // 数字键盘失焦回调
  keyboard_show.value = false;
  if (phone.value.length === 11) { // 手机号码位数正确时可以发送验证码
    disabled.value = false;
  }
};

/******************** 发送验证码模块 START ********************/

// const countDown = ref(60);
// const countDownHandle = () => {
//   // 倒计时
//   if (countDown.value === 0) {
//     countDown.value = 60;
//   } else {
//     countDown.value--;
//     setTimeout(() => {
//       countDownHandle()
//     }, 1000)
//   }
// }

// 设置发送短信倒计时
// TAG: vant 自带倒计时函数
const limit = ref(60000); // 配置倒计时秒数
const countDown = useCountDown({
  // 倒计时 24 小时
  time: limit.value,
  onFinish: () => {
    countDown.reset();
  }
});

const sendCode = async () => { // 发送验证码
  countDown.start();
  // 验证码接口
  const { code } = await smsAPI({ phone: phone.value });
  if (code === 1) {
    showSuccessToast('发送成功');
  }
};

/******************** 表单业务逻辑 START ********************/

const user_name = ref('')
const phone = ref('');
const pin = ref('');
const disabled = ref(true);

// 表单填写验证
const form = ref(null);
let valid = null;
let submit = () => {
  valid = form.value.validate();
  valid
    .then(() => {
      form.value.submit();
    })
    .catch(error => {
      console.error(error);
      showToast({
        message: '请检查后再次提交',
        icon: 'cross',
      });
    })
};

/**
 * 手机号码校验
 * 函数返回 true 表示校验通过,false 表示不通过
 * @param {*} val
 */
const validator = (val) => {
  let flag = false;
  // 简单判断手机号位数
  if (/1\d{10}/.test(val) && phone.value.length === 11) {
    disabled.value = false;
    flag = true;
  } else {
    disabled.value = true;
    flag = false;
  }
  return flag
};

// 过滤输入的验证码 只能四位
const formatter = (value) => value.substring(0, 4);

// 保存用户信息
const onSubmit = (values) => {
  axios.post('/srv/?a=c_auth', {
    user_name: values.user_name,
    phone: values.phone,
    pin: values.pin,
  })
    .then(res => {
      if (res.data.code === 1) {
        showSuccessToast('录入成功')
        if ($route.query.back_url) {
          $router.push($route.query.back_url)
        } else {
          $router.go(-1)
        }
      } else {
        console.warn(res.data);
        if (!res.data.show) return false;
        showToast({
          message: res.data.msg,
          icon: 'close',
        });
      }
    })
    .catch(err => {
      console.error(err);
    })
};

/****************************************/

</script>

<script>
import mixin from 'common/mixin';

export default {
  mixins: [mixin.init],
  data() {
    return {

    }
  },
  mounted() {

  },
  methods: {
  }
}
</script>

<style lang="less" scoped>
.verify-user-page {
  padding: 1rem;
  .title {
    color: @base-color;
    font-size: 1.25rem;
    padding: 1rem;
    text-align: center;
    font-weight: bold;
  }
  .input-content {
    padding: 1rem;
  }
}
</style>