Files
WAI_Project_UNIX/uni_modules/cool-open-web/utssdk/app-harmony/index.uts
2025-07-21 16:47:04 +08:00

37 lines
1.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { OpenWebNative } from "./openWeb.ets";
/**
* 在鸿蒙系统中打开指定的网页URL
* @param url 要打开的网页地址支持http、https等协议
* @returns 返回操作结果true表示成功false表示失败
*/
export function openWeb(url: string): boolean {
// 参数验证检查URL是否为空或无效
if (url == null || url.trim() == "") {
console.error("openWeb: URL参数不能为空");
return false;
}
try {
let trimmedUrl = url.trim();
// 基本URL格式验证
if (!trimmedUrl.includes(".") || trimmedUrl.length < 4) {
console.error("openWeb: 无效的URL格式 -", trimmedUrl);
return false;
}
// 如果URL不包含协议默认添加https://
if (!trimmedUrl.startsWith("http://") && !trimmedUrl.startsWith("https://") && !trimmedUrl.startsWith("//")) {
trimmedUrl = "https://" + trimmedUrl;
}
// 调用鸿蒙原生实现
return OpenWebNative.openUrl(trimmedUrl);
} catch (e) {
// 捕获可能的异常
console.error("openWeb: 打开URL时发生错误 -", e);
return false;
}
}