<template>
  <view class="content"></view>
</template>

<script>
  var main, receiver, filter;
  var _codeQueryTag = false;
  export default {
    name: 'seiminScan',
    data() {
      return {
        scanCode: ''
      }
    },
    created: function(option) {
      this.initScan()
      this.startScan();
    },
    onHide: function() {
      this.stopScan();
    },
    destroyed: function() {
      /*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/
      this.stopScan();
    },
    methods: {
      initScan() {
        main = plus.android.runtimeMainActivity();
        let IntentFilter = plus.android.importClass('android.content.IntentFilter');
        filter = new IntentFilter();
        filter.addAction("ds");
        receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
          onReceive: (context, intent) => {
            plus.android.importClass(intent);
            let code = intent.getStringExtra("data");
            this.queryCode(code);
          }
        });
      },
      startScan() {
        main.registerReceiver(receiver, filter);
      },
      stopScan() {
        main.unregisterReceiver(receiver);
      },
      queryCode: function(code) {
        //防重复  
        if (_codeQueryTag) return false;
        _codeQueryTag = true;
        setTimeout(function() {
          _codeQueryTag = false;
        }, 150);
        let id = code
        uni.$emit('scancodedate', {
          code: id
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #efeff4;
  }

  .content {
    text-align: center;
  }
</style>
