vue网页版实现pda扫码功能

通过input框获取焦点来接收扫码参数

<template>
  <div>
    <input class="input" type="password" ref="searchInput" v-model="scanCodeValue" :placeholder="placeholder" @focus="focusFn"/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      scanCodeValue:'',
      placeholder:'请扫码...'
    }
  },
  created() {
    // 抓取扫描枪数据
    document.onkeydown = (e) => {
        // 自动获取input输入框焦点
      this.$nextTick( () => {
        this.$refs.searchInput.focus();
      })
      if (e.which === 229) { // 安卓版PDA扫描枪扫描完后完成键的keycode=229
          // 其他类型可根据keycode值来确认哪一个是完成值
        setTimeout(() => {
          if (this.scanCodeValue.length < 3) return
          this.scanCodeValue = ''
        },500)
        return
      }
    }
  },

  watch:{
    scanCodeValue(value,oldValue) {
        if(!!value){
          if(value.includes('storehouse')&&value.includes('location')){
            this.$emit('goBack', value)
          }else{
            this.placeholder='扫码失败,请重新扫码...'
          }
        }
    }
  },

  destroyed(){
    //取消键盘监听事件
      document.onkeydown = null
  },
  methods: {
    focusFn(){
      this.$refs.searchInput.setAttribute('readonly', 'readonly');
        setTimeout(() => {
          this.$refs.searchInput.removeAttribute('readonly');
        }, 200);
    },
  }
}
</script>

<style>
  .input{
    height: 30px;
  }
  .btn{
    margin-top: 15px;
  }
  .btn button{
    width: 50px;
    height: 30px;
    background-color: #409EFF;
    color: #fff;
    border: none;
    border-radius: 4px;
  }
</style>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值