scanner.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <div class="content"></div>
  3. </template>
  4. <script>
  5. var main,receiver,filter;
  6. var _codeQueryTag = false;
  7. export default {
  8. data() {
  9. return {
  10. scanCode: ''
  11. }
  12. },
  13. created () {
  14. // #ifdef APP-PLUS
  15. this.initScan();
  16. this.startScan();
  17. // #endif
  18. },
  19. onHide(){
  20. // #ifdef APP-PLUS
  21. this.stopScan();
  22. // #endif
  23. },
  24. destroyed(){
  25. /*页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果*/
  26. // #ifdef APP-PLUS
  27. this.stopScan();
  28. // #endif
  29. },
  30. methods: {
  31. initScan() {
  32. let _this = this;
  33. main = plus.android.runtimeMainActivity();//获取activity
  34. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  35. filter = new IntentFilter();
  36. filter.addAction("com.android.server.scannerservice.broadcast"); // 换你的广播动作
  37. receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{
  38. onReceive : function(context, intent) {
  39. plus.android.importClass(intent);
  40. let code = intent.getStringExtra("scannerdata");// 换你的广播标签
  41. _this.queryCode(code);
  42. }});
  43. },
  44. startScan(){
  45. main.registerReceiver(receiver,filter);
  46. },
  47. stopScan(){
  48. main.unregisterReceiver(receiver);
  49. },
  50. queryCode(code){
  51. //防重复
  52. if(_codeQueryTag)return false;
  53. _codeQueryTag = true;
  54. setTimeout(function(){
  55. _codeQueryTag = false;
  56. },150);
  57. var id = code
  58. uni.$emit('scan',id)
  59. }
  60. }
  61. }
  62. </script>
  63. <style>
  64. /* page {
  65. background-color: #efeff4;
  66. }
  67. .content {
  68. text-align: center;
  69. } */
  70. </style>