scanCodePage.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!-- H5 扫码页面 -->
  2. <template>
  3. <view class="scanCodePage">
  4. <video id="video-canvas" autoplay :controls="false"></video>
  5. </view>
  6. </template>
  7. <script>
  8. import { BrowserMultiFormatReader } from '@zxing/library';
  9. import { scanTheCodeDataTreating } from '@/utils/scanTheCode'
  10. export default {
  11. data() {
  12. return {
  13. codeReader: null,
  14. deviceId: null,
  15. path:'',
  16. }
  17. },
  18. mounted() {
  19. this.$set(this,'path',this.$route.query.path);
  20. this.initScanner();
  21. },
  22. methods: {
  23. async initScanner() {
  24. let self = this;
  25. const videoDom = document.getElementById('video-canvas').getElementsByTagName('video')[0];
  26. videoDom.id = 'zxing-video';
  27. this.codeReader = new BrowserMultiFormatReader();
  28. try {
  29. const devices = await this.codeReader.listVideoInputDevices();
  30. this.deviceId = devices[devices.length - 1].deviceId;
  31. this.startScanning();
  32. } catch (err) {
  33. uni.showToast({
  34. mask: true,
  35. icon: "none",
  36. position: "center",
  37. title: "请启用摄像头权限",
  38. duration: 2000
  39. });
  40. setTimeout(() => {
  41. uni.navigateBack();
  42. }, 2000);
  43. }
  44. },
  45. startScanning() {
  46. let self = this;
  47. this.codeReader.decodeFromVideoDevice(
  48. this.deviceId,
  49. 'zxing-video',
  50. (result, err) => {
  51. if (result) {
  52. scanTheCodeDataTreating(this.path,result.text);
  53. self.codeReader.reset();
  54. }
  55. }
  56. );
  57. }
  58. },
  59. beforeDestroy() {
  60. this.codeReader?.reset();
  61. }
  62. }
  63. </script>
  64. <style lang="stylus" scoped>
  65. .scanCodePage{
  66. flex:1;
  67. display: flex;
  68. flex-direction: column;
  69. overflow: hidden;
  70. ::v-deep #video-canvas {
  71. height:100%;
  72. width:100%;
  73. .uni-video-cover{
  74. display: none;
  75. }
  76. }
  77. }
  78. </style>