Index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div id="reader"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import { onMounted, ref, onUnmounted } from 'vue'
  6. import { Html5Qrcode } from 'html5-qrcode'
  7. // import { useRouter } from 'vue-router'
  8. import { Html5QrcodeResult, CameraDevice } from './interface'
  9. // 事件
  10. const emit = defineEmits(['success', 'failure']);
  11. const cameraId = ref('')
  12. const devicesInfo = ref<any>('')
  13. const html5QrCode = ref<any>(null)
  14. // const router = useRouter()
  15. onMounted(() => {
  16. // open()
  17. })
  18. onUnmounted(() => {
  19. console.log('qr-onUnmounted')
  20. stop()
  21. })
  22. const open = () => {
  23. Html5Qrcode.getCameras()
  24. .then((devices: CameraDevice[]) => {
  25. console.log('摄像头信息', devices)
  26. if (devices && devices.length) {
  27. // 如果有2个摄像头,1为前置的
  28. if (devices.length > 1) {
  29. cameraId.value = devices[1].id
  30. } else {
  31. cameraId.value = devices[0].id
  32. }
  33. devicesInfo.value = devices
  34. // start开始扫描
  35. start()
  36. }
  37. })
  38. .catch((err) => {
  39. alert(err);
  40. // handle err
  41. console.log('获取设备信息失败', err) // 获取设备信息失败
  42. })
  43. }
  44. const start = () => {
  45. html5QrCode.value = new Html5Qrcode('reader')
  46. console.log('html5QrCode', html5QrCode)
  47. html5QrCode.value.start(
  48. cameraId.value, // retreived in the previous step.
  49. {
  50. fps: 10, // 设置每秒多少帧
  51. qrbox: { width: 250, height: 250 }, // 设置取景范围
  52. // focusMode: 'continuous',
  53. // aspectRatio: true,
  54. // disableFlip: true,
  55. // scannable, rest shaded.
  56. },
  57. (decodedText: string, decodedResult: Html5QrcodeResult) => {
  58. // do something when code is read. For example:
  59. // if (qrCodeMessage) {
  60. // getCode(qrCodeMessage);
  61. // stop();
  62. // }
  63. console.log('扫描的结果', decodedText, decodedResult)
  64. emit('success', decodedResult);
  65. stop();
  66. // if (decodedText) {
  67. // router.push('order');
  68. // }
  69. },
  70. (errorMessage: any) => {
  71. // parse error, ideally ignore it. For example:
  72. // console.log(`QR Code no longer in front of camera.`);
  73. emit('failure', errorMessage);
  74. stop();
  75. console.log('暂无额扫描结果', errorMessage)
  76. }
  77. )
  78. .catch((err: any) => {
  79. // Start failed, handle it. For example,
  80. emit('failure', err);
  81. console.log(`Unable to start scanning, error: ${err}`)
  82. })
  83. }
  84. const stop = () => {
  85. html5QrCode.value
  86. .stop()
  87. .then((ignore: any) => {
  88. // QR Code scanning is stopped.
  89. console.log('QR Code scanning stopped.', ignore)
  90. })
  91. .catch((err: any) => {
  92. // Stop failed, handle it.
  93. console.log('Unable to stop scanning.', err)
  94. })
  95. }
  96. // 暴露方法
  97. defineExpose({
  98. open,
  99. stop
  100. })
  101. </script>
  102. <style scoped>
  103. #reader {
  104. position: absolute;
  105. width: 100%;
  106. top: 50%;
  107. left: 0;
  108. transform: translateY(-50%);
  109. /* background-color: red; */
  110. /* border: 2px solid red; */
  111. /* box-sizing: border-box; */
  112. }
  113. </style>