index.copy.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="content">
  3. <uni-table border stripe>
  4. <uni-tr>
  5. <uni-th width="150" align="center">EPC</uni-th>
  6. </uni-tr>
  7. <uni-tr v-for="(item, index) in scanDatas" :key="index">
  8. <uni-td>{{ item }}</uni-td>
  9. </uni-tr>
  10. </uni-table>
  11. </view>
  12. </template>
  13. <script setup>
  14. import { ref } from "vue";
  15. import {onLoad, onReady, onUnload} from '@dcloudio/uni-app'
  16. import {getArrValue} from "js-fast-way";
  17. // 获取 module
  18. const rfidModule = uni.requireNativePlugin("DeviceModule_RFID");
  19. //渲染完成
  20. onReady(() => {
  21. // #ifdef APP-PLUS
  22. rfidModuleInit()
  23. // #endif
  24. })
  25. //按键操作
  26. const isRfidInit = ref(true)
  27. const rfidModuleInit = () => {
  28. isRfidInit.value = true
  29. uni.showLoading({
  30. title: 'RFID模块加载中...',
  31. mask: true,
  32. });
  33. /**
  34. 手机按键监听事件,可在此编写一些逻辑,如使用按键触发扫描,更多详细信息请查阅uni官方文档
  35. 需要注意:退出界面必须移除监听,否则再进入页面重复注册监听会出现多次触发、回调失效的问题
  36. */
  37. plus.key.addEventListener('keydown', keyListener);
  38. //初始化
  39. setTimeout(() => {
  40. // 使用模块前必须先初始化RDIF模块
  41. let { code } = rfidModule.init();
  42. if (code === 0) {
  43. uni.hideLoading();
  44. } else if (code === -1) {
  45. uni.hideLoading();
  46. uni.showToast({
  47. title: 'RFID模块加载失败',
  48. icon: 'error',
  49. duration: 3000
  50. })
  51. } else {
  52. uni.hideLoading();
  53. }
  54. isRfidInit.value = false
  55. }, 400);
  56. }
  57. //按键操作
  58. const keyListener = ({keyCode}) => {
  59. if (keyCode === 293 || keyCode === 312) {
  60. if (isScan.value) {
  61. stopScan()
  62. } else {
  63. startScan()
  64. }
  65. }
  66. }
  67. //开始扫描
  68. const isScan = ref(false)
  69. const startScan = () => {
  70. if (isRfidInit.value) return
  71. isScan.value = true
  72. rfidModule.startScan((res) => {
  73. if (res.code === 0) {
  74. uni.showToast({
  75. icon: "success",
  76. title: '开启扫描成功'
  77. })
  78. } else if (res.code === 1) {
  79. startScanData(res.data)
  80. }
  81. })
  82. }
  83. //扫描结果处理
  84. const scanDatas = ref([])
  85. const startScanData = async (data) => {
  86. const arr = getArrValue(data)
  87. let epcs = scanDatas.value
  88. for (let i = 0; i < arr.length; i++) {
  89. const epc = arr[i].epc
  90. if (epcs.indexOf(epc) === -1) {
  91. epcs.push(epc)
  92. }
  93. }
  94. scanDatas.value = epcs
  95. }
  96. //停止扫描
  97. const stopScan = () => {
  98. const { code } = rfidModule.stopScan()
  99. if (code === 0) {
  100. isScan.value = false
  101. uni.showToast({
  102. icon: "success",
  103. title: '关闭扫描成功'
  104. })
  105. } else {
  106. uni.showToast({
  107. icon: "error",
  108. title: res.message
  109. })
  110. }
  111. }
  112. //页面卸载
  113. onUnload(()=>{
  114. // #ifdef APP-PLUS
  115. plus.key.removeEventListener('keydown', keyListener)
  116. // 使用完毕必须释放RDIF模块
  117. rfidModule.free();
  118. // #endif
  119. })
  120. </script>