captchaInput.vue 3.78 KB
<template>
  <div class="row-center captcha_input_wrapper">
    <input
      v-for="(item,index) in captchas"
      :key="index"
      v-model="item.num"
      :id="'captcha'+index"
      @input="inputFinash(index)"
      @focus="adjust(index)"
      @keydown="inputDirection(index)"
      class="captcha_input_box row-center"
      :style="customStyle"
      type="tel"
      maxlength="1">
  </div>
</template>

<script>
export default {
  name: 'CaptchaInput',
  props: {
    customStyle: {
      type: String,
      required: false,
      default: ''
    },
    captchasLenght: {
      type: Number,
      required: false,
      default: 4
    },
    captchaList: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 当前输入框
      activeInput: 0,

      captchas: [
        // { num: '' },
        // { num: '' },
        // { num: '' },
        // { num: '' },
        // { num: '' },
        // { num: '' }
      ]
    }
  },
  watch: {
    captchas: {
      handler: function (value) {
        console.log(value)
        const str = value.map((item) => {
          return item.num
        }).join('')
        this.$emit('update:captchaList', str)
      },
      deep: true
    },
    captchasLenght: {
      handler: function (value) {
        for (let index = 0; index < this.captchasLenght; index++) {
          // const element = array[index];
          // list[index] = this.item
          // this.captchas[index] = { num: '' }
          this.$set(this.captchas, index, { num: '' })
        }
      },
      immediate: true
    }
  },
  computed: {
    test() {
      const list = []
      for (let index = 0; index < this.captchasLenght; index++) {
        // const element = array[index];
        list[index] = this.item
      }
      return list
      // return list.map((item) => {
      //   item = this.item
      //   return item
      // })
    }
  },
  methods: {
    // 自动校准输入顺序
    adjust(index) {
      const dom = document.getElementById('captcha' + this.activeInput)
      if (index !== this.activeInput && dom) {
        dom.focus()
      }
    },
    // 控制前后方向
    inputDirection(index) {
      const val = this.captchas[index].num
      // debugger
      console.log(event.keyCode)
      // 回退键处理
      if (event.keyCode == 8 && val == '') {
        console.log(index)
        // 重新校准
        const dom = document.getElementById('captcha' + (index - 1))
        // 解决第一位为-1 导致能选其他空的位置输入
        index = index - 1
        this.activeInput = index == -1 ? 0 : index

        if (dom) dom.focus()
      }
      if (event.keyCode != 8 && val != '') {
        const dom = document.getElementById('captcha' + (index + 1))
        this.activeInput = index + 1
        if (dom) dom.focus()
      }
    },
    // 输入框相互联动
    inputFinash(index) {
      const val = this.captchas[index].num
      this.activeInput = val ? index + 1 : index - 1
      const dom = document.getElementById('captcha' + this.activeInput)
      if (dom) dom.focus()
      if (index == this.captchas.length - 1) {
        const code = this.captchas.map((x) => x.num).join('')
        if (code.length == 6) {
          this.$emit('finish', code)
        }
      }
    }
  }
}
</script>

<style lang='scss'>
.row-center {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.captcha_input_wrapper {
  width: 100%;
}
.captcha_input_box {
  width: 48px;
  height: 60px;
  margin-right: 12px;
  background: rgba(255, 255, 255, 1);
  border-radius: 6px;
  border: 1px solid #dddddd;
  font-size: 40px;
  text-align: center;
  color: #1e243a;
}
</style>