fix
This commit is contained in:
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -14,6 +14,7 @@ import {
|
||||
dispatchEmbedScanResult,
|
||||
acknowledgeEmbedScanConsumed,
|
||||
dispatchEmbedScanError,
|
||||
setEmbedScanHostEnabled,
|
||||
} from "./services/provider/scan";
|
||||
import {
|
||||
setEmbedScanResultForwarder,
|
||||
@@ -139,7 +140,7 @@ function broadcastScanErrorToEmbedChildren(error) {
|
||||
});
|
||||
}
|
||||
|
||||
function broadcastScanResultToEmbedChildren(result) {
|
||||
function broadcastScanResultToEmbedChildren(result, meta) {
|
||||
if (embedChildSources.size === 0 || result == null || result === "") {
|
||||
return;
|
||||
}
|
||||
@@ -151,6 +152,7 @@ function broadcastScanResultToEmbedChildren(result) {
|
||||
v: EMBED_V,
|
||||
kind: "forwardScanResult",
|
||||
result,
|
||||
scanSource: meta && meta.source,
|
||||
},
|
||||
"*"
|
||||
);
|
||||
@@ -217,7 +219,9 @@ function embedChildOnMessage(ev) {
|
||||
}
|
||||
if (data.kind === "forwardScanResult") {
|
||||
if (typeof data.result === "string") {
|
||||
const consumed = dispatchEmbedScanResult(data.result);
|
||||
const consumed = dispatchEmbedScanResult(data.result, {
|
||||
source: data.scanSource,
|
||||
});
|
||||
if (consumed && resolveUseParentProxy()) {
|
||||
window.parent.postMessage(
|
||||
{
|
||||
@@ -349,6 +353,7 @@ export function installEmbedHost(lib) {
|
||||
embedHostInstalled = true;
|
||||
setEmbedScanResultForwarder(broadcastScanResultToEmbedChildren);
|
||||
setEmbedScanErrorForwarder(broadcastScanErrorToEmbedChildren);
|
||||
setEmbedScanHostEnabled(true);
|
||||
window.addEventListener("message", (ev) => {
|
||||
const data = ev.data;
|
||||
if (!isEmbedMessage(data)) {
|
||||
|
||||
@@ -11,9 +11,9 @@ export function setEmbedScanErrorForwarder(fn) {
|
||||
embedScanErrorForwarder = typeof fn === "function" ? fn : null;
|
||||
}
|
||||
|
||||
export function forwardEmbedScanResultIfNeeded(result) {
|
||||
export function forwardEmbedScanResultIfNeeded(result, meta) {
|
||||
if (embedScanResultForwarder && result != null && result !== "") {
|
||||
embedScanResultForwarder(result);
|
||||
embedScanResultForwarder(result, meta);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ let _scan_error_listener_list = [];
|
||||
let _scan_resolve = null;
|
||||
let _scan_closing = false;
|
||||
let _scan_next_start_time = 0;
|
||||
let _embed_scan_host_enabled = false;
|
||||
|
||||
const SCAN_RESTART_DELAY = 500;
|
||||
const BRIDGE_SCAN_TIMEOUT = 5000;
|
||||
@@ -134,10 +135,10 @@ function parseBarcodeString(input) {
|
||||
}
|
||||
|
||||
function __checkScanner() {
|
||||
if (_scan_listener_list.length > 0) {
|
||||
if (_scan_listener_list.length > 0 || _embed_scan_host_enabled) {
|
||||
startScanner((result) => {
|
||||
result = parseBarcodeString(result);
|
||||
__scannerResult(result);
|
||||
__scannerResult(result, { source: "scanner", skipBeep: true });
|
||||
});
|
||||
} else {
|
||||
stopScanner();
|
||||
@@ -155,10 +156,17 @@ function __match(result, match) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function __shouldSkipBeep(meta) {
|
||||
if (meta && meta.skipBeep) {
|
||||
return true;
|
||||
}
|
||||
const source = meta && meta.source;
|
||||
return source === "scanner" || source === "bridge" || source === "wx";
|
||||
}
|
||||
|
||||
function __result(result) {
|
||||
function __result(result, meta) {
|
||||
result = parseBarcodeString(result);
|
||||
forwardEmbedScanResultIfNeeded(result);
|
||||
forwardEmbedScanResultIfNeeded(result, meta);
|
||||
let matched = false;
|
||||
for (let i = 0; i < _scan_listener_list.length; i++) {
|
||||
const item = _scan_listener_list[i];
|
||||
@@ -169,7 +177,9 @@ function __result(result) {
|
||||
}
|
||||
}
|
||||
if (matched) {
|
||||
playScanBeep();
|
||||
if (!__shouldSkipBeep(meta)) {
|
||||
playScanBeep();
|
||||
}
|
||||
_scan_next_start_time = Date.now() + getScanRestartDelay();
|
||||
}
|
||||
return matched;
|
||||
@@ -289,10 +299,10 @@ function __stopCurrentScan() {
|
||||
* 父页通过 postMessage 将识别结果投递到嵌入 iframe 时调用(与本地扫码枪/监听同一链路)。
|
||||
* @returns {boolean} 是否有监听消费了该结果
|
||||
*/
|
||||
export function dispatchEmbedScanResult(raw) {
|
||||
export function dispatchEmbedScanResult(raw, meta) {
|
||||
const result =
|
||||
typeof raw === "string" ? parseBarcodeString(raw) : raw;
|
||||
return __scannerResult(result);
|
||||
return __scannerResult(result, meta);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -324,9 +334,9 @@ export function acknowledgeEmbedScanConsumed(raw) {
|
||||
resolve && resolve({ result });
|
||||
}
|
||||
|
||||
function __scannerResult(result) {
|
||||
function __scannerResult(result, meta) {
|
||||
if (!__hasMatchedListener(result)) {
|
||||
return __result(result);
|
||||
return __result(result, meta);
|
||||
}
|
||||
if (isScanning()) {
|
||||
const resolve = __finishScan();
|
||||
@@ -336,13 +346,13 @@ function __scannerResult(result) {
|
||||
_scan_closing = false;
|
||||
}, 0);
|
||||
});
|
||||
const matched = __result(result);
|
||||
const matched = __result(result, meta);
|
||||
resolve && resolve({
|
||||
result
|
||||
});
|
||||
return matched;
|
||||
}
|
||||
return __result(result);
|
||||
return __result(result, meta);
|
||||
}
|
||||
|
||||
function __startBridgeScan() {
|
||||
@@ -355,7 +365,7 @@ function __startBridgeScan() {
|
||||
if (!resp || !resp.result) {
|
||||
return resp;
|
||||
}
|
||||
if (__result(resp.result)) {
|
||||
if (__result(resp.result, { source: "bridge" })) {
|
||||
return resp;
|
||||
}
|
||||
if (isScanning()) {
|
||||
@@ -369,7 +379,7 @@ function __startBridgeScan() {
|
||||
if (!err || !err.result) {
|
||||
throw err;
|
||||
}
|
||||
if (__result(err.result)) {
|
||||
if (__result(err.result, { source: "bridge" })) {
|
||||
return err;
|
||||
}
|
||||
if (isScanning()) {
|
||||
@@ -393,7 +403,7 @@ function __startWxScan() {
|
||||
if (!resp || !resp.result) {
|
||||
return resp;
|
||||
}
|
||||
if (__result(resp.result)) {
|
||||
if (__result(resp.result, { source: "wx" })) {
|
||||
return resp;
|
||||
}
|
||||
return resp;
|
||||
@@ -402,7 +412,7 @@ function __startWxScan() {
|
||||
throw err;
|
||||
}
|
||||
if (err && err.result) {
|
||||
if (__result(err.result)) {
|
||||
if (__result(err.result, { source: "wx" })) {
|
||||
return err;
|
||||
}
|
||||
return err;
|
||||
@@ -481,6 +491,15 @@ export function isScanning() {
|
||||
return _scan_status === "scanning";
|
||||
}
|
||||
|
||||
export function setEmbedScanHostEnabled(enabled) {
|
||||
const nextEnabled = enabled !== false;
|
||||
if (_embed_scan_host_enabled === nextEnabled) {
|
||||
return;
|
||||
}
|
||||
_embed_scan_host_enabled = nextEnabled;
|
||||
__checkScanner();
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
for (let i = 0; i < _scan_listener_list.length; i++) {
|
||||
const item = _scan_listener_list[i];
|
||||
|
||||
Reference in New Issue
Block a user