export default {
  data () {
    return {
      numCount: 60,
      timer: null
    }
  },
  methods: {
    validatePhone (formName) {
      this.$refs[formName].validateField('phone', error => {
        if (!error) {
          this.funGetCode()
        } else {
          return false
        }
      })
    },
    funCutDown () {
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        if (this.numCount === 0) {
          clearInterval(this.timer)
          this.numCount = 0
          return
        }
        this.numCount--
      }, 1000)
    },
    // 获取验证码
    async funGetCode () {
      const numCount = this.numCount
      const { phone } = this.form
      if (numCount < 60 && numCount > 0) {
        return
      }
      this.numCount = 60
      this.funCutDown()
      const data = await this.$fetch('/v1/send/code', {
        phone
      })
      if (data.code === 200) {
        this.$message.success('发送成功')
      } else {
        clearInterval(this.timer)
        this.numCount = 60
      }
    }
  }
}
