Compare commits

...

2 Commits

Author SHA1 Message Date
iqudoo
23c91267a2 添加对微信小程序环境的支持 2026-04-30 19:43:13 +08:00
iqudoo
3856b6e8c9 fix 2026-04-30 19:35:24 +08:00
9 changed files with 129 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
# scan-code-jssdk
统一扫码 JSSDK支持桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、选择图片识别和扫码枪输入。
统一扫码 JSSDK支持桥接扫码、微信小程序、微信 JSSDK 扫码、Web 摄像头扫码、选择图片识别和扫码枪输入。
## 功能

View File

@@ -181,7 +181,7 @@
<main class="page">
<section class="hero">
<h1>IScan 通用扫码 SDK</h1>
<p>统一接入桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p>
<p>统一接入桥接扫码、微信小程序、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p>
</section>
<section class="grid">

2
dist/index.html vendored
View File

@@ -161,7 +161,7 @@
section {
margin-bottom: 16px;
}</style></head><body><main class="page"><section class="hero"><h1>IScan 通用扫码 SDK</h1><p>统一接入桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p></section><section class="grid"><div class="card"><h2>功能说明</h2><ul class="feature-list"><li>支持桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</li><li>浏览器环境使用WASM库识别二维码和条形码。</li><li>扫码结果由监听规则统一回调,支持正则匹配和优先级排序。</li></ul></div><div class="card"><h2>当前状态</h2><p>SDK 状态:<span id="status" class="status">loading</span></p><p>运行环境:</p><pre id="output" class="panel"></pre></div></section><section class="card"><h2>操作</h2><p>点击开始后会按桥接、微信、Web 摄像头、图片识别的顺序选择可用扫码方式。</p><div class="actions"><button onclick="startScan()" class="btn">开始扫码</button> <button onclick="scanVideo()" class="btn secondary">开启视频扫码</button> <button onclick="scanImage()" class="btn secondary">选择图片识别</button> <button onclick="stopScan()" class="btn secondary">停止扫码</button></div></section><section class="card"><h2>扫码结果</h2><pre id="result" class="panel result"></pre></section><section class="card"><h2>错误信息</h2><pre id="error" class="panel error"></pre><p style="color: #919191">错误信息可能来源于扫码结果、扫码过程、扫码初始化等。</p></section><section class="card code"><h2>接入方式</h2><pre>IScan.config({
}</style></head><body><main class="page"><section class="hero"><h1>IScan 通用扫码 SDK</h1><p>统一接入桥接扫码、微信小程序、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p></section><section class="grid"><div class="card"><h2>功能说明</h2><ul class="feature-list"><li>支持桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</li><li>浏览器环境使用WASM库识别二维码和条形码。</li><li>扫码结果由监听规则统一回调,支持正则匹配和优先级排序。</li></ul></div><div class="card"><h2>当前状态</h2><p>SDK 状态:<span id="status" class="status">loading</span></p><p>运行环境:</p><pre id="output" class="panel"></pre></div></section><section class="card"><h2>操作</h2><p>点击开始后会按桥接、微信、Web 摄像头、图片识别的顺序选择可用扫码方式。</p><div class="actions"><button onclick="startScan()" class="btn">开始扫码</button> <button onclick="scanVideo()" class="btn secondary">开启视频扫码</button> <button onclick="scanImage()" class="btn secondary">选择图片识别</button> <button onclick="stopScan()" class="btn secondary">停止扫码</button></div></section><section class="card"><h2>扫码结果</h2><pre id="result" class="panel result"></pre></section><section class="card"><h2>错误信息</h2><pre id="error" class="panel error"></pre><p style="color: #919191">错误信息可能来源于扫码结果、扫码过程、扫码初始化等。</p></section><section class="card code"><h2>接入方式</h2><pre>IScan.config({
webScanEnabled: true,
webScanCanvasEnabled: true,
webScanBeepEnabled: true,

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -7,9 +7,12 @@ const IScan = exportSDK(core, null, "config", "setStatusListener", "onScanListen
function dispatchIScanReady() {
_global.__IScanReady__ && _global.__IScanReady__();
if (!_global.dispatchEvent) {
return;
}
if (typeof Event === "function") {
_global.dispatchEvent(new Event("IScanReady"));
} else {
} else if (typeof document !== "undefined") {
let event = document.createEvent("Event");
event.initEvent("IScanReady", true, true);
_global.dispatchEvent(event);

View File

@@ -7,13 +7,23 @@ let _events = {};
let _callbacks = {};
let _bridge = "__bridge_client__";
function getWindow() {
if (typeof window === "undefined") {
return null;
}
return window;
}
function getBridgeName() {
return getConfig("bridgeName") ? getConfig("bridgeName") : _bridge;
}
function _callRuntime(func, ...options) {
let funcs = func.split('.');
let instant = window;
let instant = getWindow();
if (!instant) {
return;
}
while (funcs.length > 1) {
instant = instant[funcs.shift()];
}
@@ -31,9 +41,13 @@ function onCallback(name, callback) {
}
function _checkInit() {
const win = getWindow();
if (!win) {
return;
}
let methodName = `${getBridgeName()}_handle_callback`;
if (!window[methodName]) {
window[methodName] = (res) => {
if (!win[methodName]) {
win[methodName] = (res) => {
let { method, payload, code, request_id } = toAny(res, {});
let data = toAny(payload, {});
if (request_id) {
@@ -48,7 +62,9 @@ function _checkInit() {
}
export function inRuntime() {
return !!window[getBridgeName()]
const win = getWindow();
return !!win
&& !!win[getBridgeName()]
&& getConfig("bridgeEnabled") !== false;
}

View File

@@ -6,6 +6,13 @@ let _scannerLastInputTime = 0;
const SCANNER_INPUT_INTERVAL = 100;
function getWindow() {
if (typeof window === "undefined") {
return null;
}
return window;
}
function clearScannerValue() {
_scannerValue = "";
_scannerLastInputTime = 0;
@@ -69,13 +76,17 @@ export function startScanner(callback){
if (!callback || typeof callback !== "function") {
return;
}
const win = getWindow();
if (!win) {
return;
}
_scannerCallback = callback;
if (_scannerStatus === "scanning") {
return;
}
_scannerStatus = "scanning";
clearScannerValue();
window.addEventListener("keydown", onScannerKeydown);
win.addEventListener("keydown", onScannerKeydown);
}
export function stopScanner(){
@@ -85,5 +96,6 @@ export function stopScanner(){
_scannerStatus = "ready";
_scannerCallback = null;
clearScannerValue();
window.removeEventListener("keydown", onScannerKeydown);
}
const win = getWindow();
win && win.removeEventListener("keydown", onScannerKeydown);
}

View File

@@ -383,8 +383,13 @@ function detectImageFile(detector, file) {
}
export function startScanForImage() {
return createBarcodeDetector(getConfig("webScanType")).then(detector => {
return chooseImageFile().then(file => detectImageFile(detector, file));
return chooseImageFile().then(file => {
if (!file) {
return null;
}
return createBarcodeDetector(getConfig("webScanType")).then(detector => {
return detectImageFile(detector, file);
});
}).then(code => {
if (code && code.rawValue) {
return {

View File

@@ -9,10 +9,13 @@ let _wxReadyPromise = null;
let _wxReady = false;
function getWx() {
if (typeof window === "undefined") {
return null;
if (typeof wx !== "undefined") {
return wx;
}
return window.wx;
if (typeof window !== "undefined") {
return window.wx;
}
return null;
}
function loadWxScript() {
@@ -78,9 +81,15 @@ export function isWxEnv() {
&& /micromessenger/i.test(navigator.userAgent || "");
}
export function isWxMiniProgramEnv() {
const wx = getWx();
return !!(wx && wx.scanCode);
}
export function isSupportWxScan() {
const wx = getWx();
return isWxEnv()
return isWxMiniProgramEnv()
|| isWxEnv()
&& _wxReady
&& wx
&& wx.scanQRCode;
@@ -128,7 +137,73 @@ export function initWxJssdk() {
return _wxReadyPromise;
}
function getWxScanErrorMessage(err) {
if (!err) {
return "扫码失败";
}
let errorMsg = err.errMsg || "扫码失败";
if (typeof err === "object") {
const errorDetails = [];
if (err.errCode) {
errorDetails.push("错误代码: " + err.errCode);
}
if (err.errDesc) {
errorDetails.push("错误描述: " + err.errDesc);
}
try {
const errorStr = JSON.stringify(err, null, 2);
if (errorStr && errorStr !== "{}" && errorStr !== "{\"errMsg\":\"" + errorMsg + "\"}") {
errorDetails.push(errorStr);
} else {
errorDetails.push(errorMsg);
}
} catch (e) {
}
if (errorDetails.length) {
errorMsg = errorDetails.join("\n");
}
}
return errorMsg;
}
function startScanForWxMiniProgram(options) {
return new Promise(resolve => {
const wx = getWx();
if (!wx || !wx.scanCode) {
resolve({
success: false,
error: "微信小程序API不可用"
});
return;
}
const {
scanType = ["qrCode", "barCode"],
onlyFromCamera = false
} = options || {};
wx.scanCode({
onlyFromCamera,
scanType,
success: res => {
resolve({
success: true,
result: res.result,
code: res.result
});
},
fail: err => {
resolve({
success: false,
error: getWxScanErrorMessage(err)
});
}
});
});
}
export function startScanForWx(options) {
if (isWxMiniProgramEnv()) {
return startScanForWxMiniProgram(options);
}
return initWxJssdk().then(() => {
return new Promise((resolve, reject) => {
const {