监听识别

This commit is contained in:
iqudoo
2026-06-09 12:36:34 +08:00
parent 0b1b145b9c
commit d574f08faf
4 changed files with 40 additions and 9 deletions

7
dist/index.d.ts vendored
View File

@@ -274,6 +274,10 @@ interface ScanListenerInfo {
* 监听级别
*/
level?: number;
/**
* 为 true 时仅接收同 key 主动扫码startScan/scanImage 等)的结果,忽略扫码枪
*/
selfKeyOnly?: boolean;
/**
* 监听回调
*/
@@ -333,8 +337,9 @@ interface IScan {
* @param key 监听key
* @param match 监听匹配
* @param level 监听级别
* @param selfKeyOnly 为 true 时仅接收同 key 主动扫码结果,忽略扫码枪
*/
onScanListener(callback: ScanResultCallback, key: string, match?: string, level?: number): ScanListenerInfo;
onScanListener(callback: ScanResultCallback, key: string, match?: string, level?: number, selfKeyOnly?: boolean): ScanListenerInfo;
/**
* 取消监听扫码结果
* @param callback 监听回调或监听key

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -139,8 +139,20 @@ function parseBarcodeString(input) {
}
}
function __needsHardwareScanner() {
if (_embed_scan_host_enabled) {
return true;
}
for (let i = 0; i < _scan_listener_list.length; i++) {
if (!_scan_listener_list[i].selfKeyOnly) {
return true;
}
}
return false;
}
function __checkScanner() {
if (_scan_listener_list.length > 0 || _embed_scan_host_enabled) {
if (__needsHardwareScanner()) {
startScanner((result) => {
result = parseBarcodeString(result);
__scannerResult(result, { source: "scanner", skipBeep: true });
@@ -174,8 +186,15 @@ function __resolveScanKey(meta) {
return _scan_session_key;
}
function __listenerMatches(item, result, scanKey) {
if (scanKey && item.key !== scanKey) {
function __listenerMatches(item, result, scanKey, meta) {
if (item.selfKeyOnly) {
if (meta && meta.source === "scanner") {
return false;
}
if (!scanKey || item.key !== scanKey) {
return false;
}
} else if (scanKey && item.key !== scanKey) {
return false;
}
return __match(result, item.match);
@@ -212,7 +231,7 @@ function __result(result, meta) {
let matched = false;
for (let i = 0; i < _scan_listener_list.length; i++) {
const item = _scan_listener_list[i];
if (item.listener && __listenerMatches(item, result, scanKey)) {
if (item.listener && __listenerMatches(item, result, scanKey, meta)) {
matched = true;
item.listener({ result, key: item.key });
break;
@@ -294,7 +313,7 @@ function __hasMatchedListener(result, meta) {
const scanKey = __resolveScanKey(meta);
for (let i = 0; i < _scan_listener_list.length; i++) {
const item = _scan_listener_list[i];
if (item.listener && __listenerMatches(item, result, scanKey)) {
if (item.listener && __listenerMatches(item, result, scanKey, meta)) {
return true;
}
}
@@ -572,7 +591,7 @@ export function clear() {
__checkScanner();
}
export function onScanListener(listener, key, match, level) {
export function onScanListener(listener, key, match, level, selfKeyOnly) {
if (!key || typeof key !== 'string') {
return;
}
@@ -591,11 +610,13 @@ export function onScanListener(listener, key, match, level) {
item.level = level;
item.match = match;
item.listener = listener;
item.selfKeyOnly = selfKeyOnly === true;
} else {
item = {
key,
match: match || "",
level: level || 0,
selfKeyOnly: selfKeyOnly === true,
listener: listener,
cancel: () => {
const index = _scan_listener_list.indexOf(item);

7
types/index.d.ts vendored
View File

@@ -274,6 +274,10 @@ interface ScanListenerInfo {
* 监听级别
*/
level?: number;
/**
* 为 true 时仅接收同 key 主动扫码startScan/scanImage 等)的结果,忽略扫码枪
*/
selfKeyOnly?: boolean;
/**
* 监听回调
*/
@@ -333,8 +337,9 @@ interface IScan {
* @param key 监听key
* @param match 监听匹配
* @param level 监听级别
* @param selfKeyOnly 为 true 时仅接收同 key 主动扫码结果,忽略扫码枪
*/
onScanListener(callback: ScanResultCallback, key: string, match?: string, level?: number): ScanListenerInfo;
onScanListener(callback: ScanResultCallback, key: string, match?: string, level?: number, selfKeyOnly?: boolean): ScanListenerInfo;
/**
* 取消监听扫码结果
* @param callback 监听回调或监听key