app-continue-scancode.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="app-scan-container">
  3. </view>
  4. </template>
  5. <script>
  6. export default {
  7. name: "app-continue-scancode",
  8. data() {
  9. return {
  10. value: '',
  11. timer: null,
  12. barcode: null,
  13. scanCodes: [],
  14. viewContent: null,
  15. codeType: [
  16. plus.barcode.CODE128,
  17. plus.barcode.QR,
  18. plus.barcode.EAN13,
  19. plus.barcode.EAN8,
  20. plus.barcode.UPCA,
  21. plus.barcode.UPCE,
  22. plus.barcode.CODABAR,
  23. plus.barcode.CODE39,
  24. plus.barcode.CODE93,
  25. plus.barcode.ITF,
  26. ]
  27. };
  28. },
  29. mounted() {
  30. //使用H5+原生界面控件,参考https://ask.dcloud.net.cn/article/35036
  31. const pages = getCurrentPages();
  32. const page = pages[pages.length - 1];
  33. // plus.navigator.setFullscreen(true); //全屏
  34. const currentWebview = page.$getAppWebview();
  35. this.initBarcode(currentWebview)
  36. this.createView(currentWebview)
  37. },
  38. destroyed() {
  39. this.closeBarcode()
  40. },
  41. methods: {
  42. initBarcode(currentWebview) {
  43. this.barcode = plus.barcode.create('barcode', this.codeType, {
  44. top: '0px',
  45. left: '0px',
  46. width: '100%',
  47. height: '300', //设置扫码框的高度
  48. position: 'static',
  49. scanbarColor: '#28E153',
  50. frameColor: '#28E153'
  51. });
  52. this.barcode.onmarked = this.onmarked;
  53. currentWebview.append(this.barcode);
  54. const res = uni.getSystemInfoSync();
  55. if (res.platform == 'android') {
  56. //安卓机
  57. this.barcode.start();
  58. }
  59. },
  60. placeBeep() {
  61. var innerAudioContext = uni.createInnerAudioContext();
  62. innerAudioContext.autoplay = true;
  63. innerAudioContext.src = 'http://61.163.210.202:10992/erp_updates/beep.mp3';
  64. innerAudioContext.onPlay(() => {
  65. console.log('开始播放');
  66. });
  67. innerAudioContext.onError((res) => {
  68. console.log(res);
  69. console.log(res.errMsg);
  70. console.log(res.errCode);
  71. });
  72. // innerAudioContext.onEnded(() => {
  73. // innerAudioContext.destroy();
  74. // });
  75. },
  76. async onmarked(type, result) {
  77. //todo 播放音频
  78. //this.placeBeep();
  79. console.log('Success: type=' + type + ', result=' + result);
  80. if (!result) return this.barcode && this.barcode.start();
  81. this.timer = setTimeout(() => {
  82. this.barcode && this.barcode.start();
  83. }, 1000)
  84. this.$emit('scanHitERPCode', {
  85. code: result
  86. })
  87. },
  88. closeBarcode() {
  89. this.barcode && this.barcode.close();
  90. this.viewContent && this.viewContent.close()
  91. this.barcode = null
  92. this.viewContent = null
  93. },
  94. // 创建展示类内容组件
  95. createView(currentWebview) {
  96. this.viewContent = new plus.nativeObj.View(
  97. 'content', {
  98. top: '230px',
  99. left: '0px',
  100. height: '130px',
  101. width: '100%'
  102. },
  103. [{
  104. tag: 'font',
  105. id: 'scanTips',
  106. text: '将订单二维码放入框中进行扫描',
  107. textStyles: {
  108. size: '16px',
  109. color: '#ffffff',
  110. whiteSpace: 'normal'
  111. },
  112. position: {
  113. top: '80px',
  114. left: '10%',
  115. width: '80%',
  116. height: 'wrap_content'
  117. }
  118. }]
  119. );
  120. currentWebview.append(this.viewContent);
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. .app-scan-container {
  127. height: 100%;
  128. overflow: hidden;
  129. }
  130. </style>