vue扫码枪组件封装,避免影响到其他表单输入或者其他用户操作

一、扫码枪的一些实现方法

在需求中经常会遇到扫码枪的需求,最大的难点在于,如何界定用户输入还是扫码枪输入。

1.输入速度检查

通过输入字符串的速度来确定是人为输入还是扫码枪输入,缺点是认为快速输入可能也会触发,可以限制文本长度,例如输入10个字符,用了20毫秒,来进一步确定是否为机器输入

2.约定扫描对象的结构

约定扫码获取的字符串,例如“[scanner] 100240000”,拼接约定的前缀或者后缀来实现判断

3.配置扫码枪

扫码枪一般官方提供了一些更改触发的方式,查阅官方说明

二、组件将采用第一种方式来实现一个扫码功能(并且可以在用户移出当前窗口后给予提示)

提示文字可以自己修改,或者改变文字提示的位置在这里插入图片描述

2.1使用方式

<ScannerInput @scan="handleScan"></ScannerInput>
<script>
 handleScan(scannedData) {
     console.log('Scanned Data:', scannedData);
     // 在这里处理扫描到的数据
   }
</script>

2.2 组件源码

 <!--
 * @file: 扫码枪工具组件

-->
 <!--
 * @file: 扫码枪工具组件

-->
<template>
  <div :style="computedStyle">
    <el-tag v-if="!this.isPageFocused" type="danger"> {{ tipText }}</el-tag>
    <!-- 空的div,不需要实际的输入元素 -->
    <el-input style="position: absolute;top:-1000px"  type="password" ref="scanInput" v-model="scanVal" @focus="inputFocus"></el-input>
  </div>
</template>

<script>

export default {
  name: 'ScannerInput',
  props: {
    // 输入字符间的最大时间间隔(毫秒),用于判断是否为扫码枪输入
    maxInterval: {
      type: Number,
      default: 40,
    },
    // 扫码枪输入的最小长度
    minLength: {
      type: Number,
      default: 8,
    },
    tipText: {
      type: String,
      default: '请聚焦到页面以进行扫码!!!!',
    },
    left: {
      type: String,
      default: '50%',
    },
    top: {
      type: String,
      default: '50px',
    },
  },
  data() {
    return {
      scannerInput: '',
      lastKeyPressTime: 0,
      isPageFocused: true, // 页面聚焦状态
      scanVal: '',
    };
  },
  computed: {
    computedStyle() {
      return {
        position: 'fixed',
        left: this.left,
        top: this.top,
        zIndex: 999999,
        transform: 'translate(-50%, -50%)',
        timer: null,
      };
    },
  },
  methods: {
    handleKeyPress(event) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      const currentTime = Date.now();
      const timeDiff = currentTime - this.lastKeyPressTime;
      this.lastKeyPressTime = currentTime;
      // 正常处理扫码
      // 根据时间间隔判断是否为扫码枪输入
      if ((timeDiff < this.maxInterval || timeDiff === currentTime) && !event.ctrlKey) {
        this.$refs.scanInput.focus();
        this.scannerInput += event.key;
      } else {
        // 重置scannerInput,因为这可能是正常的键盘输入
        this.scannerInput = event.key;

        this.timer = setTimeout(() => {
          this.lastKeyPressTime = 0;
        }, 300);
      }
      // 检查扫码数据长度和结束字符
      if (event.key === 'Enter' && this.scannerInput.length >= this.minLength) {
        this.$emit('scan', this.scanVal);
        this.scannerInput = '';
        this.scanVal = '';
        this.lastKeyPressTime = 0;
      }
    },
    inputFocus() {
      console.log('聚焦', 123);
    },
    handleWindowBlur() {
      console.warn('请聚焦到页面以进行扫码');
      this.isPageFocused = false;
      this.$emit('pageBlur', !this.isPageFocused);
    },
    handleWindowFocus() {
      this.lastKeyPressTime = 0;
      this.isPageFocused = true;

      this.$emit('pageBlur', !this.isPageFocused);
    },
  },

  mounted() {
    window.addEventListener('keydown', this.handleKeyPress);
    window.addEventListener('blur', this.handleWindowBlur);
    window.addEventListener('focus', this.handleWindowFocus);
  },
  beforeUnmount() {
    console.log(234234);
    window.removeEventListener('keydown', this.handleKeyPress);
    window.removeEventListener('blur', this.handleWindowBlur);
    window.removeEventListener('focus', this.handleWindowFocus);
  },
};
</script>
<style lang="scss" scoped>
</style>



  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 是一个用于构建用户界面的渐进式 JavaScript 框架,Vue 扫码能够实现无焦点捕获扫码输入扫码是一种硬件设备,它能够将二维码或条形码的信息转化为可识别的数据。传统上,为了使用扫码,用户需要将光标聚焦于输入框中,然后才能够将扫码对准二维码或条形码进行扫描。然而,使用 Vue 扫码插件,我们能够实现无焦点捕获扫码输入的功能。 通过在 Vue 组件中使用合适的库或插件,我们可以实现这一功能。这样做的原理是将扫码的扫描结果直接传递给指定输入框,而不需要用户手动聚焦于输入框中。 在 Vue 的生命周期钩子中,我们可以监听扫码设备的事件,如 "scan"。当扫码扫描到二维码或条形码时,将触发这个事件。我们可以在这个事件中通过 JavaScript 来处理扫描结果,然后将其赋值给指定的输入框。 同时,我们可以借助 Vue 的双向数据绑定功能,实现将扫码扫描结果的值动态更新到其他需要使用这个值的地方。 需要注意的是,为了确保无焦点捕获扫码输入的功能正常工作,我们需要在 Vue 组件中的对应输入框上添加适当的事件监听器,以便识别到扫码输入。 总的来说,通过使用 Vue 扫码插件和相关的库或插件,我们可以实现无焦点捕获扫码输入的功能,提高用户的扫码体验和输入效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值