修复了扫一扫的bug

This commit is contained in:
iqudoo
2026-05-03 23:47:27 +08:00
parent 4afa5ec8cb
commit bc025f7f74
3 changed files with 82 additions and 4 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -10,10 +10,38 @@ import {
resolveUseParentProxy, resolveUseParentProxy,
} from "./services/embedProxy"; } from "./services/embedProxy";
import { createUUID } from "./utils/uuid"; import { createUUID } from "./utils/uuid";
import { dispatchHardwareScanResult } from "./services/provider/scan";
import { startScanner, stopScanner } from "./services/scanner";
const EMBED_SOURCE = "IScanEmbed"; const EMBED_SOURCE = "IScanEmbed";
const EMBED_V = 1; const EMBED_V = 1;
/** 嵌入子页已注册的监听 key与父页监听对应用于 refcount 决定是否在本页挂载扫码枪按键监听) */
const childEmbedScanGunKeys = new Set();
/** offScanListener(fn) 时反查对应的 key */
const embedScanGunWeakListenerKey = new WeakMap();
function syncEmbedScanGunForwardFromChild() {
if (typeof window === "undefined" || !resolveUseParentProxy()) {
return;
}
if (childEmbedScanGunKeys.size === 0) {
stopScanner();
return;
}
startScanner((result) => {
window.parent.postMessage(
{
source: EMBED_SOURCE,
v: EMBED_V,
kind: "forwardScanGun",
result,
},
"*"
);
});
}
function isEmbedMessage(data) { function isEmbedMessage(data) {
return data && data.source === EMBED_SOURCE && data.v === EMBED_V; return data && data.source === EMBED_SOURCE && data.v === EMBED_V;
} }
@@ -113,7 +141,11 @@ function deserializeEmbedInvokeResult(methodKey, raw) {
const key = raw.key; const key = raw.key;
return { return {
key, key,
cancel: () => embedInvoke("offScanListener", [key]), cancel: () => {
childEmbedScanGunKeys.delete(key);
syncEmbedScanGunForwardFromChild();
embedInvoke("offScanListener", [key]).catch(() => {});
},
}; };
} }
return raw; return raw;
@@ -306,6 +338,17 @@ export function installEmbedHost(lib) {
); );
return; return;
} }
/** 嵌入子页焦点下扫码枪的 keydown子页转成字符串后透出再走主页面监听与嵌入回调链路 */
if (data.kind === "forwardScanGun") {
if (!ev.source || ev.source === window) {
return;
}
if (typeof data.result !== "string") {
return;
}
dispatchHardwareScanResult(data.result);
return;
}
if (data.kind !== "invoke") { if (data.kind !== "invoke") {
return; return;
} }
@@ -383,12 +426,37 @@ function createInvokeTransport(lib, method, methodName, initNames) {
if (!key || typeof key !== "string" || typeof listener !== "function") { if (!key || typeof key !== "string" || typeof listener !== "function") {
return; return;
} }
embedScanGunWeakListenerKey.set(listener, key);
childEmbedScanGunKeys.add(key);
syncEmbedScanGunForwardFromChild();
embedInvoke(methodName, params).catch(() => {}); embedInvoke(methodName, params).catch(() => {});
return { return {
key, key,
cancel: () => embedInvoke("offScanListener", [key]), cancel: () => {
childEmbedScanGunKeys.delete(key);
syncEmbedScanGunForwardFromChild();
embedInvoke("offScanListener", [key]).catch(() => {});
},
}; };
} }
if (methodName === "offScanListener") {
const p0 = params[0];
if (typeof p0 === "string") {
childEmbedScanGunKeys.delete(p0);
} else if (typeof p0 === "function") {
const rk = embedScanGunWeakListenerKey.get(p0);
if (rk != null && rk !== "") {
childEmbedScanGunKeys.delete(rk);
}
}
syncEmbedScanGunForwardFromChild();
return embedInvoke(methodName, params);
}
if (methodName === "clear") {
childEmbedScanGunKeys.clear();
syncEmbedScanGunForwardFromChild();
return embedInvoke(methodName, params);
}
return embedInvoke(methodName, params); return embedInvoke(methodName, params);
} }
if (!isReadyCalled() && initNames && initNames.indexOf(method) < 0) { if (!isReadyCalled() && initNames && initNames.indexOf(method) < 0) {

View File

@@ -156,6 +156,16 @@ function __stopCurrentScan() {
return Promise.resolve(); return Promise.resolve();
} }
/**
* 由嵌入子页转发过来的扫码枪按键结果,与 startScanner 走同一匹配与回调链路
*(仅在父页的 installEmbedHost 处理 forwardScanGun 时调用)。
*/
export function dispatchHardwareScanResult(raw) {
const result =
typeof raw === "string" ? parseBarcodeString(raw) : raw;
__scannerResult(result);
}
function __scannerResult(result) { function __scannerResult(result) {
if (!__hasMatchedListener(result)) { if (!__hasMatchedListener(result)) {
return; return;
@@ -381,7 +391,7 @@ export function startScan() {
scanPromise = __startWxScan(); scanPromise = __startWxScan();
} else if (isSupportWebScan()) { } else if (isSupportWebScan()) {
unlockScanBeep(); unlockScanBeep();
scanPromise = startScanForWeb(getConfig("webScanCanvasStyle"), __result); scanPromise = startScanForWeb(__result);
} else if (isSupportImageScan()) { } else if (isSupportImageScan()) {
scanPromise = __startImageScan(); scanPromise = __startImageScan();
} else { } else {