This commit is contained in:
iqudoo
2026-06-17 20:17:50 +08:00
parent 378c7e4ba5
commit 23b97548cf
5 changed files with 98 additions and 1 deletions

4
dist/index.d.ts vendored
View File

@@ -18,6 +18,10 @@ interface ScanConfigOptions {
* 跨域 iframe 无法读取父页地址时需手动设为当前微信内打开的页面链接。
*/
wxJssdkSignatureUrl?: string,
/**
* 页面 URL 变化后是否自动重新初始化微信 JSSDK默认 true避免扫一扫失效
*/
wxJssdkAutoReinitOnUrlChange?: boolean,
/**
* 桥接是否启用,默认启用
*/

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -13,6 +13,7 @@ import { runPaymentDialogChildAction } from "./paymentDialogChild";
registerPaymentDialogChildRunner(runPaymentDialogChildAction);
import { resolvePaymentAmount } from "./paymentAmount";
import { ensureWechatOpenid } from "./oauth";
function buildInvokeResult(payType, extra) {
return Object.assign({ payType, invoked: true }, extra || {});

View File

@@ -3,12 +3,15 @@ import { readWxLikeEnvFromWindow, getParentWxEnvReport } from "../embedEnvProbe"
import { resolveUseParentProxy } from "../embedProxy";
import { request } from "../../utils/request";
import { toAny } from "../../utils/toany";
import { printDebug } from "../../utils/logger";
const WX_JS_SDK_URL = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
const WX_SCAN_API = "scanQRCode";
let _wxReadyPromise = null;
let _wxReady = false;
let _wxSignatureUrl = "";
let _urlWatchInstalled = false;
function getWx() {
if (typeof wx !== "undefined") {
@@ -69,6 +72,84 @@ function getPageUrlForWxJssdkSignature() {
return window.location.href.split("#")[0];
}
function isWxJssdkAutoReinitEnabled() {
return getConfig("wxJssdkAutoReinitOnUrlChange") !== false;
}
function getWxJssdkWatchWindow() {
const override = getConfig("wxJssdkSignatureUrl");
if (override || typeof window === "undefined") {
return typeof window !== "undefined" ? window : null;
}
if (resolveUseParentProxy()) {
try {
if (window.parent && window.parent !== window) {
return window.parent;
}
} catch (e) {
// 跨域父页不可读,回退监听当前页
}
}
return window;
}
function resetWxJssdkState() {
_wxReady = false;
_wxReadyPromise = null;
}
function isWxSignatureUrlChanged() {
const current = getPageUrlForWxJssdkSignature();
return !!(_wxSignatureUrl && current && _wxSignatureUrl !== current);
}
function patchHistoryMethod(history, methodName, callback) {
if (!history || typeof history[methodName] !== "function") {
return;
}
const original = history[methodName];
if (original.__iscanWxPatched__) {
return;
}
const patched = function patchedHistoryMethod() {
const result = original.apply(this, arguments);
callback();
return result;
};
patched.__iscanWxPatched__ = true;
history[methodName] = patched;
}
function installWxJssdkUrlWatcher() {
if (_urlWatchInstalled || !isWxJssdkAutoReinitEnabled()) {
return;
}
const watchWin = getWxJssdkWatchWindow();
if (!watchWin || !watchWin.addEventListener) {
return;
}
_urlWatchInstalled = true;
const scheduleReinit = () => {
if (!isWxEnv() || !isWxJssdkAutoReinitEnabled()) {
return;
}
if (!isWxSignatureUrlChanged()) {
return;
}
printDebug("wx jssdk url changed, reinitializing:", getPageUrlForWxJssdkSignature());
resetWxJssdkState();
initWxJssdk().catch((err) => {
printDebug("wx jssdk reinit failed:", err && err.message ? err.message : err);
});
};
watchWin.addEventListener("popstate", scheduleReinit);
watchWin.addEventListener("hashchange", scheduleReinit);
patchHistoryMethod(watchWin.history, "pushState", scheduleReinit);
patchHistoryMethod(watchWin.history, "replaceState", scheduleReinit);
}
function fetchWxConfig() {
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
if (!!initWechatJssdk.sdkConfig) {
@@ -139,6 +220,10 @@ export function initWxJssdk() {
if (!isWxEnv()) {
return Promise.resolve();
}
installWxJssdkUrlWatcher();
if (isWxSignatureUrlChanged()) {
resetWxJssdkState();
}
if (_wxReadyPromise) {
return _wxReadyPromise;
}
@@ -158,10 +243,12 @@ export function initWxJssdk() {
return new Promise((resolve, reject) => {
wx.ready(() => {
_wxReady = true;
_wxSignatureUrl = getPageUrlForWxJssdkSignature();
resolve(wx);
});
wx.error(err => {
_wxReady = false;
_wxSignatureUrl = "";
_wxReadyPromise = null;
reject(err);
});
@@ -171,6 +258,7 @@ export function initWxJssdk() {
});
}).catch(err => {
_wxReady = false;
_wxSignatureUrl = "";
_wxReadyPromise = null;
throw err;
});

4
types/index.d.ts vendored
View File

@@ -18,6 +18,10 @@ interface ScanConfigOptions {
* 跨域 iframe 无法读取父页地址时需手动设为当前微信内打开的页面链接。
*/
wxJssdkSignatureUrl?: string,
/**
* 页面 URL 变化后是否自动重新初始化微信 JSSDK默认 true避免扫一扫失效
*/
wxJssdkAutoReinitOnUrlChange?: boolean,
/**
* 桥接是否启用,默认启用
*/