添加对微信小程序环境的支持

This commit is contained in:
iqudoo
2026-04-30 19:43:13 +08:00
parent 3856b6e8c9
commit 23c91267a2
8 changed files with 122 additions and 16 deletions

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

@@ -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 {