优化:兼容微信扫码带条码类型的结果
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
@@ -19,9 +19,44 @@ function getScanRestartDelay() {
|
||||
return toAny(getConfig("scanRestartDelay"), SCAN_RESTART_DELAY);
|
||||
}
|
||||
|
||||
function parseBarcodeString(input) {
|
||||
// 常见条形码类型列表(全小写,用于不区分大小写匹配)
|
||||
const commonBarcodeTypes = [
|
||||
"CODE128", "CODE39", "CODE93", "EAN13", "EAN8", "UPC-A", "UPC-E",
|
||||
"ITF", "ITF14", "CODABAR", "PDF417", "QRCODE", "DATAMATRIX", "AZTEC",
|
||||
"EAN-13", "EAN-8", "UPC-A", "UPC-E", "ITF-14", "CODE-128", "CODE-39", "CODE-93"
|
||||
].map(type => type.toLowerCase()); // 全部转为小写便于不区分大小写比较
|
||||
// 检查输入是否为字符串,确保健壮性
|
||||
if (typeof input !== 'string') {
|
||||
return input;
|
||||
}
|
||||
// 按第一个逗号分割(允许值内部包含逗号)
|
||||
const commaIndex = input.indexOf(',');
|
||||
if (commaIndex === -1) {
|
||||
// 没有逗号,不符合格式,返回原值
|
||||
return input;
|
||||
}
|
||||
const possibleType = input.substring(0, commaIndex).trim();
|
||||
const value = input.substring(commaIndex + 1).trim();
|
||||
// 如果类型或值为空,认为不符合规范,返回原值
|
||||
if (possibleType === "" || value === "") {
|
||||
return input;
|
||||
}
|
||||
// 检查类型是否在常见条形码类型中(不区分大小写)
|
||||
const isKnownType = commonBarcodeTypes.includes(possibleType.toLowerCase());
|
||||
if (isKnownType) {
|
||||
// 符合条件,返回条形码值
|
||||
return value;
|
||||
} else {
|
||||
// 不符合条件,返回原字符串
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
function __checkScanner() {
|
||||
if (_scan_listener_list.length > 0) {
|
||||
startScanner((result) => {
|
||||
result = parseBarcodeString(result);
|
||||
__scannerResult(result);
|
||||
});
|
||||
} else {
|
||||
@@ -42,6 +77,7 @@ function __match(result, match) {
|
||||
|
||||
|
||||
function __result(result) {
|
||||
result = parseBarcodeString(result);
|
||||
let matched = false;
|
||||
for (let i = 0; i < _scan_listener_list.length; i++) {
|
||||
const item = _scan_listener_list[i];
|
||||
|
||||
@@ -38,6 +38,19 @@ function normalizeScannerValue(value) {
|
||||
.replace(/\u2014/g, "_");
|
||||
}
|
||||
|
||||
function isInputElementFocused() {
|
||||
const win = getWindow();
|
||||
const activeElement = win && win.document && win.document.activeElement;
|
||||
if (!activeElement || activeElement === win.document.body || activeElement === win.document.documentElement) {
|
||||
return false;
|
||||
}
|
||||
const tagName = activeElement.tagName && activeElement.tagName.toLowerCase();
|
||||
if (tagName === "input" || tagName === "textarea" || tagName === "select") {
|
||||
return true;
|
||||
}
|
||||
return activeElement.isContentEditable === true;
|
||||
}
|
||||
|
||||
function stopScannerEvent(event) {
|
||||
event.preventDefault && event.preventDefault();
|
||||
event.stopPropagation && event.stopPropagation();
|
||||
@@ -47,6 +60,10 @@ function onScannerKeydown(event) {
|
||||
if (_scannerStatus !== "scanning") {
|
||||
return;
|
||||
}
|
||||
if (isInputElementFocused()) {
|
||||
clearScannerValue();
|
||||
return;
|
||||
}
|
||||
if (event.ctrlKey || event.metaKey || event.altKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user