接入原生壳外链打开路径
H5 新增 openHostExternalUrl 并归一化外链 URL 备案号和资产调试原图在 native_app 中优先交给宿主打开 补充 HostBridge 外链测试和宿主壳文档
This commit is contained in:
@@ -4,8 +4,12 @@ import type {
|
||||
HapticsImpactPayload,
|
||||
HostBridgeMethod,
|
||||
HostBridgeRuntimeResult,
|
||||
OpenExternalUrlPayload,
|
||||
ShareOpenPayload,
|
||||
} from '../../../packages/shared/src/contracts/hostBridge';
|
||||
import {
|
||||
normalizeHostBridgeExternalUrl,
|
||||
} from '../../../packages/shared/src/contracts/hostBridge';
|
||||
import type {
|
||||
WechatMiniProgramPayParams,
|
||||
WechatMiniProgramVirtualPayParams,
|
||||
@@ -75,6 +79,8 @@ export type HostAppTitleRequest = {
|
||||
|
||||
export type HostShareOpenRequest = ShareOpenPayload;
|
||||
|
||||
export type HostExternalUrlRequest = OpenExternalUrlPayload;
|
||||
|
||||
function isUnsupportedHostBridgeError(error: unknown) {
|
||||
return (
|
||||
error instanceof Error &&
|
||||
@@ -387,6 +393,25 @@ function buildAbsoluteUrl(value: string) {
|
||||
return new URL(value, window.location.origin).href;
|
||||
}
|
||||
|
||||
function normalizeHostExternalUrl(url: string) {
|
||||
const trimmedUrl = url.trim();
|
||||
if (!trimmedUrl) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return normalizeHostBridgeExternalUrl(trimmedUrl);
|
||||
}
|
||||
|
||||
try {
|
||||
return normalizeHostBridgeExternalUrl(
|
||||
new URL(trimmedUrl, window.location.origin).toString(),
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function canUseHostShareGrid(context: HostRuntimeContext = {}) {
|
||||
return getHostRuntime(context).kind === 'wechat_mini_program';
|
||||
}
|
||||
@@ -461,6 +486,25 @@ export async function openHostShare(params: HostShareOpenRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function openHostExternalUrl({ url }: HostExternalUrlRequest) {
|
||||
if (getHostRuntime().kind !== 'native_app') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const normalizedUrl = normalizeHostExternalUrl(url);
|
||||
if (!normalizedUrl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return await requestNativeHostBoolean('app.openExternalUrl', {
|
||||
url: normalizedUrl,
|
||||
});
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function postWechatMiniProgramMessage(message: unknown) {
|
||||
return setHostShareTarget(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user