plus-navigator.uts 1.04 KB
/**
 * plus.navigator 的 uni-app x 替代实现
 * 用于导航栏相关操作
 */

/**
 * 获取状态栏高度
 */
export function getStatusbarHeight(): number {
	// #ifdef APP
	const systemInfo = uni.getSystemInfoSync();
	const height = systemInfo.statusBarHeight;
	// UTS 要求条件语句必须使用布尔类型
	if (height != null && height > 0) {
		return height;
	}
	return 0;
	// #endif

	// #ifndef APP
	return 0;
	// #endif
}

/**
 * 设置用户代理(User Agent)
 * 注意:uni-app x 中 WebView 的 UA 设置需要在创建 WebView 时配置
 */
export function setUserAgent(userAgent: string): void {
	// #ifdef APP
	// uni-app x 中 WebView 的 UA 需要在创建时设置
	// 使用 web-view 组件时,可以通过属性设置
	console.warn("[plus-navigator] setUserAgent 需要在创建 WebView 时设置");
	// #endif

	// #ifndef APP
	// H5 可以设置 navigator.userAgent
	if (typeof navigator != "undefined") {
		Object.defineProperty(navigator, "userAgent", {
			get: () => userAgent,
			configurable: true
		});
	}
	// #endif
}