useBarcodeModule.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * 初始化idata扫描模块
  3. */
  4. export function useInit() {
  5. //获取module
  6. var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule");
  7. //使用示例
  8. barcodeModel.initScan((ret) => {
  9. console.log("初始化扫描", ret);
  10. });
  11. }
  12. // 存储当前活跃页面的回调函数
  13. const callbackRegistry = {
  14. activePagePath: null,
  15. callbacks: new Map(), // 使用Map存储每个页面路径对应的回调函数
  16. };
  17. /**
  18. * 全局事件处理函数 - 内部使用
  19. * 只执行当前活跃页面的回调函数
  20. */
  21. const globalEventHandler = function(e) {
  22. if (!callbackRegistry.activePagePath) return;
  23. const callback = callbackRegistry.callbacks.get(callbackRegistry.activePagePath);
  24. if (callback) {
  25. // 只执行当前活跃页面的回调
  26. callback(e);
  27. }
  28. };
  29. // 初始化全局事件监听器
  30. let isGlobalListenerInitialized = false;
  31. function initGlobalListener() {
  32. if (isGlobalListenerInitialized) return;
  33. const globalEvent = uni.requireNativePlugin("globalEvent");
  34. // 确保移除任何可能存在的旧监听器
  35. globalEvent.removeEventListener("iDataBarcodeEvent", globalEventHandler);
  36. // 添加全局单一监听器
  37. globalEvent.addEventListener("iDataBarcodeEvent", globalEventHandler);
  38. isGlobalListenerInitialized = true;
  39. }
  40. /**
  41. * 全局监听扫描后结果
  42. * @param onChange 回调函数
  43. * @returns {Function} 返回移除监听器的函数
  44. */
  45. export function useGlobalEvent(onChange) {
  46. // 确保全局监听器已初始化
  47. initGlobalListener();
  48. // 获取当前页面路径
  49. const pages = getCurrentPages();
  50. const currentPage = pages[pages.length - 1];
  51. const pagePath = currentPage ? currentPage.route : null;
  52. if (!pagePath) {
  53. console.error("无法获取当前页面路径");
  54. return () => {};
  55. }
  56. // 更新当前活跃页面
  57. callbackRegistry.activePagePath = pagePath;
  58. // 注册当前页面的回调
  59. callbackRegistry.callbacks.set(pagePath, onChange);
  60. console.log(`页面 ${pagePath} 已注册扫描回调`);
  61. // 返回一个清理函数,用于手动移除监听器
  62. return () => {
  63. callbackRegistry.callbacks.delete(pagePath);
  64. console.log(`页面 ${pagePath} 的扫描回调已移除`);
  65. };
  66. }
  67. /**
  68. * 更新当前活跃页面,在页面显示时调用
  69. * 在App.vue的onShow中调用
  70. */
  71. export function updateActivePageOnShow() {
  72. const pages = getCurrentPages();
  73. const currentPage = pages[pages.length - 1];
  74. const pagePath = currentPage ? currentPage.route : null;
  75. if (!pagePath) {
  76. console.error("无法获取当前页面路径");
  77. return;
  78. }
  79. // 更新当前活跃页面
  80. callbackRegistry.activePagePath = pagePath;
  81. console.log(`当前活跃页面已更新为: ${pagePath}`);
  82. }
  83. /**
  84. * 全局卸载监听事件
  85. * 完全移除全局事件监听器
  86. */
  87. export function useGlobalEventRemove() {
  88. const globalEvent = uni.requireNativePlugin("globalEvent");
  89. globalEvent.removeEventListener("iDataBarcodeEvent", globalEventHandler);
  90. isGlobalListenerInitialized = false;
  91. callbackRegistry.callbacks.clear();
  92. callbackRegistry.activePagePath = null;
  93. console.log("全局扫描监听器已完全移除");
  94. }
  95. /**
  96. * 移除特定页面的回调
  97. * @param pagePath 页面路径,如果不提供则移除当前页面的回调
  98. */
  99. export function removePageCallback(pagePath) {
  100. if (!pagePath) {
  101. const pages = getCurrentPages();
  102. const currentPage = pages[pages.length - 1];
  103. pagePath = currentPage ? currentPage.route : null;
  104. }
  105. if (!pagePath) {
  106. console.error("无法获取页面路径");
  107. return;
  108. }
  109. callbackRegistry.callbacks.delete(pagePath);
  110. console.log(`页面 ${pagePath} 的扫描回调已移除`);
  111. }