添加对微信小程序环境的支持
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user