/** * 初始化idata扫描模块 */ export function useInit() { //获取module var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule") //使用示例 barcodeModel.initScan((ret) => { console.log('初始化扫描', ret) }); } /** * 全局监听扫描后结果 * @param onChange 回调函数 * @returns {Function} 返回移除监听器的函数 */ export function useGlobalEvent(onChange) { //页面监听event事件,建议在页面onLoad方法里调用 var globalEvent = uni.requireNativePlugin('globalEvent'); const handler = function(e) { onChange && onChange(e) } // 先移除可能存在的旧监听器 globalEvent.removeEventListener('iDataBarcodeEvent', handler); // 添加新的监听器 globalEvent.addEventListener('iDataBarcodeEvent', handler); // 返回一个清理函数,用于手动移除监听器 return () => { globalEvent.removeEventListener('iDataBarcodeEvent', handler); } } //全局卸载监听事件 export function useGlobalEventRemove() { var globalEvent = uni.requireNativePlugin('globalEvent'); globalEvent.removeEventListener('iDataBarcodeEvent'); }