添加对微信小程序环境的支持
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# scan-code-jssdk
|
# scan-code-jssdk
|
||||||
|
|
||||||
统一扫码 JSSDK,支持桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、选择图片识别和扫码枪输入。
|
统一扫码 JSSDK,支持桥接扫码、微信小程序、微信 JSSDK 扫码、Web 摄像头扫码、选择图片识别和扫码枪输入。
|
||||||
|
|
||||||
## 功能
|
## 功能
|
||||||
|
|
||||||
|
|||||||
@@ -181,7 +181,7 @@
|
|||||||
<main class="page">
|
<main class="page">
|
||||||
<section class="hero">
|
<section class="hero">
|
||||||
<h1>IScan 通用扫码 SDK</h1>
|
<h1>IScan 通用扫码 SDK</h1>
|
||||||
<p>统一接入桥接扫码、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p>
|
<p>统一接入桥接扫码、微信小程序、微信 JSSDK 扫码、Web 摄像头扫码、图片识别和扫码枪输入。</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="grid">
|
<section class="grid">
|
||||||
|
|||||||
2
dist/index.html
vendored
2
dist/index.html
vendored
@@ -161,7 +161,7 @@
|
|||||||
|
|
||||||
section {
|
section {
|
||||||
margin-bottom: 16px;
|
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,
|
webScanEnabled: true,
|
||||||
webScanCanvasEnabled: true,
|
webScanCanvasEnabled: true,
|
||||||
webScanBeepEnabled: true,
|
webScanBeepEnabled: true,
|
||||||
|
|||||||
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -7,9 +7,12 @@ const IScan = exportSDK(core, null, "config", "setStatusListener", "onScanListen
|
|||||||
|
|
||||||
function dispatchIScanReady() {
|
function dispatchIScanReady() {
|
||||||
_global.__IScanReady__ && _global.__IScanReady__();
|
_global.__IScanReady__ && _global.__IScanReady__();
|
||||||
|
if (!_global.dispatchEvent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (typeof Event === "function") {
|
if (typeof Event === "function") {
|
||||||
_global.dispatchEvent(new Event("IScanReady"));
|
_global.dispatchEvent(new Event("IScanReady"));
|
||||||
} else {
|
} else if (typeof document !== "undefined") {
|
||||||
let event = document.createEvent("Event");
|
let event = document.createEvent("Event");
|
||||||
event.initEvent("IScanReady", true, true);
|
event.initEvent("IScanReady", true, true);
|
||||||
_global.dispatchEvent(event);
|
_global.dispatchEvent(event);
|
||||||
|
|||||||
@@ -7,13 +7,23 @@ let _events = {};
|
|||||||
let _callbacks = {};
|
let _callbacks = {};
|
||||||
let _bridge = "__bridge_client__";
|
let _bridge = "__bridge_client__";
|
||||||
|
|
||||||
|
function getWindow() {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
function getBridgeName() {
|
function getBridgeName() {
|
||||||
return getConfig("bridgeName") ? getConfig("bridgeName") : _bridge;
|
return getConfig("bridgeName") ? getConfig("bridgeName") : _bridge;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _callRuntime(func, ...options) {
|
function _callRuntime(func, ...options) {
|
||||||
let funcs = func.split('.');
|
let funcs = func.split('.');
|
||||||
let instant = window;
|
let instant = getWindow();
|
||||||
|
if (!instant) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
while (funcs.length > 1) {
|
while (funcs.length > 1) {
|
||||||
instant = instant[funcs.shift()];
|
instant = instant[funcs.shift()];
|
||||||
}
|
}
|
||||||
@@ -31,9 +41,13 @@ function onCallback(name, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _checkInit() {
|
function _checkInit() {
|
||||||
|
const win = getWindow();
|
||||||
|
if (!win) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let methodName = `${getBridgeName()}_handle_callback`;
|
let methodName = `${getBridgeName()}_handle_callback`;
|
||||||
if (!window[methodName]) {
|
if (!win[methodName]) {
|
||||||
window[methodName] = (res) => {
|
win[methodName] = (res) => {
|
||||||
let { method, payload, code, request_id } = toAny(res, {});
|
let { method, payload, code, request_id } = toAny(res, {});
|
||||||
let data = toAny(payload, {});
|
let data = toAny(payload, {});
|
||||||
if (request_id) {
|
if (request_id) {
|
||||||
@@ -48,7 +62,9 @@ function _checkInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function inRuntime() {
|
export function inRuntime() {
|
||||||
return !!window[getBridgeName()]
|
const win = getWindow();
|
||||||
|
return !!win
|
||||||
|
&& !!win[getBridgeName()]
|
||||||
&& getConfig("bridgeEnabled") !== false;
|
&& getConfig("bridgeEnabled") !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,13 @@ let _scannerLastInputTime = 0;
|
|||||||
|
|
||||||
const SCANNER_INPUT_INTERVAL = 100;
|
const SCANNER_INPUT_INTERVAL = 100;
|
||||||
|
|
||||||
|
function getWindow() {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
function clearScannerValue() {
|
function clearScannerValue() {
|
||||||
_scannerValue = "";
|
_scannerValue = "";
|
||||||
_scannerLastInputTime = 0;
|
_scannerLastInputTime = 0;
|
||||||
@@ -69,13 +76,17 @@ export function startScanner(callback){
|
|||||||
if (!callback || typeof callback !== "function") {
|
if (!callback || typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const win = getWindow();
|
||||||
|
if (!win) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
_scannerCallback = callback;
|
_scannerCallback = callback;
|
||||||
if (_scannerStatus === "scanning") {
|
if (_scannerStatus === "scanning") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_scannerStatus = "scanning";
|
_scannerStatus = "scanning";
|
||||||
clearScannerValue();
|
clearScannerValue();
|
||||||
window.addEventListener("keydown", onScannerKeydown);
|
win.addEventListener("keydown", onScannerKeydown);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopScanner(){
|
export function stopScanner(){
|
||||||
@@ -85,5 +96,6 @@ export function stopScanner(){
|
|||||||
_scannerStatus = "ready";
|
_scannerStatus = "ready";
|
||||||
_scannerCallback = null;
|
_scannerCallback = null;
|
||||||
clearScannerValue();
|
clearScannerValue();
|
||||||
window.removeEventListener("keydown", onScannerKeydown);
|
const win = getWindow();
|
||||||
|
win && win.removeEventListener("keydown", onScannerKeydown);
|
||||||
}
|
}
|
||||||
@@ -9,10 +9,13 @@ let _wxReadyPromise = null;
|
|||||||
let _wxReady = false;
|
let _wxReady = false;
|
||||||
|
|
||||||
function getWx() {
|
function getWx() {
|
||||||
if (typeof window === "undefined") {
|
if (typeof wx !== "undefined") {
|
||||||
return null;
|
return wx;
|
||||||
}
|
}
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
return window.wx;
|
return window.wx;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadWxScript() {
|
function loadWxScript() {
|
||||||
@@ -78,9 +81,15 @@ export function isWxEnv() {
|
|||||||
&& /micromessenger/i.test(navigator.userAgent || "");
|
&& /micromessenger/i.test(navigator.userAgent || "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isWxMiniProgramEnv() {
|
||||||
|
const wx = getWx();
|
||||||
|
return !!(wx && wx.scanCode);
|
||||||
|
}
|
||||||
|
|
||||||
export function isSupportWxScan() {
|
export function isSupportWxScan() {
|
||||||
const wx = getWx();
|
const wx = getWx();
|
||||||
return isWxEnv()
|
return isWxMiniProgramEnv()
|
||||||
|
|| isWxEnv()
|
||||||
&& _wxReady
|
&& _wxReady
|
||||||
&& wx
|
&& wx
|
||||||
&& wx.scanQRCode;
|
&& wx.scanQRCode;
|
||||||
@@ -128,7 +137,73 @@ export function initWxJssdk() {
|
|||||||
return _wxReadyPromise;
|
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) {
|
export function startScanForWx(options) {
|
||||||
|
if (isWxMiniProgramEnv()) {
|
||||||
|
return startScanForWxMiniProgram(options);
|
||||||
|
}
|
||||||
return initWxJssdk().then(() => {
|
return initWxJssdk().then(() => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const {
|
const {
|
||||||
|
|||||||
Reference in New Issue
Block a user