useBarcodeModule.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. * 全局监听扫描后结果
  14. * @param onChange 回调函数
  15. * @returns {Function} 返回移除监听器的函数
  16. */
  17. export function useGlobalEvent(onChange) {
  18. //页面监听event事件,建议在页面onLoad方法里调用
  19. var globalEvent = uni.requireNativePlugin('globalEvent');
  20. const handler = function(e) {
  21. onChange && onChange(e)
  22. }
  23. // 先移除可能存在的旧监听器
  24. globalEvent.removeEventListener('iDataBarcodeEvent', handler);
  25. // 添加新的监听器
  26. globalEvent.addEventListener('iDataBarcodeEvent', handler);
  27. // 返回一个清理函数,用于手动移除监听器
  28. return () => {
  29. globalEvent.removeEventListener('iDataBarcodeEvent', handler);
  30. }
  31. }
  32. //全局卸载监听事件
  33. export function useGlobalEventRemove() {
  34. var globalEvent = uni.requireNativePlugin('globalEvent');
  35. globalEvent.removeEventListener('iDataBarcodeEvent');
  36. }