PasswordInputer.vue 3.7 KB
<template>
  <v-card flat class="PasswordInputer">
    <div class="title">请输入交易密码</div>
    <div class="PasswordInputBlock">
      <input class="PasswordInput" id="PasswordInput-0" ref="0" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[0]" @input="passwordInput" @keydown="keydown">
      <input class="PasswordInput" id="PasswordInput-1" ref="1" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[1]" @input="passwordInput" @keydown="keydown">
      <input class="PasswordInput" id="PasswordInput-2" ref="2" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[2]" @input="passwordInput" @keydown="keydown">
      <input class="PasswordInput" id="PasswordInput-3" ref="3" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[3]" @input="passwordInput" @keydown="keydown">
      <input class="PasswordInput" id="PasswordInput-4" ref="4" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[4]" @input="passwordInput" @keydown="keydown">
      <input class="PasswordInput" id="PasswordInput-5" ref="5" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[5]" @input="passwordInput" @keydown="keydown">				
    </div>
    <div class="submitButton" @click="submit">确  定</div>
  </v-card>
</template>

<script>
export default {
  data: () => ({
    password: '',
    inputValue: {
      0: '',
      1: '',
      2: '',
      3: '',
      4: '',
      5: '',
    },
  }),
  props: {
    display: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    display: function (val, oldVal) {
      if (val) {
        this.reset()
        this.$refs['0'].focus()
      }
    },
  },
  methods: {
    reset() {
      this.password = ''
      this.inputValue = {
        0: '',
        1: '',
        2: '',
        3: '',
        4: '',
        5: ''
      }
    },
    passwordInput(e) {
      let id = e.target.id
      let index = parseInt(id.split('-')[1])
      if (e.inputType === 'deleteContentBackward') return
      if (index < 5) {
        index++
        let next = this.$refs[index.toString()]
        next.focus()
        // if (this.inputValue[index] !== '') {
        //   next.setSelectionRange(0, 1)
        //   return
        // }
      } else {
        this.inputValue[5] = this.inputValue[5].slice(0,1)
        this.password = this.passwordConnect()
      }
    },
    passwordConnect() {
      let result = ''
      for (let i = 0; i < 6; i++) {
        let code = this.inputValue[i]
        if (code) {
          result += code
        } else {
          return false
        }
      }
      return result
    },
    keydown(e) {
      if (e.key !== 'Backspace') return
      const id = e.target.id
      let index = parseInt(id.split('-')[1])
      if (index === 0) return
      if (index === 5 && this.inputValue[5] !== '') {
        this.inputValue[5] = ''
        return
      }
      const before = this.$refs[(index - 1).toString()]
      before.focus()
    },
    submit() {
      if (this.password.length !== 6) return
      this.$emit('passwordSubmit', this.password)
    }
  }
}
</script>

<style lang="scss" scoped>
  .PasswordInputer {
    background-color: #fff;
    color: #000;
    
    .title {
      margin-top: 1rem;
    }
    .PasswordInputBlock {
			margin-top: 2.9rem;
			text-align: center;

			.PasswordInput {
				width: 2.5rem;
				height: 2.5rem;
				line-height: 2.5rem;
				background-color: #fff;
				border: 1px solid $gold;
				outline: none;
				text-align: center;
				font-size: 1.5rem;
				margin: 0 0.75rem;
			}
    }
    
    .submitButton {
      height: 3.5rem;
      font-size: 1.2rem;
      line-height: 3.5rem;
      border-top: 1px solid #ccc;
      border-bottom: 1px solid #ccc;
      margin-top: 3rem;
    }
  }
</style>