版本发布
This commit is contained in:
36
uni_modules/cool-open-web/utssdk/app-harmony/index.uts
Normal file
36
uni_modules/cool-open-web/utssdk/app-harmony/index.uts
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
45
uni_modules/cool-open-web/utssdk/app-harmony/openWeb.ets
Normal file
45
uni_modules/cool-open-web/utssdk/app-harmony/openWeb.ets
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Want, common } from '@kit.AbilityKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
|
||||
/**
|
||||
* 原生打开网页控制类
|
||||
* 用于在鸿蒙系统中打开网页URL
|
||||
*/
|
||||
export class OpenWebNative {
|
||||
/**
|
||||
* 打开指定的网页URL
|
||||
* @param url 要打开的网页地址
|
||||
* @returns 返回操作结果,true表示成功,false表示失败
|
||||
*/
|
||||
static openUrl(url: string): boolean {
|
||||
try {
|
||||
// 获取应用上下文
|
||||
const context = getContext() as common.UIAbilityContext;
|
||||
|
||||
// 构建Want对象,用于启动浏览器
|
||||
const want: Want = {
|
||||
action: 'ohos.want.action.viewData', // 查看数据的标准动作
|
||||
entities: ['entity.system.browsable'], // 可浏览实体
|
||||
uri: url // 目标URL
|
||||
};
|
||||
|
||||
// 启动浏览器应用
|
||||
context.startAbility(want)
|
||||
.then(() => {
|
||||
console.info(`成功打开URL: ${url}`);
|
||||
})
|
||||
.catch((error: BusinessError) => {
|
||||
console.error(`打开URL失败: 错误码 ${error.code}, 错误信息 ${error.message}`);
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
// 捕获意外错误
|
||||
const error: BusinessError = err as BusinessError;
|
||||
console.error(
|
||||
`发生意外错误: 错误码 ${error.code}, 错误信息 ${error.message}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user