UNPKG

vite-uni-dev-tool

Version:

vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试

205 lines (173 loc) 4.83 kB
import { onHide, onShow, onUnload } from '@dcloudio/uni-app'; import { ref, watch } from 'vue'; import type { Ref } from 'vue'; /** * android PDA * * h5 扫码枪 * * @export * @param {(code: string) => void} callback * @param {{ * disabled?: Ref<boolean>; * broadcastAction?: string; * broadcastStringDataLabel?: string; * scanDelay?: number; * onScanEnd: (code: string) => void; * }} [options] */ export default function useScanCode( callback: (code: string) => void, options?: { /** 禁用扫描 */ disabled?: Ref<boolean>; /** 广播动作 */ broadcastAction?: string; /** 广播标签 */ broadcastStringDataLabel?: string; /** * 扫描延迟 单位 ms * 默认 150 */ scanDelay?: number; /** * 扫描结束 * @param {string} code */ onScanEnd?: (code: string) => void; }, ) { let main: any; let show = ref(true); let lastTime: number | null = null; let nextTime: number | null = null; let lastCode: string | null = null; let nextCode: string | null = null; let code = ''; let _codeQueryTag = false; let receiver: PlusAndroidInstanceObject; let filter: { addAction: (arg0: string) => void; }; // #ifdef APP-PLUS function startScan() { main.registerReceiver(receiver, filter); } function stopScan() { main.unregisterReceiver(receiver); } function queryCode(code: string) { if (_codeQueryTag) return false; _codeQueryTag = true; setTimeout(function () { _codeQueryTag = false; }, options?.scanDelay || 150); show.value && callback && callback(code); show.value && options?.onScanEnd?.(code); // 单独使用该 hooks 时,删除收集 // @ts-ignore show.value && uni?.__dev_tool_scan_code_collection__?.(code, 'broadcast'); } function initScan() { if (!options?.broadcastAction || !options.broadcastStringDataLabel) { throw new Error( 'PAD 扫描必须传入 broadcastAction, broadcastStringDataLabel', ); } main = plus.android.runtimeMainActivity(); //获取activity const IntentFilter: any = plus.android.importClass( 'android.content.IntentFilter', ); filter = new IntentFilter(); filter.addAction(options?.broadcastAction); // 换你的广播动作 receiver = plus.android.implements( 'io.dcloud.feature.internal.reflect.BroadcastReceiver', { onReceive: function (_context: any, intent: any) { plus.android.importClass(intent); let code = intent.getStringExtra(options?.broadcastStringDataLabel); // 换你的广播标签 queryCode(code); }, }, ); } onShow(() => { initScan(); startScan(); show.value = true && !options?.disabled?.value; }); onHide(() => { show.value = false; stopScan(); }); onUnload(() => { show.value = false; stopScan(); }); watch( () => [options?.disabled?.value], () => { show.value = !options?.disabled?.value; }, ); // #endif // #ifdef H5 function queryCodeH5(e: KeyboardEvent) { if (_codeQueryTag) return; if (e.key === 'Enter') { e.preventDefault(); } if (e.key === 'CapsLock') { e.preventDefault(); } nextCode = e.key; nextTime = Date.now(); if (lastCode !== null && lastTime !== null && nextTime - lastTime <= 40) { code += lastCode; } else if ( lastCode !== null && lastTime !== null && nextTime - lastTime > 100 ) { code = ''; } lastCode = nextCode; lastTime = nextTime; if (e.key === 'Enter' && code.length > 0) { _codeQueryTag = true; setTimeout(function () { _codeQueryTag = false; }, options?.scanDelay || 150); callback?.(code); options?.onScanEnd?.(code); // 单独使用该 hooks 时,删除收集 // @ts-ignore uni?.__dev_tool_scan_code_collection__?.(code, 'keypress'); code = ''; } } onShow(() => { show.value = true; }); onHide(() => { window.removeEventListener('keypress', queryCodeH5); }); onUnload(() => { window.removeEventListener('keypress', queryCodeH5); }); watch( () => [options?.disabled?.value, show.value], () => { if (!options?.disabled?.value && show.value) { window.removeEventListener('keypress', queryCodeH5); window.addEventListener('keypress', queryCodeH5); } else { window.removeEventListener('keypress', queryCodeH5); } }, { immediate: true, }, ); // #endif }