111
This commit is contained in:
1 parent
25a3dba931
commit
2191e6e4cf
7 files changed
+117
-39
No files matched your search
@@ -4,6 +4,65 @@ const H5_MAX_STATUS_BAR_HEIGHT = 200;
|
||||
let currentStatusBarHeight = H5_MIN_STATUS_BAR_HEIGHT;
|
||||
let statusBarStyleObserver = null;
|
||||
|
||||
// H5 产物运行在 5+ WebView 中时优先交给 App 打开,普通浏览器则在当前页跳转。
|
||||
export function openExternalUrl(url, source = 'unknown') {
|
||||
const logPrefix = `[openExternalUrl][${source}]`;
|
||||
if (!url) {
|
||||
console.error(logPrefix, '跳转失败:URL 为空');
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasGlobalThis = typeof globalThis !== 'undefined';
|
||||
const plusObject = hasGlobalThis ? globalThis.plus : null;
|
||||
const runtime = plusObject?.runtime;
|
||||
const currentUrl = typeof window !== 'undefined' ? window.location.href : '';
|
||||
console.log(logPrefix, '开始跳转', {
|
||||
targetUrl: url,
|
||||
currentUrl,
|
||||
userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '',
|
||||
documentReadyState: typeof document !== 'undefined' ? document.readyState : '',
|
||||
hasWindow: typeof window !== 'undefined',
|
||||
hasGlobalThis,
|
||||
hasPlus: Boolean(plusObject),
|
||||
hasPlusRuntime: Boolean(runtime),
|
||||
hasPlusOpenURL: typeof runtime?.openURL === 'function',
|
||||
});
|
||||
|
||||
if (runtime?.openURL) {
|
||||
try {
|
||||
console.log(logPrefix, '命中 5+ WebView,准备调用 plus.runtime.openURL');
|
||||
runtime.openURL(url, (error) => {
|
||||
console.error(logPrefix, 'plus.runtime.openURL 错误回调', error);
|
||||
});
|
||||
console.log(logPrefix, 'plus.runtime.openURL 已调用(该 API 无成功回调)');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(logPrefix, 'plus.runtime.openURL 抛出异常', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
try {
|
||||
console.warn(logPrefix, '未检测到 plus.runtime.openURL,回退到 window.location.assign');
|
||||
window.location.assign(url);
|
||||
console.log(logPrefix, 'window.location.assign 已调用');
|
||||
window.setTimeout(() => {
|
||||
if (window.location.href === currentUrl) {
|
||||
console.warn(logPrefix, '1.5 秒后页面 URL 未变化,跳转可能被 WebView 拦截');
|
||||
}
|
||||
}, 1500);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(logPrefix, 'window.location.assign 抛出异常', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
console.error(logPrefix, '跳转失败:window 和 plus.runtime 均不可用');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 将 URL 参数、宿主全局变量和 postMessage 消息中的高度统一转换为数字。
|
||||
function normalizeStatusBarHeight(value) {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
|
||||
Reference in new issue
Block a user