monitorScanCode.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <view class="content"></view>
  3. </template>
  4. <script>
  5. var main, receiver;
  6. var shakeIsShow = false;
  7. export default {
  8. name:'monitorScanCode',
  9. data() {
  10. return {
  11. };
  12. },
  13. created(option) {
  14. this.monitorScan();
  15. },
  16. // 组件销毁
  17. destroyed(){
  18. this.cancelScan();
  19. },
  20. methods: {
  21. // 广播监听绑定
  22. monitorScan() {
  23. // PDA扫描必须设置广播模式
  24. main = plus.android.runtimeMainActivity(); //获取activity
  25. let IntentFilter = plus.android.importClass('android.content.IntentFilter');
  26. let filter = new IntentFilter();
  27. filter.addAction('android.intent.ACTION_DECODE_DATA'); //广播动作
  28. let that = this;
  29. let context = plus.android.importClass('android.content.Context');
  30. let Intent = plus.android.importClass('android.content.Intent');
  31. receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
  32. onReceive: function(context, intent) {
  33. plus.android.importClass(intent); //引入intent类
  34. let data = intent.getStringExtra('barcode_string'); //广播数据标签
  35. that.antiShake(data);
  36. }
  37. });
  38. main.registerReceiver(receiver, filter); //注册监听
  39. },
  40. //取消监听扫描头
  41. cancelScan() {
  42. main.unregisterReceiver(receiver);
  43. },
  44. //防重复
  45. antiShake: function(code) {
  46. if (shakeIsShow) return false;
  47. shakeIsShow = true;
  48. setTimeout(function() {
  49. shakeIsShow = false;
  50. }, 150);
  51. this.$emit("resultCode", code)
  52. }
  53. }
  54. };
  55. </script>
  56. <style>
  57. page {
  58. background-color: #efeff4;
  59. }
  60. .content {
  61. text-align: center;
  62. }
  63. </style>