ScanCode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var main, receiver, filter
  2. export default {
  3. data () {
  4. return {
  5. scanCode: '',
  6. _codeQueryTag: false
  7. }
  8. },
  9. created: function (option) {
  10. console.log('created')
  11. console.log(this.$store.state.app.scancodedate)
  12. if (uni.getSystemInfoSync().platform == 'android') {
  13. this.initScan()
  14. this.startScan()
  15. }
  16. const _this = this
  17. uni.addInterceptor('navigateTo', {
  18. success (e) {
  19. _this.watchRouter()
  20. }
  21. })
  22. uni.addInterceptor('redirectTo', {
  23. success (e) {
  24. _this.watchRouter()
  25. }
  26. })
  27. uni.addInterceptor('switchTab', {
  28. success (e) {
  29. _this.watchRouter()
  30. }
  31. })
  32. uni.addInterceptor('navigateBack', {
  33. success (e) {
  34. _this.watchRouter()
  35. }
  36. })
  37. },
  38. methods: {
  39. initScan () {
  40. console.log('---------------------initScan---------')
  41. let _this = this
  42. main = plus.android.runtimeMainActivity() //获取activity
  43. var IntentFilter = plus.android.importClass(
  44. 'android.content.IntentFilter'
  45. )
  46. filter = new IntentFilter()
  47. filter.addAction('com.android.serial.BARCODEPORT_RECEIVEDDATA_ACTION') // 换你的广播动作
  48. receiver = plus.android.implements(
  49. 'io.dcloud.feature.internal.reflect.BroadcastReceiver',
  50. {
  51. onReceive: function (context, intent) {
  52. plus.android.importClass(intent)
  53. let code = intent.getStringExtra('DATA') // 换你的广播标签
  54. _this.queryCode(code)
  55. }
  56. }
  57. )
  58. },
  59. watchRouter () {
  60. this.$store.commit('app/RESET_SCANCODEDATE')
  61. },
  62. startScan () {
  63. main.registerReceiver(receiver, filter)
  64. },
  65. stopScan () {
  66. main.unregisterReceiver(receiver)
  67. },
  68. queryCode: function (code) {
  69. //防重复
  70. const _this = this
  71. if (_this._codeQueryTag) return false
  72. _this._codeQueryTag = true
  73. setTimeout(function () {
  74. _this._codeQueryTag = false
  75. }, 150)
  76. var id = code
  77. let par = {
  78. code: id
  79. }
  80. console.log(
  81. 'this.$store.state.app.scancodedate',
  82. this.$store.state.app.scancodedate
  83. )
  84. uni.$emit(this.$store.state.app.scancodedate, par)
  85. }
  86. }
  87. }