
<template>
  <div class="input-ver">
    <el-input v-model="code" @change="$emit('change', code)" placeholder="请输入验证码"></el-input>
    <img @click="getUrl" v-if="url" :src="url" alt="img-url">
  </div>
</template>

<script>
let running = false
let time = 60
let text = '获取验证码'
let interval

export default {
  data() {
    return {
      url: '',
      code: '',
      time: time + 1,
    }
  },
  computed: {
    text() {
      if(this.time < 1 || this.time > time) {
        running = false
        if(this.time < 1) {
          clearInterval(interval)
          this.time = time + 1
        }
        return text
      }
      return `重新获取(${this.time}s)`
    }
  },
  mounted() {
    this.getUrl()
  },
  methods: {
    run() {
      if(running) return
      this.time--
      interval = setInterval(() => {
        this.time--
      }, 1000)
    },
   getUrl() {
      this.url = `/api/captcha/image?_${~~(Math.random() * 1e7)}`
    }
  }
}
</script>

<style scoped>
.input-ver {
  display: flex;
  align-items: center;
}
.el-input {
  width: 195px;
  margin-right: 10px;
}
img {
  flex: 1;
  width: 115px;
  height: 40px;
  cursor: pointer;
}
</style>
